From 261fa4d78af0629de38ab8863bf8d3080aea3951 Mon Sep 17 00:00:00 2001 From: Kevin Hamacher Date: Tue, 18 Feb 2020 14:35:11 +0100 Subject: [PATCH] GBC: Fix vram selection --- src/display.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/display.rs b/src/display.rs index a589085..2d94b86 100644 --- a/src/display.rs +++ b/src/display.rs @@ -64,6 +64,9 @@ const MONOCHROME_PALETTE: &'static [[u8; 3]; 4] = &[ struct CgbPalette([u8; 8]); impl CgbPalette { fn get_color(&self, n: usize) -> sdl2::pixels::Color { + if n == 0 { + return sdl2::pixels::Color::RGB(255, 255, 255); + } let v = ((self.0[2 * n + 1] as u16) << 8) | (self.0[2 * n] as u16); let r = (v & 0b1_1111) as u8; let g = ((v >> 5) & 0b1_1111) as u8; @@ -333,7 +336,7 @@ impl Display { pub fn write_byte(&mut self, addr: u16, val: u8) { match addr { 0x8000..=0x9FFF => { - if self.vram_bank == 0 { + if (self.vram_bank & 1) == 0 { self.vram0[(addr - 0x8000) as usize] = val } else { self.vram1[(addr - 0x8000) as usize] = val @@ -392,7 +395,7 @@ impl Display { pub fn read_byte(&self, addr: u16) -> u8 { match addr { 0x8000..=0x9FFF => { - if self.vram_bank == 0 { + if (self.vram_bank & 1) == 0 { self.vram0[(addr - 0x8000) as usize] } else { self.vram1[(addr - 0x8000) as usize] @@ -431,6 +434,8 @@ impl Display { let vram_offset: usize = background_map + ((tile_index_y as usize) * 32) + tile_index_x as usize; */ + // Background attributes are only in this memory range. + assert!(vram_offset >= 0x1800 && vram_offset < 0x2000, format!("offset: {:04X}", vram_offset)); BgMapAttributes(self.vram1[vram_offset]) } @@ -669,6 +674,11 @@ impl Display { } } + pub fn dump_vram(&self) { + std::fs::File::create("vram0.dat").unwrap().write_all(&self.vram0); + std::fs::File::create("vram1.dat").unwrap().write_all(&self.vram1); + } + #[inline] fn renderscan(&mut self) { // Points to the background map offset to use. @@ -738,13 +748,9 @@ impl Display { let tile_id = self.vram0[vram_offset]; // GBC stuff - let bg_attribs = self.get_background_attribute(vram_offset); // Get BG map attributes - let vram = if bg_attribs.vram_bank_number() == 0 { - &*self.vram0 - } else { - &*self.vram1 - }; + let bg_attribs = self.get_background_attribute(vram_offset); + let vram = [&*self.vram0, &*self.vram1][bg_attribs.vram_bank_number()]; // TODO: Priority let tile_offset_y = if bg_attribs.vertical_flip() {