#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """The reference's half of the SingleStepTests lockstep: one line per case. reference_sst.py CORPUS_DIR --out OUT [--limit N] [--jobs N] Runs the first ``--limit`` cases of every file through spc700-python's ``SPC700`` on a bench like its ``tests/sst_runner.py``'s (64 KiB RAM, the CPU reused through a file, the touched memory cleared after each case), with nothing masked, and writes the final registers, the step's cycle count, the memory the case touched and every cycle (kind, address, value, dummy), in the format ``sst --dump`` writes from this crate. ``SPC700_PYTHON`` names the checkout. """ from __future__ import annotations import argparse import json import multiprocessing import os 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 REGISTERS = ("a", "x", "y", "sp", "pc", "psw") LIMIT: int | None = None class Bench: def __init__(self) -> None: self.memory = bytearray(0x10000) self.log: list[tuple[str, int | None, int | None, bool]] = [] self.cpu = SPC700(self.read, self.write, self.idle) def read(self, address: int) -> int: value = self.memory[address] self.log.append(("read", address, value, self.cpu.dummy)) return value def write(self, address: int, value: int) -> None: self.memory[address] = value self.log.append(("write", address, value, False)) def idle(self) -> None: self.log.append(("wait", None, None, False)) def dump_case(bench: Bench, case: dict) -> str: initial, final = case["initial"], case["final"] memory = bench.memory for address, value in initial["ram"]: memory[address] = value bench.log.clear() cpu = bench.cpu cpu.restore_state(CPUState(**{name: initial[name] for name in REGISTERS})) cycles = cpu.step() state = cpu.capture_state() touched = sorted({a for a, _ in initial["ram"]} | {a for _, a, _, _ in bench.log if a is not None}) line = (f"{case['name'].replace(' ', '_')} {state.a:x} {state.x:x} {state.y:x} {state.sp:x} " f"{state.pc:x} {state.psw:x} {int(state.stopped)} {cycles} |") # fmt: skip line += "".join(f" {a:x}={memory[a]:x}" for a in touched) line += " |" for kind, address, value, dummy in bench.log: address_text = "-" if address is None else f"{address:x}" value_text = "-" if value is None else f"{value:x}" line += f" {kind[0]}:{address_text}:{value_text}:{int(dummy)}" for address in {a for a, _ in initial["ram"]} | {a for a, _ in final["ram"]}: memory[address] = 0 for _, address, _, _ in bench.log: if address is not None: memory[address] = 0 return line def run_file(path: Path) -> list[str]: bench = Bench() return [dump_case(bench, case) for case in json.loads(path.read_text())[:LIMIT]] def init(limit: int | None) -> None: global LIMIT LIMIT = limit def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("corpus", type=Path) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--limit", type=int) parser.add_argument("--jobs", type=int, default=4) args = parser.parse_args() files = sorted(args.corpus.glob("*.json")) partial = args.out.with_suffix(".part") with multiprocessing.Pool(args.jobs, initializer=init, initargs=(args.limit,)) as pool: with partial.open("w") as out: for lines in pool.imap(run_file, files): for line in lines: out.write(line + "\n") partial.rename(args.out) return 0 if __name__ == "__main__": sys.exit(main())