#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Build thumb_undefined.gba with the Arm GNU toolchain (arm-none-eabi). The toolchain is the one gba-python pins (Arm GNU Toolchain 14.3.rel1, fetched by gba-python's scripts/fetch_references.py); pass --toolchain to use another arm-none-eabi bin directory. Intermediates go to build/ (ignored). The header gets the logo every cartridge carries (the BIOS refuses to boot without it), copied from a cartridge image you supply, and its complement check, as gbafix does. ARM_NONE_EABI_BIN=... GBA_LOGO_FROM=some.gba python tests/hw/thumb_undefined/build.py """ from __future__ import annotations import argparse import hashlib import os import subprocess import sys from pathlib import Path HERE = Path(__file__).resolve().parent OUT = HERE / "thumb_undefined.gba" BUILD = HERE / "build" TOOLCHAIN = os.environ.get("ARM_NONE_EABI_BIN") # the toolchain's bin directory # Header bytes 0x004-0x09F: the logo every cartridge carries; the BIOS compares them with its # own copy and stops if they differ (GBATEK "004h..09Fh - Nintendo Logo"). It is Nintendo's # artwork, so it is not in this repository: the build copies it from a cartridge image the # player supplies (--logo-from, or the environment variable GBA_LOGO_FROM), checked by hash. LOGO_OFFSET, LOGO_SIZE = 0x04, 156 LOGO_SHA256 = "08a0153cfd6b0ea54b938f7d209933fa849da0d56f5a34c481060c9ff2fad818" CFLAGS = [ "-mcpu=arm7tdmi", "-mthumb", "-mthumb-interwork", "-O2", "-ffreestanding", "-fno-builtin", "-Wall", "-Wextra", ] def run(command: list[str]) -> None: print(" ".join(str(c) for c in command)) subprocess.run(command, check=True) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument( "--toolchain", type=Path, default=TOOLCHAIN, help="arm-none-eabi bin directory" ) parser.add_argument( "--logo-from", type=Path, default=os.environ.get("GBA_LOGO_FROM"), help="a GBA cartridge image to copy the header logo from (or GBA_LOGO_FROM)", ) args = parser.parse_args() if args.toolchain is None or args.logo_from is None: parser.error("give --toolchain (or ARM_NONE_EABI_BIN) and --logo-from (or GBA_LOGO_FROM)") logo = args.logo_from.read_bytes()[LOGO_OFFSET : LOGO_OFFSET + LOGO_SIZE] if hashlib.sha256(logo).hexdigest() != LOGO_SHA256: print(f"{args.logo_from} does not carry the cartridge header logo", file=sys.stderr) return 1 tool = lambda name: str(args.toolchain / f"arm-none-eabi-{name}") # noqa: E731 if not Path(tool("gcc")).exists(): print(f"no arm-none-eabi-gcc in {args.toolchain}", file=sys.stderr) return 1 BUILD.mkdir(exist_ok=True) objects = [] for source in ("crt0.s", "probes.s"): obj = BUILD / (source + ".o") run([tool("gcc"), "-mcpu=arm7tdmi", "-c", str(HERE / source), "-o", str(obj)]) objects.append(str(obj)) obj = BUILD / "main.o" run([tool("gcc"), *CFLAGS, "-c", str(HERE / "main.c"), "-o", str(obj)]) objects.append(str(obj)) elf = BUILD / "thumb_undefined.elf" run( [ tool("gcc"), "-mcpu=arm7tdmi", "-nostdlib", "-T", str(HERE / "gba.ld"), *objects, "-lgcc", "-Wl,--no-warn-rwx-segments", "-Wl,-Map," + str(BUILD / "thumb_undefined.map"), "-o", str(elf), ] ) raw = BUILD / "thumb_undefined.raw" run([tool("objcopy"), "-O", "binary", str(elf), str(raw)]) rom = bytearray(raw.read_bytes()) rom[LOGO_OFFSET : LOGO_OFFSET + LOGO_SIZE] = logo check = 0 for byte in rom[0xA0:0xBD]: check -= byte rom[0xBD] = (check - 0x19) & 0xFF rom += bytes(-len(rom) % 0x400) # whole kilobytes, for flash cart tools OUT.write_bytes(rom) print(f"{OUT.name}: {len(rom)} bytes") return 0 if __name__ == "__main__": sys.exit(main())