#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Build the picture scenes (tests/scenes/*.c, this repository's own MIT programs) into cartridge images under tests/referee_runs/scenes/ (ignored: rebuilt on demand), with the GNU Arm toolchain that scripts/fetch_references.py put under reference/tools/. Each scene sets up one picture and loops. They exist because the hardware-calibrated picture tests cover little of rung 3 (text and bitmap backgrounds, DISPCNT latching): the scenes give the detector (NanoBoyAdvance, scripts/board_pictures.py --scenes) affine backgrounds, every OBJ form, windows, colour effects and mosaic to disagree about. A scene matching the detector is OBSERVED evidence, never a hardware fact. The header has no Nintendo logo: the scenes boot with the free BIOS (Cult-of-GBA), which does not check it, on the board and on the detector alike. python scripts/build_scenes.py """ from __future__ import annotations import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SOURCES = ROOT / "tests" / "scenes" OUT = ROOT / "tests" / "referee_runs" / "scenes" TOOLS = ROOT / "reference" / "tools" / "arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi" / "bin" CFLAGS = [ "-marm", "-mcpu=arm7tdmi", "-O2", "-ffreestanding", "-nostdlib", "-fno-builtin", "-fno-tree-loop-distribute-patterns", "-Wall", "-Werror", "-Wno-unused-function", ] def header_complement(image: bytearray) -> None: """The header's complement check at 0BD: -(sum of 0A0-0BC) - 0x19 (GBATEK §GBA Cartridge Header; DOC).""" total = sum(image[0xA0:0xBD]) image[0xBD] = (-(total + 0x19)) & 0xFF def build(source: Path) -> Path: OUT.mkdir(parents=True, exist_ok=True) elf = OUT / f"{source.stem}.elf" image_path = OUT / f"{source.stem}.gba" gcc = str(TOOLS / "arm-none-eabi-gcc") command = [gcc, *CFLAGS, "-T", str(SOURCES / "scene.ld"), "-o", str(elf)] command += [str(SOURCES / "crt0.s"), str(source), "-lgcc"] subprocess.run(command, check=True) binary = OUT / f"{source.stem}.bin.tmp" # replaced on every build subprocess.run( [str(TOOLS / "arm-none-eabi-objcopy"), "-O", "binary", str(elf), str(binary)], check=True ) image = bytearray(binary.read_bytes()) header_complement(image) image_path.write_bytes(bytes(image)) return image_path def main() -> int: sources = [Path(a) for a in sys.argv[1:]] or sorted(SOURCES.glob("*.c")) for source in sources: print(build(source).relative_to(ROOT)) return 0 if __name__ == "__main__": sys.exit(main())