feat: replace Gamepak

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-03-10 02:50:05 -06:00
parent f8477714ae
commit 5adbc354d6
3 changed files with 24 additions and 1 deletions

View File

@ -128,6 +128,21 @@ pub fn reset(self: *Self) void {
self.io.reset(); self.io.reset();
} }
pub fn replaceGamepak(self: *Self, file_path: []const u8) !void {
// Note: `save_path` isn't owned by `Backup`
const save_path = self.pak.backup.save_path;
self.pak.deinit();
self.pak = try GamePak.init(self.allocator, self.cpu, file_path, save_path);
const read_ptr: *[table_len]?*const anyopaque = @constCast(self.read_table);
const write_ptrs: [2]*[table_len]?*anyopaque = .{ @constCast(self.write_tables[0]), @constCast(self.write_tables[1]) };
self.fillReadTable(read_ptr);
self.fillWriteTable(u32, write_ptrs[0]);
self.fillWriteTable(u8, write_ptrs[1]);
}
fn fillReadTable(self: *Self, table: *[table_len]?*const anyopaque) void { fn fillReadTable(self: *Self, table: *[table_len]?*const anyopaque) void {
const vramMirror = @import("ppu/Vram.zig").mirror; const vramMirror = @import("ppu/Vram.zig").mirror;

View File

@ -249,3 +249,8 @@ pub fn reset(cpu: *Arm7tdmi) void {
cpu.bus.reset(); cpu.bus.reset();
cpu.reset(); cpu.reset();
} }
pub fn replaceGamepak(cpu: *Arm7tdmi, file_path: []const u8) !void {
try cpu.bus.replaceGamepak(file_path);
reset(cpu);
}

View File

@ -70,7 +70,10 @@ pub fn draw(state: *State, tex_id: GLuint, cpu: *Arm7tdmi) void {
defer nfd.freePath(file_path); defer nfd.freePath(file_path);
log.info("user chose: \"{s}\"", .{file_path}); log.info("user chose: \"{s}\"", .{file_path});
// emu.loadRom(cpu, file_path); emu.replaceGamepak(cpu, file_path) catch |e| {
log.err("failed to replace GamePak: {}", .{e});
break :blk;
};
} }
} }
} }