zba/src/bus/Bios.zig

49 lines
1.3 KiB
Zig
Raw Normal View History

2022-01-07 23:33:49 +00:00
const std = @import("std");
const Allocator = std.mem.Allocator;
const Self = @This();
2022-02-04 05:55:14 +00:00
buf: ?[]u8,
2022-01-07 23:33:49 +00:00
alloc: Allocator,
2022-02-04 05:55:14 +00:00
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
var buf: ?[]u8 = null;
if (maybe_path) |path| {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
2022-01-07 23:33:49 +00:00
2022-02-04 05:55:14 +00:00
const len = try file.getEndPos();
buf = try file.readToEndAlloc(alloc, len);
}
2022-01-07 23:33:49 +00:00
return Self{
2022-02-04 05:55:14 +00:00
.buf = buf,
2022-01-07 23:33:49 +00:00
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
2022-02-04 05:55:14 +00:00
if (self.buf) |buf| self.alloc.free(buf);
2022-01-07 23:33:49 +00:00
}
2022-01-10 05:24:14 +00:00
pub fn get32(self: *const Self, idx: usize) u32 {
2022-02-04 05:55:14 +00:00
if (self.buf) |buf|
return (@as(u32, buf[idx + 3]) << 24) | (@as(u32, buf[idx + 2]) << 16) | (@as(u32, buf[idx + 1]) << 8) | (@as(u32, buf[idx]));
2022-02-17 05:23:35 +00:00
std.debug.panic("[CPU/BIOS:32] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
2022-01-07 23:33:49 +00:00
}
2022-01-10 05:24:14 +00:00
pub fn get16(self: *const Self, idx: usize) u16 {
2022-02-04 05:55:14 +00:00
if (self.buf) |buf|
return (@as(u16, buf[idx + 1]) << 8) | @as(u16, buf[idx]);
2022-02-17 05:23:35 +00:00
std.debug.panic("[CPU/BIOS:16] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
2022-01-07 23:33:49 +00:00
}
2022-01-10 05:24:14 +00:00
pub fn get8(self: *const Self, idx: usize) u8 {
2022-02-04 05:55:14 +00:00
if (self.buf) |buf|
return buf[idx];
2022-02-17 05:23:35 +00:00
std.debug.panic("[CPU/BIOS:8] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
2022-01-07 23:33:49 +00:00
}