#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rung 2's reference side: sh2-python's own MAME replay, with a CRC of its bus cycles. replay_trace.py SH2_PYTHON LOG OUT [--limit N] [--slices N] [--cache] Runs the reference's ``validation/rung2/replay.py`` ``Replay`` unchanged, but for its two bus callables, which also fold every call (kind, address, width, clock, value) into a CRC-32 and write a slice line every N calls: the lines ``sh2-replay --slices N --out`` writes. Prints the reference's report. Run it on the reference's PyPy. """ from __future__ import annotations import argparse import json import sys import zlib from pathlib import Path def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("reference", type=Path) parser.add_argument("log", type=Path) parser.add_argument("out", type=Path) parser.add_argument("--limit", type=int) parser.add_argument("--slices", type=int, default=100000) parser.add_argument("--cache", action="store_true") args = parser.parse_args() sys.path.insert(0, str(args.reference / "validation" / "rung2")) import replay stream = args.out.open("w") state = {"crc": 0, "calls": 0} def note(text: str) -> None: state["crc"] = zlib.crc32(text.encode(), state["crc"]) state["calls"] += 1 if state["calls"] % args.slices == 0: stream.write(f"slice {state['calls']} {state['crc']:08x}\n") state["crc"] = 0 class Hashing(replay.Replay): def read(self, address, width, kind): value = super().read(address, width, kind) note(f"r {address:x} {width} {kind} {self.chip.clock} {value:x}\n") return value def write(self, address, width, value, kind): note(f"w {address:x} {width} {kind} {self.chip.clock} {value:x}\n") super().write(address, width, value, kind) runner = Hashing(cache=args.cache) report = runner.run(args.log, args.limit) stream.write(f"slice {state['calls']} {state['crc']:08x}\n") stream.close() print(json.dumps(report, indent=1)) return 0 if report["stopped_at"] is None else 1 if __name__ == "__main__": sys.exit(main())