#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0-only """nuked-md's smoke test: a synthetic cartridge, no ROM from anywhere. python3 -I "$EMU_TOOLS_GPL2/tools/nuked-md/tests/smoke.py" Checks build_nuked_md.sh's version line, its usage-error (2) and missing-tree (3) exit statuses; then, if the harness is built (build/nukedmd-headless), runs a 1 KB cartridge of our own (reset vectors and a branch-to-self loop) past the board's reset and checks the 68000 bus trace: the reset vectors are read and the loop is fetched over and over. About ten seconds on the family's machine. """ from __future__ import annotations import os import struct import subprocess import sys import tempfile from pathlib import Path HERE = Path(__file__).resolve().parents[1] BINARY = HERE / "build" / "nukedmd-headless" RECORD = struct.Struct(" None: print(("ok " if ok else "FAIL ") + what) if not ok: failures.append(what) def cartridge() -> bytes: rom = bytearray(1024) rom[0:8] = struct.pack(">II", 0x00FFFE00, LOOP) # initial SSP, initial PC rom[LOOP : LOOP + 2] = b"\x60\xfe" # BRA.S to itself return bytes(rom) def main() -> int: script = ["sh", str(HERE / "build_nuked_md.sh")] done = subprocess.run([*script, "--version"], capture_output=True, text=True) check(done.returncode == 0 and done.stdout.startswith("nuked-md 0.2.0 (Nuked-MD 594ca2a)"), "--version names the tool and the pin") done = subprocess.run([*script, "--bogus"], capture_output=True, text=True) check(done.returncode == 2, "an unknown argument: exit 2") with tempfile.TemporaryDirectory(prefix="nmd-smoke-") as scratch: work = Path(scratch) env = dict(os.environ, NUKED_MD_SRC=str(work / "absent"), NMD_BUILD_DIR=str(work / "build")) done = subprocess.run(script, env=env, capture_output=True, text=True) check(done.returncode == 3, "NUKED_MD_SRC naming no tree: exit 3") if not BINARY.exists(): print("skip the run: build_nuked_md.sh has not been run") return 1 if failures else 0 rom = work / "loop.bin" rom.write_bytes(cartridge()) trace = work / "trace.bin" stop = 2 * 760_000 # half-MCLK: past the board's reset (about 693,000 MCLK) and some env = dict(os.environ, NMD_STOP=str(stop), NMD_TRACE=str(trace), NMD_TRACE_MASK="1") done = subprocess.run([str(BINARY), str(rom)], env=env, capture_output=True, cwd=work) check(done.returncode == 0, "the run completes") raw = trace.read_bytes() if trace.exists() else b"" cycles = [RECORD.unpack_from(raw, i) for i in range(0, len(raw) - RECORD.size + 1, RECORD.size)] reads = [(addr, data) for _, _, addr, data, _, kind, flags, _ in cycles if kind == 1 and flags & 1] addresses = [a for a, _ in reads] check(all(a in addresses for a in (0, 2, 4, 6)), "the reset vectors are read") loop = [d for a, d in reads if a == LOOP] check(len(loop) > 100 and all(d == 0x60FE for d in loop), "the loop is fetched over and over") return 1 if failures else 0 if __name__ == "__main__": raise SystemExit(main())