diff --git a/src/mmu.rs b/src/mmu.rs index 68d38cd38deb38fe2c47aef691ac27834c831bb5..c1af90d9528ec16e1033a61ee6345c1be50cf84a 100644 --- a/src/mmu.rs +++ b/src/mmu.rs @@ -250,7 +250,7 @@ impl Mmu { // @TODO: Implement DMA transfer in a better way // only runs the DMA transfer if the system is in CGB mode - // this avoid issues when writing to DMG unmapped registers + // this avoids issues when writing to DMG unmapped registers // that would otherwise cause the system to crash if self.mode == GameBoyMode::Cgb { let data = self.read_many(self.dma.source(), self.dma.length()); diff --git a/src/ppu.rs b/src/ppu.rs index b5ca06c46b2ff24aa41d576f10a403c85373f8dd..fb6ec67fb99ecd5ae618c49f8ea39a00256eb41c 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -1175,22 +1175,36 @@ impl Ppu { /// to the VRAM values, this should be called whenever the VRAM /// is replaced. pub fn update_vram(&mut self) { + // "saves" the old values of the VRAM bank and offset + // as they are going to be needed later, this is required + // as we're going to trick the PPU into switching banks + // over the update of the calculated values for the new VRAM, + // essentially required for the `update_tile()` method + let (vram_bank_old, vram_offset_old) = (self.vram_bank, self.vram_offset); + + // determines the number of VRAM banks available according + // to the running Game Boy running mode (CGB vs DMG) let vram_banks = if self.gb_mode == GameBoyMode::Cgb { - 2u16 + 2u8 } else { - 1u16 + 1u8 }; + for vram_bank in 0..vram_banks { - let vram_offset = vram_bank * 0x2000; + self.vram_bank = vram_bank; + self.vram_offset = self.vram_bank as u16 * 0x2000; for addr in 0x8000..=0x9fff { - let value = self.vram[(vram_offset + (addr & 0x1fff)) as usize]; + let value = self.vram[(self.vram_offset + (addr & 0x1fff)) as usize]; if addr < 0x9800 { self.update_tile(addr, value); - } else if vram_bank == 0x1 { + } else if self.vram_bank == 0x1 { self.update_bg_map_attrs(addr, value); } } } + + // restores the "old" values for VRAM bank and offset + (self.vram_bank, self.vram_offset) = (vram_bank_old, vram_offset_old); } /// Updates the tile structure with the value that has