#!/usr/bin/env python3 """Build the referee material from what scripts/fetch_references.py fetched. Everything lands under reference/build/ (gitignored); nothing needs sudo. Each step is skipped when its output already exists. - ``oss``: unpack the oss-cad-suite bundle into reference/build/oss-cad-suite (GHDL 7.0.0-dev). - ``p65c816``: analyse srg320's P65C816 VHDL (reference/src/SNES_MiSTer/ rtl/65C816) with GHDL into reference/build/p65c816, in dependency order. It exposes A_OUT[23:0], VDA, VPA, VPB, MLB and WE per cycle. Hand-written HDL, not die-traced. - ``p65c816-tb``: analyse P65C816 and tb_p65c816.vhd (a GPL tool kept outside this repository: $EMU_TOOLS_GPL/tools/65816-python/) with GHDL's LLVM back end into reference/build/p65c816-tb and elaborate the testbench executable rung 5 runs (tests/p65c816.py, scripts/run_p65c816.py). Rebuilt whenever the testbench source is newer. - ``ipl``: write the substitute S-SMP boot ROM (scripts/mame_trace.py ``IPL``) to reference/build/mame-roms/snes/spc700.rom, for running MAME by hand with ``-rompath reference/build/mame-roms``. python -I scripts/build_referees.py # all steps python -I scripts/build_referees.py p65c816 # one step """ from __future__ import annotations import argparse import os import subprocess import sys import tarfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] BUILD = ROOT / "reference" / "build" SRC = ROOT / "reference" / "src" OSS = BUILD / "oss-cad-suite" P65 = SRC / "SNES_MiSTer" / "rtl" / "65C816" #: The testbench elaborates with srg320's GPL P65C816, so it is a GPL tool, kept outside #: this repository since 2026-11-22 (docs/provenance.md, "History"), at its fixed path in #: the GPL tool repository EMU_TOOLS_GPL names: $EMU_TOOLS_GPL/tools/65816-python/ #: tb_p65c816.vhd (one variable per tool repository, emu-tools docs/contract.md section 5). GPL_TOOLS = os.environ.get("EMU_TOOLS_GPL") BENCH_PATH = "tools/65816-python/tb_p65c816.vhd" # The order the units depend on each other (65C816.qip lists them differently). P65_UNITS = ("P65816_pkg", "AddrGen", "BCDAdder", "AddSubBCD", "ALU", "MCode", "P65C816") def step_oss() -> None: if (OSS / "bin" / "ghdl").exists(): return archive = BUILD / "oss-cad-suite-linux-x64-20261005.tgz" with tarfile.open(archive) as bundle: bundle.extractall(BUILD, filter="tar") def step_p65c816() -> None: work = BUILD / "p65c816" if (work / "work-obj08.cf").exists(): return step_oss() work.mkdir(parents=True, exist_ok=True) env = dict(os.environ) env["PATH"] = f"{OSS / 'bin'}:{env['PATH']}" for unit in P65_UNITS: subprocess.run( ["ghdl", "-a", "--std=08", "-fsynopsys", str(P65 / f"{unit}.vhd")], cwd=work, env=env, check=True, ) # fmt: skip print(f"analysed {len(P65_UNITS)} units into {work}") def step_p65c816_tb() -> None: work = BUILD / "p65c816-tb" bench = Path(GPL_TOOLS or ".") / BENCH_PATH if not GPL_TOOLS or not bench.exists(): print( "tb_p65c816.vhd is a GPL tool kept outside this repository: set EMU_TOOLS_GPL " f"(expects $EMU_TOOLS_GPL/{BENCH_PATH})", file=sys.stderr, ) sys.exit(3) executable = work / "tb_p65c816" if executable.exists() and executable.stat().st_mtime > bench.stat().st_mtime: return step_oss() work.mkdir(parents=True, exist_ok=True) env = dict(os.environ) env["PATH"] = f"{OSS / 'bin'}:{env['PATH']}" sources = [P65 / f"{unit}.vhd" for unit in P65_UNITS] + [bench] for source in sources: subprocess.run( ["ghdl", "-a", "--std=08", "-fsynopsys", "-O2", str(source)], cwd=work, env=env, check=True, ) # fmt: skip subprocess.run( ["ghdl", "-e", "--std=08", "-fsynopsys", "tb_p65c816"], cwd=work, env=env, check=True ) print(f"built {executable}") def step_ipl() -> None: target = BUILD / "mame-roms" / "snes" / "spc700.rom" if target.exists(): return sys.path.insert(0, str(ROOT / "scripts")) from mame_trace import ipl target.parent.mkdir(parents=True, exist_ok=True) target.write_bytes(ipl()) print(f"wrote {target}") STEPS = { "oss": step_oss, "p65c816": step_p65c816, "p65c816-tb": step_p65c816_tb, "ipl": step_ipl, } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("steps", nargs="*", metavar="STEP", help=f"any of {', '.join(STEPS)}") args = parser.parse_args() unknown = set(args.steps) - set(STEPS) if unknown: parser.error(f"unknown step(s): {', '.join(sorted(unknown))}") for name in args.steps or STEPS: STEPS[name]() return 0 if __name__ == "__main__": sys.exit(main())