#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Measure the SingleStepTests ARM7TDMI corpus: what each file holds and what its bus records say. Reads the ``.json.bin`` files directly (the format ``v1/transcode_json.py`` documents), so nothing needs transcoding first. For every file it reports the number of cases, the instruction state (ARM or Thumb, from CPSR bit 5), and, over all cases: - transactions per case (min, max) and by kind (0 code read, 1 data read, 2 write) and size; - the NanoBoyAdvance access masks seen (bit 0 sequential, 1 code, 3 lock); - whether the ``cycle`` field ever leaves a gap between consecutive transactions (a gap would mean the generator counted internal cycles); - how many cases end with a refill (two code reads at the new PC) and how many have an unaligned data address; - how many cases change mode, take an exception (PC lands on a vector), or change the T bit. python scripts/sst_census.py # every file, table to stdout python scripts/sst_census.py --json out.json # also write the numbers """ from __future__ import annotations import argparse import json import sys from collections import Counter from concurrent.futures import ProcessPoolExecutor from itertools import pairwise from pathlib import Path from struct import unpack_from ROOT = Path(__file__).resolve().parents[1] CORPUS = ROOT / "reference" / "src" / "SingleStepTests-ARM7TDMI" / "v1" MAGIC = 0xD33DBAE0 VECTORS = {0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C} def state(buf: bytes, ptr: int) -> tuple[int, dict]: size = unpack_from(" dict: n = 0 thumb = 0 per_case = Counter() kinds = Counter() sizes = Counter() access = Counter() gaps = 0 first_cycle = Counter() refill = 0 unaligned = 0 mode_change = 0 vector = 0 t_change = 0 for initial, final, txs, _opcode, _base in cases(path): n += 1 is_thumb = bool(initial["CPSR"] & 0x20) thumb += is_thumb per_case[len(txs)] += 1 cycles = [t[4] for t in txs] if cycles: first_cycle[cycles[0]] += 1 if any(b - a > 1 for a, b in pairwise(cycles)): gaps += 1 for kind, size, addr, _data, _cycle, acc in txs: kinds[kind] += 1 sizes[(kind, size)] += 1 access[(kind, acc)] += 1 if kind and addr % size: unaligned += 1 if len(txs) >= 2 and txs[-1][0] == 0 and txs[-2][0] == 0: refill += 1 if (initial["CPSR"] ^ final["CPSR"]) & 0x1F: mode_change += 1 if (initial["CPSR"] ^ final["CPSR"]) & 0x20: t_change += 1 step = 4 if final["CPSR"] & 0x20 else 8 if (final["R"][15] - step) & 0xFFFFFFFF in VECTORS: vector += 1 return { "file": path.name.removesuffix(".json.bin"), "cases": n, "thumb": thumb, "transactions_per_case": dict(sorted(per_case.items())), "kinds": dict(sorted(kinds.items())), "sizes": {f"{k}/{s}": c for (k, s), c in sorted(sizes.items())}, "access": {f"{k}/{a}": c for (k, a), c in sorted(access.items())}, "first_cycle": dict(sorted(first_cycle.items())), "cases_with_cycle_gap": gaps, "cases_ending_in_two_code_reads": refill, "unaligned_data_accesses": unaligned, "mode_changes": mode_change, "t_bit_changes": t_change, "pc_at_vector": vector, } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--json", type=Path, help="write the numbers here too") parser.add_argument("--corpus", type=Path, default=CORPUS) args = parser.parse_args() files = sorted(args.corpus.glob("*.json.bin")) if not files: print(f"no corpus at {args.corpus}; run scripts/fetch_references.py", file=sys.stderr) return 1 with ProcessPoolExecutor() as pool: results = list(pool.map(census, files)) total = sum(r["cases"] for r in results) print(f"{len(results)} files, {total:,} cases") for r in results: txs = r["transactions_per_case"] print( f"{r['file']:32} {r['cases']:6} thumb={r['thumb']:6} " f"tx={min(txs)}-{max(txs)} gap={r['cases_with_cycle_gap']:6} " f"refill={r['cases_ending_in_two_code_reads']:6} " f"unaligned={r['unaligned_data_accesses']:6} mode={r['mode_changes']:5} " f"T={r['t_bit_changes']:5} vec={r['pc_at_vector']:5}" ) if args.json: args.json.write_text(json.dumps(results, indent=1) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())