# SPDX-License-Identifier: MIT """Immutable values for capturing and restoring uPD7725/uPD96050 state.""" from __future__ import annotations from dataclasses import dataclass from upd7725_python._alu import FLAG_C, FLAG_OV0, FLAG_OV1, FLAG_S0, FLAG_S1, FLAG_Z REGISTER_NAMES = ( "pc", "rp", "dp", "sp", "k", "l", "m", "n", "a", "b", "flaga", "flagb", "tr", "trb", "sr", "dr", "si", "so", ) def _require(name: str, value: object, maximum: int) -> None: if type(value) is not int or not 0 <= value <= maximum: raise ValueError(f"{name} must be an integer in range 0..0x{maximum:X}") @dataclass(frozen=True, slots=True) class CPUState: """Every register at an instruction boundary, and optionally the data RAM. ``ram`` is ``None`` in a registers-only capture (traces, the debugger's history): restoring such a state leaves the RAM alone. A full capture (the default of :meth:`UPD7725.capture_state`) carries it, as a save state must: on the ST010/ST011 the RAM is the game's battery save. ``flaga``/``flagb`` pack S1 S0 C Z OV1 OV0 (bits 5-0); the named properties read them as booleans. The defaults are the power-on values the core assumes (zero: U18, C6). """ pc: int = 0 rp: int = 0 dp: int = 0 sp: int = 0 stack: tuple[int, ...] = (0, 0, 0, 0) k: int = 0 l: int = 0 m: int = 0 n: int = 0 a: int = 0 b: int = 0 flaga: int = 0 flagb: int = 0 tr: int = 0 trb: int = 0 sr: int = 0 dr: int = 0 si: int = 0 so: int = 0 ram: tuple[int, ...] | None = None def __post_init__(self) -> None: for name in REGISTER_NAMES: maximum = 0x3F if name in ("flaga", "flagb") else 0xFFFF _require(name, getattr(self, name), maximum) if type(self.stack) is not tuple or not self.stack: raise ValueError("stack must be a non-empty tuple") for value in self.stack: _require("stack entry", value, 0xFFFF) if self.sp >= len(self.stack): raise ValueError("sp must index the stack") if self.ram is not None: if type(self.ram) is not tuple: raise ValueError("ram must be a tuple or None") for value in self.ram: _require("ram word", value, 0xFFFF) def registers(self) -> CPUState: """This state without the RAM.""" if self.ram is None: return self values = {name: getattr(self, name) for name in REGISTER_NAMES} return CPUState(stack=self.stack, **values) # Flags as named booleans (DS (14)). sa1 = property(lambda self: bool(self.flaga & FLAG_S1)) sa0 = property(lambda self: bool(self.flaga & FLAG_S0)) ca = property(lambda self: bool(self.flaga & FLAG_C)) za = property(lambda self: bool(self.flaga & FLAG_Z)) ova1 = property(lambda self: bool(self.flaga & FLAG_OV1)) ova0 = property(lambda self: bool(self.flaga & FLAG_OV0)) sb1 = property(lambda self: bool(self.flagb & FLAG_S1)) sb0 = property(lambda self: bool(self.flagb & FLAG_S0)) cb = property(lambda self: bool(self.flagb & FLAG_C)) zb = property(lambda self: bool(self.flagb & FLAG_Z)) ovb1 = property(lambda self: bool(self.flagb & FLAG_OV1)) ovb0 = property(lambda self: bool(self.flagb & FLAG_OV0)) # SR bits (DS (17)). rqm = property(lambda self: bool(self.sr & 0x8000)) drs = property(lambda self: bool(self.sr & 0x1000)) drc = property(lambda self: bool(self.sr & 0x0400)) __all__ = ["REGISTER_NAMES", "CPUState"]