#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """6502decoder's smoke test: no capture from anywhere, only samples made here. python3 -I "$EMU_TOOLS_GPL/tools/6502decoder/tests/smoke.py" Checks build.py's version line and its usage-error exit status (2); then, if the program is built, that build-info.json names the pin, and decodes a capture of our own: reset held low, then released, then 40 NOPs ($EA, SYNC on each opcode fetch, RDY high), which it must decode as NOPs of two cycles each (OBSERVED; the decoder's reading of our made-up samples, not a hardware claim). Under a second. """ from __future__ import annotations import json import struct 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] = [] RNW, SYNC, RDY, RST = 0x0100, 0x0200, 0x0400, 0x4000 def check(ok: bool, what: str) -> None: print(("ok " if ok else "FAIL ") + what) if not ok: failures.append(what) 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("6502decoder-build 0.1.0 (6502Decoder c32dd60)"), "--version names the tool and the pin") # fmt: skip done = subprocess.run([*script, "--bogus"], capture_output=True, text=True) check(done.returncode == 2, "an unknown argument: exit 2") if not build.PROGRAM.exists(): print("skip: not built (build.py)") else: info = json.loads((build.BUILD / "build-info.json").read_text()) check(info["upstream"]["commit"] == build.PIN, "built from the pin") with tempfile.TemporaryDirectory(prefix="decode6502-smoke-") as scratch: capture = Path(scratch) / "nops.bin" high = RNW | RDY samples = [0xFF | high] * 8 + [0xFF | high | RST] * 5 + [high | RST, 0xE0 | high | RST] for _ in range(40): samples += [0xEA | high | RST | SYNC, 0xEA | high | RST] capture.write_bytes(struct.pack(f"<{len(samples)}H", *samples)) done = subprocess.run([str(build.PROGRAM), "--phi2=", "--mem=00F", "--cpu=65c02", "-a", "-h", "-i", "-y", str(capture)], capture_output=True, text=True, timeout=30, check=False) # fmt: skip nops = [line.split(" : ") for line in done.stdout.splitlines() if " NOP " in line] check(len(nops) >= 30 and all(fields[-1].strip() == "2" for fields in nops), f"our capture decodes as two-cycle NOPs ({len(nops)} of them)") # fmt: skip print(f"{len(failures)} failed" if failures else "smoke passed") return 1 if failures else 0 if __name__ == "__main__": raise SystemExit(main())