#!/usr/bin/env python3 """Rung 5: run SingleStepTests cases through srg320's P65C816 under GHDL. python -I scripts/build_referees.py p65c816-tb # once: the testbench python -I scripts/run_p65c816.py # 100 cases per file python -I scripts/run_p65c816.py --cases 1000 --files 'ee.*' --show 5 For each case the HDL's cycles are compared with the core's and with the corpus's (tests/p65c816.py), and every difference is counted by file and by what differs: the cycle count, read or write, VDA, VPA, VPB, MLB, a value, the address of a cycle that reads or writes, or the address of an IO cycle (the datasheet gives IO cycles an address; nothing reads it). The report groups the cases by category and by who stands alone: the HDL (the core and the corpus agree), the corpus (the HDL sides with the core), the core (the HDL sides with the corpus), or all three differing. """ from __future__ import annotations import argparse import fnmatch import sys from collections import Counter from concurrent.futures import ProcessPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from p65c816 import CATEGORIES, TESTBENCH, run_core, run_corpus, run_testbench, vote from single_step import CORPUS, load_cases def run_file(job: tuple[str, int]) -> tuple[str, int, Counter, dict]: """(file, cases run, Counter[(category, side)], {category: example}).""" name, limit = job cases = load_cases(name)[:limit] hdl = run_testbench(cases) tally: Counter = Counter() examples: dict = {} for case, cycles in zip(cases, hdl, strict=True): if cycles is None: tally["loader", "failed"] += 1 continue core, corpus = run_core(case), run_corpus(case) for category, side in vote(core, cycles, corpus).items(): tally[category, side] += 1 examples.setdefault((category, side), (case["name"], core, cycles, corpus)) return name, len(cases), tally, examples def _print_example(name: str, core: list, hdl: list, corpus: list) -> None: rows = max(len(core), len(hdl), len(corpus)) table = [[c[i].text() if i < len(c) else "-" for c in (core, hdl, corpus)] for i in range(rows)] first = next((i for i, row in enumerate(table) if len(set(row)) > 1), 0) start, end = max(first - 3, 0), min(first + 6, rows) print(f" {name}: core / HDL / corpus, cycles {start}-{end - 1} of {rows}") for row in table[start:end]: mark = "" if len(set(row)) == 1 else " <<" print(f" {row[0]:24} {row[1]:24} {row[2]:24}{mark}") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--cases", type=int, default=100, help="cases per file") parser.add_argument("--files", default="*", help="glob on file names, e.g. 'ee.*'") parser.add_argument("--jobs", type=int, default=16) parser.add_argument("--show", type=int, default=0, help="examples to print per category") args = parser.parse_args() if not TESTBENCH.exists(): raise SystemExit("testbench not built: python -I scripts/build_referees.py p65c816-tb") names = sorted( p.name.removesuffix(".json") for p in CORPUS.glob("*.json") if fnmatch.fnmatch(p.name.removesuffix(".json"), args.files) ) jobs = [(name, args.cases) for name in names] total = 0 groups: dict[tuple[str, str], Counter] = {} examples: dict[tuple[str, str], list] = {} with ProcessPoolExecutor(args.jobs) as pool: for name, count, tally, found in pool.map(run_file, jobs): total += count for key, n in tally.items(): groups.setdefault(key, Counter())[name] += n for key, example in found.items(): examples.setdefault(key, []).append(example) print(f"{total:,} cases from {len(names)} files") if not groups: print("the HDL, the core and the corpus agree on every cycle") order = {category: n for n, category in enumerate((*CATEGORIES, "loader"))} for key in sorted(groups, key=lambda k: (order[k[0]], k[1])): category, side = key files = groups[key] listed = sorted(files) shown = ", ".join(listed[:24]) + (" ..." if len(listed) > 24 else "") print(f"\n{category}, {side}: {sum(files.values()):,} cases in {len(listed)} files") print(f" {shown}") for name, core, hdl, corpus in examples.get(key, [])[: args.show]: _print_example(name, core, hdl, corpus) return 0 if __name__ == "__main__": sys.exit(main())