# SPDX-License-Identifier: MIT """Immutable values for capturing and restoring SPC700 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) @dataclass(frozen=True, slots=True) class CPUState: """Complete CPU-owned state at an instruction boundary. It excludes host memory, devices, scheduling and counters, so restoring it restores the processor, not a machine. The defaults are the values docs U12 names: ares' power-on registers, which equal the state the IPL ROM leaves for uploaded code, except PC. """ a: int = 0 x: int = 0 y: int = 0 sp: int = 0xEF pc: int = 0 psw: int = 0x02 stopped: bool = False def __post_init__(self) -> None: for name in ("a", "x", "y", "sp", "psw"): _require_int(name, getattr(self, name), 0xFF) _require_int("pc", self.pc, 0xFFFF) if type(self.stopped) is not bool: raise ValueError("stopped must be a bool") @property def ya(self) -> int: """Y:A, the 16-bit pair of MOVW, ADDW, SUBW, CMPW, MUL and DIV.""" return self.y << 8 | self.a __all__ = ["CPUState"]