zba/src/bus/Bios.zig

53 lines
1.5 KiB
Zig
Raw Normal View History

2022-01-07 23:33:49 +00:00
const std = @import("std");
const Allocator = std.mem.Allocator;
2022-04-08 19:48:43 +00:00
const log = std.log.scoped(.Bios);
2022-04-14 02:21:25 +00:00
/// Size of the BIOS in bytes
pub const size = 0x4000;
2022-01-07 23:33:49 +00:00
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-04-14 00:39:35 +00:00
buf = try file.readToEndAlloc(alloc, try file.getEndPos());
2022-02-04 05:55:14 +00:00
}
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
}
pub fn read(self: *const Self, comptime T: type, addr: usize) T {
if (self.buf) |buf| {
if (addr > buf.len) {
2022-04-21 13:15:52 +00:00
log.debug("Tried to read {} from {X:0>8} (open bus)", .{ T, addr });
return 0;
}
return switch (T) {
u32 => (@as(u32, buf[addr + 3]) << 24) | (@as(u32, buf[addr + 2]) << 16) | (@as(u32, buf[addr + 1]) << 8) | (@as(u32, buf[addr])),
u16 => (@as(u16, buf[addr + 1]) << 8) | @as(u16, buf[addr]),
u8 => buf[addr],
else => @compileError("BIOS: Unsupported read width"),
};
}
2022-02-04 05:55:14 +00:00
std.debug.panic("[BIOS] ZBA tried to read {} from 0x{X:0>8} but not BIOS was present", .{ T, addr });
2022-01-07 23:33:49 +00:00
}
2022-04-08 19:48:43 +00:00
pub fn write(_: *Self, comptime T: type, addr: usize, value: T) void {
@setCold(true);
2022-04-21 13:15:52 +00:00
log.debug("Tried to write {} 0x{X:} to 0x{X:0>8} ", .{ T, value, addr });
2022-04-08 19:48:43 +00:00
}