#!/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 65816-python's ``W65C816`` exactly as its ``tests/single_step.py`` runs them (the same initial-state readings, the same step loop), with nothing masked and nothing excluded, and writes the final registers, the memory the case touched and every cycle (address, value, the eight pins), in the format ``sst --dump`` writes from this crate. Run it with the reference's interpreter. """ from __future__ import annotations import argparse import json import multiprocessing import sys from pathlib import Path from w65c816 import W65C816 REGISTERS = ("pc", "s", "p", "a", "x", "y", "dbr", "d", "pbr", "e") LIMIT: int | None = None def initial_state(state: dict) -> dict: # tests/single_step.py's initial_state. state = dict(state) if state["e"]: state["s"] = 0x0100 | (state["s"] & 0xFF) state["p"] |= 0x30 if state["p"] & 0x10: state["x"] &= 0xFF state["y"] &= 0xFF return state def dump_case(case: dict) -> str: state = initial_state(case["initial"]) memory = dict(case["initial"]["ram"]) seen: list[tuple] = [] def kinds(write: bool) -> str: pins = (cpu.vda, cpu.vpa, cpu.vpb, write, cpu.mlb, bool(cpu.e), cpu.m, cpu.x_flag) return "".join(letter if on else "-" for on, letter in zip(pins, "dpvwlemx")) def read_byte(address: int) -> int: value = memory.get(address, 0) seen.append((address, value, kinds(False))) return value def write_byte(address: int, value: int) -> None: memory[address] = value seen.append((address, value, kinds(True))) def idle(address: int) -> None: seen.append((address, None, kinds(False))) cpu = W65C816(read_byte, write_byte, idle) for name in REGISTERS: setattr(cpu, name, state[name]) opcode = memory.get(state["pbr"] << 16 | state["pc"], 0) expected = [row for row in case["cycles"] if row[0] is not None] while len(seen) < len(expected): before = len(seen) cpu.step() if len(seen) == before or opcode in (0xCB, 0xDB): break fields = " ".join(f"{int(getattr(cpu, name)):x}" for name in REGISTERS) line = f"{case['name'].replace(' ', '_')} {fields} {len(seen)} |" line += "".join(f" {a:x}={v:x}" for a, v in sorted(memory.items())) line += " |" line += "".join( f" {a:x}:{'-' if v is None else format(v, 'x')}:{k}" for a, v, k in seen ) return line def run_file(path: Path) -> list[str]: cases = json.loads(path.read_text())[:LIMIT] return [dump_case(case) for case in cases] 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())