// SPDX-License-Identifier: MIT //! Values for capturing and restoring 65C02 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 checks of the Python fields are the Rust types. use sixfiveohtwo::frame::PollFields; /// Complete CPU-owned state at an instruction boundary. /// /// Registers, `WAI` and `STP` state, the interrupt requests the host has made /// and the core has not yet taken, and what decides the next `step()`. Host /// memory, devices and cycle counters are excluded: restoring this value /// restores the processor, not a machine. The row (which part) is the CPU's, /// not state. The fields from `halted` on are 6502-rust's frame's, as in its /// `CpuState`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct CpuState { pub a: u8, pub x: u8, pub y: u8, pub s: u8, pub pc: u16, /// P as the core keeps it: bit 5 set, B (bit 4) clear; neither is a flip-flop. pub p: u8, pub waiting: bool, // WAI has run; an interrupt request ends it pub stopped: bool, // STP has run; only reset ends it pub halted: bool, // waiting or stopped pub reset_pending: bool, // IRQ a level, NMI an edge latched: DOC (DS 3.x pin descriptions), as the frame models them. 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); 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, an interrupt entry, reset or a one-cycle NOP, /// 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: 0, pc: 0, p: 0x24, waiting: false, stopped: 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 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.p & 0x30 != 0x20 { return Err("p must have bit 5 set and B (bit 4) clear".into()); } if self.waiting && self.stopped { return Err("waiting and stopped cannot both be set".into()); } if self.halted != (self.waiting || self.stopped) { return Err("halted is waiting or stopped".into()); } Ok(()) } }