diff --git a/lib/src/processor/memory/mmio/gpu/tile_window.rs b/lib/src/processor/memory/mmio/gpu/tile_window.rs index e1cc220..ac76132 100644 --- a/lib/src/processor/memory/mmio/gpu/tile_window.rs +++ b/lib/src/processor/memory/mmio/gpu/tile_window.rs @@ -1,6 +1,6 @@ use crate::{ connect::Renderer, - processor::memory::mmio::gpu::{Palette, TiledataArea, TILE_WINDOW_HEIGHT, TILE_WINDOW_WIDTH}, + processor::memory::mmio::gpu::{Palette, TILE_WINDOW_HEIGHT, TILE_WINDOW_WIDTH}, util::get_bit, }; @@ -61,25 +61,8 @@ where current_width * TILE_WINDOW_HEIGHT ]; } - for tile_y in 0..16 { - self.draw_row( - tile_y, - tile_y as usize, - TiledataArea::D8000, - palette, - memory, - is_cgb_mode, - ); - } - for tile_y in 0..8 { - self.draw_row( - tile_y, - (tile_y as usize) + 16, - TiledataArea::D9000, - palette, - memory, - is_cgb_mode, - ); + for (tile_y, data_begin) in (0x8000..0xA000).step_by(0x10 * 16).enumerate() { + self.draw_row(tile_y, data_begin, palette, memory, is_cgb_mode); } self.sprite_renderer.display(&self.sprite_buffer); @@ -87,9 +70,8 @@ where fn draw_row( &mut self, - tile_y: u8, display_y: usize, - area: TiledataArea, + data_begin: u16, palette: Palette, memory: &Vram, is_cgb_mode: bool, @@ -100,9 +82,8 @@ where TILE_WINDOW_WIDTH }; self.draw_row_from_bank( - tile_y, display_y, - area, + data_begin, palette, memory, is_cgb_mode, @@ -112,9 +93,8 @@ where ); if is_cgb_mode { self.draw_row_from_bank( - tile_y, display_y, - area, + data_begin, palette, memory, is_cgb_mode, @@ -128,9 +108,8 @@ where #[allow(clippy::too_many_arguments)] fn draw_row_from_bank( &mut self, - tile_y: u8, display_y: usize, - area: TiledataArea, + data_begin: u16, palette: Palette, memory: &Vram, is_cgb_mode: bool, @@ -139,20 +118,22 @@ where line_width: usize, ) { for tile_x in 0..16 { - let tile_num = (tile_y * 16) + tile_x; - let data_begin = area.get_addr(tile_num); let attributes = BgAttributes { tile_bank: bank, ..Default::default() }; + let data_begin = data_begin + ((tile_x * 16) as u16); for px_y in 0..8_u16 { let lsbs = memory - .get_with_bank((data_begin + (px_y * 2)).unwrap(), attributes.tile_bank) + .get_with_bank( + (data_begin + (px_y * 2)).try_into().unwrap(), + attributes.tile_bank, + ) .unwrap(); let msbs = memory .get_with_bank( - (data_begin + (1 + (px_y * 2))).unwrap(), + (data_begin + (1 + (px_y * 2))).try_into().unwrap(), attributes.tile_bank, ) .unwrap(); @@ -167,8 +148,10 @@ where palette.map_bits(lsb, msb).0 }; - self.sprite_buffer[offset + real_px_x + (real_px_y * line_width)] = - colour.rgb_bytes(None, is_cgb_mode).into(); + let addr = offset + real_px_x + (real_px_y * line_width); + if addr < self.sprite_buffer.len() { + self.sprite_buffer[addr] = colour.rgb_bytes(None, is_cgb_mode).into(); + } } } }