# SPDX-License-Identifier: GPL-2.0-or-later """Fetch WinUAE at its pinned commit and build the 68000 CPU-tester referee. python3 -I build_winuae.py # fetch if needed, build build/winuae_referee python3 -I build_winuae.py --version # winuae-referee 0.2.0 (WinUAE 6e0d5ae) # consumers (m68000-python) set EMU_TOOLS_GPL2 and run the documented path, # $EMU_TOOLS_GPL2/tools/winuae/build/winuae_referee Part of the winuae tool in emu-tools-gpl2 (README; GPL-2.0-only as built). The build steps were moved out of m68000-python's validation/referees/build_referees.py on 2026-11-19 so that the WinUAE-derived driver lives outside the MIT repository, then folded from the stand-alone m68000-winuae-referee into this repository on 2026-11-24. WinUAE's source is fetched at the pin into upstream/winuae-/ (git-ignored, never committed) and the build lands in build/ (git-ignored). WinUAE: the CPU tester's 68000 core. cpudefs.cpp is the tree's own (build68k's output from table68k, committed upstream). gencpu, compiled with CPU_TESTER set (the tester's documented build step), generates cpuemu_90_test.cpp and its table; winuae_referee.cpp #includes the tester's cputest.cpp and drives that core. Two Unix adjustments, both made to copies in build/: od-unix's machdep/m68k.h defines cctrue() inline where the tester defines its own (the Windows header only declares it), and gencpu.cpp's CPU_TESTER switch is a #define set to 0. Exit status as the family's tool contract says: 0 built, 2 a usage error, 3 the source could not be fetched, anything else a failed build. Nothing is ever deleted: a half-fetched tree stops the script until it is moved aside. """ from __future__ import annotations import json import os import re import shutil import subprocess import sys import tarfile import tempfile import time from concurrent.futures import ThreadPoolExecutor from pathlib import Path from urllib.request import urlopen HERE = Path(__file__).resolve().parent SOURCES = HERE / "upstream" BUILD = HERE / "build" TOOL = "winuae-referee" VERSION = "0.2.0" WINUAE_REPOSITORY = "https://github.com/tonioni/WinUAE" WINUAE_REVISION = "6e0d5ae15a9e7ae071d64fa052a34429ed787476" # 2026-10-27 JOBS = min(8, os.cpu_count() or 1) NICE = ["nice", "-n", "10"] def fetch(name: str, repository: str, revision: str) -> Path: """Download and extract ``repository`` at ``revision``; return the tree.""" target = SOURCES / f"{name}-{revision[:12]}" if (target / ".complete").exists(): return target if target.exists(): raise SystemExit(f"{target} is incomplete (an interrupted fetch?): move it aside first") SOURCES.mkdir(parents=True, exist_ok=True) url = f"{repository}/archive/{revision}.tar.gz" print(f"downloading {url}", flush=True) with tempfile.TemporaryDirectory(dir=SOURCES) as temporary: archive = Path(temporary) / "source.tar.gz" try: with urlopen(url, timeout=300) as response, archive.open("wb") as output: shutil.copyfileobj(response, output, 1 << 20) except OSError as error: print(f"could not fetch {url}: {error}", file=sys.stderr) raise SystemExit(3) from error with tarfile.open(archive, "r:gz") as bundle: bundle.extractall(temporary, filter="data") (root,) = [p for p in Path(temporary).iterdir() if p.is_dir()] root.rename(target) (target / ".complete").write_text(revision + "\n") return target def run(command: list[str], cwd: Path) -> None: result = subprocess.run(NICE + command, cwd=cwd, capture_output=True, text=True) if result.returncode: sys.stderr.write(result.stdout + result.stderr) raise SystemExit(f"failed in {cwd}: {' '.join(command[:4])} ...") def compile_all(jobs: list[tuple[list[str], Path]]) -> None: with ThreadPoolExecutor(JOBS) as pool: list(pool.map(lambda job: run(*job), jobs)) def build_winuae() -> Path: source = fetch("winuae", WINUAE_REPOSITORY, WINUAE_REVISION) out = BUILD out.mkdir(parents=True, exist_ok=True) gen = out / "gen" gen.mkdir(exist_ok=True) overlay = out / "overlay" / "machdep" overlay.mkdir(parents=True, exist_ok=True) # od-unix defines cctrue() inline; the tester defines it itself. header = (source / "od-unix" / "machdep" / "m68k.h").read_text() header, count = re.subn( r"static inline int cctrue\(int cc\)\n\{.*?\n\}\n", "extern int cctrue(int cc);\n", header, flags=re.S, ) if count != 1: raise SystemExit("od-unix/machdep/m68k.h: cctrue() not found where expected") (overlay / "m68k.h").write_text(header) shutil.copy(HERE / "winuae_shim.h", out / "winuae_shim.h") include = [ f"-I{out / 'overlay'}", f"-I{gen}", f"-I{source / 'od-unix'}", f"-I{source}", f"-I{source / 'include'}", f"-I{source / 'cputest'}", ] flags = [ "-w", "-fpermissive", "-std=gnu++17", "-include", str(out / "winuae_shim.h"), "-DUAE_TARGET_UNIX=1", "-DUAE_NOGUI=1", ] print("winuae: gencpu (CPU_TESTER)", flush=True) # gencpu with the tester switch on gencpu = (source / "gencpu.cpp").read_text() if "#define CPU_TESTER 0\n" not in gencpu: raise SystemExit("gencpu.cpp: CPU_TESTER switch not found") (out / "gencpu_tester.cpp").write_text( gencpu.replace("#define CPU_TESTER 0\n", "#define CPU_TESTER 1\n") ) run( [ "g++", *flags, *include, "-O1", "-o", str(out / "gencpu_tester"), str(out / "gencpu_tester.cpp"), str(source / "readcpu.cpp"), str(source / "cpudefs.cpp"), str(HERE / "winuae_shim.cpp"), ], out, ) subprocess.run([str(out / "gencpu_tester")], cwd=gen, check=True, stdout=subprocess.DEVNULL) defines = [f"-DCPUEMU_{n}" for n in range(90, 96)] + ["-DCPU_TESTER"] units = [ gen / "cpustbl_test.cpp", *[gen / f"cpuemu_{n}_test.cpp" for n in range(90, 96)], source / "cpudefs.cpp", source / "cputest_support.cpp", source / "newcpu_common.cpp", source / "readcpu.cpp", source / "disasm.cpp", source / "fpp.cpp", source / "fpp_softfloat.cpp", source / "softfloat" / "softfloat.cpp", source / "softfloat" / "softfloat_decimal.cpp", source / "softfloat" / "softfloat_fpsp.cpp", source / "ini.cpp", HERE / "winuae_shim.cpp", HERE / "winuae_referee.cpp", ] objects = [] jobs = [] for unit in units: obj = out / (unit.stem + ".o") objects.append(obj) jobs.append( (["g++", *flags, *defines, *include, "-O2", "-c", str(unit), "-o", str(obj)], out) ) print(f"winuae: compiling {len(units)} units ({JOBS} jobs)", flush=True) compile_all(jobs) binary = out / "winuae_referee" run(["g++", "-o", str(binary), *map(str, objects), "-lz"], out) (out / "PIN").write_text(WINUAE_REVISION + "\n") compiler = subprocess.run(["g++", "--version"], capture_output=True, text=True).stdout info = { "tool": TOOL, "tool_version": VERSION, "compiler": compiler.splitlines()[0] if compiler else "unknown", "cflags": " ".join(["-O2", *flags[:3], *flags[5:], *defines]), "built": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "upstream": WINUAE_REVISION, "source": "fetched", "patches": [], } (out / "build-info.json").write_text(json.dumps(info) + "\n") print(f"built {binary}", flush=True) return binary def main(argv: list[str]) -> int: if argv == ["--version"]: print(f"{TOOL} {VERSION} (WinUAE {WINUAE_REVISION[:7]})") return 0 if argv: print("usage: python3 -I build_winuae.py [--version]", file=sys.stderr) return 2 build_winuae() return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv[1:]))