Skip to content

Commit

Permalink
fix mbc1 rom addressing and support 0x9000 addressing for rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanballs committed Sep 10, 2024
1 parent fe6ecfd commit cedb13b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/cartridge/mbc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
_ => {
Expand Down
2 changes: 2 additions & 0 deletions src/gameboy/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions src/mmu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ impl MMU {

// Interrupt enable
0xFFFF => self.ie,

_ => unreachable!(),
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/mmu/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn window_loop(rx: Receiver<PPU>, 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),
Expand All @@ -94,7 +94,7 @@ pub fn window_loop(rx: Receiver<PPU>, 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,
Expand Down

0 comments on commit cedb13b

Please sign in to comment.