feat(ui): add the option to choose the BIOS

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-06-19 15:19:43 -05:00
parent 44818a4d5b
commit e8bc798120
3 changed files with 38 additions and 6 deletions

View File

@ -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 { pub fn init(allocator: Allocator, maybe_path: ?[]const u8) !Self {
if (maybe_path == null) return .{ .buf = null, .allocator = allocator }; 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); const buf = try allocator.alloc(u8, Self.size);
errdefer allocator.free(buf); 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(); defer file.close();
const file_len = try file.readAll(buf); const len = try file.readAll(self.buf orelse return error.UnallocatedBuffer);
if (file_len != Self.size) log.err("Expected BIOS to be {}B, was {}B", .{ Self.size, file_len }); if (len != Self.size) log.err("Expected BIOS to be {}B, was {}B", .{ Self.size, len });
return Self{ .buf = buf, .allocator = allocator };
} }
pub fn reset(self: *Self) void { pub fn reset(self: *Self) void {

View File

@ -253,3 +253,11 @@ pub fn replaceGamepak(cpu: *Arm7tdmi, file_path: []const u8) !void {
try cpu.bus.replaceGamepak(file_path); try cpu.bus.replaceGamepak(file_path);
reset(cpu); 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);
}

View File

@ -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.title = handleTitle(&cpu.bus.pak.title);
state.emulation = .{ .Transition = .Active }; 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)) { if (zgui.beginMenu("Emulation", true)) {