#!/usr/bin/env python3 """Build dma_stop.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 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) and its complement check, as gbafix does. python tests/hw/dma_stop/build.py """ from __future__ import annotations import argparse import subprocess import sys from pathlib import Path HERE = Path(__file__).resolve().parent OUT = HERE / "dma_stop.gba" BUILD = HERE / "build" TOOLCHAIN = Path( "/data/emu/gba-python/reference/tools/arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi/bin" ) # Header bytes 0x004-0x09F. Every GBA cartridge carries them; the BIOS # compares them with its own copy and stops if they differ (GBATEK # "004h..09Fh - Nintendo Logo"). LOGO = bytes.fromhex( "24ffae51699aa2213d84820a84e409ad11248b98c0817f21a352be199309ce20" "10464a4af82731ec58c7e83382e3cebf85f4df94ce4b09c194568ac01372a7fc" "9f844d73a3ca9a615897a327fc039876231dc7610304ae56bf38840040a70efd" "ff52fe036f9530f197fbc08560d68025a963be03014e38e2f9a234ffbb3e0344" "780090cb88113a9465c07c6387f03cafd625e48b380aac7221d4f807" ) 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" ) args = parser.parse_args() 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 / "dma_stop.elf" run( [ tool("gcc"), "-mcpu=arm7tdmi", "-nostdlib", "-T", str(HERE / "gba.ld"), *objects, "-lgcc", "-Wl,--no-warn-rwx-segments", "-Wl,-Map," + str(BUILD / "dma_stop.map"), "-o", str(elf), ] ) raw = BUILD / "dma_stop.raw" run([tool("objcopy"), "-O", "binary", str(elf), str(raw)]) rom = bytearray(raw.read_bytes()) rom[0x04:0xA0] = 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())