From 808633deb763927ec92621acb82e12bcfe9ed507 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:11:49 -0300 Subject: [PATCH] chore: refactor bios.zig and pak.zig --- src/bus.zig | 4 ++-- src/bus/Bios.zig | 38 ++++++++++++++++++++++++++++++++++++++ src/bus/GamePak.zig | 35 +++++++++++++++++++++++++++++++++++ src/bus/bios.zig | 39 --------------------------------------- src/bus/pak.zig | 36 ------------------------------------ 5 files changed, 75 insertions(+), 77 deletions(-) create mode 100644 src/bus/Bios.zig create mode 100644 src/bus/GamePak.zig delete mode 100644 src/bus/bios.zig delete mode 100644 src/bus/pak.zig diff --git a/src/bus.zig b/src/bus.zig index 03f2d37..ce27ae9 100644 --- a/src/bus.zig +++ b/src/bus.zig @@ -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; diff --git a/src/bus/Bios.zig b/src/bus/Bios.zig new file mode 100644 index 0000000..4785894 --- /dev/null +++ b/src/bus/Bios.zig @@ -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]; +} diff --git a/src/bus/GamePak.zig b/src/bus/GamePak.zig new file mode 100644 index 0000000..0d851b3 --- /dev/null +++ b/src/bus/GamePak.zig @@ -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]; +} diff --git a/src/bus/bios.zig b/src/bus/bios.zig deleted file mode 100644 index 84e5a29..0000000 --- a/src/bus/bios.zig +++ /dev/null @@ -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]; - } -}; diff --git a/src/bus/pak.zig b/src/bus/pak.zig deleted file mode 100644 index 3f5e170..0000000 --- a/src/bus/pak.zig +++ /dev/null @@ -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]; - } -};