feat: integrate zig-clap with ZBA

This commit is contained in:
2022-02-04 01:55:14 -04:00
parent 6ab4610a81
commit 3e4f9eddb2
6 changed files with 73 additions and 25 deletions

View File

@@ -3,33 +3,46 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const Self = @This();
buf: []u8,
buf: ?[]u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !Self {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
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();
const len = try file.getEndPos();
const len = try file.getEndPos();
buf = try file.readToEndAlloc(alloc, len);
}
return Self{
.buf = try file.readToEndAlloc(alloc, len),
.buf = buf,
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
if (self.buf) |buf| self.alloc.free(buf);
}
pub fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));
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]));
std.debug.panic("[CPU|BIOS:32] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
}
pub fn get16(self: *const Self, idx: usize) u16 {
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
if (self.buf) |buf|
return (@as(u16, buf[idx + 1]) << 8) | @as(u16, buf[idx]);
std.debug.panic("[CPU|BIOS:16] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
}
pub fn get8(self: *const Self, idx: usize) u8 {
return self.buf[idx];
if (self.buf) |buf|
return buf[idx];
std.debug.panic("[CPU|BIOS:8] ZBA tried to read from 0x{X:0>8} but no BIOS was provided.", .{idx});
}