diff --git a/src/Bus.zig b/src/Bus.zig index 04ad473..d253612 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -43,14 +43,14 @@ pub fn read32(self: *const Self, addr: u32) u32 { return switch (addr) { // General Internal Memory 0x0000_0000...0x0000_3FFF => self.bios.get32(addr), - 0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000), - 0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_0000), + 0x0200_0000...0x02FF_FFFF => self.ewram.get32(addr & 0x3FFFF), + 0x0300_0000...0x03FF_FFFF => self.iwram.get32(addr & 0x7FFF), 0x0400_0000...0x0400_03FE => io.read32(self, addr), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.get32(addr - 0x0500_0000), + 0x0500_0000...0x05FF_FFFF => self.ppu.palette.get32(addr & 0x3FF), 0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(addr - 0x0600_0000), - 0x0700_0000...0x0700_03FF => self.ppu.oam.get32(addr - 0x0700_0000), + 0x0700_0000...0x07FF_FFFF => self.ppu.oam.get32(addr & 0x3FF), // External Memory (Game Pak) 0x0800_0000...0x09FF_FFFF => self.pak.get32(addr - 0x0800_0000), @@ -69,14 +69,14 @@ pub fn write32(self: *Self, addr: u32, word: u32) void { switch (addr) { // General Internal Memory - 0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word), - 0x0300_0000...0x0300_7FFF => self.ewram.set32(addr - 0x0300_0000, word), + 0x0200_0000...0x02FF_FFFF => self.ewram.set32(addr & 0x3FFFF, word), + 0x0300_0000...0x03FF_FFFF => self.iwram.set32(addr & 0x7FFF, word), 0x0400_0000...0x0400_03FE => io.write32(self, addr, word), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.set32(addr - 0x0500_0000, word), + 0x0500_0000...0x05FF_FFFF => self.ppu.palette.set32(addr & 0x3FF, word), 0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word), - 0x0700_0000...0x0700_03FF => self.ppu.oam.set32(addr - 0x0700_0000, word), + 0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word), else => log.warn("32-bit write of 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }), } @@ -86,14 +86,14 @@ pub fn read16(self: *const Self, addr: u32) u16 { return switch (addr) { // General Internal Memory 0x0000_0000...0x0000_3FFF => self.bios.get16(addr), - 0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000), - 0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_0000), + 0x0200_0000...0x02FF_FFFF => self.ewram.get16(addr & 0x3FFFF), + 0x0300_0000...0x03FF_FFFF => self.iwram.get16(addr & 0x7FFF), 0x0400_0000...0x0400_03FE => io.read16(self, addr), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.get16(addr - 0x0500_0000), + 0x0500_0000...0x05FF_FFFF => self.ppu.palette.get16(addr & 0x3FF), 0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(addr - 0x0600_0000), - 0x0700_0000...0x0700_03FF => self.ppu.oam.get16(addr - 0x0700_0000), + 0x0700_0000...0x07FF_FFFF => self.ppu.oam.get16(addr & 0x3FF), // External Memory (Game Pak) 0x0800_0000...0x09FF_FFFF => self.pak.get16(addr - 0x0800_0000), @@ -108,14 +108,14 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void { // TODO: write16 can write to GamePak Flash switch (addr) { // General Internal Memory - 0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword), - 0x0300_0000...0x0300_7FFF => self.ewram.set16(addr - 0x0300_0000, halfword), + 0x0200_0000...0x02FF_FFFF => self.ewram.set16(addr & 0x3FFFF, halfword), + 0x0300_0000...0x03FF_FFFF => self.iwram.set16(addr & 0x7FFF, halfword), 0x0400_0000...0x0400_03FE => io.write16(self, addr, halfword), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.set16(addr - 0x0500_0000, halfword), + 0x0500_0000...0x05FF_FFFF => self.ppu.palette.set16(addr & 0x3FF, halfword), 0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword), - 0x0700_0000...0x0700_03FF => self.ppu.oam.set16(addr - 0x0700_0000, halfword), + 0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword), else => std.debug.panic("16-bit write of 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }), } @@ -125,14 +125,14 @@ pub fn read8(self: *const Self, addr: u32) u8 { return switch (addr) { // General Internal Memory 0x0000_0000...0x0000_3FFF => self.bios.get8(addr), - 0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000), - 0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_0000), + 0x0200_0000...0x02FF_FFFF => self.ewram.get8(addr & 0x3FFFF), + 0x0300_0000...0x03FF_FFFF => self.iwram.get8(addr & 0x7FFF), 0x0400_0000...0x0400_03FE => io.read8(self, addr), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.get8(addr - 0x0500_0000), + 0x0500_0000...0x05FF_FFFF => self.ppu.palette.get8(addr & 0x3FF), 0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(addr - 0x0600_0000), - 0x0700_0000...0x0700_03FF => self.ppu.oam.get8(addr - 0x0700_0000), + 0x0700_0000...0x07FF_FFFF => self.ppu.oam.get8(addr & 0x3FF), // External Memory (Game Pak) 0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000), @@ -147,8 +147,8 @@ pub fn read8(self: *const Self, addr: u32) u8 { pub fn write8(self: *Self, addr: u32, byte: u8) void { switch (addr) { // General Internal Memory - 0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte), - 0x0300_0000...0x0300_7FFF => self.ewram.set8(addr - 0x0300_0000, byte), + 0x0200_0000...0x02FF_FFFF => self.ewram.set8(addr & 0x3FFFF, byte), + 0x0300_0000...0x03FF_FFFF => self.iwram.set8(addr & 0x7FFF, byte), 0x0400_0000...0x0400_03FE => io.write8(self, addr, byte), // External Memory (Game Pak) diff --git a/src/bus/Ewram.zig b/src/bus/Ewram.zig index 2c9cd03..2f66dfc 100644 --- a/src/bus/Ewram.zig +++ b/src/bus/Ewram.zig @@ -7,7 +7,7 @@ buf: []u8, alloc: Allocator, pub fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x8000); + const buf = try alloc.alloc(u8, 0x40000); std.mem.set(u8, buf, 0); return Self{ @@ -21,14 +21,12 @@ pub fn deinit(self: Self) void { } 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])); + return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx)); } pub fn set32(self: *Self, 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 fn get16(self: *const Self, idx: usize) u16 { diff --git a/src/bus/Iwram.zig b/src/bus/Iwram.zig index 2f66dfc..2c9cd03 100644 --- a/src/bus/Iwram.zig +++ b/src/bus/Iwram.zig @@ -7,7 +7,7 @@ buf: []u8, alloc: Allocator, pub fn init(alloc: Allocator) !Self { - const buf = try alloc.alloc(u8, 0x40000); + const buf = try alloc.alloc(u8, 0x8000); std.mem.set(u8, buf, 0); return Self{ @@ -21,12 +21,14 @@ pub fn deinit(self: Self) void { } pub fn get32(self: *const Self, idx: usize) u32 { - return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx)); + 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 fn set32(self: *Self, idx: usize, word: u32) void { - self.set16(idx + 2, @truncate(u16, word >> 16)); - self.set16(idx, @truncate(u16, word)); + 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); } pub fn get16(self: *const Self, idx: usize) u16 {