# SPDX-License-Identifier: MIT """The conformance kit: per-cycle digests a transcription (pokey-rust) must reproduce. Each case is a deterministic stimulus (the stream generators of scripts/streams.py, plus directed pot, keyboard and serial cases) played on the shipped ``POKEY`` (not the referee's ``BoxAligned``), one command per cycle, the access first, then the tick (B0.1). Every cycle contributes one record: the value read (or -1), ``levels()``, ``irq``, ``scan_code``, ``dump_active``, ``sod``, ``oclk``, ``bclk_out``: only what pins and reads show, never internal counters (POT registers and ALLPOT appear through the directed cases' reads). The kit file holds, per case, the CRC-32 of each 1,000-cycle slice and of the whole case. python -I scripts/conformance.py --write # regenerate validation/conformance.txt python -I scripts/conformance.py # check the core against it (exit 1 on a difference) """ from __future__ import annotations import argparse import os import sys import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, os.environ.get("POKEY_SRC") or str(ROOT / "src")) sys.path.insert(0, str(ROOT / "scripts")) from pokey_python import POKEY # noqa: E402 from streams import apply_pins, rung3_stream, rung4_stream, rung6_stream # noqa: E402 KIT = ROOT / "validation" / "conformance.txt" SLICE = 1000 def directed() -> dict[str, list[tuple]]: pots = [("W", 15, 7), ("P", 0x0F), ("W", 11, 0), *[("R", n % 9) for n in range(300)], ("W", 15, 3), ("P", 0x00), ("W", 11, 0), *[("R", n % 9) for n in range(30000)]] # fmt: skip keys = [("W", 14, 0xC0), ("W", 15, 2), ("K", 2), *[("R", 9)] * 9000, ("K", 1), *[("R", 15)] * 9000, ("W", 15, 3), ("K", 0), *[("R", 14)] * 20000] # fmt: skip serial = [("W", 8, 0x40), ("W", 0, 0), ("W", 2, 255), ("W", 15, 0x2B), ("W", 6, 3), ("W", 9, 0), *[("R", 14)] * 3000, ("W", 15, 0x8B), *[("R", 14)] * 3000, ("W", 15, 0x13), *[("R", 14)] * 3000] # fmt: skip return {"pots": pots, "keyboard": keys, "serial": serial} def cases() -> dict[str, list[tuple]]: out = {f"rung3-{s}": rung3_stream(s, 20000, stimers=s % 2 == 0) for s in (1, 2, 3, 4)} out |= {f"rung4-{s}": rung4_stream(s, 20000) for s in (1, 2)} out |= {f"rung6-{s}": rung6_stream(s, 40000) for s in (1, 2)} out |= directed() return out def digests(commands: list[tuple]) -> list[int]: chip = POKEY() crcs = [] crc = 0 n = 0 for command in commands: op = command[0] if op in ("K", "P", "S"): apply_pins(chip, op, command[1]) continue for _ in range(command[1] if op == "N" else 1): value = -1 if op == "W": chip.write(command[1], command[2]) elif op == "R": value = chip.read(command[1]) chip.tick() bclk = chip.bclk_out record = (value, *chip.levels(), int(chip.irq), chip.scan_code, int(chip.dump_active), chip.sod, chip.oclk, -1 if bclk is None else bclk) # fmt: skip crc = zlib.crc32(repr(record).encode(), crc) n += 1 if n % SLICE == 0: crcs.append(crc) crcs.append(crc) return crcs def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--write", action="store_true") args = parser.parse_args() lines = [] for name, commands in cases().items(): crcs = digests(commands) lines.append(f"{name} {len(crcs) - 1} " + " ".join(f"{c:08x}" for c in crcs)) if args.write: KIT.write_text("# SPDX-License-Identifier: MIT\n" + "\n".join(lines) + "\n", encoding="ascii") print(f"wrote {len(lines)} cases to {KIT.relative_to(ROOT)}") return 0 want = [x for x in KIT.read_text(encoding="ascii").splitlines() if not x.startswith("#")] bad = [line.split()[0] for line, ref in zip(lines, want, strict=True) if line != ref] print(f"{len(lines) - len(bad)}/{len(lines)} cases identical" + (f"; differ: {bad}" if bad else "")) return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())