chore(cpu): merge halted and state properties

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-08-15 23:26:01 -05:00
parent a77d0a0f62
commit 6215eccb2f
2 changed files with 24 additions and 16 deletions

View File

@ -14,8 +14,6 @@ pub struct Cpu {
reg: Registers, reg: Registers,
flags: Flags, flags: Flags,
ime: ImeState, ime: ImeState,
// TODO: Merge halted and state properties
halted: Option<HaltKind>,
state: State, state: State,
} }
@ -45,24 +43,34 @@ impl Cpu {
}) })
} }
pub(crate) fn ime(&self) -> &ImeState { pub(crate) fn ime(&self) -> ImeState {
&self.ime self.ime
} }
pub(crate) fn set_ime(&mut self, state: ImeState) { pub(crate) fn set_ime(&mut self, state: ImeState) {
self.ime = state; self.ime = state;
} }
pub(crate) fn halt(&mut self, state: HaltKind) { pub(crate) fn halt_cpu(&mut self, kind: HaltKind) {
self.halted = Some(state); self.state = State::Halt(kind);
} }
fn resume(&mut self) { fn resume_execution(&mut self) {
self.halted = None; self.state = State::Execute;
} }
pub(crate) fn halted(&self) -> Option<HaltKind> { pub(crate) fn is_halted(&self) -> bool {
self.halted match self.state {
State::Halt(_) => true,
_ => false,
}
}
pub(crate) fn halt_kind(&self) -> Option<HaltKind> {
match self.state {
State::Halt(kind) => Some(kind),
_ => None,
}
} }
pub fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> { pub fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> {
@ -119,7 +127,7 @@ impl Cpu {
return elapsed; return elapsed;
} }
if let Some(kind) = self.halted() { if let Some(kind) = self.halt_kind() {
use HaltKind::*; use HaltKind::*;
self.bus.clock(); self.bus.clock();
@ -192,7 +200,7 @@ impl Cpu {
let enable = self.int_enable(); let enable = self.int_enable();
// TODO: Ensure that this behaviour is correct // 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 // When we're here either a HALT with IME set or
// a HALT with IME not set and No pending Interrupts was called // 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 // nothing actually needs to be added here. This is just documentation
// since it's a bit weird why nothing is being done // 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)] #[derive(Debug, Clone, Copy)]
enum State { enum State {
Execute, Execute,
// Halt, Halt(HaltKind),
// Stop, // Stop,
} }

View File

@ -590,12 +590,12 @@ impl Instruction {
// HALT | Enter CPU low power consumption mode until interrupt occurs // HALT | Enter CPU low power consumption mode until interrupt occurs
use HaltKind::*; use HaltKind::*;
let kind = match *cpu.ime() { let kind = match cpu.ime() {
ImeState::Enabled => ImeEnabled, ImeState::Enabled => ImeEnabled,
_ if cpu.int_request() & cpu.int_enable() != 0 => SomePending, _ if cpu.int_request() & cpu.int_enable() != 0 => SomePending,
_ => NonePending, _ => NonePending,
}; };
cpu.halt(kind); cpu.halt_cpu(kind);
Cycle::new(4) Cycle::new(4)
} }
Instruction::ADC(source) => match source { Instruction::ADC(source) => match source {