chore: cleanup BIOS struct init code

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-12-27 06:42:06 -06:00
parent caaa60d1a8
commit 1ecbbc7d29
1 changed files with 12 additions and 11 deletions

View File

@ -13,7 +13,7 @@ const Self = @This();
buf: ?[]u8, buf: ?[]u8,
allocator: Allocator, allocator: Allocator,
addr_latch: u32, addr_latch: u32 = 0,
// https://github.com/ITotalJustice/notorious_beeg/issues/106 // https://github.com/ITotalJustice/notorious_beeg/issues/106
pub fn read(self: *Self, comptime T: type, r15: u32, address: u32) T { pub fn read(self: *Self, comptime T: type, r15: u32, address: u32) T {
@ -62,18 +62,19 @@ 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 {
const buf: ?[]u8 = if (maybe_path) |path| blk: { if (maybe_path == null) return .{ .buf = null, .allocator = allocator };
const file = try std.fs.cwd().openFile(path, .{}); const path = maybe_path.?;
defer file.close();
break :blk try file.readToEndAlloc(allocator, try file.getEndPos()); const buf = try allocator.alloc(u8, Self.size);
} else null; errdefer allocator.free(buf);
return Self{ const file = try std.fs.cwd().openFile(path, .{});
.buf = buf, defer file.close();
.allocator = allocator,
.addr_latch = 0, 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 };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {