#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Survey the SingleStepTests r3000 corpus (reference/src/r3000/v1/*.json.bin). Reads the binary format as SingleStepTests' transcode_json.py (MIT) documents it, without importing it: per file a 32-bit case count, then per case a 51-byte Pascal name, opcode, opcode address, the initial and final states, and the bus cycles. A state is 32 GPRs, HI, LO, EPC, TAR, CAUSE, PC, then the branch delay (u32 target, u32 slot, u32 take) and the load delay (i32 target register, -1 = none; u32 value). Note: transcode_json.py's comments call the first triple "load" and the second pair "branch", but its dictionary (and its README) name them the other way round; the dictionary is what is used here. python -I scripts/survey_sst.py # one line per file, then totals """ from __future__ import annotations import collections import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CORPUS = ROOT / "reference" / "src" / "r3000" / "v1" STATE = struct.Struct("=32I6I3IiI") CYCLE = struct.Struct("=qIqI") def read_state(buf: bytes, ptr: int) -> tuple[int, dict]: values = STATE.unpack_from(buf, ptr) state = { "R": values[0:32], "hi": values[32], "lo": values[33], "EPC": values[34], "TAR": values[35], "CAUSE": values[36], "PC": values[37], "branch": {"target": values[38], "slot": bool(values[39]), "take": bool(values[40])}, "load": {"target": values[41], "val": values[42]}, } return ptr + STATE.size, state def cases(path: Path): buf = path.read_bytes() (count,) = struct.unpack_from("=i", buf, 0) ptr = 4 for _ in range(count): name = buf[ptr + 1: ptr + 1 + buf[ptr]].decode("ascii") ptr += 51 opcode, address = struct.unpack_from("=II", buf, ptr) ptr += 8 ptr, initial = read_state(buf, ptr) ptr, final = read_state(buf, ptr) (ncycles,) = struct.unpack_from("=I", buf, ptr) ptr += 4 cycles = [CYCLE.unpack_from(buf, ptr + i * CYCLE.size) for i in range(ncycles)] ptr += ncycles * CYCLE.size yield {"name": name, "opcode": opcode, "address": address, "initial": initial, "final": final, "cycles": cycles} if ptr != len(buf): raise ValueError(f"{path.name}: {len(buf) - ptr} trailing bytes") def main() -> int: files = sorted(CORPUS.glob("*.json.bin")) if not files: print("no corpus: run scripts/fetch_references.py first", file=sys.stderr) return 1 total = 0 totals: collections.Counter[str] = collections.Counter() for path in files: stats: collections.Counter[str] = collections.Counter() actions: collections.Counter[int] = collections.Counter() for case in cases(path): stats["cases"] += 1 stats["load pending before"] += case["initial"]["load"]["target"] != -1 stats["load pending after"] += case["final"]["load"]["target"] != -1 stats["in branch slot"] += case["initial"]["branch"]["slot"] stats["cause changed"] += case["initial"]["CAUSE"] != case["final"]["CAUSE"] for _, action, _, _ in case["cycles"]: actions[action] += 1 total += stats["cases"] totals.update(stats) kinds = " ".join(f"{k}:{v}" for k, v in sorted(actions.items())) print(f"{path.name[:-9]:8} {stats['cases']:5} cases, load-before {stats['load pending before']:4}," f" load-after {stats['load pending after']:4}, slot {stats['in branch slot']:4}," f" cause-changed {stats['cause changed']:4}, bus actions {kinds}") print(f"\n{len(files)} files, {total} cases; " + ", ".join(f"{k} {v}" for k, v in totals.items())) return 0 if __name__ == "__main__": sys.exit(main())