GameBoy Advance

From Lazarus wiki
Jump to navigationJump to search

Let's build a compiler for gba with fpc

Tools needed

-FPC binaries for your platform, ver 2.0.0 ([1])

-FPC sources, ver 2.0.0 ([2])

-Gnu make (should be enough the one packed with fpc binaries, eg. in bin/i386-win32 directory)

-An emulator with integrated debugger or, at least, a memory viewer, like Boycott Advance ([3] main site seems down) or Visual Boy Advance Development version ([4])

-fpc4gba package ([5])


Foreword

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)

Preparing all stuff

Install fpc compiler as usual in a directory of your choice (eg. c:\fpc); extract fpc sources in fpc binaries directory (eg. c:\fpc\source).


Compiler modification

Copy the files you have found in "Compiler" directory of fpc4gba package, following this scheme:

 - t_gba.pas    in %FreePascal%\source\compiler\systems
 - i_gba.pas    in %FreePascal%\source\compiler\systems
 - cputarg.pas  in %FreePascal%\source\compiler\arm
 - compiler.pas in %FreePascal%\source\compiler
 - systems.pas  in %FreePascal%\source\compiler

Now open a dos prompt, go to %FreePascal%\source\compiler and run

 make PPC_TARGET=arm

Go to %FreePascal%\bin, make a new subdirectory 'arm-gba' and copy there the new generated file ppcarm.exe. Extract and copy there the files in "binutils" directory of this package (as.exe, ld.exe, objcopy.exe and cygwin1.dll). In order to compile a working rtl, we need to make a copy of these 3 exe files and rename it arm-gba-as.exe, arm-gba-ld.exe and arm-gba-objcopy.exe.

Now add '%FreePascal%\bin\i386-win32\arm-gba' in the search path.

FPCMake modification

This utility is useful when you try to build the rtl, because it generates all makefiles starting from a much simpler makefile.fpc.

Copy the files you found in "FPCMake" directory of fpc4gba package in the directory

 %FreePascal%\source\utils\fpcm

Go to %FreePascal%\source\utils\fpcm and run

 make

Go to %FreePascal%\bin\i386-win32 and copy there the new generated file fpcmake.exe.


RTL Modification

Go in %FreePascal%\source\rtl, make a copy of 'linux' directory and rename it 'gba'. Go in this new 'gba' directory and delete all subdirectories, except 'arm'. Delete the files system.pp, syslinux.pp, makefile and makefile.fpc. Copy the files you have found in "RTL" directory of fpc4gba package, following this scheme:

 - system.pp    in %FreePascal%\source\rtl\gba
 - sysgba.pp    in %FreePascal%\source\rtl\gba
 - system.inc   in %FreePascal%\source\rtl\gba
 - unix.pp      in %FreePascal%\source\rtl\gba
 - makefile.fpc in %FreePascal%\source\rtl\gba
 - prt0.as      in %FreePascal%\source\rtl\gba\arm

Go to %FreePascal%\source\rtl, open makefile.fpc and add a new target:

 ...
 ...
 [target]
 ...
 dirs_gba=gba

In a dos prompt, go to %FreePascal%\source\rtl and run

 fpcmake -Tall -r -w

This command rebuild all makefile. Now do a

 make distclean

Then run

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

At the end of the compiling process, you can find a new directory:

 %FreePascal%\source\rtl\units\arm-gba

Copy the directory 'arm-gba' and all files inside in

 %FreePascal%\units


Done!

Our job is done! Now you have a freepascal compiler that can build apps for gba target. It is not just about calling "ppcarm yourfile.pp"... In order to make things a bit simpler, copy following text in a batch file:

 @echo off
 REM You should change "placeholder" according with your file name and
 REM the path, according with your fpc installation directory
 
 ppcarm -Tgba -XS -Cn -FuC:\fpc\units\arm-gba -alnrt placeholder.pp
 arm-gba-as -o placeholder.o placeholder.s
 arm-gba-ld -Ttext 0x08000000 -Tbss 0x03000000 -L. link.res -o placeholder.elf
 arm-gba-objcopy -v -O binary placeholder.elf placeholder.gba
 
 pause


Next steps?

Now we need some libraries for gba. I have translated a lib from c, but it needs some changes and some testing too. In all c libraries I have found, there are always variables declared as "volatile". FPC does not allow "volatile" variables, so I'm not sure that my translation in pascal is 100% accurate... =( Next step should be to have thumb code and, AFAIK, Fpk & Co. are on it. About prt0.as: the file provided works fine for our initial purposes, but someday we should use a startup file more advanced for handling all gba capabilities. I have tryed to use Jeff Frohwein's crt0.s and lnkscript ([6]), but I had some troubles. For small projects it seems to work fine, but if I try to link a project a bit bigger, the built rom header seems broken.

You can find more infos on FPC 4 GBA Initiative site ([7]) and on my personal page ([8])