#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0-only """mednafen's smoke test: no ROM from anywhere, only a two-byte program built here. python3 -I "$EMU_TOOLS_GPL2/tools/mednafen/tests/smoke.py" Checks build.py's version line and its usage-error exit status (2); then, for each machine that is built, that its build-info.json names the pinned tarball and its options; and, if the ``pce`` trace build exists, runs it headless on an 8 KiB card whose reset vector points at ``BRA *`` ($E000: 80 FE) for 1,000 instructions and checks the trace: every instruction at $E000, the cycle count rising by 16 at a time (a taken BRA, 4 CPU cycles at the power-on low speed, each 4 of the trace's 7.16 MHz units; OBSERVED, not a hardware claim), and MMU lines between them. Under a few seconds. """ from __future__ import annotations import json import os import subprocess import sys import tempfile from pathlib import Path HERE = Path(__file__).resolve().parents[1] sys.path.insert(0, str(HERE)) import build # noqa: E402 failures: list[str] = [] def check(ok: bool, what: str) -> None: print(("ok " if ok else "FAIL ") + what) if not ok: failures.append(what) def card() -> bytes: image = bytearray(b"\xff" * 8192) image[0:2] = b"\x80\xfe" # $E000: BRA * for vector in range(0x1FF6, 0x2000, 2): image[vector : vector + 2] = b"\x00\xe0" return bytes(image) def trace(program: Path, count: int) -> list[str]: with tempfile.TemporaryDirectory(prefix="mednafen-smoke-") as scratch: out = Path(scratch) (out / "home").mkdir() cart = out / "cart.pce" cart.write_bytes(card()) env = dict(os.environ, SDL_VIDEODRIVER="dummy", SDL_AUDIODRIVER="dummy", MEDNAFEN_HOME=str(out / "home"), MEDNAFEN_TRACE=str(out / "error.log"), MEDNAFEN_TRACE_COUNT=str(count)) # fmt: skip subprocess.run([str(program), "-sound", "0", "-video.fs", "0", "-nothrottle", "1", "-filesys.path_firmware", str(out / "home"), str(cart)], env=env, cwd=out, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=120, check=False) # fmt: skip log = out / "error.log" return log.read_text().splitlines() if log.exists() else [] def main() -> int: script = [sys.executable, "-I", str(HERE / "build.py")] done = subprocess.run([*script, "--version"], capture_output=True, text=True) check(done.returncode == 0 and done.stdout.startswith("mednafen-build 0.1.0 (Mednafen 1.32.1, de7eb94a)"), "--version names the tool and the pin") # fmt: skip done = subprocess.run([*script, "--machine", "nes"], capture_output=True, text=True) check(done.returncode == 2, "an unknown machine: exit 2") for machine, spec in build.MACHINES.items(): record = build.BUILD / machine / "build-info.json" if not build.binary(machine).exists() or not record.exists(): print(f"skip {machine}: not built (build.py --machine {machine})") continue info = json.loads(record.read_text()) check(info["upstream"]["sha256"] == build.SHA256 and info["patches"] == spec["patches"] and info["configure"] == spec["configure"], f"{machine}: built from the pinned tarball with its patches and options") # fmt: skip program = build.binary("pce") if program.exists(): lines = trace(program, 1000) insns = [line.split() for line in lines if not line.startswith(("M ", "["))] cycles = [int(row[6]) for row in insns] check(len(insns) == 1000 and all(row[0].upper() == "E000" for row in insns), "pce: 1,000 instructions traced, every one the BRA at $E000") # fmt: skip check(len(cycles) > 1 and all(b - a == 16 for a, b in zip(cycles[1:], cycles[2:])), "pce: the cycle count rises by 16 a BRA (4 low-speed cycles)") # fmt: skip check(any(line.startswith("M ") for line in lines), "pce: MMU lines in the trace") print(f"{len(failures)} failed" if failures else "smoke passed") return 1 if failures else 0 if __name__ == "__main__": raise SystemExit(main())