From 6215eccb2f9524f6d919bf5b207f6f07c9b33538 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sun, 15 Aug 2021 23:26:01 -0500 Subject: [PATCH] chore(cpu): merge halted and state properties --- src/cpu.rs | 36 ++++++++++++++++++++++-------------- src/instruction.rs | 4 ++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 9fad643..927880e 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -14,8 +14,6 @@ pub struct Cpu { reg: Registers, flags: Flags, ime: ImeState, - // TODO: Merge halted and state properties - halted: Option, state: State, } @@ -45,24 +43,34 @@ impl Cpu { }) } - pub(crate) fn ime(&self) -> &ImeState { - &self.ime + pub(crate) fn ime(&self) -> ImeState { + self.ime } pub(crate) fn set_ime(&mut self, state: ImeState) { self.ime = state; } - pub(crate) fn halt(&mut self, state: HaltKind) { - self.halted = Some(state); + pub(crate) fn halt_cpu(&mut self, kind: HaltKind) { + self.state = State::Halt(kind); } - fn resume(&mut self) { - self.halted = None; + fn resume_execution(&mut self) { + self.state = State::Execute; } - pub(crate) fn halted(&self) -> Option { - self.halted + pub(crate) fn is_halted(&self) -> bool { + match self.state { + State::Halt(_) => true, + _ => false, + } + } + + pub(crate) fn halt_kind(&self) -> Option { + match self.state { + State::Halt(kind) => Some(kind), + _ => None, + } } pub fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> { @@ -119,7 +127,7 @@ impl Cpu { return elapsed; } - if let Some(kind) = self.halted() { + if let Some(kind) = self.halt_kind() { use HaltKind::*; self.bus.clock(); @@ -192,7 +200,7 @@ impl Cpu { let enable = self.int_enable(); // TODO: Ensure that this behaviour is correct - if self.halted.is_some() { + if self.is_halted() { // When we're here either a HALT with IME set or // a HALT with IME not set and No pending Interrupts was called @@ -201,7 +209,7 @@ impl Cpu { // nothing actually needs to be added here. This is just documentation // since it's a bit weird why nothing is being done - self.resume(); + self.resume_execution(); } } @@ -264,7 +272,7 @@ impl Cpu { #[derive(Debug, Clone, Copy)] enum State { Execute, - // Halt, + Halt(HaltKind), // Stop, } diff --git a/src/instruction.rs b/src/instruction.rs index c99e709..bd07741 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -590,12 +590,12 @@ impl Instruction { // HALT | Enter CPU low power consumption mode until interrupt occurs use HaltKind::*; - let kind = match *cpu.ime() { + let kind = match cpu.ime() { ImeState::Enabled => ImeEnabled, _ if cpu.int_request() & cpu.int_enable() != 0 => SomePending, _ => NonePending, }; - cpu.halt(kind); + cpu.halt_cpu(kind); Cycle::new(4) } Instruction::ADC(source) => match source {