#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """The reference's half of rungs 1 and 2: SingleStepTests ARM7TDMI on arm7tdmi-python. Run with the reference's interpreter, the pinned clone first on its path (scripts/env.sh). sst_reference.py CORPUS --gate-json OUT [--jobs N] # the reference's own gate, as JSON sst_reference.py CORPUS --crc DIR [--jobs N] # one hash per case, for the lockstep sst_reference.py CORPUS --dump NAME INDEX # one case's record ``--gate-json`` runs the reference's ``tests/sst_runner.run_file`` on every file and writes what its ``scripts/sst_gate.py --json`` writes; ``arm7-sst --json`` must write the same counts. ``--crc`` hashes everything a case does (the bytes ``src/bin/arm7-sst.rs`` hashes): each bus callback in order as `` None: self.log.append(("I", address, 1 if self.cpu.coprocessor_cycle else 0)) def record(bench: RecordingBench) -> bytes: out = bytearray() for entry in bench.log: if entry[0] == "I": out += struct.pack(" str: path, out = Path(job[0]), Path(job[1]) name = path.name.removesuffix(".json.bin") bench = RecordingBench() hashes = bytearray() cases = load(path) for case in cases: bench.run(case) hashes += struct.pack(" int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("corpus", type=Path) parser.add_argument("--gate-json", type=Path) parser.add_argument("--crc", type=Path) parser.add_argument("--dump", nargs=2) parser.add_argument("--jobs", type=int, default=4) args = parser.parse_args() files = sorted(args.corpus.glob("*.json.bin")) if args.dump: name, index = args.dump[0], int(args.dump[1]) case = load(args.corpus / f"{name}.json.bin", index + 1)[index] bench = RecordingBench() bench.run(case) print(f"opcode {case.opcode:08x} cycles {bench.cycles}") for entry in bench.log: if entry[0] == "I": print(f" 3 0 {entry[1]:08x} 00000000 {entry[2]}") else: print(f" {entry[0]} {entry[1]} {entry[2]:08x} {entry[3]:08x} {entry[4]}") from tests.sst_runner import NAMES for k, word in enumerate(bench.cpu.capture_state().to_sst()): print(f" {NAMES[k]} {word:08x}") return 0 with ProcessPoolExecutor(args.jobs) as pool: if args.crc: args.crc.mkdir(parents=True, exist_ok=True) todo = [(str(p), str(args.crc)) for p in files if not (args.crc / (p.name.removesuffix(".json.bin") + ".crc")).exists()] for line in pool.map(crc_file, todo): print(line, flush=True) if args.gate_json: report = dict(pool.map(gate_file, [str(p) for p in files])) part = args.gate_json.with_suffix(".part") part.write_text(json.dumps(report, indent=1) + "\n", encoding="utf-8") part.rename(args.gate_json) return 0 if __name__ == "__main__": sys.exit(main())