Compare commits

..

24 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka c5136e1464 fix: ensure code builds + works
the gdbstub branch got merged into main, rebasing on top of main led to
a bunch of merge conflicts that had to be resolved. Unfortunately some
things got missed, and this commit covers the immediate problems that
the rebase caused
2023-02-23 17:23:51 -06:00
Rekai Nyangadzayi Musuka c7d562b9ac chore: update for loop in RingBuffer impl 2023-02-23 16:38:02 -06:00
Rekai Nyangadzayi Musuka 71dd667d89 chore: update gui libs to latest zig master 2023-02-23 16:38:02 -06:00
Rekai Nyangadzayi Musuka e5850d51bb chore: add gui deps to README.md 2023-02-23 16:38:02 -06:00
Rekai Nyangadzayi Musuka a901602ce5 chore: update nfd-zig
respond to build.zig changes in zig master
2023-02-23 16:38:02 -06:00
Rekai Nyangadzayi Musuka c39baef28f feat: implement menu bar + add file picker dep 2023-02-23 16:38:00 -06:00
Rekai Nyangadzayi Musuka c0083e242b feat: show game title as imgui screen title 2023-02-23 16:34:22 -06:00
Rekai Nyangadzayi Musuka 23bc42f2d6 chore: update zgui 2023-02-23 16:34:22 -06:00
Rekai Nyangadzayi Musuka 6de1da7aa0 feat: add scheduler ui 2023-02-23 16:34:22 -06:00
Rekai Nyangadzayi Musuka dac0928d77 feat: pause emu when UI reads emu state 2023-02-23 16:34:21 -06:00
Rekai Nyangadzayi Musuka 51dcc0147a feat: implement ui for register, interrupt 2023-02-23 16:33:05 -06:00
Rekai Nyangadzayi Musuka 5f4f2f50a0 feat: add system information window 2023-02-23 16:33:03 -06:00
Rekai Nyangadzayi Musuka 9fe871c30c fix: update zgui to work with sdl2 vcpkg package 2023-02-23 16:31:02 -06:00
Rekai Nyangadzayi Musuka 7f533210f7 feat: add imgui support using zgui 2023-02-23 16:30:59 -06:00
Rekai Nyangadzayi Musuka 7234ecab37 Merge pull request 'Implement a GDBSTUB Server' (#6) from gdbstub into main
Reviewed-on: #6
2023-02-23 22:18:26 +00:00
Rekai Nyangadzayi Musuka ddf4599162 chore: update dependencies 2023-02-23 02:45:59 -06:00
Rekai Nyangadzayi Musuka 01f5410180 feat: allow gui and gdbstub to run in parallel 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 49706842af fix: run more than just the CPU when stepping via gdb 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 2798a90d83 chore: update zba-gdbstub to zig master 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 518b868249 feat: respond to API changes for software bkpts 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 755115660b feat: allow gdb writes to certain mem regions 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 6709f8c551 chore: update gdbstub lib 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 1f3cdd9513 feat: add gdb support to zba 2023-02-23 02:40:24 -06:00
Rekai Nyangadzayi Musuka 65af6aa499 feat: add gdbstub library 2023-02-23 02:40:23 -06:00
8 changed files with 247 additions and 79 deletions

3
.gitmodules vendored
View File

@ -13,6 +13,9 @@
[submodule "lib/zig-toml"] [submodule "lib/zig-toml"]
path = lib/zig-toml path = lib/zig-toml
url = https://github.com/aeronavery/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
[submodule "lib/zgui"] [submodule "lib/zgui"]
path = lib/zgui path = lib/zgui
url = https://git.musuka.dev/paoda/zgui url = https://git.musuka.dev/paoda/zgui

View File

@ -2,6 +2,7 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const Sdk = @import("lib/SDL.zig/Sdk.zig"); const Sdk = @import("lib/SDL.zig/Sdk.zig");
const gdbstub = @import("lib/zba-gdbstub/build.zig");
const zgui = @import("lib/zgui/build.zig"); const zgui = @import("lib/zgui/build.zig");
const nfd = @import("lib/nfd-zig/build.zig"); const nfd = @import("lib/nfd-zig/build.zig");
@ -43,6 +44,8 @@ pub fn build(b: *std.build.Builder) void {
// OpenGL 3.3 Bindings // OpenGL 3.3 Bindings
exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } }); exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } });
// gdbstub
gdbstub.link(exe);
// NativeFileDialog(ue) Bindings // NativeFileDialog(ue) Bindings
exe.linkLibrary(nfd.makeLib(b, target, optimize)); exe.linkLibrary(nfd.makeLib(b, target, optimize));
exe.addModule("nfd", nfd.getModule(b)); exe.addModule("nfd", nfd.getModule(b));

1
lib/zba-gdbstub Submodule

@ -0,0 +1 @@
Subproject commit acb59994fcfb83d36efef47810a9dc791c6e542e

View File

@ -198,55 +198,6 @@ fn fillReadTableExternal(self: *Self, addr: u32) ?*anyopaque {
return &self.pak.buf[masked_addr]; 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 { fn readIo(self: *const Self, comptime T: type, address: u32) T {
return io.read(self, T, address) orelse self.openBus(T, address); 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); 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 { fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T {
@setCold(true); @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 { fn readBackup(self: *const Self, comptime T: type, unaligned_address: u32) T {
const value = self.pak.backup.read(unaligned_address); 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 { fn slowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void {
@setCold(true); @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 { inline fn rotateBy(comptime T: type, address: u32) u32 {
return switch (T) { return switch (T) {
u32 => address & 3, u32 => address & 3,

View File

@ -166,3 +166,59 @@ fn sleep(timer: *Timer, wake_time: u64) ?u64 {
fn spinLoop(timer: *Timer, wake_time: u64) void { fn spinLoop(timer: *Timer, wake_time: u64) void {
while (true) if (timer.read() > wake_time) break; while (true) if (timer.read() > wake_time) break;
} }
pub const EmuThing = struct {
const Self = @This();
const Interface = @import("gdbstub").Emulator;
const Allocator = std.mem.Allocator;
cpu: *Arm7tdmi,
scheduler: *Scheduler,
pub fn init(cpu: *Arm7tdmi, scheduler: *Scheduler) Self {
return .{ .cpu = cpu, .scheduler = scheduler };
}
pub fn interface(self: *Self, allocator: Allocator) Interface {
return Interface.init(allocator, 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 {
self.cpu.bus.dbgWrite(u8, addr, value);
}
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;
// 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 (!did_step) {
if (!cpu.stepDmaTransfer()) {
if (cpu.isHalted()) {
// Fast-forward to next Event
sched.tick = sched.queue.peek().?.tick;
} else {
cpu.step();
did_step = true;
}
}
if (sched.tick >= sched.nextTimestamp()) sched.handleEvent(cpu);
}
}
};

View File

@ -986,8 +986,7 @@ pub const Ppu = struct {
cpu.handleInterrupt(); 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()) if (!self.dispstat.vblank.read())
dma.onBlanking(cpu.bus, .HBlank); dma.onBlanking(cpu.bus, .HBlank);

View File

@ -4,14 +4,17 @@ const known_folders = @import("known_folders");
const clap = @import("clap"); const clap = @import("clap");
const config = @import("config.zig"); const config = @import("config.zig");
const emu = @import("core/emu.zig");
const Gui = @import("platform.zig").Gui; const Gui = @import("platform.zig").Gui;
const Bus = @import("core/Bus.zig"); const Bus = @import("core/Bus.zig");
const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi; const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi;
const Scheduler = @import("core/scheduler.zig").Scheduler; const Scheduler = @import("core/scheduler.zig").Scheduler;
const FilePaths = @import("util.zig").FilePaths; const FilePaths = @import("util.zig").FilePaths;
const FpsTracker = @import("util.zig").FpsTracker;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Atomic = std.atomic.Atomic;
const log = std.log.scoped(.Cli); const log = std.log.scoped(.Cli);
pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level; pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level;
@ -20,6 +23,7 @@ const params = clap.parseParamsComptime(
\\-h, --help Display this help and exit. \\-h, --help Display this help and exit.
\\-s, --skip Skip BIOS. \\-s, --skip Skip BIOS.
\\-b, --bios <str> Optional path to a GBA BIOS ROM. \\-b, --bios <str> Optional path to a GBA BIOS ROM.
\\ --gdb Run ZBA from the context of a GDB Server
\\<str> Path to the GBA GamePak ROM. \\<str> Path to the GBA GamePak ROM.
\\ \\
); );
@ -89,7 +93,46 @@ pub fn main() void {
var gui = Gui.init(allocator, bus.pak.title, &bus.apu) catch |e| exitln("failed to init gui: {}", .{e}); var gui = Gui.init(allocator, bus.pak.title, &bus.apu) catch |e| exitln("failed to init gui: {}", .{e});
defer gui.deinit(); defer gui.deinit();
gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); var quit = Atomic(bool).init(false);
if (result.args.gdb) {
const Server = @import("gdbstub").Server;
const EmuThing = @import("core/emu.zig").EmuThing;
var wrapper = EmuThing.init(&cpu, &scheduler);
var emulator = wrapper.interface(allocator);
defer emulator.deinit();
log.info("Ready to connect", .{});
var server = Server.init(emulator) catch |e| exitln("failed to init gdb server: {}", .{e});
defer server.deinit(allocator);
log.info("Starting GDB Server Thread", .{});
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();
var pause = Atomic(bool).init(false);
const thread = std.Thread.spawn(.{}, emu.run, .{ &quit, &pause, &cpu, &scheduler, &tracker }) catch |e| exitln("emu thread panicked: {}", .{e});
defer thread.join();
gui.run(.{
.cpu = &cpu,
.scheduler = &scheduler,
.tracker = &tracker,
.quit = &quit,
.pause = &pause,
}) catch |e| exitln("main thread panicked: {}", .{e});
}
} }
fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths { fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths {

View File

@ -307,16 +307,13 @@ pub const Gui = struct {
_ = zgui.begin("Information", .{}); _ = zgui.begin("Information", .{});
defer zgui.end(); defer zgui.end();
{ for (0..8) |i| {
var i: usize = 0; zgui.text("R{}: 0x{X:0>8}", .{ i, cpu.r[i] });
while (i < 8) : (i += 1) {
zgui.text("R{}: 0x{X:0>8}", .{ i, cpu.r[i] });
zgui.sameLine(.{}); zgui.sameLine(.{});
const prefix = if (8 + i < 10) " " else ""; const prefix = if (8 + i < 10) " " else "";
zgui.text("{s}R{}: 0x{X:0>8}", .{ prefix, 8 + i, cpu.r[8 + i] }); zgui.text("{s}R{}: 0x{X:0>8}", .{ prefix, 8 + i, cpu.r[8 + i] });
}
} }
zgui.separator(); zgui.separator();
@ -415,12 +412,25 @@ pub const Gui = struct {
} }
} }
{ // {
zgui.showDemoWindow(null); // zgui.showDemoWindow(null);
} // }
} }
pub fn run(self: *Self, cpu: *Arm7tdmi, scheduler: *Scheduler) !void { const RunOptions = struct {
quit: *std.atomic.Atomic(bool),
pause: ?*std.atomic.Atomic(bool) = null,
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;
const pause = opt.pause;
const obj_ids = Self.genBufferObjects(); const obj_ids = Self.genBufferObjects();
defer gl.deleteBuffers(3, @as(*const [3]c_uint, &obj_ids)); defer gl.deleteBuffers(3, @as(*const [3]c_uint, &obj_ids));
@ -431,20 +441,16 @@ pub const Gui = struct {
const fbo_id = try Self.genFrameBufObject(out_tex); const fbo_id = try Self.genFrameBufObject(out_tex);
defer gl.deleteFramebuffers(1, &fbo_id); defer gl.deleteFramebuffers(1, &fbo_id);
var tracker = FpsTracker.init();
var quit = std.atomic.Atomic(bool).init(false);
var pause = std.atomic.Atomic(bool).init(false);
const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, &pause, cpu, scheduler, &tracker });
defer thread.join();
defer quit.store(true, .Monotonic); // Terminate Emulator Thread
emu_loop: while (true) { emu_loop: while (true) {
var event: SDL.SDL_Event = undefined; // `quit` from RunOptions may be modified by the GDBSTUB thread,
// so we want to recognize that it may change to `true` and exit the GUI thread
if (quit.load(.Monotonic)) break :emu_loop;
// Quit Signal from Dear Imgui // Outside of `SDL.SDL_QUIT` below, the DearImgui UI might signal that the program
// should exit, in which case we should also handle this
if (self.state.should_quit) break :emu_loop; if (self.state.should_quit) break :emu_loop;
var event: SDL.SDL_Event = undefined;
while (SDL.SDL_PollEvent(&event) != 0) { while (SDL.SDL_PollEvent(&event) != 0) {
_ = zgui.backend.processEvent(&event); _ = zgui.backend.processEvent(&event);
@ -494,12 +500,17 @@ pub const Gui = struct {
} }
} }
// We Access non-atomic parts of the Emulator here // If `Gui.run` has been passed with a `pause` atomic, we should
// pause the emulation thread while we access the data there
{ {
pause.store(true, .Monotonic); // TODO: Is there a nicer way to express this?
defer pause.store(false, .Monotonic); if (pause) |val| val.store(true, .Monotonic);
defer if (pause) |val| val.store(false, .Monotonic);
self.state.fps_hist.push(tracker.value()) catch {}; // Add FPS count to the histogram
if (tracker) |t| {
self.state.fps_hist.push(t.value()) catch {};
}
// Draw GBA Screen to Texture // Draw GBA Screen to Texture
{ {
@ -524,6 +535,8 @@ pub const Gui = struct {
SDL.SDL_GL_SwapWindow(self.window); SDL.SDL_GL_SwapWindow(self.window);
} }
quit.store(true, .Monotonic); // Signals to emu thread to exit
} }
fn glGetProcAddress(ctx: SDL.SDL_GLContext, proc: [:0]const u8) ?*anyopaque { fn glGetProcAddress(ctx: SDL.SDL_GLContext, proc: [:0]const u8) ?*anyopaque {