#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Write the reference's rung 0 states as SingleStepTests-shaped cases. rung0_states.py OUT_DIR The reference's rung 0 (its ``tests/test_bus_schedule.py``, ``test_bus_schedule``) draws, for every opcode but SLEEP and STOP, one random 64 KiB memory and 300 random states in it (``random.Random(opcode)``; the CPU state's fields drawn in the order its ``CPUState(...)`` call writes them; Y = 1 for DBNZ Y on every fourth; the compare that matches written into memory on every fourth for CBNE d, CBNE d+X and DBNZ d), runs one step of each, and compares its cycles with docs/timing.md's template. This script draws the same states the same way (the generator is restated here: the test module imports pytest), runs each through the reference core, and writes it as a case: the registers, the first value the step saw at every address it touched as the RAM (later states see the memory earlier ones left, as in the test), and as many cycle rows as it made. ``sst --dump`` and ``reference_sst.py`` then run every case on both cores (this crate's rung 0). ``SPC700_PYTHON`` names the checkout. """ from __future__ import annotations import json import os import random import sys from pathlib import Path sys.path.insert(0, str(Path(os.environ["SPC700_PYTHON"]) / "src")) from spc700_python import SPC700, CPUState # noqa: E402 from spc700_python._flags import FLAG_P # noqa: E402 STATES = 300 def cases(opcode: int) -> list[dict]: rng = random.Random(opcode) memory = bytearray(rng.randbytes(0x10000)) first: dict[int, int] = {} count = [0] def read(address: int) -> int: first.setdefault(address, memory[address]) count[0] += 1 return memory[address] def write(address: int, value: int) -> None: first.setdefault(address, memory[address]) count[0] += 1 memory[address] = value def idle() -> None: count[0] += 1 cpu = SPC700(read, write, idle) found = [] for seed in range(STATES): a = rng.randrange(256) x = rng.randrange(256) y = 1 if opcode == 0xFE and seed % 4 == 0 else rng.randrange(256) sp = rng.randrange(256) pc = rng.randrange(0x10000) psw = rng.randrange(256) state = CPUState(a=a, x=x, y=y, sp=sp, pc=pc, psw=psw) memory[pc] = opcode if seed % 4 == 0: # the paths random data rarely takes: the compare that matches dp = 0x100 if psw & FLAG_P else 0 byte1 = memory[(pc + 1) & 0xFFFF] if opcode == 0x2E: memory[dp + byte1] = a elif opcode == 0xDE: memory[dp + ((byte1 + x) & 0xFF)] = a elif opcode == 0x6E: memory[dp + byte1] = 1 first.clear() count[0] = 0 cpu.restore_state(state) cpu.step() initial = {"a": a, "x": x, "y": y, "sp": sp, "pc": pc, "psw": psw} found.append({ "name": f"{opcode:02x} {seed}", "initial": {**initial, "ram": sorted(first.items())}, "final": {**initial, "ram": []}, "cycles": [[None, None, "wait"]] * count[0], }) # fmt: skip return found def main() -> int: out = Path(sys.argv[1]) out.mkdir(parents=True, exist_ok=True) for opcode in range(256): if opcode in (0xEF, 0xFF): continue (out / f"{opcode:02x}.json").write_text(json.dumps(cases(opcode), separators=(",", ":"))) return 0 if __name__ == "__main__": sys.exit(main())