#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Build what the rungs need 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. Run with ``python -I``: the inputs are fetched, untrusted files. - ``spctest``: cut the three raw SPC700 programs (``spc_tests0..2``, loaded at $0300 by the S-CPU) out of gilyon's pinned ``spctest.sfc`` into reference/build/spctest/spc_testsN.bin. They are the board-free form of the 558-test SPC700 suite: run them from $0300 in the minimal harness (see docs/validation.md "gilyon spctest"). The ROM places them at file offsets $0A79, $8000 and $10000 (ld65 segments RODATA, BANK1, BANK2); the step checks that each starts with ``MOV $F4,#$00 / MOV $F1,#$00`` (8F 00 F4 8F 00 F1). (Reassembling them from the release's ``spc_tests*.asm`` would need spcasm, whose 2.0.0 release fails to build on stable Rust 1.93 here; the start pattern and MAME running the ROM to "Success" stand in for that.) - ``iplrom``: write the S-SMP's 64-byte IPL ROM, as printed in anomie's pinned SPC700 doc, to reference/build/iplrom.bin after checking it against MAME's CRC-32 44bb3a40 / SHA-1 97e35255. The harness and MAME both use it; it is Sony/Nintendo's code and is never committed. python -I scripts/build_referees.py # all steps python -I scripts/build_referees.py spctest # one step """ from __future__ import annotations import argparse import re import sys import zlib from hashlib import sha1 from pathlib import Path ROOT = Path(__file__).resolve().parents[1] BUILD = ROOT / "reference" / "build" SPCTEST = ROOT / "reference" / "roms" / "snes-tests" / "spctest" ANOMIE_DOC = ROOT / "reference" / "docs" / "anomie-spc700.txt" START = bytes([0x8F, 0x00, 0xF4, 0x8F, 0x00, 0xF1]) # (file offset, end) of each program in spctest.sfc; the end is the bank end, # and trailing bytes after the program are harmless padding. BLOBS = {0: (0x0A79, 0x8000), 1: (0x8000, 0x10000), 2: (0x10000, 0x18000)} def step_spctest() -> None: out = BUILD / "spctest" if all((out / f"spc_tests{n}.bin").exists() for n in BLOBS): return rom = (SPCTEST / "spctest.sfc").read_bytes() out.mkdir(parents=True, exist_ok=True) for n, (start, end) in BLOBS.items(): if rom[start : start + len(START)] != START: raise SystemExit(f"spc_tests{n}: no program at ${start:X} in spctest.sfc") (out / f"spc_tests{n}.bin").write_bytes(rom[start:end]) print(f"spctest: spc_tests{n}.bin ({end - start} bytes from ${start:X})") def step_iplrom() -> None: target = BUILD / "iplrom.bin" if target.exists(): return text = ANOMIE_DOC.read_text(encoding="latin-1") block = text.split("IPL BOOT ROM", 1)[1].split(".ORG", 1)[0] data = bytes(int(h, 16) for h in re.findall(r"\$([0-9A-F]{2})", block)) if zlib.crc32(data) != 0x44BB3A40 or sha1(data).hexdigest() != ( "97e352553e94242ae823547cd853eecda55c20f0" ): raise SystemExit("anomie's IPL ROM bytes do not match MAME's spc700.rom") target.parent.mkdir(parents=True, exist_ok=True) target.write_bytes(data) print("iplrom: 64 bytes, CRC-32 44bb3a40") STEPS = {"spctest": step_spctest, "iplrom": step_iplrom} def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("steps", nargs="*", help=f"any of {', '.join(STEPS)} (default: all)") 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())