// SPDX-License-Identifier: MIT //! The public W65C816 CPU (`cpu.py`). //! //! The host owns memory and every device: it gives the CPU a [`Bus`] with //! `read`, `write` and (optionally) `idle`, calls [`W65C816::step`], and runs //! its devices inside the callbacks for as long as each cycle lasts on its //! board. See the reference's docs/handoff-brief.md, "The bus contract". //! //! Differences forced by Rust: the Python class mixes six modules into one //! class over 6502-python's `CPUFrame`; here they are `impl` blocks of one //! struct in the same six files, and the frame's four hooks are the //! [`Part`] trait's. The constructor `W65C816(read_byte, write_byte, idle)` is //! `W65C816::new(bus)`; the bus callables are the [`Bus`] trait, so there is //! no runtime replacement of them (a host wraps its bus instead, as the //! trace tool does). The table is a constant, not built per class. use crate::control::ABORT_LATCH; use crate::core::{Bus, OPCODE, W65C816}; use crate::dispatch::OPCODES; use crate::state::CpuState; use sixfiveohtwo::frame::{Frame, Part}; impl Part for W65C816 { fn frame(&self) -> &Frame { &self.frame } fn frame_mut(&mut self) -> &mut Frame { &mut self.frame } fn status(&self) -> u8 { self.p } fn accept_reset(&mut self) -> u32 { self.reset_sequence() } fn halted_cycle(&mut self) -> u32 { self.halted_step() } fn enter_interrupt(&mut self, kind: u8) -> u32 { self.enter_interrupt_sequence(kind) } fn execute(&mut self) -> u32 { self.cycles = 0; let pc = self.pc; let opcode = self.cycle_read((self.pbr as u32) << 16 | pc as u32, OPCODE); self.pc = pc.wrapping_add(1); let (handler, mode) = OPCODES[opcode as usize]; self.dispatch(handler, mode); self.cycles } fn boundary_interrupt(&mut self) -> u8 { if let Some(abort) = self.boundary_abort() { return abort; } let p = self.p; self.frame.boundary_interrupt(p) } } impl W65C816 { /// A 65C816 on `bus`, in emulation mode, not yet reset. pub fn new(bus: T) -> Self { Self::init_registers(bus) } /// Execute one instruction, interrupt entry, reset or halted cycle; return /// the number of bus callbacks it made ([`Part::step`]). #[inline] pub fn step(&mut self) -> u32 { Part::step(self) } /// True while WAI waits or STP has stopped the clock (the frame's `halted`). pub fn halted(&self) -> bool { self.frame.halted } /// The M flag as the M/X pin shows it: true for an 8-bit accumulator. pub fn m(&self) -> bool { self.p & crate::core::M != 0 } /// The X flag as the M/X pin shows it: true for 8-bit index registers. pub fn x_flag(&self) -> bool { self.p & crate::core::X != 0 } // -- state ----------------------------------------------------------- /// Return an immutable snapshot of all CPU-owned state at a boundary. /// /// No host reads, no side effects. Memory and devices are the host's and /// are not included. pub fn capture_state(&self) -> CpuState { let poll = self.frame.poll_fields(); CpuState { a: self.a, x: self.x, y: self.y, s: self.s, d: self.d, dbr: self.dbr, pbr: self.pbr, pc: self.pc, p: self.p, e: self.e, waiting: self.waiting, stopped: self.stopped, abort_pending: self.frame.requests & ABORT_LATCH != 0, halted: poll.halted, reset_pending: poll.reset_pending, maskable_interrupt_pending: poll.maskable_interrupt_pending, non_maskable_interrupt_pending: poll.non_maskable_interrupt_pending, polled_i: poll.polled_i, polled_maskable_interrupt: poll.polled_maskable_interrupt, polled_non_maskable_interrupt: poll.polled_non_maskable_interrupt, skipped_poll: poll.skipped_poll, } } /// Restore a captured CPU state without touching the host. pub fn restore_state(&mut self, state: &CpuState) -> Result<(), String> { state.validate()?; self.a = state.a; self.x = state.x; self.y = state.y; self.s = state.s; self.d = state.d; self.dbr = state.dbr; self.pbr = state.pbr; self.pc = state.pc; self.p = state.p; self.e = state.e; self.waiting = state.waiting; self.stopped = state.stopped; self.frame.restore_poll(&state.poll_fields()); if state.abort_pending { self.frame.requests |= ABORT_LATCH; } Ok(()) } }