From 184054349f09526606fd973cfd18cc666170a073 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 9 Jul 2024 00:59:33 -0500 Subject: [PATCH] CPU constructor overhaul --- src/cpu.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index b073b03..a955c3b 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -81,14 +81,61 @@ impl CPU { /// /// [CPU Registers]: https://gbdev.io/pandocs/Power_Up_Sequence.html#cpu-registers pub fn new() -> Self { - let mut out = Self::default(); - out.af.wide_mut().0 = 0x1100 | Flags::new().with_z(true).into_bits() as u16; - out.bc.wide_mut().0 = 0x0100; - out.de.wide_mut().0 = 0xff56; - out.hl.wide_mut().0 = 0x000d; - out.pc.0 = 0x0100; - out.sp.0 = 0xfffe; - out + Self::new_mgb() + } + + pub fn new_dmg() -> Self { + Self::default() + .with_af(0x0100 | Flags::new().with_z(true).into_bits() as u16) + .with_bc(0x0013) + .with_de(0x00d8) + .with_hl(0x014d) + .with_pc(0x0100) + .with_sp(0xfffe) + } + + pub fn new_mgb() -> Self { + Self::default() + .with_af(0xff00 | Flags::new().with_z(true).into_bits() as u16) + .with_bc(0x0013) + .with_de(0x00d8) + .with_hl(0x014d) + .with_pc(0x0100) + .with_sp(0xfffe) + } + + pub fn new_cgb() -> Self { + Self::default() + .with_af(0x1100 | Flags::new().with_z(true).into_bits() as u16) + .with_bc(0x0100) + .with_de(0xff56) + .with_hl(0x000d) + .with_pc(0x0100) + .with_sp(0xfffe) + } + + pub fn with_pc(self, value: u16) -> Self { + Self { pc: Wrapping(value), ..self } + } + + pub fn with_sp(self, value: u16) -> Self { + Self { sp: Wrapping(value), ..self } + } + + pub fn with_af(self, value: u16) -> Self { + Self { af: SplitRegister::from(value), ..self } + } + + pub fn with_bc(self, value: u16) -> Self { + Self { bc: SplitRegister::from(value), ..self } + } + + pub fn with_de(self, value: u16) -> Self { + Self { de: SplitRegister::from(value), ..self } + } + + pub fn with_hl(self, value: u16) -> Self { + Self { hl: SplitRegister::from(value), ..self } } }