From 528b88eeb7504c6ed985cae8f1ce3982596b31b1 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 16 Mar 2021 02:36:09 -0500 Subject: [PATCH] feat: implement Obj Palette 0 and 1 --- src/bus.rs | 4 +++ src/ppu.rs | 90 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 05fcd03..3101199 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -131,6 +131,8 @@ impl Bus { 0xFF43 => self.ppu.pos.scroll_x, 0xFF44 => self.ppu.pos.line_y, 0xFF47 => self.ppu.monochrome.bg_palette.into(), + 0xFF48 => self.ppu.monochrome.obj_palette_0.into(), + 0xFF49 => self.ppu.monochrome.obj_palette_1.into(), _ => unimplemented!("Unable to read {:#06X} in I/O Registers", addr), } } @@ -212,6 +214,8 @@ impl Bus { 0xFF43 => self.ppu.pos.scroll_x = byte, 0xFF44 => self.ppu.pos.line_y = byte, 0xFF47 => self.ppu.monochrome.bg_palette = byte.into(), + 0xFF48 => self.ppu.monochrome.obj_palette_0 = byte.into(), + 0xFF49 => self.ppu.monochrome.obj_palette_1 = byte.into(), 0xFF50 => { // Disable Boot ROM if byte != 0 { diff --git a/src/ppu.rs b/src/ppu.rs index 65789e6..172cdb6 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -169,11 +169,6 @@ pub struct ScreenPosition { pub line_y: u8, } -#[derive(Debug, Clone, Copy, Default)] -pub struct Monochrome { - pub bg_palette: BackgroundPalette, -} - bitfield! { pub struct LCDControl(u8); impl Debug; @@ -304,33 +299,82 @@ impl From for GrayShade { } } +impl From for u8 { + fn from(shade: GrayShade) -> Self { + shade as u8 + } +} + #[derive(Debug, Clone, Copy, Default)] -pub struct BackgroundPalette { - colour: GrayShade, - colour2: GrayShade, - colour3: GrayShade, - colour0: GrayShade, // FIXME: Is this supposed to be colour0? +pub struct Monochrome { + pub bg_palette: BackgroundPalette, + pub obj_palette_0: ObjectPalette, + pub obj_palette_1: ObjectPalette, +} + +bitfield! { + pub struct BackgroundPalette(u8); + impl Debug; + pub from into GrayShade, i3_colour, set_i3_colour: 7, 6; + pub from into GrayShade, i2_colour, set_i2_colour: 5, 4; + pub from into GrayShade, i1_colour, set_i1_colour: 3, 2; + pub from into GrayShade, i0_colour, set_i0_colour: 1, 0; +} + +impl Copy for BackgroundPalette {} +impl Clone for BackgroundPalette { + fn clone(&self) -> Self { + *self + } +} + +impl Default for BackgroundPalette { + fn default() -> Self { + Self(0) + } } impl From for BackgroundPalette { fn from(byte: u8) -> Self { - Self { - colour: (byte >> 6).into(), - colour2: ((byte >> 4) & 0x03).into(), - colour3: ((byte >> 2) & 0x03).into(), - colour0: (byte & 0x03).into(), - } + Self(byte) } } impl From for u8 { fn from(palette: BackgroundPalette) -> Self { - // FIXME: There is a bug here, see the above FIXME - let colour0: u8 = palette.colour0 as u8; - let colour1: u8 = palette.colour3 as u8; - let colour2: u8 = palette.colour2 as u8; - let colour3: u8 = palette.colour0 as u8; - - colour3 << 6 | colour2 << 4 | colour1 << 2 | colour0 + palette.0 + } +} + +bitfield! { + pub struct ObjectPalette(u8); + impl Debug; + pub from into GrayShade, i3_colour, set_i3_colour: 7, 6; + pub from into GrayShade, i2_colour, set_i2_colour: 5, 4; + pub from into GrayShade, i1_colour, set_i1_colour: 3, 2; +} + +impl Copy for ObjectPalette {} +impl Clone for ObjectPalette { + fn clone(&self) -> Self { + *self + } +} + +impl Default for ObjectPalette { + fn default() -> Self { + Self(0) + } +} + +impl From for ObjectPalette { + fn from(byte: u8) -> Self { + Self(byte) + } +} + +impl From for u8 { + fn from(palette: ObjectPalette) -> Self { + palette.0 } }