From 9973dc8714d84f473aa604cd74dc3656f9d76d5f Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 6 Sep 2021 23:45:04 -0300 Subject: [PATCH] fix(cartridge): don't read from RAM that doesn't exist --- src/cartridge.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cartridge.rs b/src/cartridge.rs index 910a723..0416145 100644 --- a/src/cartridge.rs +++ b/src/cartridge.rs @@ -217,7 +217,9 @@ impl MBCIo for MBC1 { 0x4000..=0x7FFF => { Address(0x4000 * self.high_bank() as usize + (addr as usize - 0x4000)) } - 0xA000..=0xBFFF if self.mem_enabled => Value(self.memory[self.ram_addr(addr)]), + 0xA000..=0xBFFF if self.mem_enabled && self.ram_size != RamSize::None => { + Value(self.memory[self.ram_addr(addr)]) + } 0xA000..=0xBFFF => Value(0xFF), _ => unreachable!("A read from {:#06X} should not be handled by MBC1", addr), } @@ -233,7 +235,7 @@ impl MBCIo for MBC1 { } 0x4000..=0x5FFF => self.ram_bank = byte & 0x03, 0x6000..=0x7FFF => self.mode = (byte & 0x01) == 0x01, - 0xA000..=0xBFFF if self.mem_enabled => { + 0xA000..=0xBFFF if self.mem_enabled && self.ram_size != RamSize::None => { let ram_addr = self.ram_addr(addr); self.memory[ram_addr] = byte; } @@ -503,7 +505,7 @@ impl Default for MBCKind { } } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] enum RamSize { None = 0x00, Unused = 0x01,