From 7d1dd3c448419693a7a9fbf23eebcd4e01129944 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 4 Jan 2024 15:38:25 -0500 Subject: [PATCH] Block VRAM/OAM R/W in certain modes --- src/ppu.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ppu.rs b/src/ppu.rs index 39b90ea..3c6a2b8 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -426,8 +426,20 @@ impl PPU { impl Memory for PPU { fn read(&self, a: u16) -> u8 { match a { - 0x8000..=0x9FFF => self.ram[self.ram_bank * 0x2000 + a as usize - 0x8000], - 0xFE00..=0xFE9F => self.oam[a as usize - 0xFE00], + 0x8000..=0x9FFF => { + if self.ppu_mode != PPUMode::Draw { + self.ram[self.ram_bank * 0x2000 + a as usize - 0x8000] + } else { + 0xFF + } + }, + 0xFE00..=0xFE9F => { + if self.ppu_mode != PPUMode::Draw && self.ppu_mode != PPUMode::OAMScan { + self.oam[a as usize - 0xFE00] + } else { + 0xFF + } + }, 0xFF40 => self.lcdc.bits(), 0xFF41 => { let mut lcds = self.lcds; @@ -454,8 +466,16 @@ impl Memory for PPU { fn write(&mut self, a: u16, v: u8) { match a { - 0x8000..=0x9FFF => self.ram[self.ram_bank * 0x2000 + a as usize - 0x8000] = v, - 0xFE00..=0xFE9F => self.oam[a as usize - 0xFE00] = v, + 0x8000..=0x9FFF => { + if self.ppu_mode != PPUMode::Draw { + self.ram[self.ram_bank * 0x2000 + a as usize - 0x8000] = v + } + }, + 0xFE00..=0xFE9F => { + if self.ppu_mode != PPUMode::Draw && self.ppu_mode != PPUMode::OAMScan { + self.oam[a as usize - 0xFE00] = v + } + }, 0xFF40 => { self.lcdc = LCDC::from_bits(v).unwrap(); if !self.lcdc.contains(LCDC::LCD_ENABLE) {