From abe2fc431e3f8115287b99ab444e1d82bc947d52 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:11:48 -0300 Subject: [PATCH] fix(bus): restrict Game ROM and VRAM to a 16-bit bus --- src/bus.zig | 1 - src/bus/pak.zig | 2 +- src/ppu.zig | 8 +++----- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/bus.zig b/src/bus.zig index 3f73f54..ba0acd0 100644 --- a/src/bus.zig +++ b/src/bus.zig @@ -98,7 +98,6 @@ pub const Bus = struct { pub fn write16(self: *@This(), addr: u32, halfword: u16) void { // TODO: write16 can write to GamePak Flash - switch (addr) { // General Internal Memory 0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:16] write 0x{X:} to 0x{X:} in IWRAM", .{ halfword, addr }), diff --git a/src/bus/pak.zig b/src/bus/pak.zig index c9ab442..d622e5e 100644 --- a/src/bus/pak.zig +++ b/src/bus/pak.zig @@ -23,7 +23,7 @@ pub const GamePak = struct { } pub inline fn get32(self: *const @This(), 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])); + return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx)); } pub inline fn get16(self: *const @This(), idx: usize) u16 { diff --git a/src/ppu.zig b/src/ppu.zig index 783ab1f..e149c0b 100644 --- a/src/ppu.zig +++ b/src/ppu.zig @@ -32,14 +32,12 @@ const Vram = struct { } pub inline fn get32(self: *const @This(), 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])); + return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx)); } pub inline fn set32(self: *@This(), idx: usize, word: u32) void { - self.buf[idx + 3] = @truncate(u8, word >> 24); - self.buf[idx + 2] = @truncate(u8, word >> 16); - self.buf[idx + 1] = @truncate(u8, word >> 8); - self.buf[idx] = @truncate(u8, word); + self.set16(idx + 2, @truncate(u16, word >> 16)); + self.set16(idx, @truncate(u16, word)); } pub inline fn get16(self: *const @This(), idx: usize) u16 {