#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Build what the oracles need from the pinned sources, into reference/build/ (gitignored). 1. ``decode6502`` from hoglet67/6502Decoder (GPL-3.0; an external program this repository only runs): the decoder for the logic-analyser captures. Since 2026-11-25 (decisions page T3) its source and build are outside this repository: this runs ``$EMU_TOOLS_GPL/tools/6502decoder/build.py`` as a program (``EMU_TOOLS_GPL`` names a checkout of emu-tools-gpl; exit status 3 without it), which builds ``$EMU_TOOLS_GPL/tools/6502decoder/build/decode6502``. 2. Klaus Dormann's 65C02 extended opcodes test, assembled once per variant row from amb5l's ca65 port by setting its two switches, and Bruce Clark's decimal test with ``cputype = 1`` (65C02). Each image is 64 KB, loads at $0000 and starts at $0400 (the linker configuration is amb5l's ``ca65/example.cfg``). row wdc_op rkwl_wdc_op what the test then expects wdc 1 1 bit instructions tested; $CB/$DB not executed rockwell 0 1 bit instructions tested; $CB/$DB one-byte NOPs sc 0 0 x7/xF and $CB/$DB all one-byte NOPs python -I scripts/build_tools.py # both python -I scripts/build_tools.py --only dormann Needs gcc and cc65 2.19 (``ca65``/``ld65`` on PATH, or in the directory the ``CC65_BIN`` environment variable names). The sources are edited only in memory: the switch lines are replaced in a copy written under reference/build/. """ from __future__ import annotations import argparse import hashlib import os import re import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "reference" / "src" BUILD = ROOT / "reference" / "build" CC65 = Path(os.environ["CC65_BIN"]) if os.environ.get("CC65_BIN") else None #: variant row: (wdc_op, rkwl_wdc_op) DORMANN_ROWS = {"wdc": (1, 1), "rockwell": (0, 1), "sc": (0, 0)} def tool(name: str) -> str: found = shutil.which(name) if found: return found if CC65 is not None and (CC65 / name).exists(): return str(CC65 / name) raise SystemExit( f"{name} not found: put cc65 2.19 on PATH or set CC65_BIN to its bin directory" + (f" (CC65_BIN is {CC65})" if CC65 is not None else " (CC65_BIN is not set)") ) def build_decoder() -> None: root = os.environ.get("EMU_TOOLS_GPL") script = Path(root) / "tools" / "6502decoder" / "build.py" if root else None if script is None or not script.exists(): print("decode6502: set EMU_TOOLS_GPL (its build is $EMU_TOOLS_GPL/tools/6502decoder/build.py)", file=sys.stderr) # fmt: skip sys.exit(3) done = subprocess.run([sys.executable, "-I", str(script)], check=False) if done.returncode: sys.exit(done.returncode) def assemble(text: str, stem: str, config: Path) -> Path: work = BUILD / "dormann" work.mkdir(parents=True, exist_ok=True) source = work / f"{stem}.ca65" source.write_text(text, encoding="utf-8") obj, image = work / f"{stem}.o", work / f"{stem}.bin" subprocess.run( [tool("ca65"), "-l", str(work / f"{stem}.lst"), "-o", str(obj), str(source)], check=True ) subprocess.run( [ tool("ld65"), str(obj), "-o", str(image), "-m", str(work / f"{stem}.map"), "-C", str(config), ], check=True, ) digest = hashlib.sha256(image.read_bytes()).hexdigest() print(f"built {image.relative_to(ROOT)} {image.stat().st_size} bytes sha256 {digest}") return image def set_switch(text: str, name: str, value: int) -> str: changed, count = re.subn(rf"(?m)^{name}\s*=\s*\d+", f"{name} = {value}", text) if count != 1: raise SystemExit(f"expected one '{name} = N' line, found {count}") return changed def build_dormann() -> None: ca65 = SRC / "dormann" / "ca65" config = ca65 / "example.cfg" extended = (ca65 / "65C02_extended_opcodes_test.ca65").read_text(encoding="latin-1") for row, (wdc_op, rkwl_wdc_op) in DORMANN_ROWS.items(): text = set_switch(set_switch(extended, "wdc_op", wdc_op), "rkwl_wdc_op", rkwl_wdc_op) assemble(text, f"65C02_extended_opcodes_test_{row}", config) decimal = (ca65 / "6502_decimal_test.ca65").read_text(encoding="latin-1") assemble(set_switch(decimal, "cputype", 1), "65C02_decimal_test", config) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--only", choices=("decoder", "dormann")) args = parser.parse_args() if args.only != "dormann": build_decoder() if args.only != "decoder": build_dormann() return 0 if __name__ == "__main__": sys.exit(main())