From cedb13b522a3d439a652647e1afcfa946e8b35db Mon Sep 17 00:00:00 2001 From: Jonathan Balls Date: Tue, 10 Sep 2024 15:10:46 +0100 Subject: [PATCH] fix mbc1 rom addressing and support 0x9000 addressing for rendering --- src/cartridge/mbc1.rs | 6 ++++-- src/gameboy/debugger.rs | 2 ++ src/mmu/mod.rs | 2 -- src/mmu/ppu.rs | 10 ++++++++-- src/renderer.rs | 4 ++-- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/cartridge/mbc1.rs b/src/cartridge/mbc1.rs index 993f617..2d4887d 100644 --- a/src/cartridge/mbc1.rs +++ b/src/cartridge/mbc1.rs @@ -21,13 +21,15 @@ impl MBC for MBC1 { match addr { 0x0..=0x3FFF => self.rom[addr as usize], 0x4000..=0x7FFF => { - let bank_number: u16 = if self.rom_bank == 0 { + let bank: u16 = if self.rom_bank == 0 { 1 } else { self.rom_bank as u16 }; - return self.rom[(bank_number * 0x4000 | addr) as usize]; + let idx = bank * 0x4000 | (addr & 0x3FFF); + + return self.rom[idx as usize]; } 0xA000..=0xBFFF => self.ram[addr as usize - 0xA000], _ => { diff --git a/src/gameboy/debugger.rs b/src/gameboy/debugger.rs index 94e3462..bd8cbf7 100644 --- a/src/gameboy/debugger.rs +++ b/src/gameboy/debugger.rs @@ -45,6 +45,8 @@ impl GameBoy { "d" | "debug" => println!("{:#?}", &self), + "ppu" => println!("{:#?}", &self.mmu.ppu), + "f" | "flush" => { //pub fn flush(&self) { // self.tx.send(self.clone()).unwrap(); diff --git a/src/mmu/mod.rs b/src/mmu/mod.rs index 06ed72c..9fa5d14 100644 --- a/src/mmu/mod.rs +++ b/src/mmu/mod.rs @@ -83,8 +83,6 @@ impl MMU { // Interrupt enable 0xFFFF => self.ie, - - _ => unreachable!(), } } diff --git a/src/mmu/ppu.rs b/src/mmu/ppu.rs index 4ac2f82..4b84420 100644 --- a/src/mmu/ppu.rs +++ b/src/mmu/ppu.rs @@ -165,8 +165,14 @@ impl PPU { return result; } - pub fn get_tile(&self, tile_index: usize) -> Tile { - let start_address = 0x8000 + (tile_index * 16) as u16; + pub fn get_tile(&self, tile_index: u8) -> Tile { + let start_address = if self.lcdc & 0x10 > 0 { + 0x8000 + ((tile_index as u16) * 16) as u16 + } else { + let offset = ((tile_index as i8) as i16) * 16; + 0x9000_u16.wrapping_add(offset as u16) + }; + let mut ret = [[0u8; 8]; 8]; for i in 0..8 { diff --git a/src/renderer.rs b/src/renderer.rs index bac7354..c85ca65 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -77,7 +77,7 @@ pub fn window_loop(rx: Receiver, tx: Sender<(bool, Key)>, game_title: &Stri let tile_index = ppu.get_byte(0x9800 + i); render_tile( - ppu.get_tile(tile_index as usize), + ppu.get_tile(tile_index), &mut buffer, (i as usize * 8) % 256, ((i as usize / 32) * 8).wrapping_sub(ppu.scy as usize), @@ -94,7 +94,7 @@ pub fn window_loop(rx: Receiver, tx: Sender<(bool, Key)>, game_title: &Stri //let flags = ppu.get_byte(start_position + 3); render_tile( - ppu.get_tile(tile_index as usize), + ppu.get_tile(tile_index), &mut buffer, x_position.wrapping_sub(8) as usize, y_position.wrapping_sub(16) as usize,