GameBoy Advance

From Lazarus wiki
Revision as of 21:20, 6 April 2013 by Jwdietrich (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

GBA port is still in an initial stage, though it should be pretty usable. Initial compiler port was by Jason McMillen. New compiler and RTL port was started and mantained by Francesco Lombardi.

GameBoy Advance port


  • The 2.1.x compiler has compiler support for Gameboy Advance.
  • ARM CPU is supported.
  • ASM THUMB mode is not supported.
  • Base RTL units *should* be complete.

Port notes

FPC does not have some features you will find in gcc-aimed source code for gba.

  • At this time, you can't use THUMB asm instruction set
  • It is not possible to relocate variables in pascal source directly. In gcc you can write:
__attribute__ ((section (".mySection"))

A workaround is declaring the variable in an external asm file:

  @ external file ExtVars.s
   .section .mySection
   .global myVar

then declare it in pascal source code as external:

{$L ExtVars.o}  // You need to assemble ExtVars.s and link it
  myVar: integer; cvar; external;
  • FPC does not allow volatile declarations. Just ignore it, because the fpc compiler does not handle such an optimization.
  • The GCC preprocessor is a bit more complex than the fpc one. All #define that require a parameter can be safely replaced by an inline procedure, declaring the parameter as an integer:
// GCC preprocessor define...
#define SCREEN_BASE(m) ((m) << 8)

// fpc becomes:
function ScreenBase(m: integer): integer; inline;
  ScreenBase := m shl 8;

Building compiler

In this document I will refer to a windows system. It is possible to download a batch file that could help in the building and installation process.

In order to build fpc for gba you need a working copy of fpc binaries. Be sure that fpc path (eg. c:\fpc\bin\i386-win32) is at the first place in your search path (because compatibility problems with eg. Delphi make.exe)

Step 1 - Getting Cross Binutils

You need cross binutils for arm-gba. You can get it (for win32) from Extract them to some dir in the path on your machine. We will suppose that you extracted the cross binutils to: %FreePascal%\bin\arm-gba where %FreePascal% is the path of your fpc binary installation.

Step 2 - Making Cross compiler

Now you need to build fpc cross compiler for ARM processor. To do that you need the latest fpc 2.1.x sources and a working installation of fpc 2.0.x.

Get the latest 2.1.x source from Subversion repository:

We will suppose that your Free Pascal 2.1.x source code is located here: %FreePascal%\sources

Step 3 - The Build process

In order to build the cross compiler it is necessary to have a correct PATH environment variable pointing to cross binutils. In Windows You should add %FreePascal%\bin\arm-gba to the PATH. Now open a dos prompt, go to %FreePascal%\source\compiler and run

  make PPC_TARGET=arm

At the end of building process, you should have a ppcarm.exe file in %FreePascal%\source\compiler directory.

Now copy these files to %FreePascal%\bin\arm-gba.

In the dos prompt go to %FreePascal%\source\rtl\gba and do

  make CPU_TARGET=arm OS_TARGET=gba PP=ppcarm OPT="-Tgba"

At the end of the compiling process, you can find a new directory called %FreePascal%\source\rtl\units\arm-gba Now copy the directory 'arm-gba' and all files inside in %FreePascal%\units

Step 4 - Configuration file

Now you need to create fpc.cfg configuration file in %FreePascal%\bin\arm-gba folder in order to use ppcarm.exe easily.

Create an empty fpc.cfg file in %FreePascal%\bin\arm-gba folder and add the following lines to it:


Of course you should replace D:\freepascal with your fpc installation directory.


Our job is done! Now you should have a freepascal compiler that can build apps for gba target. Though it is not just about calling "ppcarm yourfile.pp"... First of all, you need to copy lnkscript and prt0.s (that you have found in %FreePascal%\source\rtl\gba) in your project directory. In order to make things a bit simpler, copy following text in a batch file:

 @echo off
 REM "placeholder" should be changed according with your file name
 ppcarm -gw -s -XX -Xs -Si -Ooregvar placeholder.pp
 arm-gba-as -o prt0.o prt0.s
 arm-gba-as -o placeholder.o placeholder.s
 arm-gba-ld -g --gc-sections -T lnkscript -L. link.res -o placeholder.elf
 arm-gba-objcopy -v -O binary placeholder.elf placeholder.gba

Next steps?

In the SVN repository there is gbaunits, a package that incapsulates a library I have translated from c, but it needs some changes and some testing too. About prt0.s, lnkscript and binutils: these files come from devkitPro.


A zip package of arm-gba binaries is available here or mirrored on my web site