# SPDX-License-Identifier: GPL-2.0-or-later """Fetch WinUAE at its pinned commit and build the 68000 CPU-tester referee. python build_winuae.py # fetch if needed, build build/winuae/winuae_referee export WINUAE_REFEREE=$PWD/build/winuae/winuae_referee # for m68000-python's referee.py Part of m68000-winuae-referee (GPL-2.0-or-later, README). The build steps were moved here from m68000-python's validation/referees/build_referees.py on 2026-11-19 so that the WinUAE-derived driver lives outside the MIT repository. Sources land in src/winuae-/ and the build in build/winuae/. 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. """ from __future__ import annotations import os import re import shutil import subprocess import sys import tarfile import tempfile from concurrent.futures import ThreadPoolExecutor from pathlib import Path from urllib.request import urlopen HERE = Path(__file__).resolve().parent SOURCES = HERE / "src" BUILD = HERE / "build" WINUAE_REPOSITORY = "https://github.com/tonioni/WinUAE" WINUAE_REVISION = "1977af501f6c3389c2eefe119ecb10c82d6582f3" # 2026-09-17 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(): shutil.rmtree(target) 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" with urlopen(url, timeout=300) as response, archive.open("wb") as output: shutil.copyfileobj(response, output, 1 << 20) 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 / "winuae" 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") print(f"built {binary}", flush=True) return binary if __name__ == "__main__": build_winuae()