#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Write the reference's rung 0 states as SingleStepTests-shaped cases. rung0_states.py OUT_DIR [--states N] The reference's rung 0 (its ``tests/test_schedule.py``) draws, for every opcode in emulation mode and in native mode with each M/X pair, random states biased to the conditional cycles' paths, runs one instruction (a whole block move) through the core and compares its cycles with the datasheet's. This script draws the same states with the same generator and seeds (its ``random_state``, ``run_core`` and ``overwrites_itself``, imported), and writes each as a case in the SingleStepTests format: the state, the first value the instruction saw at every address it touched as the RAM, and as many cycle rows as the reference made. ``sst --dump`` and ``reference_sst.py`` then run every case on both cores (rung 0 of this crate): each cycle identical to the reference's is each cycle the reference's rung 0 held to the datasheet. Run it with the reference's interpreter; ``W65C816_PYTHON`` names the checkout. """ from __future__ import annotations import argparse import json import os import random import sys from pathlib import Path sys.path.insert(0, str(Path(os.environ["W65C816_PYTHON"]) / "tests")) from test_schedule import MODES, overwrites_itself, random_state, run_core # noqa: E402 def cases(opcode: int, mode: str, states: int) -> list[dict]: # test_schedule.check's loop, without the datasheet comparison. rng = random.Random(opcode << 8 | list(MODES).index(mode)) found = [] while len(found) < states: state, memory = random_state(rng, opcode, mode) initial = dict(memory) got, first = run_core(state, memory, rng.randrange(1 << 30)) if overwrites_itself(state, got): continue ram = {**first, **initial} found.append({ "name": f"{opcode:02x} {mode} {len(found)}", "initial": {**state, "ram": sorted(ram.items())}, "final": {**state, "ram": []}, "cycles": [[cycle.address, None, "--------"] for cycle in got], }) # fmt: skip return found def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("out", type=Path) parser.add_argument("--states", type=int, default=200) args = parser.parse_args() args.out.mkdir(parents=True, exist_ok=True) for opcode in range(256): for mode in MODES: path = args.out / f"{opcode:02x}.{mode}.json" path.write_text(json.dumps(cases(opcode, mode, args.states), separators=(",", ":"))) return 0 if __name__ == "__main__": sys.exit(main())