Refactor LCDControl

This commit is contained in:
Franco Colmenarez 2021-10-31 07:32:19 -05:00
parent 5723c3b3b6
commit b0c07a5264

View File

@ -18,6 +18,7 @@ enum Pixel {
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
struct ColorPalette(u8, u8, u8, u8); struct ColorPalette(u8, u8, u8, u8);
#[derive(Debug, Copy, Clone)]
pub enum LCDControl { pub enum LCDControl {
LCDEnable, LCDEnable,
WindowTileMapAddress, WindowTileMapAddress,
@ -29,6 +30,29 @@ pub enum LCDControl {
BackgroundPriority, BackgroundPriority,
} }
impl LCDControl {
fn get_bit_index(&self) -> BitIndex {
match self {
LCDControl::LCDEnable => BitIndex::I7,
LCDControl::WindowTileMapAddress => BitIndex::I6,
LCDControl::WindowEnable => BitIndex::I5,
LCDControl::BackgroundWindowTileAddress => BitIndex::I4,
LCDControl::BackgroundTileMapAddress => BitIndex::I3,
LCDControl::ObjectSize => BitIndex::I2,
LCDControl::ObjectEnable => BitIndex::I1,
LCDControl::BackgroundPriority => BitIndex::I0,
}
}
pub fn get(&self, byte: u8) -> bool {
get_bit(byte, self.get_bit_index())
}
pub fn set(&self, byte: u8, val: bool) -> u8 {
set_bit(byte, val, self.get_bit_index())
}
}
pub enum LCDStatusModeFlag { pub enum LCDStatusModeFlag {
HBlank, HBlank,
VBlank, VBlank,
@ -175,31 +199,12 @@ impl PPU {
pub fn get_lcd_control(bus: &Bus, control: LCDControl) -> bool { pub fn get_lcd_control(bus: &Bus, control: LCDControl) -> bool {
let byte = bus.read(LCD_CONTROL_ADDRESS); let byte = bus.read(LCD_CONTROL_ADDRESS);
match control { control.get(byte)
LCDControl::LCDEnable => get_bit(byte, BitIndex::I7),
LCDControl::WindowTileMapAddress => get_bit(byte, BitIndex::I6),
LCDControl::WindowEnable => get_bit(byte, BitIndex::I5),
LCDControl::BackgroundWindowTileAddress => get_bit(byte, BitIndex::I4),
LCDControl::BackgroundTileMapAddress => get_bit(byte, BitIndex::I3),
LCDControl::ObjectSize => get_bit(byte, BitIndex::I2),
LCDControl::ObjectEnable => get_bit(byte, BitIndex::I1),
LCDControl::BackgroundPriority => get_bit(byte, BitIndex::I0),
}
} }
fn set_lcd_control(bus: &mut Bus, control: LCDControl, val: bool) { fn set_lcd_control(bus: &mut Bus, control: LCDControl, val: bool) {
let mut byte = bus.read(LCD_CONTROL_ADDRESS); let mut byte = bus.read(LCD_CONTROL_ADDRESS);
byte = match control { bus.write(LCD_CONTROL_ADDRESS, control.set(byte, val));
LCDControl::LCDEnable => set_bit(byte, val, BitIndex::I7),
LCDControl::WindowTileMapAddress => set_bit(byte, val, BitIndex::I6),
LCDControl::WindowEnable => set_bit(byte, val, BitIndex::I5),
LCDControl::BackgroundWindowTileAddress => set_bit(byte, val, BitIndex::I4),
LCDControl::BackgroundTileMapAddress => set_bit(byte, val, BitIndex::I3),
LCDControl::ObjectSize => set_bit(byte, val, BitIndex::I2),
LCDControl::ObjectEnable => set_bit(byte, val, BitIndex::I1),
LCDControl::BackgroundPriority => set_bit(byte, val, BitIndex::I0),
};
bus.write(LCD_CONTROL_ADDRESS, byte);
} }
pub fn get_lcd_status(bus: &Bus, status: LCDStatus) -> bool { pub fn get_lcd_status(bus: &Bus, status: LCDStatus) -> bool {