Compare commits

...

8 Commits

10 changed files with 207 additions and 67 deletions

3
.gitmodules vendored
View File

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

@ -13,7 +13,7 @@ This is a simple (read: incomplete) for-fun long-term project. I hope to get "mo
- [x] Affine Sprites - [x] Affine Sprites
- [ ] Windowing (see [this branch](https://git.musuka.dev/paoda/zba/src/branch/window)) - [ ] Windowing (see [this branch](https://git.musuka.dev/paoda/zba/src/branch/window))
- [ ] Audio Resampler (Having issues with SDL2's) - [ ] Audio Resampler (Having issues with SDL2's)
- [ ] Immediate Mode GUI - [x] Immediate Mode GUI (see [this branch](https://git.musuka.dev/paoda/zba/src/branch/imgui))
- [ ] Refactoring for easy-ish perf boosts - [ ] Refactoring for easy-ish perf boosts
## Usage ## Usage
@ -91,17 +91,17 @@ zig-datetime | <https://github.com/frmdstryr/zig-datetime>
`bitfields.zig` | [https://github.com/FlorenceOS/Florence](https://github.com/FlorenceOS/Florence/blob/aaa5a9e568/lib/util/bitfields.zig) `bitfields.zig` | [https://github.com/FlorenceOS/Florence](https://github.com/FlorenceOS/Florence/blob/aaa5a9e568/lib/util/bitfields.zig)
`gl.zig` | <https://github.com/MasterQ32/zig-opengl> `gl.zig` | <https://github.com/MasterQ32/zig-opengl>
Use `git submodule update --init` from the project root to pull the git submodules `SDL.zig`, `zig-clap`, `known-folders`, `zig-toml` and `zig-datetime` Use `git submodule update --init` from the project root to pull the git relevant git submodules
Be sure to provide SDL2 using: Be sure to provide SDL2 using:
- Linux: Your distro's package manager - Linux: Your distro's package manager
- MacOS: ¯\\\_(ツ)_/¯ - MacOS: `brew` (install [this formula](https://formulae.brew.sh/formula/sdl2))
- Windows: [`vcpkg`](https://github.com/Microsoft/vcpkg) (install `sdl2:x64-windows`) - Windows: [`vcpkg`](https://github.com/Microsoft/vcpkg) (install `sdl2:x64-windows`)
`SDL.zig` will provide a helpful compile error if the zig compiler is unable to find SDL2. `SDL.zig` will provide a helpful compile error if the zig compiler is unable to find SDL2.
Once you've got all the dependencies, execute `zig build -Drelease-fast`. The executable is located at `zig-out/bin/`. Once you've got all the dependencies, execute `zig build -Doptimize=ReleaseSafe`. The executable is located at `zig-out/bin/`.
## Controls ## Controls

View File

@ -1,6 +1,8 @@
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
@ -23,28 +25,30 @@ pub fn build(b: *std.build.Builder) void {
exe.setMainPkgPath("."); // Necessary so that src/main.zig can embed example.toml exe.setMainPkgPath("."); // Necessary so that src/main.zig can embed example.toml
// Known Folders (%APPDATA%, XDG, etc.) // Known Folders (%APPDATA%, XDG, etc.)
exe.addPackagePath("known_folders", "lib/known-folders/known-folders.zig"); exe.addAnonymousModule("known_folders", .{ .source_file = .{ .path = "lib/known-folders/known-folders.zig" } });
// DateTime Library // DateTime Library
exe.addPackagePath("datetime", "lib/zig-datetime/src/main.zig"); exe.addAnonymousModule("datetime", .{ .source_file = .{ .path = "lib/zig-datetime/src/main.zig" } });
// Bitfield type from FlorenceOS: https://github.com/FlorenceOS/ // Bitfield type from FlorenceOS: https://github.com/FlorenceOS/
// exe.addPackage(.{ .name = "bitfield", .path = .{ .path = "lib/util/bitfield.zig" } }); exe.addAnonymousModule("bitfield", .{ .source_file = .{ .path = "lib/util/bitfield.zig" } });
exe.addPackagePath("bitfield", "lib/util/bitfield.zig");
// Argument Parsing Library // Argument Parsing Library
exe.addPackagePath("clap", "lib/zig-clap/clap.zig"); exe.addAnonymousModule("clap", .{ .source_file = .{ .path = "lib/zig-clap/clap.zig" } });
// TOML Library // TOML Library
exe.addPackagePath("toml", "lib/zig-toml/src/toml.zig"); exe.addAnonymousModule("toml", .{ .source_file = .{ .path = "lib/zig-toml/src/toml.zig" } });
// OpenGL 3.3 Bindings // OpenGL 3.3 Bindings
exe.addPackagePath("gl", "lib/gl.zig"); exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "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, null); const sdk = Sdk.init(b, null);
sdk.link(exe, .dynamic); sdk.link(exe, .dynamic);
exe.addPackage(sdk.getNativePackage("sdl2")); exe.addModule("sdl2", sdk.getNativeModule());
exe.install(); exe.install();

@ -1 +1 @@
Subproject commit 59a458abd9220703ca4d8b7f99080780c61c2f8c Subproject commit 6b33f1f4299ec8814e9fb3b206cda37791ced574

@ -1 +1 @@
Subproject commit 6b37490ac7285133bf09441850b8102c9728a776 Subproject commit 53fe3b676f32e59d46f4fd201d7ab200e5f6cb98

1
lib/zba-gdbstub Submodule

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

@ -1 +1 @@
Subproject commit 6c9ca9025199b145c42a75d10cadb3f97879ee6d Subproject commit 272d8e2088b2cae037349fb260dc05ec46bba422

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

@ -163,3 +163,56 @@ 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;
// 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,6 +22,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.
\\ \\
); );
@ -87,10 +88,38 @@ pub fn main() void {
cpu.fastBoot(); cpu.fastBoot();
} }
var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e}); if (result.args.gdb) {
defer gui.deinit(); 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 wrapper = EmuThing.init(&cpu, &scheduler);
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();
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 {