From 65af6aa49926a4840c57388b5190dcb4c2d2c7d5 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 15 Dec 2022 03:44:17 -0400 Subject: [PATCH 1/9] feat: add gdbstub library --- .gitmodules | 3 +++ build.zig | 5 +++++ lib/zba-gdbstub | 1 + 3 files changed, 9 insertions(+) create mode 160000 lib/zba-gdbstub diff --git a/.gitmodules b/.gitmodules index 78bd5b0..f45d1ad 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "lib/zig-toml"] path = lib/zig-toml url = https://github.com/aeronavery/zig-toml +[submodule "lib/zba-gdbstub"] + path = lib/zba-gdbstub + url = https://git.musuka.dev/paoda/zba-gdbstub diff --git a/build.zig b/build.zig index 5679ec3..2cf2bb2 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,8 @@ const std = @import("std"); const builtin = @import("builtin"); + const Sdk = @import("lib/SDL.zig/Sdk.zig"); +const Gdbstub = @import("lib/zba-gdbstub/build.zig"); pub fn build(b: *std.build.Builder) void { // Minimum Zig Version @@ -40,6 +42,9 @@ pub fn build(b: *std.build.Builder) void { // OpenGL 3.3 Bindings exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } }); + // gdbstub + Gdbstub.link(exe); + // Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig const sdk = Sdk.init(b, null); sdk.link(exe, .dynamic); diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub new file mode 160000 index 0000000..08bf0f9 --- /dev/null +++ b/lib/zba-gdbstub @@ -0,0 +1 @@ +Subproject commit 08bf0f9201a0cd6d317e3f73a26d245eedfd1121 -- 2.34.1 From 1f3cdd951347bc5a73ca4a4c3418aad998267bb4 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 15 Dec 2022 19:37:38 -0400 Subject: [PATCH 2/9] feat: add gdb support to zba --- lib/zba-gdbstub | 2 +- src/core/emu.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 34 ++++++++++++++++++++++++++--- 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index 08bf0f9..4bca44e 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit 08bf0f9201a0cd6d317e3f73a26d245eedfd1121 +Subproject commit 4bca44e5f2a4aa19361b51b7617a21f46f88eef4 diff --git a/src/core/emu.zig b/src/core/emu.zig index 1d97864..ece7e58 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -162,3 +162,59 @@ fn sleep(timer: *Timer, wake_time: u64) ?u64 { fn spinLoop(timer: *Timer, wake_time: u64) void { while (true) if (timer.read() > wake_time) break; } + +pub const EmuThing = struct { + const Self = @This(); + const Interface = @import("gdbstub").Emulator; + + cpu: *Arm7tdmi, + scheduler: *Scheduler, + + pub fn init(cpu: *Arm7tdmi, scheduler: *Scheduler) Self { + return .{ .cpu = cpu, .scheduler = scheduler }; + } + + pub fn interface(self: *Self) Interface { + return Interface.init(self); + } + + pub fn read(self: *const Self, addr: u32) u8 { + return self.cpu.bus.dbgRead(u8, addr); + } + + pub fn write(self: *Self, addr: u32, value: u8) void { + _ = value; + _ = self; + _ = addr; + + std.debug.panic("TODO: Implement Debug Writes?", .{}); + } + + pub fn registers(self: *const Self) *[16]u32 { + return &self.cpu.r; + } + + pub fn cpsr(self: *const Self) u32 { + return self.cpu.cpsr.raw; + } + + pub fn step(self: *Self) void { + const cpu = self.cpu; + const sched = self.scheduler; + + // TODO: How can I make it easier to keep this in lock-step with runFrame? + while (true) { + if (!cpu.stepDmaTransfer()) { + if (cpu.isHalted()) { + // Fast-forward to next Event + sched.tick = sched.queue.peek().?.tick; + } else { + cpu.step(); + break; // this function won't return until we've actually stepped once + } + } + + if (sched.tick >= sched.nextTimestamp()) sched.handleEvent(cpu); + } + } +}; diff --git a/src/main.zig b/src/main.zig index 463f388..cc89a24 100644 --- a/src/main.zig +++ b/src/main.zig @@ -22,6 +22,7 @@ const params = clap.parseParamsComptime( \\-h, --help Display this help and exit. \\-s, --skip Skip BIOS. \\-b, --bios Optional path to a GBA BIOS ROM. + \\ --gdb Run ZBA from the context of a GDB Server \\ Path to the GBA GamePak ROM. \\ ); @@ -87,10 +88,37 @@ pub fn main() void { cpu.fastBoot(); } - var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e}); - defer gui.deinit(); + if (result.args.gdb) { + const Server = @import("gdbstub").Server; + const EmuThing = @import("core/emu.zig").EmuThing; - gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); + var emu_thing = EmuThing.init(&cpu, &scheduler); + const emulator = emu_thing.interface(); + + { + const frames_per_second: usize = 60; + const emu = @import("core/emu.zig"); + + var i: usize = 0; + while (i < frames_per_second * 120) : (i += 1) { + emu.runFrame(&scheduler, &cpu); + + std.debug.print("Frame {:0>3}/{:0>3}\r", .{ i, frames_per_second * 120 }); + } + } + + log.info("Ready to connect", .{}); + + var server = Server.init(emulator) catch |e| exitln("failed to init gdb server: {}", .{e}); + defer server.deinit(allocator); + + server.run(allocator) catch |e| exitln("gdb server crashed: {}", .{e}); + } else { + var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e}); + defer gui.deinit(); + + gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); + } } fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !FilePaths { -- 2.34.1 From 6709f8c551829b01e985cdb4842f279b1d578c5b Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 26 Jan 2023 22:24:55 -0600 Subject: [PATCH 3/9] chore: update gdbstub lib --- lib/zba-gdbstub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index 4bca44e..82bad92 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit 4bca44e5f2a4aa19361b51b7617a21f46f88eef4 +Subproject commit 82bad92fcf92415f5ee1a1c3c0432c48073a7139 -- 2.34.1 From 755115660bfd58a719e2409708b38097e12114d7 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sun, 29 Jan 2023 07:03:46 -0600 Subject: [PATCH 4/9] feat: allow gdb writes to certain mem regions --- lib/zba-gdbstub | 2 +- src/core/Bus.zig | 148 +++++++++++++++++++++++++++++++---------------- src/core/emu.zig | 6 +- 3 files changed, 101 insertions(+), 55 deletions(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index 82bad92..81ff227 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit 82bad92fcf92415f5ee1a1c3c0432c48073a7139 +Subproject commit 81ff227ea76d5b44a89e422e380e0fe15aec02aa diff --git a/src/core/Bus.zig b/src/core/Bus.zig index 66c1ebf..29e9076 100644 --- a/src/core/Bus.zig +++ b/src/core/Bus.zig @@ -198,55 +198,6 @@ fn fillReadTableExternal(self: *Self, addr: u32) ?*anyopaque { return &self.pak.buf[masked_addr]; } -pub fn dbgRead(self: *const Self, comptime T: type, unaligned_address: u32) T { - const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; - const page = unaligned_address >> bits; - const offset = unaligned_address & (page_size - 1); - - // We're doing some serious out-of-bounds open-bus reads - if (page >= table_len) return self.openBus(T, unaligned_address); - - if (self.read_table[page]) |some_ptr| { - // We have a pointer to a page, cast the pointer to it's underlying type - const Ptr = [*]const T; - const ptr = @ptrCast(Ptr, @alignCast(@alignOf(std.meta.Child(Ptr)), some_ptr)); - - // Note: We don't check array length, since we force align the - // lower bits of the address as the GBA would - return ptr[forceAlign(T, offset) / @sizeOf(T)]; - } - - return self.dbgSlowRead(T, unaligned_address); -} - -fn dbgSlowRead(self: *const Self, comptime T: type, unaligned_address: u32) T { - const page = @truncate(u8, unaligned_address >> 24); - const address = forceAlign(T, unaligned_address); - - return switch (page) { - // General Internal Memory - 0x00 => blk: { - if (address < Bios.size) - break :blk self.bios.dbgRead(T, self.cpu.r[15], unaligned_address); - - break :blk self.openBus(T, address); - }, - 0x02 => unreachable, // handled by fastmem - 0x03 => unreachable, // handled by fastmem - 0x04 => self.readIo(T, address), - - // Internal Display Memory - 0x05 => unreachable, // handled by fastmem - 0x06 => unreachable, // handled by fastmem - 0x07 => unreachable, // handled by fastmem - - // External Memory (Game Pak) - 0x08...0x0D => self.pak.dbgRead(T, address), - 0x0E...0x0F => self.readBackup(T, unaligned_address), - else => self.openBus(T, address), - }; -} - fn readIo(self: *const Self, comptime T: type, address: u32) T { return io.read(self, T, address) orelse self.openBus(T, address); } @@ -336,6 +287,27 @@ pub fn read(self: *Self, comptime T: type, unaligned_address: u32) T { return self.slowRead(T, unaligned_address); } +pub fn dbgRead(self: *const Self, comptime T: type, unaligned_address: u32) T { + const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; + const page = unaligned_address >> bits; + const offset = unaligned_address & (page_size - 1); + + // We're doing some serious out-of-bounds open-bus reads + if (page >= table_len) return self.openBus(T, unaligned_address); + + if (self.read_table[page]) |some_ptr| { + // We have a pointer to a page, cast the pointer to it's underlying type + const Ptr = [*]const T; + const ptr = @ptrCast(Ptr, @alignCast(@alignOf(std.meta.Child(Ptr)), some_ptr)); + + // Note: We don't check array length, since we force align the + // lower bits of the address as the GBA would + return ptr[forceAlign(T, offset) / @sizeOf(T)]; + } + + return self.dbgSlowRead(T, unaligned_address); +} + fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T { @setCold(true); @@ -366,6 +338,34 @@ fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T { }; } +fn dbgSlowRead(self: *const Self, comptime T: type, unaligned_address: u32) T { + const page = @truncate(u8, unaligned_address >> 24); + const address = forceAlign(T, unaligned_address); + + return switch (page) { + // General Internal Memory + 0x00 => blk: { + if (address < Bios.size) + break :blk self.bios.dbgRead(T, self.cpu.r[15], unaligned_address); + + break :blk self.openBus(T, address); + }, + 0x02 => unreachable, // handled by fastmem + 0x03 => unreachable, // handled by fastmem + 0x04 => self.readIo(T, address), + + // Internal Display Memory + 0x05 => unreachable, // handled by fastmem + 0x06 => unreachable, // handled by fastmem + 0x07 => unreachable, // handled by fastmem + + // External Memory (Game Pak) + 0x08...0x0D => self.pak.dbgRead(T, address), + 0x0E...0x0F => self.readBackup(T, unaligned_address), + else => self.openBus(T, address), + }; +} + fn readBackup(self: *const Self, comptime T: type, unaligned_address: u32) T { const value = self.pak.backup.read(unaligned_address); @@ -406,6 +406,31 @@ pub fn write(self: *Self, comptime T: type, unaligned_address: u32, value: T) vo } } +/// Mostly Identical to `Bus.write`, slowmeme is handled by `Bus.dbgSlowWrite` +pub fn dbgWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { + const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; + const page = unaligned_address >> bits; + const offset = unaligned_address & (page_size - 1); + + // We're doing some serious out-of-bounds open-bus writes, they do nothing though + if (page >= table_len) return; + + if (self.write_tables[@boolToInt(T == u8)][page]) |some_ptr| { + // We have a pointer to a page, cast the pointer to it's underlying type + const Ptr = [*]T; + const ptr = @ptrCast(Ptr, @alignCast(@alignOf(std.meta.Child(Ptr)), some_ptr)); + + // Note: We don't check array length, since we force align the + // lower bits of the address as the GBA would + ptr[forceAlign(T, offset) / @sizeOf(T)] = value; + } else { + // we can return early if this is an 8-bit OAM write + if (T == u8 and @truncate(u8, unaligned_address >> 24) == 0x07) return; + + self.dbgSlowWrite(T, unaligned_address, value); + } +} + fn slowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { @setCold(true); @@ -431,6 +456,31 @@ fn slowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) vo } } +fn dbgSlowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { + @setCold(true); + + const page = @truncate(u8, unaligned_address >> 24); + const address = forceAlign(T, unaligned_address); + + switch (page) { + // General Internal Memory + 0x00 => self.bios.write(T, address, value), + 0x02 => unreachable, // completely handled by fastmem + 0x03 => unreachable, // completely handled by fastmem + 0x04 => return, // FIXME: Let debug writes mess with I/O + + // Internal Display Memory + 0x05 => self.ppu.palette.write(T, address, value), + 0x06 => self.ppu.vram.write(T, self.ppu.dispcnt, address, value), + 0x07 => unreachable, // completely handled by fastmem + + // External Memory (Game Pak) + 0x08...0x0D => return, // FIXME: Debug Write to Backup/GPIO w/out messing with state + 0x0E...0x0F => return, // FIXME: Debug Write to Backup w/out messing with state + else => {}, + } +} + inline fn rotateBy(comptime T: type, address: u32) u32 { return switch (T) { u32 => address & 3, diff --git a/src/core/emu.zig b/src/core/emu.zig index ece7e58..6226d71 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -183,11 +183,7 @@ pub const EmuThing = struct { } pub fn write(self: *Self, addr: u32, value: u8) void { - _ = value; - _ = self; - _ = addr; - - std.debug.panic("TODO: Implement Debug Writes?", .{}); + self.cpu.bus.dbgWrite(u8, addr, value); } pub fn registers(self: *const Self) *[16]u32 { -- 2.34.1 From 518b868249eb785eb55e0fc9278c1b77825a06d5 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sun, 29 Jan 2023 07:06:06 -0600 Subject: [PATCH 5/9] feat: respond to API changes for software bkpts --- lib/zba-gdbstub | 2 +- src/core/emu.zig | 5 +++-- src/main.zig | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index 81ff227..d7b8d7a 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit 81ff227ea76d5b44a89e422e380e0fe15aec02aa +Subproject commit d7b8d7acb15ce3518480c59218bf2c0a17fa529f diff --git a/src/core/emu.zig b/src/core/emu.zig index 6226d71..1dc1d1e 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -166,6 +166,7 @@ fn spinLoop(timer: *Timer, wake_time: u64) void { pub const EmuThing = struct { const Self = @This(); const Interface = @import("gdbstub").Emulator; + const Allocator = std.mem.Allocator; cpu: *Arm7tdmi, scheduler: *Scheduler, @@ -174,8 +175,8 @@ pub const EmuThing = struct { return .{ .cpu = cpu, .scheduler = scheduler }; } - pub fn interface(self: *Self) Interface { - return Interface.init(self); + pub fn interface(self: *Self, allocator: Allocator) Interface { + return Interface.init(allocator, self); } pub fn read(self: *const Self, addr: u32) u8 { diff --git a/src/main.zig b/src/main.zig index cc89a24..6b8ef0c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -92,8 +92,9 @@ pub fn main() void { const Server = @import("gdbstub").Server; const EmuThing = @import("core/emu.zig").EmuThing; - var emu_thing = EmuThing.init(&cpu, &scheduler); - const emulator = emu_thing.interface(); + var wrapper = EmuThing.init(&cpu, &scheduler); + var emulator = wrapper.interface(allocator); + defer emulator.deinit(); { const frames_per_second: usize = 60; -- 2.34.1 From 2798a90d83c01e08e789c78b5b536e3678fe6d46 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 7 Feb 2023 17:05:38 -0600 Subject: [PATCH 6/9] chore: update zba-gdbstub to zig master --- lib/zba-gdbstub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index d7b8d7a..c1158b5 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit d7b8d7acb15ce3518480c59218bf2c0a17fa529f +Subproject commit c1158b547e54d82f5ac7aba4cf372c6e061b1eab -- 2.34.1 From 49706842af25080756e308f25496e3848f89727b Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 13 Feb 2023 17:15:39 -0600 Subject: [PATCH 7/9] fix: run more than just the CPU when stepping via gdb --- src/core/emu.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/emu.zig b/src/core/emu.zig index 1dc1d1e..1a84c11 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -199,15 +199,18 @@ pub const EmuThing = struct { const cpu = self.cpu; const sched = self.scheduler; + // Is true when we have executed one (1) instruction + var did_step: bool = false; + // TODO: How can I make it easier to keep this in lock-step with runFrame? - while (true) { + while (!did_step) { if (!cpu.stepDmaTransfer()) { if (cpu.isHalted()) { // Fast-forward to next Event sched.tick = sched.queue.peek().?.tick; } else { cpu.step(); - break; // this function won't return until we've actually stepped once + did_step = true; } } -- 2.34.1 From 01f54101806826b49ad843adafff53c30b399c77 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 13 Feb 2023 20:01:40 -0600 Subject: [PATCH 8/9] feat: allow gui and gdbstub to run in parallel --- lib/zba-gdbstub | 2 +- src/core/ppu.zig | 3 +-- src/main.zig | 47 +++++++++++++++++++++++++++++------------------ src/platform.zig | 28 ++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index c1158b5..6d6a109 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit c1158b547e54d82f5ac7aba4cf372c6e061b1eab +Subproject commit 6d6a109a086361d300da675db8445ede74be58f6 diff --git a/src/core/ppu.zig b/src/core/ppu.zig index 9e37e4c..a6da4a9 100644 --- a/src/core/ppu.zig +++ b/src/core/ppu.zig @@ -986,8 +986,7 @@ pub const Ppu = struct { cpu.handleInterrupt(); } - // See if HBlank DMA is present and not enabled - + // If we're not also in VBlank, attempt to run any pending DMA Reqs if (!self.dispstat.vblank.read()) dma.onBlanking(cpu.bus, .HBlank); diff --git a/src/main.zig b/src/main.zig index 6b8ef0c..ba2666e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,14 +4,17 @@ const known_folders = @import("known_folders"); const clap = @import("clap"); const config = @import("config.zig"); +const emu = @import("core/emu.zig"); const Gui = @import("platform.zig").Gui; const Bus = @import("core/Bus.zig"); const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi; const Scheduler = @import("core/scheduler.zig").Scheduler; const FilePaths = @import("util.zig").FilePaths; - +const FpsTracker = @import("util.zig").FpsTracker; const Allocator = std.mem.Allocator; +const Atomic = std.atomic.Atomic; + const log = std.log.scoped(.Cli); const width = @import("core/ppu.zig").width; const height = @import("core/ppu.zig").height; @@ -88,6 +91,10 @@ pub fn main() void { cpu.fastBoot(); } + var quit = Atomic(bool).init(false); + var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e}); + defer gui.deinit(); + if (result.args.gdb) { const Server = @import("gdbstub").Server; const EmuThing = @import("core/emu.zig").EmuThing; @@ -96,29 +103,33 @@ pub fn main() void { var emulator = wrapper.interface(allocator); defer emulator.deinit(); - { - const frames_per_second: usize = 60; - const emu = @import("core/emu.zig"); - - var i: usize = 0; - while (i < frames_per_second * 120) : (i += 1) { - emu.runFrame(&scheduler, &cpu); - - std.debug.print("Frame {:0>3}/{:0>3}\r", .{ i, frames_per_second * 120 }); - } - } - log.info("Ready to connect", .{}); var server = Server.init(emulator) catch |e| exitln("failed to init gdb server: {}", .{e}); defer server.deinit(allocator); - server.run(allocator) catch |e| exitln("gdb server crashed: {}", .{e}); - } else { - var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e}); - defer gui.deinit(); + log.info("Starting GDB Server Thread", .{}); - gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); + const thread = std.Thread.spawn(.{}, Server.run, .{ &server, allocator, &quit }) catch |e| exitln("gdb server thread crashed: {}", .{e}); + defer thread.join(); + + gui.run(.{ + .cpu = &cpu, + .scheduler = &scheduler, + .quit = &quit, + }) catch |e| exitln("main thread panicked: {}", .{e}); + } else { + var tracker = FpsTracker.init(); + + const thread = std.Thread.spawn(.{}, emu.run, .{ &quit, &scheduler, &cpu, &tracker }) catch |e| exitln("emu thread panicked: {}", .{e}); + defer thread.join(); + + gui.run(.{ + .cpu = &cpu, + .scheduler = &scheduler, + .tracker = &tracker, + .quit = &quit, + }) catch |e| exitln("main thread panicked: {}", .{e}); } } diff --git a/src/platform.zig b/src/platform.zig index 44316a6..c5edb98 100644 --- a/src/platform.zig +++ b/src/platform.zig @@ -154,9 +154,17 @@ pub const Gui = struct { return tex_id; } - pub fn run(self: *Self, cpu: *Arm7tdmi, scheduler: *Scheduler) !void { - var quit = std.atomic.Atomic(bool).init(false); - var tracker = FpsTracker.init(); + const RunOptions = struct { + quit: *std.atomic.Atomic(bool), + tracker: ?*FpsTracker = null, + cpu: *Arm7tdmi, + scheduler: *Scheduler, + }; + + pub fn run(self: *Self, opt: RunOptions) !void { + const cpu = opt.cpu; + const tracker = opt.tracker; + const quit = opt.quit; var buffer_ids = Self.generateBuffers(); defer { @@ -169,13 +177,15 @@ pub const Gui = struct { const tex_id = Self.generateTexture(cpu.bus.ppu.framebuf.get(.Renderer)); defer gl.deleteTextures(1, &tex_id); - const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, scheduler, cpu, &tracker }); - defer thread.join(); - var title_buf: [0x100]u8 = undefined; emu_loop: while (true) { var event: SDL.SDL_Event = undefined; + + // This might be true if the emu is running via a gdbstub server + // and the gdb stub exits first + if (quit.load(.Monotonic)) break :emu_loop; + while (SDL.SDL_PollEvent(&event) != 0) { switch (event.type) { SDL.SDL_QUIT => break :emu_loop, @@ -238,8 +248,10 @@ pub const Gui = struct { gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, null); SDL.SDL_GL_SwapWindow(self.window); - const dyn_title = std.fmt.bufPrintZ(&title_buf, "ZBA | {s} [Emu: {}fps] ", .{ self.title, tracker.value() }) catch unreachable; - SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); + if (tracker) |t| { + const dyn_title = std.fmt.bufPrintZ(&title_buf, "ZBA | {s} [Emu: {}fps] ", .{ self.title, t.value() }) catch unreachable; + SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); + } } quit.store(true, .Monotonic); // Terminate Emulator Thread -- 2.34.1 From ddf459916247a342b4f1ed32c03baf853baeec9e Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 23 Feb 2023 02:45:59 -0600 Subject: [PATCH 9/9] chore: update dependencies --- lib/zba-gdbstub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/zba-gdbstub b/lib/zba-gdbstub index 6d6a109..acb5999 160000 --- a/lib/zba-gdbstub +++ b/lib/zba-gdbstub @@ -1 +1 @@ -Subproject commit 6d6a109a086361d300da675db8445ede74be58f6 +Subproject commit acb59994fcfb83d36efef47810a9dc791c6e542e -- 2.34.1