// SPDX-License-Identifier: MIT //! Values for capturing and restoring W65C816 processor state (`state.py`). //! //! Differences forced by Rust: the reference's frozen dataclass validates in //! its constructor (`__post_init__`); a Rust struct literal cannot, so //! [`CpuState::validate`] holds the same checks and `restore_state` calls it. //! The type and range checks of the Python fields are the Rust types (`e` is //! a `u8`, checked to be 0 or 1). use sixfiveohtwo::frame::PollFields; /// Complete CPU-owned state at an instruction boundary. /// /// Registers, the interrupt requests the host has made and the core has not /// yet taken, and the internal state that decides what the next `step()` /// does. Host memory, devices, scheduling and cycle counters are excluded: /// restoring this value restores the processor, not a machine. The fields /// after `abort_pending` are 6502-rust's frame's, as in its `CpuState`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct CpuState { pub a: u16, // C, all 16 bits pub x: u16, pub y: u16, pub s: u16, pub d: u16, pub dbr: u8, pub pbr: u8, pub pc: u16, /// P as the core keeps it: in emulation mode bits 5 and 4 (M and X) are set. pub p: u8, pub e: u8, pub waiting: bool, // WAI has run; an interrupt request ends it pub stopped: bool, // STP has run; only reset ends it pub abort_pending: bool, // an ABORT latched and not yet taken pub halted: bool, // waiting or stopped pub reset_pending: bool, pub maskable_interrupt_pending: bool, // the IRQ line, a level pub non_maskable_interrupt_pending: bool, // an NMI edge latched and not yet taken /// The I flag the last instruction's poll saw, when that differs in /// timing from P (CLI, SEI, PLP, REP, SEP, RTI); None otherwise. pub polled_i: Option, /// The IRQ line and the NMI latch as the last poll saw them, when that is /// not what the requests above say; None when the poll agrees with them. pub polled_maskable_interrupt: Option, pub polled_non_maskable_interrupt: Option, /// The last step was BRK, COP, an interrupt entry or reset, which do not /// poll: the next boundary runs an instruction. pub skipped_poll: bool, } impl Default for CpuState { fn default() -> Self { CpuState { a: 0, x: 0, y: 0, s: 0x01FF, d: 0, dbr: 0, pbr: 0, pc: 0, p: 0x34, e: 1, waiting: false, stopped: false, abort_pending: false, halted: false, reset_pending: false, maskable_interrupt_pending: false, non_maskable_interrupt_pending: false, polled_i: None, polled_maskable_interrupt: None, polled_non_maskable_interrupt: None, skipped_poll: false, } } } impl CpuState { /// The 24-bit address of the next instruction: PBR:PC. pub fn address(&self) -> u32 { (self.pbr as u32) << 16 | self.pc as u32 } /// The frame's fields of this state. pub fn poll_fields(&self) -> PollFields { PollFields { halted: self.halted, reset_pending: self.reset_pending, maskable_interrupt_pending: self.maskable_interrupt_pending, non_maskable_interrupt_pending: self.non_maskable_interrupt_pending, polled_i: self.polled_i, polled_maskable_interrupt: self.polled_maskable_interrupt, polled_non_maskable_interrupt: self.polled_non_maskable_interrupt, skipped_poll: self.skipped_poll, } } /// The checks of the reference's `CPUState.__post_init__` that the field /// types do not already make. pub fn validate(&self) -> Result<(), String> { if self.e > 1 { return Err("e must be 0 or 1".into()); } if self.e != 0 && (self.p & 0x30 != 0x30 || self.s >> 8 != 1 || self.x > 0xFF) { return Err( "in emulation mode M and X are set, S is in page one, X and Y 8-bit".into(), ); } if self.p & 0x10 != 0 && (self.x > 0xFF || self.y > 0xFF) { return Err("with X set the index registers are 8-bit".into()); } if self.halted != (self.waiting || self.stopped) { return Err("halted is waiting or stopped".into()); } Ok(()) } }