chore: refactor bios.zig and pak.zig

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-01-07 19:33:49 -04:00
parent 9f64804763
commit e9ec124e33
5 changed files with 75 additions and 77 deletions

View File

@ -2,8 +2,8 @@ const std = @import("std");
const Scheduler = @import("scheduler.zig").Scheduler;
const Io = @import("bus/io.zig").Io;
const Bios = @import("bus/bios.zig").Bios;
const GamePak = @import("bus/pak.zig").GamePak;
const Bios = @import("bus/Bios.zig");
const GamePak = @import("bus/GamePak.zig");
const Ppu = @import("ppu.zig").Ppu;
const Allocator = std.mem.Allocator;

38
src/bus/Bios.zig Normal file
View File

@ -0,0 +1,38 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Self = @This();
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !Self {
const file = try std.fs.cwd().openFile(path, .{ .read = true });
defer file.close();
const len = try file.getEndPos();
return Self{
.buf = try file.readToEndAlloc(alloc, len),
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const Self, idx: usize) u32 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
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]));
}
pub inline fn get16(self: *const Self, idx: usize) u16 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
pub inline fn get8(self: *const Self, idx: usize) u8 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
return self.buf[idx];
}

35
src/bus/GamePak.zig Normal file
View File

@ -0,0 +1,35 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Self = @This();
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !Self {
const file = try std.fs.cwd().openFile(path, .{ .read = true });
defer file.close();
const len = try file.getEndPos();
return Self{
.buf = try file.readToEndAlloc(alloc, len),
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
}
pub inline fn get16(self: *const Self, idx: usize) u16 {
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
pub inline fn get8(self: *const Self, idx: usize) u8 {
return self.buf[idx];
}

View File

@ -1,39 +0,0 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const Bios = struct {
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !@This() {
const file = try std.fs.cwd().openFile(path, .{ .read = true });
defer file.close();
const len = try file.getEndPos();
return @This(){
.buf = try file.readToEndAlloc(alloc, len),
.alloc = alloc,
};
}
pub fn deinit(self: @This()) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const @This(), idx: usize) u32 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
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]));
}
pub inline fn get16(self: *const @This(), idx: usize) u16 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
pub inline fn get8(self: *const @This(), idx: usize) u8 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
return self.buf[idx];
}
};

View File

@ -1,36 +0,0 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const GamePak = struct {
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !@This() {
const file = try std.fs.cwd().openFile(path, .{ .read = true });
defer file.close();
const len = try file.getEndPos();
return @This(){
.buf = try file.readToEndAlloc(alloc, len),
.alloc = alloc,
};
}
pub fn deinit(self: @This()) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const @This(), idx: usize) u32 {
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
}
pub inline fn get16(self: *const @This(), idx: usize) u16 {
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
pub inline fn get8(self: *const @This(), idx: usize) u8 {
return self.buf[idx];
}
};