From 1d041e2d573279326c3c82488e83f84967fc6dfd Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 6 Feb 2023 11:42:45 +1100 Subject: [PATCH] gpu improvements --- src/processor/gpu.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/processor/gpu.rs b/src/processor/gpu.rs index 2715744..cb6a3e2 100644 --- a/src/processor/gpu.rs +++ b/src/processor/gpu.rs @@ -15,16 +15,25 @@ enum TilemapArea { T9C00, } +impl TilemapArea { + fn get_offset(&self) -> u16 { + match self { + TilemapArea::T9800 => 0x9800, + TilemapArea::T9C00 => 0x9C00, + } + } +} + enum TiledataArea { D8000, - D8800, + D9000, } impl TiledataArea { fn get_addr(&self, addr: u8) -> u16 { match self { TiledataArea::D8000 => 0x8000 + (addr as u16), - TiledataArea::D8800 => 0x8800_u16.wrapping_add_signed(as_signed(addr) as i16), + TiledataArea::D9000 => 0x9000_u16.wrapping_add_signed(as_signed(addr) as i16), } } } @@ -45,7 +54,7 @@ struct LCDC { bg_window_enable: bool, } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] enum Colour { White, LightGray, @@ -159,7 +168,7 @@ impl CPU { tile_area: if get_bit(reg, 4) { TiledataArea::D8000 } else { - TiledataArea::D8800 + TiledataArea::D9000 }, bg_tilemap: if get_bit(reg, 3) { TilemapArea::T9C00 @@ -261,15 +270,15 @@ impl CPU { offset_y: u8, ) { let tile_line = (scanline as usize) + (offset_y as usize); - let tile_row = tile_line / 8; - let tile_px = ((tile_line as u16) % 8) * 2; - let row_addr = (tile_row * 32) as u16 + get_tilemap_offset(tilemap); + let tilemap_row = tile_line / 8; + let tile_px = (tile_line) % 8; + let tiledata_offset = tile_px * 2; + let row_addr = (tilemap_row * 32) as u16 + tilemap.get_offset(); for x in 0..32 { - let tile_addr = self.memory.get(row_addr + x) + tile_px as u8; - let lsbs = self.memory.get(tiledata.get_addr(tile_addr)); - let msbs = self.memory.get(tiledata.get_addr(tile_addr + 1)); - // let lsbs = self.memory.get(row_addr + tile_px + (x * 2)); - // let msbs = self.memory.get(row_addr + tile_px + (x * 2) + 1); + let tile_addr = + tiledata.get_addr(self.memory.get(row_addr + x)) + tiledata_offset as u16; + let lsbs = self.memory.get(tile_addr); + let msbs = self.memory.get(tile_addr + 1); for px_x in 0..8 { let lsb = get_bit(lsbs, px_x); let msb = get_bit(msbs, px_x); @@ -304,13 +313,6 @@ fn scale_buffer(buffer: &Vec, width: usize, height: usize, factor: usize) - v } -fn get_tilemap_offset(tilemap: &TilemapArea) -> u16 { - match tilemap { - TilemapArea::T9800 => 0x9800, - TilemapArea::T9C00 => 0x9C00, - } -} - fn bits_to_mapped_colour(lsb: bool, msb: bool, palette: Palette) -> Colour { match (lsb, msb) { (true, true) => palette.three,