diff --git a/src/core/emu.zig b/src/core/emu.zig index 8c239a1..13ae666 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Header = @import("cartridge.zig").Header; const Scheduler = @import("Scheduler.zig"); +const Ui = @import("../platform.zig").Ui; const Allocator = std.mem.Allocator; @@ -404,6 +405,16 @@ pub fn fastBoot(system: System) void { } } +pub const Sync = struct { + const Atomic = std.atomic.Value; + + should_quit: Atomic(bool) = Atomic(bool).init(false), + + pub fn init(self: *Sync) void { + self.* = .{}; + } +}; + pub const debug = struct { const Interface = @import("gdbstub").Emulator; const Server = @import("gdbstub").Server; @@ -457,6 +468,8 @@ pub const debug = struct { system: System, scheduler: *Scheduler, + tick: u64 = 0, + pub fn init(system: System, scheduler: *Scheduler) @This() { return .{ .system = system, .scheduler = scheduler }; } @@ -516,12 +529,20 @@ pub const debug = struct { inline else => |halt| { if (!dma9.step(system.arm946es) and comptime halt != .arm9) { system.arm946es.step(); - system.arm946es.step(); + + switch (proc) { + .nds9 => did_step = true, + .nds7 => system.arm946es.step(), + } } if (!dma7.step(system.arm7tdmi) and comptime halt != .arm7) { - system.arm7tdmi.step(); - did_step = true; + if (proc == .nds7 or self.tick % 2 == 0) system.arm7tdmi.step(); + + if (proc == .nds7) { + did_step = true; + self.tick += 1; + } } }, } @@ -535,8 +556,8 @@ pub const debug = struct { }; } - pub fn run(allocator: Allocator, system: System, scheduler: *Scheduler, should_quit: *AtomicBool) !void { - var wrapper = Wrapper(.nds7).init(system, scheduler); + pub fn run(allocator: Allocator, ui: *Ui, scheduler: *Scheduler, system: System, sync: *Sync) !void { + var wrapper = Wrapper(.nds9).init(system, scheduler); var emu_interface = wrapper.interface(allocator); defer emu_interface.deinit(); @@ -544,7 +565,9 @@ pub const debug = struct { var server = try Server.init(emu_interface, .{ .target = nds7.target, .memory_map = nds7.memory_map }); defer server.deinit(allocator); - const thread = try std.Thread.spawn(.{}, Server.run, .{ &server, allocator, should_quit }); + const thread = try std.Thread.spawn(.{}, Server.run, .{ &server, allocator, &sync.should_quit }); defer thread.join(); + + try ui.debug_run(scheduler, system, sync); } }; diff --git a/src/core/nds7/Bus.zig b/src/core/nds7/Bus.zig index b36cdf1..1e7179c 100644 --- a/src/core/nds7/Bus.zig +++ b/src/core/nds7/Bus.zig @@ -71,7 +71,7 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T } return switch (aligned_addr) { - 0x0000_0000...0x01FF_FFFF => self.bios.read(T, address), + 0x0000_0000...0x01FF_FFFF => self.bios.read(T, aligned_addr), 0x0200_0000...0x02FF_FFFF => std.mem.readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], .little), 0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) { 0b00 => std.mem.readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], .little), @@ -105,7 +105,7 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v } switch (aligned_addr) { - 0x0000_0000...0x01FF_FFFF => self.bios.write(T, address, value), + 0x0000_0000...0x01FF_FFFF => self.bios.write(T, aligned_addr, value), 0x0200_0000...0x02FF_FFFF => std.mem.writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], value, .little), 0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) { 0b00 => std.mem.writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value, .little), diff --git a/src/core/nds9/Bus.zig b/src/core/nds9/Bus.zig index c8bfc74..e223143 100644 --- a/src/core/nds9/Bus.zig +++ b/src/core/nds9/Bus.zig @@ -81,7 +81,7 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T 0x0500_0000...0x05FF_FFFF => std.mem.readInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], .little), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(T, .nds9, aligned_addr), 0x0700_0000...0x07FF_FFFF => std.mem.readInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..byte_count], .little), - 0xFFFF_0000...0xFFFF_FFFF => self.bios.read(T, address), + 0xFFFF_0000...0xFFFF_FFFF => self.bios.read(T, aligned_addr), else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }), }; } @@ -111,7 +111,7 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v 0x0500_0000...0x05FF_FFFF => std.mem.writeInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(T, .nds9, aligned_addr, value), 0x0700_0000...0x07FF_FFFF => std.mem.writeInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little), - 0xFFFF_0000...0xFFFF_FFFF => self.bios.write(T, address, value), + 0xFFFF_0000...0xFFFF_FFFF => self.bios.write(T, aligned_addr, value), else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }), } } diff --git a/src/main.zig b/src/main.zig index c9355a5..16e8ce2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,6 +6,7 @@ const emu = @import("core/emu.zig"); const Ui = @import("platform.zig").Ui; const SharedCtx = @import("core/emu.zig").SharedCtx; const System = @import("core/emu.zig").System; +const Sync = @import("core/emu.zig").Sync; const Scheduler = @import("core/Scheduler.zig"); const Allocator = std.mem.Allocator; @@ -68,16 +69,20 @@ pub fn main() !void { emu.fastBoot(system); + var ui = try Ui.init(allocator); + defer ui.deinit(allocator); + + ui.setTitle(rom_title); + + const sync = try allocator.create(Sync); + defer allocator.destroy(sync); + + sync.init(); + if (result.args.gdb == 0) { - var ui = try Ui.init(allocator); - defer ui.deinit(allocator); - - ui.setTitle(rom_title); - try ui.run(&scheduler, system); + try ui.run(&scheduler, system, sync); } else { - var should_quit: std.atomic.Value(bool) = std.atomic.Value(bool).init(false); - - try emu.debug.run(allocator, system, &scheduler, &should_quit); + try emu.debug.run(allocator, &ui, &scheduler, system, sync); } } diff --git a/src/platform.zig b/src/platform.zig index 91284e8..5c5143d 100644 --- a/src/platform.zig +++ b/src/platform.zig @@ -8,9 +8,11 @@ const imgui = @import("ui/imgui.zig"); const emu = @import("core/emu.zig"); const System = @import("core/emu.zig").System; +const Sync = @import("core/emu.zig").Sync; const KeyInput = @import("core/io.zig").KeyInput; const ExtKeyIn = @import("core/io.zig").ExtKeyIn; const Scheduler = @import("core/Scheduler.zig"); +const FrameBuffer = @import("core/ppu.zig").FrameBuffer; const Allocator = std.mem.Allocator; @@ -86,133 +88,161 @@ pub const Ui = struct { return SDL.SDL_GL_GetProcAddress(proc.ptr); } - pub fn run(self: *Self, scheduler: *Scheduler, system: System) !void { - // TODO: Sort this out please + pub fn setTitle(self: *@This(), title: [12]u8) void { + self.state.title = title ++ [_:0]u8{}; + } - const vao_id = opengl_impl.vao(); - defer gl.deleteVertexArrays(1, &[_]GLuint{vao_id}); - - const top_tex = opengl_impl.screenTex(system.bus9.ppu.fb.top(.front)); - const btm_tex = opengl_impl.screenTex(system.bus9.ppu.fb.btm(.front)); - const top_out_tex = opengl_impl.outTex(); - const btm_out_tex = opengl_impl.outTex(); - defer gl.deleteTextures(4, &[_]GLuint{ top_tex, top_out_tex, btm_tex, btm_out_tex }); - - const top_fbo = try opengl_impl.frameBuffer(top_out_tex); - const btm_fbo = try opengl_impl.frameBuffer(btm_out_tex); - defer gl.deleteFramebuffers(2, &[_]GLuint{ top_fbo, btm_fbo }); - - const prog_id = try opengl_impl.program(); - defer gl.deleteProgram(prog_id); + pub fn run(self: *Self, scheduler: *Scheduler, system: System, sync: *Sync) !void { + const id = try opengl_impl.runInit(&system.bus9.ppu.fb); + defer id.deinit(); var event: SDL.SDL_Event = undefined; - emu_loop: while (true) { - emu.runFrame(scheduler, system); + while (!sync.should_quit.load(.Monotonic)) { + emu.runFrame(scheduler, system); // TODO: run emu in separate thread while (SDL.SDL_PollEvent(&event) != 0) { _ = zgui.backend.processEvent(&event); - - switch (event.type) { - SDL.SDL_QUIT => break :emu_loop, - SDL.SDL_WINDOWEVENT => { - if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) { - std.log.debug("window resized to: {}x{}", .{ event.window.data1, event.window.data2 }); - - self.state.dim.width = @intCast(event.window.data1); - self.state.dim.height = @intCast(event.window.data2); - } - }, - SDL.SDL_KEYDOWN => { - // TODO: Make use of compare_and_xor? - const key_code = event.key.keysym.sym; - - var keyinput: KeyInput = .{ .raw = 0x0000 }; - var extkeyin: ExtKeyIn = .{ .raw = 0x0000 }; - - switch (key_code) { - SDL.SDLK_UP => keyinput.up.set(), - SDL.SDLK_DOWN => keyinput.down.set(), - SDL.SDLK_LEFT => keyinput.left.set(), - SDL.SDLK_RIGHT => keyinput.right.set(), - SDL.SDLK_c => keyinput.a.set(), - SDL.SDLK_x => keyinput.b.set(), - SDL.SDLK_d => extkeyin.x.set(), - SDL.SDLK_s => extkeyin.y.set(), - SDL.SDLK_a => keyinput.shoulder_l.set(), - SDL.SDLK_f => keyinput.shoulder_r.set(), - SDL.SDLK_RETURN => keyinput.start.set(), - SDL.SDLK_RSHIFT => keyinput.select.set(), - else => {}, - } - - const input = (@as(u32, extkeyin.raw) << 16) | keyinput.raw; - system.bus9.io.shr.input.set(.And, ~input); - }, - SDL.SDL_KEYUP => { - // TODO: Make use of compare_and_xor? - const key_code = event.key.keysym.sym; - - var keyinput: KeyInput = .{ .raw = 0x0000 }; - var extkeyin: ExtKeyIn = .{ .raw = 0x0000 }; - - switch (key_code) { - SDL.SDLK_UP => keyinput.up.set(), - SDL.SDLK_DOWN => keyinput.down.set(), - SDL.SDLK_LEFT => keyinput.left.set(), - SDL.SDLK_RIGHT => keyinput.right.set(), - SDL.SDLK_c => keyinput.a.set(), - SDL.SDLK_x => keyinput.b.set(), - SDL.SDLK_d => extkeyin.x.set(), - SDL.SDLK_s => extkeyin.y.set(), - SDL.SDLK_a => keyinput.shoulder_l.set(), - SDL.SDLK_f => keyinput.shoulder_r.set(), - SDL.SDLK_RETURN => keyinput.start.set(), - SDL.SDLK_RSHIFT => keyinput.select.set(), - else => {}, - } - - const input = (@as(u32, extkeyin.raw) << 16) | keyinput.raw; - system.bus9.io.shr.input.set(.Or, input); - }, - else => {}, - } + handleInput(&event, system, &self.state, sync); } { - gl.bindFramebuffer(gl.FRAMEBUFFER, top_fbo); + gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo); defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); gl.viewport(0, 0, nds_width, nds_height); - opengl_impl.drawScreen(top_tex, prog_id, vao_id, system.bus9.ppu.fb.top(.front)); + opengl_impl.drawScreen(id.top_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.top(.front)); } { - gl.bindFramebuffer(gl.FRAMEBUFFER, btm_fbo); + gl.bindFramebuffer(gl.FRAMEBUFFER, id.btm_fbo); defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); gl.viewport(0, 0, nds_width, nds_height); - opengl_impl.drawScreen(btm_tex, prog_id, vao_id, system.bus9.ppu.fb.btm(.front)); + opengl_impl.drawScreen(id.btm_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.btm(.front)); } - const zgui_redraw = imgui.draw(&self.state, top_out_tex, btm_out_tex, system); + imgui.draw(&self.state, id.top_out_tex, id.btm_out_tex, system); - if (zgui_redraw) { - // Background Colour - const size = zgui.io.getDisplaySize(); - gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1])); - gl.clearColor(0, 0, 0, 1.0); - gl.clear(gl.COLOR_BUFFER_BIT); - - zgui.backend.draw(); - } + // Background Colour + const size = zgui.io.getDisplaySize(); + gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1])); + gl.clearColor(0, 0, 0, 1.0); + gl.clear(gl.COLOR_BUFFER_BIT); + zgui.backend.draw(); SDL.SDL_GL_SwapWindow(self.window); } } - pub fn setTitle(self: *@This(), title: [12]u8) void { - self.state.title = title ++ [_:0]u8{}; + pub fn debug_run(self: *Self, _: *Scheduler, system: System, sync: *Sync) !void { + const id = try opengl_impl.runInit(&system.bus9.ppu.fb); + defer id.deinit(); + + var event: SDL.SDL_Event = undefined; + + while (!sync.should_quit.load(.Monotonic)) { + while (SDL.SDL_PollEvent(&event) != 0) { + _ = zgui.backend.processEvent(&event); + handleInput(&event, system, &self.state, sync); + } + + { + gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo); + defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); + + gl.viewport(0, 0, nds_width, nds_height); + opengl_impl.drawScreen(id.top_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.top(.front)); + } + + { + gl.bindFramebuffer(gl.FRAMEBUFFER, id.btm_fbo); + defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); + + gl.viewport(0, 0, nds_width, nds_height); + opengl_impl.drawScreen(id.btm_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.btm(.front)); + } + + imgui.draw(&self.state, id.top_out_tex, id.btm_out_tex, system); + + // Background Colour + const size = zgui.io.getDisplaySize(); + gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1])); + gl.clearColor(0, 0, 0, 1.0); + gl.clear(gl.COLOR_BUFFER_BIT); + + zgui.backend.draw(); + SDL.SDL_GL_SwapWindow(self.window); + } + } + + fn handleInput(event: *SDL.SDL_Event, system: System, state: *imgui.State, sync: *Sync) void { + switch (event.type) { + SDL.SDL_QUIT => sync.should_quit.store(true, .Monotonic), + SDL.SDL_WINDOWEVENT => { + if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) { + std.log.debug("window resized to: {}x{}", .{ event.window.data1, event.window.data2 }); + + state.dim.width = @intCast(event.window.data1); + state.dim.height = @intCast(event.window.data2); + + zgui.io.setDisplaySize(@floatFromInt(event.window.data1), @floatFromInt(event.window.data2)); + } + }, + SDL.SDL_KEYDOWN => { + // TODO: Make use of compare_and_xor? + const key_code = event.key.keysym.sym; + + var keyinput: KeyInput = .{ .raw = 0x0000 }; + var extkeyin: ExtKeyIn = .{ .raw = 0x0000 }; + + switch (key_code) { + SDL.SDLK_UP => keyinput.up.set(), + SDL.SDLK_DOWN => keyinput.down.set(), + SDL.SDLK_LEFT => keyinput.left.set(), + SDL.SDLK_RIGHT => keyinput.right.set(), + SDL.SDLK_c => keyinput.a.set(), + SDL.SDLK_x => keyinput.b.set(), + SDL.SDLK_d => extkeyin.x.set(), + SDL.SDLK_s => extkeyin.y.set(), + SDL.SDLK_a => keyinput.shoulder_l.set(), + SDL.SDLK_f => keyinput.shoulder_r.set(), + SDL.SDLK_RETURN => keyinput.start.set(), + SDL.SDLK_RSHIFT => keyinput.select.set(), + else => {}, + } + + const input = (@as(u32, extkeyin.raw) << 16) | keyinput.raw; + system.bus9.io.shr.input.set(.And, ~input); + }, + SDL.SDL_KEYUP => { + // TODO: Make use of compare_and_xor? + const key_code = event.key.keysym.sym; + + var keyinput: KeyInput = .{ .raw = 0x0000 }; + var extkeyin: ExtKeyIn = .{ .raw = 0x0000 }; + + switch (key_code) { + SDL.SDLK_UP => keyinput.up.set(), + SDL.SDLK_DOWN => keyinput.down.set(), + SDL.SDLK_LEFT => keyinput.left.set(), + SDL.SDLK_RIGHT => keyinput.right.set(), + SDL.SDLK_c => keyinput.a.set(), + SDL.SDLK_x => keyinput.b.set(), + SDL.SDLK_d => extkeyin.x.set(), + SDL.SDLK_s => extkeyin.y.set(), + SDL.SDLK_a => keyinput.shoulder_l.set(), + SDL.SDLK_f => keyinput.shoulder_r.set(), + SDL.SDLK_RETURN => keyinput.start.set(), + SDL.SDLK_RSHIFT => keyinput.select.set(), + else => {}, + } + + const input = (@as(u32, extkeyin.raw) << 16) | keyinput.raw; + system.bus9.io.shr.input.set(.Or, input); + }, + else => {}, + } } }; @@ -222,6 +252,46 @@ fn panic() noreturn { } const opengl_impl = struct { + const Ids = struct { + vao_id: GLuint, + + top_tex: GLuint, + btm_tex: GLuint, + top_out_tex: GLuint, + btm_out_tex: GLuint, + + top_fbo: GLuint, + btm_fbo: GLuint, + + prog_id: GLuint, + + fn deinit(self: Ids) void { + gl.deleteProgram(self.prog_id); + gl.deleteFramebuffers(2, &[_]GLuint{ self.top_fbo, self.btm_fbo }); + gl.deleteTextures(4, &[_]GLuint{ self.top_tex, self.top_out_tex, self.btm_tex, self.btm_out_tex }); + gl.deleteVertexArrays(1, &[_]GLuint{self.vao_id}); + } + }; + + fn runInit(fb: *const FrameBuffer) !Ids { + const top_out_tex = opengl_impl.outTex(); + const btm_out_tex = opengl_impl.outTex(); + + return .{ + .vao_id = opengl_impl.vao(), + + .top_tex = opengl_impl.screenTex(fb.top(.front)), + .btm_tex = opengl_impl.screenTex(fb.btm(.front)), + .top_out_tex = top_out_tex, + .btm_out_tex = btm_out_tex, + + .top_fbo = try opengl_impl.frameBuffer(top_out_tex), + .btm_fbo = try opengl_impl.frameBuffer(btm_out_tex), + + .prog_id = try opengl_impl.program(), + }; + } + fn drawScreen(tex_id: GLuint, prog_id: GLuint, vao_id: GLuint, buf: []const u8) void { gl.bindTexture(gl.TEXTURE_2D, tex_id); defer gl.bindTexture(gl.TEXTURE_2D, 0); diff --git a/src/ui/imgui.zig b/src/ui/imgui.zig index 4e2be8f..6dca574 100644 --- a/src/ui/imgui.zig +++ b/src/ui/imgui.zig @@ -18,7 +18,7 @@ pub const State = struct { dim: Dimensions = .{ .width = 1600, .height = 900 }, }; -pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: System) bool { +pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: System) void { _ = system; zgui.backend.newFrame(@floatFromInt(state.dim.width), @floatFromInt(state.dim.height)); @@ -36,6 +36,4 @@ pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: Syste zgui.image(@ptrFromInt(top_tex), .{ .w = w, .h = h }); zgui.image(@ptrFromInt(btm_tex), .{ .w = w, .h = h }); } - - return true; }