#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Turn the register streams of the reference's validation/closed_box.py into lockstep scripts (rung 3), which both cores then run (scripts/lockstep_ref.py, examples/ym2612-trace.rs). PYTHONPATH=REFERENCE/src python3 scripts/closed_box_streams.py OUT_DIR The stream definitions (``STREAMS``, 55 of them, and ``s_status``) are imported from the reference's module; its harness is neither built nor run, and no model is involved: the comparison is the reference core against this one. Each stream is fed as the reference's ``run_core`` feeds its core: one ``run(6)`` per internal clock (a ``c`` call, which traces ``channel_values()`` at every sample), reads where the stream reads, and its writes where it writes. Every stream is written twice, as the two tables of closed_box.py give it: ``t1-NAME`` the same writes, ``t2-NAME`` with operator 1's key bit of each $28 write given one sample late (``key_skew``, alignment 4 of its docstring: the extra $28 write, the BUSY clear and the restored address, at the clock run_core makes them). The status stream is ``t1-status`` (closed_box.py runs it plain). Each script ends with the whole state and the frames. """ from __future__ import annotations import sys from pathlib import Path def convert(stream, key_skew: bool) -> list[str]: """The script for one stream; the logic of closed_box.run_core, as calls.""" lines = [f"variant {stream.chip}"] clock = 0 address = (0, 0) operator1_key = [0] * 8 pending: list[tuple[int, int]] = [] for event in stream.events: if event[0] == "w": port, value = event[1], event[2] if not port & 1: address = (port, value) elif key_skew and address == (0, 0x28): pending.append((clock + 24, value)) held = operator1_key[value & 7] operator1_key[value & 7] = (value >> 4) & 1 value = (value & ~0x10) | (held << 4) lines.append(f"w {port} {value:02X}") elif event[0] == "r": lines.append(f"r {event[1]}") else: count = 0 for _ in range(event[1]): count += 1 clock += 1 if pending and pending[0][0] == clock: lines.append(f"c {count}") count = 0 lines += ["w 0 28", f"w 1 {pending.pop(0)[1]:02X}", "busy0"] lines.append(f"w {address[0]} {address[1]:02X}") if count: lines.append(f"c {count}") lines += ["state", "take"] return lines def main() -> int: out_dir = Path(sys.argv[1]) import ym2612_python reference = Path(ym2612_python.__file__).resolve().parents[2] sys.path.insert(0, str(reference / "validation")) import closed_box # the stream definitions; nothing is built or run out_dir.mkdir(parents=True, exist_ok=True) count = 0 for name, make in closed_box.STREAMS.items(): for table, skew in (("t1", False), ("t2", True)): lines = convert(make(), skew) (out_dir / f"{table}-{name}.txt").write_text("\n".join(lines) + "\n") count += 1 (out_dir / "t1-status.txt").write_text("\n".join(convert(closed_box.s_status(), False)) + "\n") print(f"{len(closed_box.STREAMS)} streams and the status stream: {count + 1} scripts in {out_dir}") return 0 if __name__ == "__main__": raise SystemExit(main())