#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rung 1's reference side: sh2-python's corpus cases, each with a CRC of its bus cycles. corpus_trace.py SH2_PYTHON OUT [--step N] The reference's ``validation/rung1/corpus.py`` ``run_core``, unchanged but for one thing: the two bus callables also fold every call (kind, address, width, clock, value) into a CRC-32, so the result line and the bus stream can both be compared with ``sh2-corpus --lockstep`` (one line per case: id, CRC, result). Run it on the reference's PyPy. """ from __future__ import annotations import gzip import sys import zlib from pathlib import Path def main() -> int: root, out = Path(sys.argv[1]), Path(sys.argv[2]) step = int(sys.argv[4]) if len(sys.argv) > 4 and sys.argv[3] == "--step" else 1 sys.path.insert(0, str(root / "src")) sys.path.insert(0, str(root / "validation" / "rung1")) import corpus from sh2_python import FETCH, SH7604 with gzip.open(root / "validation" / "rung1" / "cases.txt.gz", "rt") as handle: lines = [line for line in handle if line.startswith("C ")] with out.open("w") as stream: for line in lines[::step]: case = corpus.parse_case(line) code = corpus.xorshift_fill(corpus.CODE_SIZE, case["seed"]) data = corpus.xorshift_fill(corpus.DATA_SIZE, case["seed"] ^ corpus.DATA_SEED_XOR) regions = ((corpus.CODE_BASE, code), (corpus.DATA_BASE, data)) for address, word in case["words"].items(): for base, buf in regions: if base <= address < base + len(buf): buf[address - base : address - base + 2] = word.to_bytes(2, "big") images = [bytes(buf) for _, buf in regions] outside = [] crc = [0] chip = None def locate(address, width, fetch=False): address &= 0x1FFFFFFF for base, buf in regions: if base <= address and address + (width >> 3) <= base + len(buf): return buf, address - base if not fetch: outside.append(address) return None, 0 def read(address, width, kind): buf, offset = locate(address, width, bool(kind & FETCH)) value = 0 if buf is None else int.from_bytes(buf[offset : offset + (width >> 3)], "big") crc[0] = zlib.crc32(f"r {address:x} {width} {kind} {chip.clock} {value:x}\n".encode(), crc[0]) return value def write(address, width, value, kind): crc[0] = zlib.crc32(f"w {address:x} {width} {kind} {chip.clock} {value:x}\n".encode(), crc[0]) buf, offset = locate(address, width) if buf is not None: buf[offset : offset + (width >> 3)] = value.to_bytes(width >> 3, "big") chip = SH7604(read, write) chip.R = list(case["r"]) chip.sr, chip.gbr, chip.vbr = case["sr"], case["gbr"], case["vbr"] chip.mach, chip.macl, chip.pr = case["mach"], case["macl"], case["pr"] chip.pc = case["pc"] status = "timeout" for _ in range(16): chip.step() if chip.delay_target < 0 and chip.pc in case["landings"]: status = "ok" break if outside: status = "outside" regs = { "pc": chip.pc, "sr": chip.sr, "gbr": chip.gbr, "vbr": chip.vbr, "mach": chip.mach, "macl": chip.macl, "pr": chip.pr, } # fmt: skip regs.update({f"r{i}": v for i, v in enumerate(chip.R)}) memory = {} for (base, buf), image in zip(regions, images, strict=True): for i, (a, b) in enumerate(zip(buf, image, strict=True)): if a != b: memory[base + i] = a result = corpus.format_result(case["id"], status, regs, memory) stream.write(f"{case['id']} {crc[0]:08x} {result}\n") return 0 if __name__ == "__main__": sys.exit(main())