#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Write the lockstep scenarios (conformance/README.md has the format). make_scenarios.py [--programs] Writes to ``conformance/scenarios/`` (committed; nothing third-party in them): ``random-memory-*``, 64 KiB of seeded random bytes from random registers, with a reset between steps now and then (SLEEP and STOP stop the core until one): every opcode in every PSW state, reached by real execution, every cycle compared. With ``--programs``, also writes to the gitignored ``external/scenarios/`` the test programs of the reference's rungs 2 and 3 in the harness, from ``$SPC700_PYTHON``'s ``reference/``: spctest's first program with the S-CPU side scripted, to its end; and PeterLemon's seven CPU tests, raw and as snapshots, their first 300,000 boundaries. Stdlib only; the generator's random numbers are baked into the files. """ from __future__ import annotations import argparse import json import os import random from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SCENARIOS = ROOT / "conformance" / "scenarios" EXTERNAL = ROOT / "external" / "scenarios" TIMERS = SCENARIOS / "harness-io.bin" def random_memory(seed: int) -> dict: rng = random.Random(seed) steps = 50_000 events = [] for step in range(1, steps): if rng.random() < 0.002: events.append({"step": step, "action": "reset"}) elif rng.random() < 0.02: # somewhere else, running (a stopped core waits for this) events.append({"step": step, "action": "jump", "pc": rng.getrandbits(16), "psw": rng.getrandbits(8)}) return { "fill": {"seed": rng.getrandbits(32)}, "registers": {"a": rng.getrandbits(8), "x": rng.getrandbits(8), "y": rng.getrandbits(8), "sp": rng.getrandbits(8), "pc": rng.getrandbits(16), "psw": rng.getrandbits(8)}, "steps": steps, "events": events, } # fmt: skip def write(folder: Path, name: str, scenario: dict) -> None: folder.mkdir(parents=True, exist_ok=True) (folder / f"{name}.json").write_text(json.dumps(scenario, separators=(",", ":")) + "\n") def programs(reference: Path) -> None: spctest = reference / "reference" / "build" / "spctest" / "spc_tests0.bin" write(EXTERNAL, "program-spctest0", {"program": {"file": str(spctest), "org": 0x0300, "spctest": True}, "steps": 1_000_000, "until_stop": True}) # fmt: skip cputest = reference / "reference" / "src" / "PeterLemon-SNES" / "CPUTest" / "SPC700" for name in ("ADC", "AND", "DEC", "EOR", "INC", "ORA", "SBC"): for form, path in (("raw", cputest / name / f"SPC700{name}.spc"), ("snapshot", cputest / f"SPC700{name}.spc")): write(EXTERNAL, f"program-{name}-{form}", {"program": {"file": str(path), "org": 0x0200}, "steps": 300_000, "until_stop": True}) # fmt: skip def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--programs", action="store_true") args = parser.parse_args() for seed in range(16): write(SCENARIOS, f"random-memory-{seed}", random_memory(seed)) # The harness's I/O page where the test programs do not reach it (timer 2, CONTROL's # port clear): a hand-assembled program, run with spctest's port values (port 1 = 1). TIMERS.write_bytes(bytes.fromhex( "e4f5 c404 8f30f1 e4f5 c405" # port 1 before and after CONTROL clears the ports "8f10fa 8f07fb 8f03fc" # MOV $FA,#$10; MOV $FB,#$07; MOV $FC,#$03 "8f07f1" # MOV $F1,#$07: timers 0-2 on "e4fd c400 e4fe c401 e4ff c402" # loop: MOV A,$FD..$FF; MOV $00..$02,A "ab03 8f05f1 2fed" # INC $03; MOV $F1,#$05 (timer 1 off); BRA loop )) # fmt: skip write(SCENARIOS, "harness-io", {"program": {"file": TIMERS.name, "org": 0x0200, "spctest": True}, "steps": 20_000}) # fmt: skip if args.programs: # Resolved, so the scenario's text (the reference cache's key) does not # depend on how the checkout was named. programs(Path(os.environ.get("SPC700_PYTHON", ROOT.parent / "spc700-python")).resolve()) return 0 if __name__ == "__main__": raise SystemExit(main())