// SPDX-License-Identifier: MIT //! The public SPC700 CPU (`cpu.py`). //! //! The host owns memory and every device. It gives the CPU a [`Bus`] with //! `read`, `write` and optionally `idle`, calls [`Spc700::step`], and advances //! its devices **inside** the callbacks: every SPC700 cycle is one callback, in //! chip order, dummy reads and internal cycles included, so the host never adds //! `step()`'s return value on top. See the reference's docs/handoff-brief.md //! "The bus contract". //! //! Differences forced by Rust: the constructor `SPC700(read_byte, write_byte, //! idle)` is `Spc700::new(bus)`; the operation tables built in `__init__` //! (`_init_alu`, `_table`) are constants and `match`es (alu.rs, dispatch.rs). use crate::core::{Bus, Spc700}; use crate::flags::{FLAG_B, FLAG_I, FLAG_P}; use crate::state::CpuState; pub const RESET_VECTOR: u16 = 0xFFFE; impl Spc700 { /// A Sony SPC700 on `bus`, in the state [`CpuState`] defaults to. pub fn new(bus: T) -> Self { let mut cpu = Spc700 { a: 0, x: 0, y: 0, sp: 0, pc: 0, psw: 0, opcode: 0, cycle: 0, dummy: false, stopped: false, bus, }; cpu.restore_state(&CpuState::default()); cpu } /// Reset the processor: PC = the word at $FFFE, read through the bus; P = B = /// I = 0; running again. A, X, Y, SP and the other flags keep their values: /// what they hold before the IPL ROM runs is unknown and the IPL overwrites /// what it uses (U12). pub fn reset(&mut self) { self.stopped = false; self.cycle = -1; let low = self.read(RESET_VECTOR); self.pc = (self.read(RESET_VECTOR + 1) as u16) << 8 | low as u16; self.psw &= !(FLAG_P | FLAG_B | FLAG_I); } /// The complete CPU-owned state as an immutable value. pub fn capture_state(&self) -> CpuState { CpuState { a: self.a, x: self.x, y: self.y, sp: self.sp, pc: self.pc, psw: self.psw, stopped: self.stopped, } } /// Replace the CPU-owned state with a captured one. pub fn restore_state(&mut self, state: &CpuState) { self.a = state.a; self.x = state.x; self.y = state.y; self.sp = state.sp; self.pc = state.pc; self.psw = state.psw; self.stopped = state.stopped; } }