"""Immutable values for capturing and restoring W65C816 processor state.""" from dataclasses import dataclass def _require_int(name: str, value: object, maximum: int) -> None: if type(value) is not int or not 0 <= value <= maximum: width = 2 if maximum == 0xFF else 4 message = f"{name} must be an integer in range 0x{'0' * width}..0x{maximum:0{width}X}" raise ValueError(message) def _require_bool(name: str, value: object) -> None: if type(value) is not bool: raise ValueError(f"{name} must be a bool") @dataclass(frozen=True, slots=True) class CPUState: """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 ``stopped`` are 6502-python's frame's, as in its ``CPUState``. """ a: int = 0 # C, all 16 bits x: int = 0 y: int = 0 s: int = 0x01FF d: int = 0 dbr: int = 0 pbr: int = 0 pc: int = 0 #: P as the core keeps it: in emulation mode bits 5 and 4 (M and X) are set. p: int = 0x34 e: int = 1 waiting: bool = False # WAI has run; an interrupt request ends it stopped: bool = False # STP has run; only reset ends it abort_pending: bool = False # an ABORT latched and not yet taken halted: bool = False # waiting or stopped reset_pending: bool = False maskable_interrupt_pending: bool = False # the IRQ line, a level non_maskable_interrupt_pending: bool = False # 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. polled_i: bool | None = None #: 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. polled_maskable_interrupt: bool | None = None polled_non_maskable_interrupt: bool | None = None #: The last step was BRK, COP, an interrupt entry or reset, which do not #: poll: the next boundary runs an instruction. skipped_poll: bool = False def __post_init__(self) -> None: for name in ("dbr", "pbr", "p"): _require_int(name, getattr(self, name), 0xFF) for name in ("a", "x", "y", "s", "d", "pc"): _require_int(name, getattr(self, name), 0xFFFF) if self.e not in (0, 1) or type(self.e) is not int: raise ValueError("e must be 0 or 1") for name in ( "waiting", "stopped", "abort_pending", "halted", "reset_pending", "maskable_interrupt_pending", "non_maskable_interrupt_pending", "skipped_poll", ): _require_bool(name, getattr(self, name)) for name in ("polled_i", "polled_maskable_interrupt", "polled_non_maskable_interrupt"): if getattr(self, name) is not None: _require_bool(name, getattr(self, name)) if self.e and (self.p & 0x30 != 0x30 or self.s >> 8 != 1 or self.x > 0xFF): raise ValueError("in emulation mode M and X are set, S is in page one, X and Y 8-bit") if self.p & 0x10 and (self.x > 0xFF or self.y > 0xFF): raise ValueError("with X set the index registers are 8-bit") if self.halted != (self.waiting or self.stopped): raise ValueError("halted is waiting or stopped") @property def address(self) -> int: """The 24-bit address of the next instruction: PBR:PC.""" return self.pbr << 16 | self.pc __all__ = ["CPUState"]