From 131685b8648be1b13102a1562eafd1cc23fadee4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Dec 2023 21:36:46 +0100 Subject: [PATCH] Fix: OAMADDx address is a word address, not a byte address --- src/snes/ppu/bus.rs | 8 ++++---- src/snes/ppu/tests.rs | 15 ++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/snes/ppu/bus.rs b/src/snes/ppu/bus.rs index de56b4f..9c3c616 100644 --- a/src/snes/ppu/bus.rs +++ b/src/snes/ppu/bus.rs @@ -141,18 +141,18 @@ where 0x2101 => Some(self.obsel = val), // OAMADDL - OAM Address and Priority Rotation (W) 0x2102 => { - let v = self.oamadd.get() & 0xFF00; - Some(self.oamadd.set(v | val as u16)) + let v = (self.oamadd.get() >> 1) & 0xFF00; + Some(self.oamadd.set((v | val as u16) << 1)) } // OAMADDH - OAM Address and Priority Rotation (W) 0x2103 => { - let v = self.oamadd.get() & 0x00FF; + let v = (self.oamadd.get() >> 1) & 0x00FF; let val = val & 0x83; // bit 10-14 unused if val & 0x80 != 0 { // Obj priority // TODO } - Some(self.oamadd.set(v | (val as u16) << 8)) + Some(self.oamadd.set((v | (val as u16) << 8) << 1)) } // OAMDATA - OAM Data Write (W) 0x2104 => { diff --git a/src/snes/ppu/tests.rs b/src/snes/ppu/tests.rs index 5e96b51..cf27af2 100644 --- a/src/snes/ppu/tests.rs +++ b/src/snes/ppu/tests.rs @@ -201,8 +201,8 @@ fn oam_write_seq() { assert_eq!(p.oam[0..5], [0xAA, 0xBB, 0xCC, 0xDD, 0]); // Test end of OAM towards the 32 byte extension table - p.write(0x2102, (510 & 0xFF) as u8); - p.write(0x2103, (510 >> 8) as u8); + p.write(0x2102, ((510 >> 1) & 0xFF) as u8); + p.write(0x2103, ((510 >> 1) >> 8) as u8); assert_eq!(p.oam[510..513], [0, 0, 0]); p.write(0x2104, 0xAA); assert_eq!(p.oam[510..513], [0, 0, 0]); @@ -216,8 +216,8 @@ fn oam_write_seq() { fn oam_write_exttable() { let mut p = ppu(); assert_eq!(p.oam[512], 0); - p.write(0x2102, (512 & 0xFF) as u8); - p.write(0x2103, (512 >> 8) as u8); + p.write(0x2102, ((512 >> 1) & 0xFF) as u8); + p.write(0x2103, ((512 >> 1) >> 8) as u8); p.write(0x2104, 0xAA); assert_eq!(p.oam[512], 0xAA); } @@ -226,8 +226,8 @@ fn oam_write_exttable() { fn oam_write_exttable_mirror() { let mut p = ppu(); assert_eq!(p.oam[512], 0); - p.write(0x2102, (0x220 & 0xFF) as u8); - p.write(0x2103, (0x220 >> 8) as u8); + p.write(0x2102, (0x120 & 0xFF) as u8); + p.write(0x2103, (0x120 >> 8) as u8); p.write(0x2104, 0xAA); assert_eq!(p.oam[512], 0xAA); } @@ -250,8 +250,9 @@ fn oam_read() { p.oam[3] = 1; p.oam[4] = 2; p.oam[5] = 3; - p.write(0x2102, 3); + p.write(0x2102, 3 >> 1); p.write(0x2103, 0); + assert_eq!(p.read(0x2138), Some(0)); assert_eq!(p.read(0x2138), Some(1)); assert_eq!(p.read(0x2138), Some(2)); assert_eq!(p.read(0x2138), Some(3));