feat: implement mirroring for IWRAM EWRAM, OAM and PALRAM

Also realized I confused IWRAM and EWRAM. This is also fixed
TODO: Implemnt Mirroring for VRRAM
This commit is contained in:
Rekai Nyangadzayi Musuka 2022-02-19 10:08:31 -05:00
parent e426f2459e
commit 9e2e8c3d1a
3 changed files with 32 additions and 32 deletions

View File

@ -43,14 +43,14 @@ pub fn read32(self: *const Self, addr: u32) u32 {
return switch (addr) { return switch (addr) {
// General Internal Memory // General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get32(addr), 0x0000_0000...0x0000_3FFF => self.bios.get32(addr),
0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000), 0x0200_0000...0x02FF_FFFF => self.ewram.get32(addr & 0x3FFFF),
0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_0000), 0x0300_0000...0x03FF_FFFF => self.iwram.get32(addr & 0x7FFF),
0x0400_0000...0x0400_03FE => io.read32(self, addr), 0x0400_0000...0x0400_03FE => io.read32(self, addr),
// Internal Display Memory // 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), 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) // External Memory (Game Pak)
0x0800_0000...0x09FF_FFFF => self.pak.get32(addr - 0x0800_0000), 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) { switch (addr) {
// General Internal Memory // General Internal Memory
0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word), 0x0200_0000...0x02FF_FFFF => self.ewram.set32(addr & 0x3FFFF, word),
0x0300_0000...0x0300_7FFF => self.ewram.set32(addr - 0x0300_0000, word), 0x0300_0000...0x03FF_FFFF => self.iwram.set32(addr & 0x7FFF, word),
0x0400_0000...0x0400_03FE => io.write32(self, addr, word), 0x0400_0000...0x0400_03FE => io.write32(self, addr, word),
// Internal Display Memory // 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), 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 }), 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) { return switch (addr) {
// General Internal Memory // General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get16(addr), 0x0000_0000...0x0000_3FFF => self.bios.get16(addr),
0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000), 0x0200_0000...0x02FF_FFFF => self.ewram.get16(addr & 0x3FFFF),
0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_0000), 0x0300_0000...0x03FF_FFFF => self.iwram.get16(addr & 0x7FFF),
0x0400_0000...0x0400_03FE => io.read16(self, addr), 0x0400_0000...0x0400_03FE => io.read16(self, addr),
// Internal Display Memory // 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), 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) // External Memory (Game Pak)
0x0800_0000...0x09FF_FFFF => self.pak.get16(addr - 0x0800_0000), 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 // TODO: write16 can write to GamePak Flash
switch (addr) { switch (addr) {
// General Internal Memory // General Internal Memory
0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword), 0x0200_0000...0x02FF_FFFF => self.ewram.set16(addr & 0x3FFFF, halfword),
0x0300_0000...0x0300_7FFF => self.ewram.set16(addr - 0x0300_0000, halfword), 0x0300_0000...0x03FF_FFFF => self.iwram.set16(addr & 0x7FFF, halfword),
0x0400_0000...0x0400_03FE => io.write16(self, addr, halfword), 0x0400_0000...0x0400_03FE => io.write16(self, addr, halfword),
// Internal Display Memory // 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), 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 }), 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) { return switch (addr) {
// General Internal Memory // General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get8(addr), 0x0000_0000...0x0000_3FFF => self.bios.get8(addr),
0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000), 0x0200_0000...0x02FF_FFFF => self.ewram.get8(addr & 0x3FFFF),
0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_0000), 0x0300_0000...0x03FF_FFFF => self.iwram.get8(addr & 0x7FFF),
0x0400_0000...0x0400_03FE => io.read8(self, addr), 0x0400_0000...0x0400_03FE => io.read8(self, addr),
// Internal Display Memory // 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), 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) // External Memory (Game Pak)
0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000), 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 { pub fn write8(self: *Self, addr: u32, byte: u8) void {
switch (addr) { switch (addr) {
// General Internal Memory // General Internal Memory
0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte), 0x0200_0000...0x02FF_FFFF => self.ewram.set8(addr & 0x3FFFF, byte),
0x0300_0000...0x0300_7FFF => self.ewram.set8(addr - 0x0300_0000, byte), 0x0300_0000...0x03FF_FFFF => self.iwram.set8(addr & 0x7FFF, byte),
0x0400_0000...0x0400_03FE => io.write8(self, addr, byte), 0x0400_0000...0x0400_03FE => io.write8(self, addr, byte),
// External Memory (Game Pak) // External Memory (Game Pak)

View File

@ -7,7 +7,7 @@ buf: []u8,
alloc: Allocator, alloc: Allocator,
pub fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{
@ -21,14 +21,12 @@ pub fn deinit(self: Self) void {
} }
pub fn get32(self: *const Self, idx: usize) u32 { 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 { pub fn set32(self: *Self, idx: usize, word: u32) void {
self.buf[idx + 3] = @truncate(u8, word >> 24); self.set16(idx + 2, @truncate(u16, word >> 16));
self.buf[idx + 2] = @truncate(u8, word >> 16); self.set16(idx, @truncate(u16, word));
self.buf[idx + 1] = @truncate(u8, word >> 8);
self.buf[idx] = @truncate(u8, word);
} }
pub fn get16(self: *const Self, idx: usize) u16 { pub fn get16(self: *const Self, idx: usize) u16 {

View File

@ -7,7 +7,7 @@ buf: []u8,
alloc: Allocator, alloc: Allocator,
pub fn init(alloc: Allocator) !Self { 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); std.mem.set(u8, buf, 0);
return Self{ return Self{
@ -21,12 +21,14 @@ pub fn deinit(self: Self) void {
} }
pub fn get32(self: *const Self, idx: usize) u32 { 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 { pub fn set32(self: *Self, idx: usize, word: u32) void {
self.set16(idx + 2, @truncate(u16, word >> 16)); self.buf[idx + 3] = @truncate(u8, word >> 24);
self.set16(idx, @truncate(u16, word)); 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 { pub fn get16(self: *const Self, idx: usize) u16 {