From 5adbc354d649d69cda2c803e65261378e1485ed6 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 10 Mar 2023 02:50:05 -0600 Subject: [PATCH] feat: replace Gamepak --- src/core/Bus.zig | 15 +++++++++++++++ src/core/emu.zig | 5 +++++ src/imgui.zig | 5 ++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/core/Bus.zig b/src/core/Bus.zig index 22b0b31..055e6cb 100644 --- a/src/core/Bus.zig +++ b/src/core/Bus.zig @@ -128,6 +128,21 @@ pub fn reset(self: *Self) void { 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 { const vramMirror = @import("ppu/Vram.zig").mirror; diff --git a/src/core/emu.zig b/src/core/emu.zig index c029943..e009a67 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -249,3 +249,8 @@ pub fn reset(cpu: *Arm7tdmi) void { cpu.bus.reset(); cpu.reset(); } + +pub fn replaceGamepak(cpu: *Arm7tdmi, file_path: []const u8) !void { + try cpu.bus.replaceGamepak(file_path); + reset(cpu); +} diff --git a/src/imgui.zig b/src/imgui.zig index 14454d5..a2dc076 100644 --- a/src/imgui.zig +++ b/src/imgui.zig @@ -70,7 +70,10 @@ pub fn draw(state: *State, tex_id: GLuint, cpu: *Arm7tdmi) void { defer nfd.freePath(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; + }; } } }