diff --git a/src/core/bus/Bios.zig b/src/core/bus/Bios.zig index 51c6095..2ae0d74 100644 --- a/src/core/bus/Bios.zig +++ b/src/core/bus/Bios.zig @@ -63,18 +63,23 @@ pub fn write(_: *Self, comptime T: type, addr: u32, value: T) void { pub fn init(allocator: Allocator, maybe_path: ?[]const u8) !Self { if (maybe_path == null) return .{ .buf = null, .allocator = allocator }; - const path = maybe_path.?; + const file_path = maybe_path.?; const buf = try allocator.alloc(u8, Self.size); errdefer allocator.free(buf); - const file = try std.fs.cwd().openFile(path, .{}); + var self: Self = .{ .buf = buf, .allocator = allocator }; + try self.load(file_path); + + return self; +} + +pub fn load(self: *Self, file_path: []const u8) !void { + const file = try std.fs.cwd().openFile(file_path, .{}); defer file.close(); - const file_len = try file.readAll(buf); - if (file_len != Self.size) log.err("Expected BIOS to be {}B, was {}B", .{ Self.size, file_len }); - - return Self{ .buf = buf, .allocator = allocator }; + const len = try file.readAll(self.buf orelse return error.UnallocatedBuffer); + if (len != Self.size) log.err("Expected BIOS to be {}B, was {}B", .{ Self.size, len }); } pub fn reset(self: *Self) void { diff --git a/src/core/emu.zig b/src/core/emu.zig index c67f492..251c01f 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -253,3 +253,11 @@ pub fn replaceGamepak(cpu: *Arm7tdmi, file_path: []const u8) !void { try cpu.bus.replaceGamepak(file_path); reset(cpu); } + +pub fn replaceBios(cpu: *Arm7tdmi, file_path: []const u8) !void { + const allocator = cpu.bus.bios.allocator; + const bios_len = 0x4000; + + cpu.bus.bios.buf = try allocator.alloc(u8, bios_len); + try cpu.bus.bios.load(file_path); +} diff --git a/src/imgui.zig b/src/imgui.zig index e754ecc..d7da187 100644 --- a/src/imgui.zig +++ b/src/imgui.zig @@ -103,6 +103,25 @@ pub fn draw(state: *State, win_dim: Dimensions, tex_id: GLuint, cpu: *Arm7tdmi) state.title = handleTitle(&cpu.bus.pak.title); state.emulation = .{ .Transition = .Active }; } + + if (zgui.menuItem("Load BIOS", .{})) blk: { + const maybe_path = nfd.openFileDialog("bin", null) catch |e| { + log.err("failed to open file dialog: {}", .{e}); + break :blk; + }; + + const file_path = maybe_path orelse { + log.warn("did not receive a file path", .{}); + break :blk; + }; + defer nfd.freePath(file_path); + + log.info("user chose: \"{s}\"", .{file_path}); + emu.replaceBios(cpu, file_path) catch |e| { + log.err("failed to replace BIOS: {}", .{e}); + break :blk; + }; + } } if (zgui.beginMenu("Emulation", true)) {