Compare commits

..

No commits in common. "ef5244374540a40a597a4258f1954896cbe165fc" and "63fa972afae7e1b6db6f7b598c329cf72c6cfb9d" have entirely different histories.

6 changed files with 33 additions and 138 deletions

3
.gitmodules vendored
View File

@ -13,6 +13,3 @@
[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

View File

@ -1,8 +1,6 @@
const std = @import("std"); 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");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
// Minimum Zig Version // Minimum Zig Version
@ -45,9 +43,6 @@ pub fn build(b: *std.build.Builder) void {
// OpenGL 3.3 Bindings // OpenGL 3.3 Bindings
exe.addPackagePath("gl", "lib/gl.zig"); exe.addPackagePath("gl", "lib/gl.zig");
// gdbstub
Gdbstub.link(exe);
// Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig // Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig
const sdk = Sdk.init(b); const sdk = Sdk.init(b);
sdk.link(exe, .dynamic); sdk.link(exe, .dynamic);

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

View File

@ -197,29 +197,8 @@ fn fillTableExternalMemory(bus: *Self, addr: usize) ?*anyopaque {
return &bus.pak.buf[masked_addr]; return &bus.pak.buf[masked_addr];
} }
// TODO: Take advantage of fastmem here too?
pub fn dbgRead(self: *const Self, comptime T: type, unaligned_address: u32) T { 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 alignment = @alignOf(std.meta.Child(Ptr));
const ptr = @ptrCast(Ptr, @alignCast(alignment, 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 page = @truncate(u8, unaligned_address >> 24);
const address = forceAlign(T, unaligned_address); const address = forceAlign(T, unaligned_address);
@ -231,18 +210,29 @@ fn dbgSlowRead(self: *const Self, comptime T: type, unaligned_address: u32) T {
break :blk self.openBus(T, address); break :blk self.openBus(T, address);
}, },
0x02 => unreachable, // handled by fastmem 0x02 => self.ewram.read(T, address),
0x03 => unreachable, // handled by fastmem 0x03 => self.iwram.read(T, address),
0x04 => self.readIo(T, address), 0x04 => self.readIo(T, address),
// Internal Display Memory // Internal Display Memory
0x05 => unreachable, // handled by fastmem 0x05 => self.ppu.palette.read(T, address),
0x06 => unreachable, // handled by fastmem 0x06 => self.ppu.vram.read(T, address),
0x07 => unreachable, // handled by fastmem 0x07 => self.ppu.oam.read(T, address),
// External Memory (Game Pak) // External Memory (Game Pak)
0x08...0x0D => self.pak.dbgRead(T, address), 0x08...0x0D => self.pak.dbgRead(T, address),
0x0E...0x0F => self.readBackup(T, unaligned_address), 0x0E...0x0F => blk: {
const value = self.pak.backup.read(unaligned_address);
const multiplier = switch (T) {
u32 => 0x01010101,
u16 => 0x0101,
u8 => 1,
else => @compileError("Backup: Unsupported read width"),
};
break :blk @as(T, value) * multiplier;
},
else => self.openBus(T, address), else => self.openBus(T, address),
}; };
} }
@ -362,24 +352,22 @@ fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T {
// External Memory (Game Pak) // External Memory (Game Pak)
0x08...0x0D => self.pak.read(T, address), 0x08...0x0D => self.pak.read(T, address),
0x0E...0x0F => self.readBackup(T, unaligned_address), 0x0E...0x0F => blk: {
const value = self.pak.backup.read(unaligned_address);
const multiplier = switch (T) {
u32 => 0x01010101,
u16 => 0x0101,
u8 => 1,
else => @compileError("Backup: Unsupported read width"),
};
break :blk @as(T, value) * multiplier;
},
else => self.openBus(T, 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);
const multiplier = switch (T) {
u32 => 0x01010101,
u16 => 0x0101,
u8 => 1,
else => @compileError("Backup: Unsupported read width"),
};
return @as(T, value) * multiplier;
}
pub fn write(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { pub fn write(self: *Self, comptime T: type, unaligned_address: u32, value: T) void {
const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits;
const page = unaligned_address >> bits; const page = unaligned_address >> bits;

View File

@ -163,59 +163,3 @@ 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;
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);
}
}
};

View File

@ -22,7 +22,6 @@ 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.
\\ \\
); );
@ -88,37 +87,10 @@ pub fn main() void {
cpu.fastBoot(); cpu.fastBoot();
} }
if (result.args.gdb) { var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e});
const Server = @import("gdbstub").Server; defer gui.deinit();
const EmuThing = @import("core/emu.zig").EmuThing;
var emu_thing = EmuThing.init(&cpu, &scheduler); gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e});
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});
}
} }
pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths { pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths {