From 03c3c8a4acd2ba026d10c950d375127c57ac656e Mon Sep 17 00:00:00 2001 From: Ajurna Date: Wed, 14 May 2025 16:33:35 +0100 Subject: [PATCH] Add LCDControlRegister for LCD control bit manipulation Introduce a new `LCDControlRegister` struct to manage and manipulate individual bits of the LCD control register. Implement `From` and `Into` conversions for seamless integration with byte-level operations. This enhances code clarity and simplifies bitwise operations related to LCD control. --- src/registers.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/registers.rs b/src/registers.rs index 2a97b8a..8eb1f04 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -1,6 +1,61 @@ -#[derive(Clone, Copy, Debug)] -#[derive(PartialEq)] +const LCD_ENABLED_BIT: u8 = 7; +const WINDOW_TILE_MAP_AREA_BIT: u8 = 6; +const WINDOW_ENABLE_BIT: u8 = 5; +const BG_AND_WINDOW_TILE_DATA_AREA_BIT: u8 = 4; +const BG_TILE_MAP_AREA_BIT: u8 = 3; +const OBJECT_SIZE_BIT: u8 = 2; +const OBJECT_ENABLE_BIT: u8 = 1; +const BG_AND_WINDOW_ENABLE_BIT: u8 = 0; +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct LCDControlRegister { + pub(crate) lcd_enabled: bool, + pub(crate) window_tile_map_area: bool, + pub(crate) window_enable: bool, + pub(crate) bg_and_window_tile_area: bool, + pub(crate) bg_tile_map_area: bool, + pub(crate) object_size: bool, + pub(crate) object_enable: bool, + pub(crate) bg_and_window_enable: bool, +} +impl From for u8 { + fn from(flag: LCDControlRegister) -> u8 { + (if flag.lcd_enabled { 1 } else { 0 }) << LCD_ENABLED_BIT | + (if flag.window_tile_map_area { 1 } else { 0 }) << WINDOW_TILE_MAP_AREA_BIT | + (if flag.window_enable { 1 } else { 0 }) << WINDOW_ENABLE_BIT | + (if flag.bg_and_window_tile_area{ 1 } else { 0 }) << BG_AND_WINDOW_TILE_DATA_AREA_BIT | + (if flag.bg_tile_map_area { 1 } else { 0 }) << BG_TILE_MAP_AREA_BIT | + (if flag.object_size { 1 } else { 0 }) << OBJECT_SIZE_BIT | + (if flag.object_enable { 1 } else { 0 }) << OBJECT_ENABLE_BIT | + (if flag.bg_and_window_enable { 1 } else { 0 }) << BG_AND_WINDOW_ENABLE_BIT + } +} + +impl From for LCDControlRegister { + fn from(byte: u8) -> Self { + let lcd_enabled = ((byte >> LCD_ENABLED_BIT) & 0b1) != 0; + let window_tile_map_area = ((byte >> WINDOW_TILE_MAP_AREA_BIT) & 0b1) != 0; + let window_enable = ((byte >> WINDOW_ENABLE_BIT) & 0b1) != 0; + let bg_and_window_tile_area = ((byte >> BG_AND_WINDOW_TILE_DATA_AREA_BIT) & 0b1) != 0; + let bg_tile_map_area = ((byte >> BG_TILE_MAP_AREA_BIT) & 0b1) != 0; + let object_size = ((byte >> OBJECT_SIZE_BIT) & 0b1) != 0; + let object_enable = ((byte >> OBJECT_ENABLE_BIT) & 0b1) != 0; + let bg_and_window_enable = ((byte >> BG_AND_WINDOW_ENABLE_BIT) & 0b1) != 0; + + LCDControlRegister { + lcd_enabled, + window_tile_map_area, + window_enable, + bg_and_window_tile_area, + bg_tile_map_area, + object_size, + object_enable, + bg_and_window_enable, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct FlagsRegister { pub(crate) zero: bool, pub(crate) subtract: bool, @@ -8,6 +63,7 @@ pub(crate) struct FlagsRegister { pub(crate) carry: bool } + const ZERO_FLAG_BYTE_POSITION: u8 = 7; const SUBTRACT_FLAG_BYTE_POSITION: u8 = 6; const HALF_CARRY_FLAG_BYTE_POSITION: u8 = 5;