software shenanigans

Compiling C Code to run on a Game Boy Advance

Joe Savage's article on writing GBA games is a great introduction to the topic. It shows example code and explains how to interface with the hardware. However, the steps for setting up your toolchain to compile for GBA hardware aren't as explicit as I would have liked. I was able to get it up and running by following these steps:

  1. Start with the devkitpro/devkitarm Docker image
  2. Within the container, install build-essential and gcc-arm-none-eabi
  3. Add /opt/devkitpro/devkitARM/bin to your PATH

I found it easier to start with the Docker image since it already has most of what you need installed already. Plus, I'm running Fedora Silverblue right now, which makes local container development easy with with toolbox preinstalled. This article isn't about either of those, but I'll provide toolbox instructions alongside the vanilla Docker ones.

Run the Docker Container

This is the only place where the instructions differ. After this step, the instructions should be the same.

In Toolbox

$ toolbox create --image docker.io/devkitpro/devkitarm gba-dev
$ toolbox enter gba-dev

In Docker

You'll probably want to attach a volume to your Docker container.

$ docker run --name devkitarm docker.io/devkitpro/devkitarm -d
$ docker exec -it devkitarm /bin/bash

Install Build Dependencies

$ sudo apt install build-essential
$ sudo apt install gcc-arm-none-eabi

Update $PATH

export PATH="/opt/devkitpro/devkitARM/bin:$PATH"

Build the Executable

These instructions are in Joe's article I linked above, but I'll put them here as well:

$ arm-none-eabi-gcc -c main.c -mthumb-interwork -mthumb -O2 -o main.o
$ arm-none-eabi-gcc main.o -mthumb-interwork -mthumb -specs=gba.specs -o main.elf
$ arm-none-eabi-objcopy -v -O binary main.elf main.gba
$ gbafix main.gba

Run in a GBA Emulator

Outside of the container, download and run a GBA emulator. I use mGBA, but you can use whatever you like. Load the .gba file and play your game!

If going with the vanilla Docker setup, you want to mount a volume so your emulator that runs outside your container can access the .gba file. Of course, you can run the emulator within the container, but YMMV on that.

Other Helpful Links

#blog #code