#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rung 4, save states: every lockstep case run through three save states. Each case of scripts/cases.tsv runs on this board interrupted three times -- saved a third of the way in at a frame's start, loaded into a freshly built machine; saved again 1,777 68000 steps into the frame half way; and 3 steps into the frame at two thirds -- and its trace must be identical, line for line, to the reference's trace of the same case run straight through (scripts/lockstep.py's cache: run that first). A state's formats are this board's own (src/state.rs); what proves it is that the interrupted run cannot be told from the uninterrupted one, on every line the lockstep writes (both CPUs' and both SH-2s' state, every memory, the 32X's registers, VDP, PWM, composite and SH-2 side, the bus streams, the pictures and the sound). python3 scripts/state_rung.py [--only NAME ...] [--jobs N] [--rust PATH] """ from __future__ import annotations import argparse import os import subprocess import sys from concurrent.futures import ThreadPoolExecutor from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import lockstep # noqa: E402 def run_case(case, rust: Path, out: Path, cache: Path): name, count, raw, cart, args = case why = lockstep.missing(cart) if why: return name, "MISSING", why reference = lockstep.cached_trace(cache, case) if not reference.exists(): return name, "MISSING", "no reference trace: run scripts/lockstep.py first" saves = [f"{count // 3}", f"{count // 2}+1777", f"{2 * count // 3}+3"] extra = [x for s in saves for x in ("--save-at", s)] path = out / f"{name}.state.trace" bios = os.environ["SEGA32X_BIOS_DIR"] r = subprocess.run(["nice", str(rust), cart, "--bios-dir", bios, "--frames", str(count), *args, *extra, "--out", str(path)], capture_output=True, text=True) # fmt: skip if r.returncode: return name, "ERROR", r.stderr.strip()[-300:] if path.read_bytes() == reference.read_bytes(): return name, "identical", f"saved at {', '.join(saves)}" return name, "DIFFERENT", lockstep.first_difference(path, reference) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--only", action="append", default=[]) parser.add_argument("--cases", type=Path, default=HERE / "cases.tsv") parser.add_argument("--jobs", type=int, default=8) parser.add_argument("--rust", type=Path, default=lockstep.REPO / "target/release/s32x-trace") parser.add_argument("--python", type=Path, default=lockstep.default_python()) parser.add_argument("--out", type=Path, default=lockstep.REPO / "external/state-traces") args = parser.parse_args() key, _head, _pins = lockstep.reference_key(args.python) cache = lockstep.REPO / "external/reference-cache" / key args.out.mkdir(parents=True, exist_ok=True) selected = [c for c in lockstep.cases(args.cases) if not args.only or c[0] in args.only] failed = 0 with ThreadPoolExecutor(args.jobs) as pool: for name, verdict, note in pool.map(lambda c: run_case(c, args.rust, args.out, cache), selected): failed += verdict != "identical" print(f"{name:24} {verdict:9} {note}", flush=True) print(f"{len(selected) - failed} of {len(selected)} identical through save states") return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())