Skip to content

Commit

Permalink
Corrected bug: CPU can not index into second attr map
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBloom committed Oct 28, 2024
1 parent b0e93b3 commit d142030
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
16 changes: 11 additions & 5 deletions spirit/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,15 @@ impl Index<WindowTileMapIndex> for MemoryMap {
second_map: check_bit_const::<6>(self.io.lcd_control),
// TODO: Not sure if this needs to be wrapping. Also, how are the X and Y bounds (< 143
// and 166, respectively) are honored. Probably should be controlled on writes...
x: x.wrapping_sub(self.io.window_position[1]),
x: x.wrapping_sub(self.io.window_position[1].wrapping_sub(7)),
y,
};
if y == 0 {
if y == 16 {
let BgTileMapInnerIndex { second_map, x, y } = index;
println!("The window position: {:?}", self.io.window_position);
let x = x as usize / 8;
let y = y as usize / 8;
let index = 0x9800 + (second_map as usize * 0x400) + (y * 32) + x;
println!("Rendering the first window line. Indexing into the {}th map at 0x{index:0>4X} = 0x9800 + 0x{y:0>4x} * 32 + 0x{x:0>4x}", second_map as usize);
println!("Rendering the first window line. Indexing into the {}th tile map at ({x}, {y})", second_map as usize);
}
&self.vram[index]
}
Expand All @@ -436,9 +435,16 @@ impl Index<WindowTileMapAttrIndex> for MemoryMap {
second_map: check_bit_const::<6>(self.io.lcd_control),
// TODO: Not sure if this needs to be wrapping. Also, how are the X and Y bounds (< 143
// and 166, respectively) are honored. Probably should be controlled on writes...
x: x.wrapping_sub(self.io.window_position[1]),
x: x.wrapping_sub(self.io.window_position[1].wrapping_sub(7)),
y, //.wrapping_add(self.io.window_position[0]),
};
if y == 16 {
let BgTileMapAttrInnerIndex { second_map, x, y } = index;
println!("The window position: {:?}", self.io.window_position);
let x = x as usize / 8;
let y = y as usize / 8;
println!("Rendering the first window line. Indexing into the {}th attr map at ({x}, {y})", second_map as usize);
}
&self.vram[index]
}
}
Expand Down
10 changes: 8 additions & 2 deletions spirit/src/mem/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Index<CpuVramIndex> for VRam {
if self.status.is_drawing() {
&DEAD_READ_ONLY_BYTE
} else {
if bank && index < 0x9C00 {
if bank {
&self.vram[1][index as usize - 0x8000]
} else {
&self.vram[0][index as usize - 0x8000]
Expand All @@ -118,9 +118,15 @@ impl IndexMut<CpuVramIndex> for VRam {
self.dead_byte = 0xFF;
&mut self.dead_byte
} else {
if bank && index < 0x9C00 {
if bank {
if index == 0x9C00 {
println!("CPU is writing to (0, 0) in the second attr map.");
}
&mut self.vram[1][index as usize - 0x8000]
} else {
if index == 0x9C00 {
println!("CPU is writing to (0, 0) in the first attr map.");
}
&mut self.vram[0][index as usize - 0x8000]
}
}
Expand Down
5 changes: 3 additions & 2 deletions spirit/src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,9 @@ impl BackgroundFiFo {
fn tick(&mut self, mem: &MemoryMap) {
let bg = self.background.is_empty().then(|| &mut self.background);
self.window.triggered =
self.y >= mem.io().window_position[0] && self.x + 8 >= mem.io().window_position[1];
let do_window = self.window.triggered && check_bit_const::<5>(mem.io().lcd_control);
self.y >= mem.io().window_position[0] && check_bit_const::<5>(mem.io().lcd_control);
let do_window =
self.window.triggered && self.x >= mem.io().window_position[1].wrapping_sub(15);
self.window.was_used |= do_window;
self.fetcher
.tick(self.y, mem, bg, do_window.then_some(self.window));
Expand Down

0 comments on commit d142030

Please sign in to comment.