# SPDX-License-Identifier: MIT """Immutable values for capturing and restoring a GSU. A :class:`CPUState` holds everything inside the chip at an instruction boundary: the registers, the prefix state, the pipeline byte, the buffers and their completion times, the code cache (contents, per-line fill counts) and both pixel caches. It does **not** hold the Game Pak ROM or RAM: those are the board's buffers (handoff-brief bus contract 5), saved with the board's state and battery file. Times are absolute input clocks of the core's own counter (``clock``), so a restored core continues where the captured one stood. """ from __future__ import annotations from dataclasses import dataclass, field from gsu_python import _sfr #: Plain integer fields, in capture order. SCALAR_FIELDS = ( "sfr", "pbr", "rombr", "rambr", "cbr", "scbr", "scmr", "colr", "por", "bramr", "cfgr", "clsr", "sreg", "dreg", "pipeline", "ramaddr", "romdr", "latch", "clock", "rom_ready", "ram_ready", "pixel_ready", "filling", ) @dataclass(frozen=True, slots=True) class CPUState: """Every register and internal latch of a GSU at an instruction boundary. ``sfr`` is the stored register: bit 6 (R) is not stored (the core derives it from ``rom_ready`` and ``clock``: U28). ``pixel_primary``/``pixel_secondary`` are ``(row offset or -1, flags, colour x 8)``; ``cache_loaded`` counts the bytes loaded in each of the 32 lines (16 = valid; C10); ``filling`` is the line being loaded or -1. ``cache`` is ``None`` in a registers-only capture (traces, the debugger's history). """ r: tuple[int, ...] = (0,) * 16 sfr: int = 0 pbr: int = 0 rombr: int = 0 rambr: int = 0 cbr: int = 0 scbr: int = 0 scmr: int = 0 colr: int = 0 por: int = 0 bramr: int = 0 cfgr: int = 0 clsr: int = 0 sreg: int = 0 dreg: int = 0 pipeline: int = 0x01 ramaddr: int = 0 romdr: int = 0 latch: int = 0 clock: int = 0 rom_ready: int = 0 ram_ready: int = 0 pixel_ready: int = 0 filling: int = -1 cache_loaded: tuple[int, ...] = (0,) * 32 pixel_primary: tuple[int, ...] = (-1, 0) + (0,) * 8 pixel_secondary: tuple[int, ...] = (-1, 0) + (0,) * 8 previous_wrote_r15: bool = False cache: bytes | None = field(default=None, repr=False) def __post_init__(self) -> None: if len(self.r) != 16 or any(type(v) is not int or not 0 <= v <= 0xFFFF for v in self.r): raise ValueError("r must be 16 words") for name in SCALAR_FIELDS: if type(getattr(self, name)) is not int: raise ValueError(f"{name} must be an integer") if len(self.cache_loaded) != 32 or len(self.pixel_primary) != 10: raise ValueError("cache_loaded must have 32 entries and a pixel cache 10") if self.cache is not None and len(self.cache) != 512: raise ValueError("cache must be 512 bytes") @property def running(self) -> bool: return bool(self.sfr & _sfr.GO) @property def flags(self) -> str: """SFR's set flags by name (R from the buffer's time).""" sfr = self.sfr | (_sfr.R if self.clock < self.rom_ready else 0) return _sfr.describe(sfr) def registers(self) -> CPUState: """This state without the cache contents.""" if self.cache is None: return self values = {name: getattr(self, name) for name in self.__slots__ if name != "cache"} return CPUState(**values) __all__ = ["SCALAR_FIELDS", "CPUState"]