diff --git a/src/apu.rs b/src/apu.rs index f581233..c7c977f 100644 --- a/src/apu.rs +++ b/src/apu.rs @@ -211,8 +211,8 @@ impl Apu { self.ch4.poly = Default::default(); self.ch4.freq = Default::default(); - self.ctrl.channel = Default::default(); - self.ctrl.out = Default::default(); + self.ctrl.channel = ChannelControl(0); + self.ctrl.out = SoundOutput(0); // Disable the Channels self.ch1.enabled = Default::default(); @@ -344,7 +344,7 @@ impl Apu { } } -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct SoundControl { /// 0xFF24 | NR50 - Channel Control channel: ChannelControl, @@ -354,6 +354,16 @@ pub(crate) struct SoundControl { enabled: bool, } +impl Default for SoundControl { + fn default() -> Self { + Self { + channel: ChannelControl(0), + out: SoundOutput(0), + enabled: Default::default(), + } + } +} + impl SoundControl { /// 0xFF24 | NR50 - Channel Control pub(crate) fn channel(&self) -> u8 { diff --git a/src/apu/types.rs b/src/apu/types.rs index 81afd82..1dfb79b 100644 --- a/src/apu/types.rs +++ b/src/apu/types.rs @@ -416,12 +416,6 @@ pub(super) mod common { } } - impl Default for WavePattern { - fn default() -> Self { - Self::OneEighth // Rationale: OneEighth is 0x00 - } - } - impl From for u8 { fn from(pattern: WavePattern) -> Self { pattern as Self @@ -479,12 +473,6 @@ impl Clone for SoundOutput { } } -impl Default for SoundOutput { - fn default() -> Self { - Self(0) - } -} - impl From for SoundOutput { fn from(byte: u8) -> Self { Self(byte) @@ -523,12 +511,6 @@ impl Clone for ChannelControl { } } -impl Default for ChannelControl { - fn default() -> Self { - Self(0) - } -} - impl From for ChannelControl { fn from(byte: u8) -> Self { Self(byte) diff --git a/src/cartridge.rs b/src/cartridge.rs index ff3d0fe..df4502e 100644 --- a/src/cartridge.rs +++ b/src/cartridge.rs @@ -297,7 +297,7 @@ struct RtClock { impl RtClock { fn inc_day(&mut self) { - // TODO: Figure out order of operations, the brackets are a bit too defenseive here + // TODO: Figure out order of operations, the brackets are a bit too defensive here let days: u16 = (((self.day_high.ninth() as u16) << 8) | self.day_low as u16) + 1; if days > 0x1FF { diff --git a/src/cpu.rs b/src/cpu.rs index b516054..b753cf6 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -19,9 +19,9 @@ impl Cpu { Self { bus: Bus::with_boot(rom), reg: Default::default(), - flags: Default::default(), - ime: Default::default(), - state: Default::default(), + flags: Flags(0), + ime: ImeState::Disabled, + state: State::Execute, } } @@ -238,12 +238,6 @@ enum State { // Stop, } -impl Default for State { - fn default() -> Self { - Self::Execute - } -} - impl Cpu { pub(crate) fn set_register(&mut self, register: Register, value: u8) { use Register::*; @@ -437,12 +431,6 @@ impl Clone for Flags { } } -impl Default for Flags { - fn default() -> Self { - Self(0) - } -} - impl Display for Flags { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { if self.z() { @@ -497,9 +485,3 @@ pub(crate) enum ImeState { Pending, Enabled, } - -impl Default for ImeState { - fn default() -> Self { - Self::Disabled - } -} diff --git a/src/interrupt.rs b/src/interrupt.rs index b6aed43..49e90ab 100644 --- a/src/interrupt.rs +++ b/src/interrupt.rs @@ -1,11 +1,20 @@ use bitfield::bitfield; -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct Interrupt { pub(crate) flag: InterruptFlag, pub(crate) enable: InterruptEnable, } +impl Default for Interrupt { + fn default() -> Self { + Self { + flag: InterruptFlag(0), + enable: InterruptEnable(0), + } + } +} + bitfield! { pub struct InterruptEnable(u8); impl Debug; @@ -23,12 +32,6 @@ impl Clone for InterruptEnable { } } -impl Default for InterruptEnable { - fn default() -> Self { - Self(0) - } -} - impl From for InterruptEnable { fn from(byte: u8) -> Self { Self(byte) @@ -58,12 +61,6 @@ impl Clone for InterruptFlag { } } -impl Default for InterruptFlag { - fn default() -> Self { - Self(0) - } -} - impl From for InterruptFlag { fn from(byte: u8) -> Self { Self(byte) diff --git a/src/ppu.rs b/src/ppu.rs index 71c8854..39d81e0 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -470,10 +470,10 @@ impl Default for Ppu { dot: Default::default(), frame_buf: Box::new([0; GB_WIDTH * GB_HEIGHT * 4]), int: Default::default(), - ctrl: Default::default(), + ctrl: LCDControl(0), monochrome: Default::default(), pos: Default::default(), - stat: Default::default(), + stat: LCDStatus(0x80), // bit 7 is always 1 oam: Default::default(), scan_dot: Default::default(), fetch: Default::default(), @@ -528,7 +528,7 @@ pub(crate) struct ScreenPosition { pub(crate) window_x: u8, } -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct Monochrome { /// 0xFF47 | BGP - Background Palette Data pub(crate) bg_palette: BackgroundPalette, @@ -538,6 +538,16 @@ pub(crate) struct Monochrome { pub(crate) obj_palette_1: ObjectPalette, } +impl Default for Monochrome { + fn default() -> Self { + Self { + bg_palette: BackgroundPalette(0), + obj_palette_0: ObjectPalette(0), + obj_palette_1: ObjectPalette(0), + } + } +} + #[derive(Debug)] pub(crate) struct ObjectAttrTable { buf: Box<[u8; OAM_SIZE]>, @@ -575,7 +585,7 @@ impl Default for ObjectAttrTable { } } -#[derive(Debug, Clone, Copy, Default)] +#[derive(Debug, Clone, Copy)] struct ObjectAttr { y: u8, x: u8, @@ -836,7 +846,7 @@ struct BgPixelProperty { shade_id: u8, } -#[derive(Debug, Default)] +#[derive(Debug)] struct ObjPixelProperty { shade_id: u8, palette_kind: ObjectPaletteKind, diff --git a/src/ppu/dma.rs b/src/ppu/dma.rs index 57b8e52..40d118f 100644 --- a/src/ppu/dma.rs +++ b/src/ppu/dma.rs @@ -1,6 +1,6 @@ use crate::Cycle; -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct DirectMemoryAccess { pub(crate) state: DmaState, cycle: Cycle, @@ -8,6 +8,16 @@ pub(crate) struct DirectMemoryAccess { pub(crate) start: DmaAddress, } +impl Default for DirectMemoryAccess { + fn default() -> Self { + Self { + state: DmaState::Disabled, + cycle: Default::default(), + start: Default::default(), + } + } +} + impl DirectMemoryAccess { pub(crate) fn tick(&mut self) -> Option<(u16, u16)> { match self.state { @@ -69,12 +79,6 @@ pub(crate) enum DmaState { Transferring, } -impl Default for DmaState { - fn default() -> Self { - Self::Disabled - } -} - #[derive(Debug, Clone, Copy, Default)] pub(crate) struct DmaAddress(Option); diff --git a/src/ppu/types.rs b/src/ppu/types.rs index c3db9de..8cb6c32 100644 --- a/src/ppu/types.rs +++ b/src/ppu/types.rs @@ -29,12 +29,6 @@ impl Clone for LCDStatus { } } -impl Default for LCDStatus { - fn default() -> Self { - Self(0x80) // bit 7 is always 1 - } -} - impl From for u8 { fn from(status: LCDStatus) -> Self { status.0 @@ -67,12 +61,6 @@ impl From for u8 { } } -impl Default for PpuMode { - fn default() -> Self { - Self::HBlank - } -} - bitfield! { pub struct LCDControl(u8); impl Debug; @@ -93,12 +81,6 @@ impl Clone for LCDControl { } } -impl Default for LCDControl { - fn default() -> Self { - Self(0) - } -} - impl From for LCDControl { fn from(byte: u8) -> Self { Self(byte) @@ -142,12 +124,6 @@ impl From for u8 { } } -impl Default for TileMapAddress { - fn default() -> Self { - Self::X9800 - } -} - #[derive(Debug, Clone, Copy)] pub enum TileDataAddress { X8800 = 0, @@ -170,12 +146,6 @@ impl From for u8 { } } -impl Default for TileDataAddress { - fn default() -> Self { - Self::X8800 - } -} - #[derive(Debug, Clone, Copy)] pub enum ObjectSize { Eight = 0, @@ -209,12 +179,6 @@ impl From for u8 { } } -impl Default for ObjectSize { - fn default() -> Self { - Self::Eight - } -} - bitfield! { pub struct BackgroundPalette(u8); impl Debug; @@ -243,12 +207,6 @@ impl Clone for BackgroundPalette { } } -impl Default for BackgroundPalette { - fn default() -> Self { - Self(0) - } -} - impl From for BackgroundPalette { fn from(byte: u8) -> Self { Self(byte) @@ -288,12 +246,6 @@ impl Clone for ObjectPalette { } } -impl Default for ObjectPalette { - fn default() -> Self { - Self(0) - } -} - impl From for ObjectPalette { fn from(byte: u8) -> Self { Self(byte) @@ -361,24 +313,12 @@ impl From for u8 { } } -impl Default for ObjectFlags { - fn default() -> Self { - Self(0) - } -} - #[derive(Debug, Clone, Copy)] pub enum ObjectPaletteKind { Zero = 0, One = 1, } -impl Default for ObjectPaletteKind { - fn default() -> Self { - Self::Zero - } -} - impl From for ObjectPaletteKind { fn from(byte: u8) -> Self { match byte & 0b01 { @@ -417,12 +357,6 @@ impl From for u8 { } } -impl Default for RenderPriority { - fn default() -> Self { - Self::Object - } -} - #[derive(Debug, Clone, Copy)] pub enum GrayShade { White = 0, @@ -442,12 +376,6 @@ impl GrayShade { } } -impl Default for GrayShade { - fn default() -> Self { - Self::White - } -} - impl From for GrayShade { fn from(byte: u8) -> Self { match byte & 0b11 { diff --git a/src/serial.rs b/src/serial.rs index f0e8804..c14ea64 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -1,6 +1,6 @@ use bitfield::bitfield; -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct Serial { /// 0xFF01 | SB - Serial Transfer Data pub(crate) next: u8, @@ -8,6 +8,15 @@ pub(crate) struct Serial { pub(crate) ctrl: SerialControl, } +impl Default for Serial { + fn default() -> Self { + Self { + next: Default::default(), + ctrl: SerialControl(0), + } + } +} + bitfield! { pub struct SerialControl(u8); impl Debug; @@ -23,12 +32,6 @@ impl Clone for SerialControl { } } -impl Default for SerialControl { - fn default() -> Self { - Self(0) - } -} - impl From for SerialControl { fn from(byte: u8) -> Self { Self(byte) @@ -47,12 +50,6 @@ enum ShiftClock { Internal = 1, } -impl Default for ShiftClock { - fn default() -> Self { - Self::External - } -} - impl From for ShiftClock { fn from(byte: u8) -> Self { match byte & 0b01 { @@ -69,12 +66,6 @@ enum ClockSpeed { Fast = 1, } -impl Default for ClockSpeed { - fn default() -> Self { - Self::Normal - } -} - impl From for ClockSpeed { fn from(byte: u8) -> Self { match byte & 0b01 { diff --git a/src/timer.rs b/src/timer.rs index 4d0ad02..61bb244 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -106,7 +106,7 @@ impl Timer { impl Default for Timer { fn default() -> Self { Self { - ctrl: Default::default(), + ctrl: TimerControl(0), counter: 0, modulo: 0, divider: 0, @@ -157,12 +157,6 @@ impl Clone for TimerControl { } } -impl Default for TimerControl { - fn default() -> Self { - Self(0) - } -} - impl From for TimerControl { fn from(byte: u8) -> Self { Self(byte)