#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """ym2203-lle's smoke test: synthetic register writes only, no ROM. python3 -I tests/smoke.py Checks the version line and the usage and missing-input exit codes; then, if build.sh has built ssg_ref, plays a tone on channel A and checks the manifest, the record count and that channel A's level moves. Exit 0 when every part that could run passed. """ from __future__ import annotations import json import struct import subprocess import sys import tempfile from pathlib import Path HERE = Path(__file__).resolve().parents[1] RUN = [sys.executable, "-I", str(HERE / "run.py")] failures: list[str] = [] def check(ok: bool, what: str) -> None: print(("ok " if ok else "FAIL ") + what) if not ok: failures.append(what) def run(*args: str) -> subprocess.CompletedProcess: return subprocess.run([*RUN, *args], capture_output=True, text=True) def main() -> int: done = run("--version") check(done.returncode == 0 and done.stdout.startswith("ym2203-lle 0.1.0 (YM2203-LLE 817387f)"), "--version names the tool and the pin") check(run().returncode == 2, "no arguments: exit 2") with tempfile.TemporaryDirectory(prefix="ym2203-lle-smoke-") as scratch: work = Path(scratch) check(run("--input", str(work / "absent.jsonl"), "--cycles", "10", "--out", str(work / "o")).returncode == 3, "a missing write script: exit 3") twice = work / "twice.jsonl" twice.write_text('{"t": 5, "reg": 0, "value": 1}\n{"t": 5, "reg": 1, "value": 0}\n') check(run("--input", str(twice), "--cycles", "10", "--out", str(work / "o")).returncode == 2, "two writes to one step: exit 2") if not (HERE / "build" / "ssg_ref").exists(): print("skip the run: build.sh has not been run") return 1 if failures else 0 # tone period 0x010 and full volume on channel A, tone A only writes = [(0, 0x10), (1, 0x00), (7, 0x3E), (8, 0x0F)] script = work / "tone.jsonl" script.write_text("".join(json.dumps({"t": 8 * (i + 1), "reg": r, "value": v}) + "\n" for i, (r, v) in enumerate(writes))) steps = 4000 out = work / "run" done = run("--input", str(script), "--cycles", str(steps), "--out", str(out)) check(done.returncode == 0, "the run completes") if done.returncode != 0: print(done.stderr) return 1 manifest = json.loads((out / "manifest.json").read_text()) check(manifest["evidence"] == "OBSERVED" and manifest["outputs"][0]["records"] == steps, "manifest: OBSERVED, one record per step") levels = [a for a, _, _ in struct.iter_unpack(" 1, "channel A's level moves (the tone)") return 1 if failures else 0 if __name__ == "__main__": raise SystemExit(main())