chore: mirror VRAM

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-04-08 00:55:04 -03:00
parent 9b9de11e0c
commit 5310c12669
2 changed files with 21 additions and 12 deletions

View File

@ -61,7 +61,7 @@ pub fn read32(self: *const Self, addr: u32) u32 {
// Internal Display Memory // Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get32(addr & 0x3FF), 0x0500_0000...0x05FF_FFFF => self.ppu.palette.get32(addr & 0x3FF),
0x0600_0000...0x0601_7FFF => self.ppu.vram.read(u32, addr - 0x0600_0000), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u32, addr),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get32(addr & 0x3FF), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.get32(addr & 0x3FF),
// External Memory (Game Pak) // External Memory (Game Pak)
@ -84,7 +84,7 @@ pub fn write32(self: *Self, addr: u32, word: u32) void {
// Internal Display Memory // Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set32(addr & 0x3FF, word), 0x0500_0000...0x05FF_FFFF => self.ppu.palette.set32(addr & 0x3FF, word),
0x0600_0000...0x0601_7FFF => self.ppu.vram.write(u32, addr - 0x0600_0000, word), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u32, addr, word),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word),
else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }), else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
@ -101,7 +101,7 @@ pub fn read16(self: *const Self, addr: u32) u16 {
// Internal Display Memory // Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get16(addr & 0x3FF), 0x0500_0000...0x05FF_FFFF => self.ppu.palette.get16(addr & 0x3FF),
0x0600_0000...0x0601_7FFF => self.ppu.vram.read(u16, addr - 0x0600_0000), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u16, addr),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get16(addr & 0x3FF), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.get16(addr & 0x3FF),
// External Memory (Game Pak) // External Memory (Game Pak)
@ -123,7 +123,7 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
// Internal Display Memory // Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set16(addr & 0x3FF, halfword), 0x0500_0000...0x05FF_FFFF => self.ppu.palette.set16(addr & 0x3FF, halfword),
0x0600_0000...0x0601_7FFF => self.ppu.vram.write(u16, addr - 0x0600_0000, halfword), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(u16, addr, halfword),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword),
0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}), 0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}),
@ -141,7 +141,7 @@ pub fn read8(self: *const Self, addr: u32) u8 {
// Internal Display Memory // Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get8(addr & 0x3FF), 0x0500_0000...0x05FF_FFFF => self.ppu.palette.get8(addr & 0x3FF),
0x0600_0000...0x0601_7FFF => self.ppu.vram.read(u8, addr - 0x0600_0000), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(u8, addr),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get8(addr & 0x3FF), 0x0700_0000...0x07FF_FFFF => self.ppu.oam.get8(addr & 0x3FF),
// External Memory (Game Pak) // External Memory (Game Pak)

View File

@ -479,16 +479,20 @@ const Vram = struct {
self.alloc.free(self.buf); self.alloc.free(self.buf);
} }
pub fn read(self: *const Self, comptime T: type, addr: usize) T { pub fn read(self: *const Self, comptime T: type, address: usize) T {
switch (T) { const addr = Self.mirror(address);
u32 => return (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
u16 => return (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]), return switch (T) {
u8 => return self.buf[addr], u32 => (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
u16 => (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]),
u8 => self.buf[addr],
else => @compileError("VRAM: Unsupported read width"), else => @compileError("VRAM: Unsupported read width"),
} };
} }
pub fn write(self: *Self, comptime T: type, addr: usize, value: T) void { pub fn write(self: *Self, comptime T: type, address: usize, value: T) void {
const addr = Self.mirror(address);
switch (T) { switch (T) {
u32 => { u32 => {
self.buf[addr + 3] = @truncate(u8, value >> 24); self.buf[addr + 3] = @truncate(u8, value >> 24);
@ -503,6 +507,11 @@ const Vram = struct {
else => @compileError("VRAM: Unsupported write width"), else => @compileError("VRAM: Unsupported write width"),
} }
} }
fn mirror(address: usize) usize {
const addr = address & 0x1FFFF; // repeated in steps of 128KiB
return if (addr >= 0x18000) addr & 0x7FFF else addr; // 64K + 32K + 32K (abcc)
}
}; };
const Oam = struct { const Oam = struct {