#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """nuked-harness's smoke test: synthetic write scripts only. python3 -I tests/smoke.py Checks the version line and the usage exit codes; then, if build.sh has built the harnesses, runs every machine on a short write script and checks the manifest, the record count and that a written tone is heard (the output is not constant). 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 script(path: Path, rows: list[dict]) -> Path: path.write_text("".join(json.dumps(r) + "\n" for r in rows)) return path def pairs(writes: list[tuple[int, int]], gap: int, start: int = 100) -> list[dict]: """Address-then-data writes, ``gap`` cycles apart.""" rows, t = [], start for address, data in writes: rows += [{"t": t, "port": 0, "data": address}, {"t": t + gap, "port": 1, "data": data}] t += 2 * gap return rows TONES = { # channel 1, algorithm 7 (all carriers), fast attack, a middle pitch, key on "ym2612": pairs([(0xB0, 7), (0xB4, 0xC0), *[(r, 0x01) for r in (0x30, 0x34, 0x38, 0x3C)], *[(r, 0x00) for r in (0x40, 0x44, 0x48, 0x4C)], *[(r, 0x1F) for r in (0x50, 0x54, 0x58, 0x5C)], (0xA4, 0x22), (0xA0, 0x69), (0x28, 0xF0)], 40), # fmt: skip # melody channel 1: instrument 1, volume 0, key on "ym2413": pairs([(0x10, 0xAC), (0x30, 0x10), (0x20, 0x1A)], 200), # tone 1: period 0x0FE, attenuation 0 "psg": [{"t": 100, "data": 0x8E}, {"t": 200, "data": 0x0F}, {"t": 300, "data": 0x90}], # channel 1: algorithm 7, both outputs, carriers loud, fast attack, key on all operators "ym2151": pairs([(0x20, 0xC7), (0x28, 0x4A), *[(r, 0x01) for r in (0x40, 0x48, 0x50, 0x58)], *[(r, 0x00) for r in (0x60, 0x68, 0x70, 0x78)], *[(r, 0x1F) for r in (0x80, 0x88, 0x90, 0x98)], (0x08, 0x78)], 64), # fmt: skip } TONES["ym3438"], TONES["ds1001"], TONES["ym2164"] = TONES["ym2612"], TONES["ym2413"], TONES["ym2151"] LAYOUT = {"ym2612": " int: with tempfile.TemporaryDirectory(prefix="nuked-smoke-") as scratch: tmp = Path(scratch) done = subprocess.run([*RUN, "--version"], capture_output=True, text=True) check(done.returncode == 0 and "335747d" in done.stdout and "f209e6e" in done.stdout, "--version names the tool and the four pins") # fmt: skip done = subprocess.run([*RUN, "--machine", "psg"], capture_output=True, text=True) check(done.returncode == 2, "missing arguments are a usage error (2)") done = subprocess.run([*RUN, "--machine", "psg", "--input", str(tmp / "none"), "--cycles", "10", "--out", str(tmp / "o")], capture_output=True, text=True) # fmt: skip check(done.returncode == 3, "a missing input is a missing input (3)") if not (HERE / "build" / "opm-events").exists(): print("skip part 2: run build.sh first") else: for machine, rows in TONES.items(): cycles = 200_000 out = tmp / machine done = subprocess.run([*RUN, "--machine", machine, "--input", str(script(tmp / f"{machine}.jsonl", rows)), "--cycles", str(cycles), "--out", str(out)], capture_output=True) # fmt: skip check(done.returncode == 0, f"{machine}: run.py exits 0") if done.returncode: continue record = json.loads((out / "manifest.json").read_text()) data = (out / f"{machine}.bin").read_bytes() values = {r[1 if machine == "psg" else 0 if LAYOUT[machine] != "= least, f"{machine}: the tone is heard ({len(values)} output values)") print(f"{len(failures)} failed" if failures else "smoke passed") return 1 if failures else 0 if __name__ == "__main__": raise SystemExit(main())