#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Measure the three CMOS sets of SingleStepTests 65x02: bytes, cycles and bus shape per opcode. Reads ``reference/src/65x02/{wdc65c02,rockwell65c02,synertek65c02}/v1/XX.json`` (one case per line) and prints, for every opcode of every set: - the cycle counts seen (``2``, ``4-5``, ...); - the instruction lengths seen, as ``final.pc - initial.pc`` (a flow change prints ``*``); - the read/write shapes seen, one letter per cycle (``rrrw``), sorted. Then it prints every opcode where the three sets differ in any of those, which is the measured variant table of docs/timing.md. python -I scripts/survey_sst.py # first 300 cases per opcode python -I scripts/survey_sst.py --cases 10000 # everything (slow: 7.7 million cases) python -I scripts/survey_sst.py --opcode 5c --show 3 # print whole cases The corpus is emulator-generated (docs/validation.md): this measures what its generator does, which is a detector's reading, not a judge's. """ from __future__ import annotations import argparse import json from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CORPUS = ROOT / "reference" / "src" / "65x02" SETS = ("wdc65c02", "rockwell65c02", "synertek65c02") def cases(path: Path, limit: int): with path.open(encoding="utf-8") as handle: for line in handle: line = line.strip().rstrip(",") if not line.startswith("{"): continue yield json.loads(line) limit -= 1 if not limit: return def measure(set_name: str, opcode: int, limit: int): path = CORPUS / set_name / "v1" / f"{opcode:02x}.json" if not path.exists() or not path.stat().st_size: return None counts, lengths, shapes = Counter(), Counter(), Counter() for case in cases(path, limit): cycles = case["cycles"] counts[len(cycles)] += 1 delta = (case["final"]["pc"] - case["initial"]["pc"]) & 0xFFFF lengths[delta if delta <= 3 else "*"] += 1 shapes["".join(kind[0] for _, _, kind in cycles)] += 1 low, high = min(counts), max(counts) return ( str(low) if low == high else f"{low}-{high}", "/".join(str(length) for length in sorted(lengths, key=str)), ",".join(sorted(shapes)), ) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--cases", type=int, default=300, help="cases read per opcode") parser.add_argument("--opcode", help="one opcode (hex) only") parser.add_argument("--show", type=int, default=0, help="with --opcode: print N whole cases") args = parser.parse_args() opcodes = [int(args.opcode, 16)] if args.opcode else range(256) if args.show: for set_name in SETS: path = CORPUS / set_name / "v1" / f"{opcodes[0]:02x}.json" print(f"== {set_name}") if path.exists() and path.stat().st_size: for case in cases(path, args.show): print(json.dumps(case)) else: print("(no cases)") return 0 different = [] print("op " + " ".join(f"{name:<34}" for name in SETS)) for opcode in opcodes: row = [measure(name, opcode, args.cases) for name in SETS] cells = ["(no cases)" if cell is None else " ".join(cell) for cell in row] print(f"{opcode:02X} " + " ".join(f"{cell:<34}" for cell in cells)) if len(set(cells)) > 1: different.append((opcode, cells)) print(f"\n{len(different)} opcodes differ between the sets:") for opcode, cells in different: print(f"{opcode:02X} " + " | ".join(cells)) return 0 if __name__ == "__main__": raise SystemExit(main())