#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Write rung 3's scripts: every stream ay8910-python's own validation runs, in the lockstep script format (scripts/lockstep_gen.py), replayed as its referee (scripts/referee.py) applies them to the core. python3 scripts/export_streams.py REFERENCE OUT_DIR [BOARD_STREAMS_DIR] - The 24 synthetic streams of REFERENCE/scripts/streams.py, on all five parts, events at their clock times the referee's way (a part that halves its clock gets them at twice the time), run to the stream's end. - With BOARD_STREAMS_DIR (ay8910-python's validation/streams/*.stream, exported from famicom-python by its scripts/export_board_streams.py; derived from ROMs, local only and never committed): each as the Sunsoft 5B in the CPU's own clocks, and as the AY-3-8910 at half the clocks (CPU cycle n is clock n // 2), as the referee's `--half-clock` runs them. Digest mode: a take every 65,536 input clocks (each the SHA-256 of every clock's three codes), every read, every port change, and the state at the end. The ports are listened to and the outside's pins set where a stream sets them. """ from __future__ import annotations import struct import sys from pathlib import Path TAKE = 1 << 16 # input clocks between takes PARTS = {"AY8910": 1, "AY8912": 1, "AY8913": 1, "YM2149": 1, "SUNSOFT_5B": 2} # The referee's event kinds (REFERENCE/scripts/streams.py). WRITE, READ, PINS_A, PINS_B, RESET, SELECT, WRITE_DATA, READ_DATA = range(8) def script(part: str, events, clocks: int) -> str: lines = [f"variant {part} record 1", "mode digest", "la 1", "lb 1"] since = 0 now = 0 def run(n: int) -> None: nonlocal since while n: piece = min(n, TAKE - since) lines.append(f"run {piece}") n -= piece since += piece if since == TAKE: lines.append("take") since = 0 for at, kind, a, b in events: if at >= clocks: break run(at - now) now = at if kind == WRITE: lines.append(f"w {a} {b}") elif kind == READ: lines += [f"s {a}", "r"] elif kind == PINS_A: lines.append(f"pa {a}") elif kind == PINS_B: lines.append(f"pb {a}") elif kind == RESET: lines.append("reset") elif kind == SELECT: lines.append(f"s {a}") elif kind == WRITE_DATA: lines.append(f"d {b}") elif kind == READ_DATA: lines.append("r") else: raise SystemExit(f"unknown event kind {kind}") run(clocks - now) lines += ["take", "state"] return "\n".join(lines) + "\n" def main() -> None: reference, out_dir = Path(sys.argv[1]), Path(sys.argv[2]) boards = Path(sys.argv[3]) if len(sys.argv) > 3 else None sys.path.insert(0, str(reference / "scripts")) from streams import STREAMS # noqa: E402 (the reference's test inputs, unchanged) out_dir.mkdir(parents=True, exist_ok=True) count = 0 for name, make in STREAMS.items(): events, clocks = make() for part, scale in PARTS.items(): native = [(at * scale, kind, a, b) for at, kind, a, b in events] text = script(part, native, clocks * scale) (out_dir / f"{name}-{part}.txt").write_text(text) count += 1 if boards is not None and boards.is_dir(): for path in sorted(boards.glob("*.stream")): board = [ (clock, WRITE_DATA, 0, value) if port else (clock, SELECT, value, 0) for clock, port, value in struct.iter_unpack("> 1, kind, a, b) for clock, kind, a, b in board] half_end = (half[-1][0] if half else 0) + 20_000 (out_dir / f"board-{path.stem}-AY8910.txt").write_text( script("AY8910", half, half_end) ) count += 2 print(f"{count} scripts in {out_dir}") if __name__ == "__main__": main()