+-------------------------------------+
| Let's build a gba compiler with fpc |
+-------------------------------------+
|          Author: Francesco Lombardi |
|    Release date: 2005.08.09         |
+-------------------------------------+


Tools needed
------------
-FPC compiler for your platform, ver 2.0.0 (http://www.freepascal.org/)
-FPC sources, ver 2.0.0 (http://www.freepascal.org/)
-MSYS (http://www.mingw.org/msys.shtml)
-An emulator with integrated debugger or, at least, a memory viewer, like 
Boycott Advance (http://www.ngemu.com/gba/bca.php main site does not work)
or Visual Boy Advance Development version (http://vba.ngemu.com/)


Foreword
--------
I'l use MSYS, because I'm confortable with POSIX-like ambient. It is a fork of 
cygwin, but more friendly for win32 users. However, feel free to use dos prompt 
if you like it. Remember that MSYS includes GNU Make utility, that is needed in 
order to build freepascal.
In the source files provided with this package, you can find occasionally some 
comments, that explain the changes made.


Preparing all things
--------------------
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). If you 
decided for MSYS, install it.


Compiler modification
---------------------
Copy the files you found in "Compiler" directory of this 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 msys (or a dos prompt), go to %FreePascal%\source\compiler and run 
"make PPC_TARGET=arm". 
Go to %FreePascal%\bin\i386-win32, make a new directory 'arm-gba' and copy here 
the new generated file ppcarm.exe. Extract and copy here the files in
win32_arm_binutils.zip (ls.exe, ld.exe, objcopy.exe and cygwin1.dll). Now add 
'%FreePascal%\bin\i386-win32\arm-gba' in the search path.


FPCMake modification
--------------------
Copy the files you found in "FPCMake" directory of this package in the directory 
        
        %FreePascal%\source\utils\fpcm

In msys (or a dos prompt), go to %FreePascal%\source\utils\fpcm and run 
"make". Go to %FreePascal%\bin\i386-win32 and copy here the new generated file
fpcmake.exe. This utility is useful when you try to build the rtl, because it 
generates all makefiles starting from a smuch more simple makefile.fpc.


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

Go to %FreePascal%\source\rtl, open makefile.fpc and add a new target:
        
        ...
        ...
        [target]
        ...
        dirs_gba=gba

In msys (or a dos prompt), go to %FreePascal%\source\rtl and run 
"fpcmake -Tall -r -w": this command rebuild all makefiles. Now do a "make 
distclean", then run 'make CPU_TARGET=arm OS_TARGET=gba PP=ppcarm OPT="-Tgba"'
At the end of the compiling, you can find a new directory:

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

Copy the directory 'arm-gba' and all its content in
        
        %FreePascal%\units


Ending
------
Now you can try to compile some pascal code, like the examples in the package: 

        ppcarm -Tgba -n -Fuc:\fpc\units\arm-gba gba.pp

Look at compile.bat. It produces a gba.gba file, but if you try to run on a gba 
emu, it does not work  (you must see if 0x04000000 address contains 0x0403).

At this point you can try a trick: remove from gba.s the following lines:

line        Asm Code
----        --------
...
[22]        bl  FPC_INITIALIZEUNITS
...
[40]        bl  FPC_DO_EXIT
...
[57]        .globl  THREADVARLIST_P$GBA
[58]        THREADVARLIST_P$GBA:
[59]        .long   0
[60]          .Le1:
[61]          .size   THREADVARLIST_P$GBA, .Le1 - THREADVARLIST_P$GBA
[62]          .balign 4
[63]          .globl FPC_THREADVARTABLES
[64]          FPC_THREADVARTABLES:
[65]          .long   2
[66]          .long   THREADVARLIST_SYSTEM
[67]          .long   THREADVARLIST_P$GBA
[68]          .Le2:
[69]          .size   FPC_THREADVARTABLES, .Le2 - FPC_THREADVARTABLES
[70]          .balign 4
[71]          .globl FPC_RESOURCESTRINGTABLES
[72]          FPC_RESOURCESTRINGTABLES:
[73]          .long   0
[74]          .Le3:
[75]          .size   FPC_RESOURCESTRINGTABLES, .Le3 - FPC_RESOURCESTRINGTABLES
[76]          .balign 4
[77]          .globl INITFINAL
[78]          INITFINAL:
[79]          .long   1,0
[80]          .long   INIT$_SYSTEM
[81]          .long   FINALIZE$_SYSTEM
[82]          .Le4:
[83]          .size   INITFINAL, .Le4 - INITFINAL
[84]          .balign 4

This 'garbage' (sorry fpk ^_^) is initialization code added from fpc compiler, 
but interferes with our initialization code.
Now run compile2.bat; the gba.gba file runs fine in the emu (flags correctly 
set, rom header good).


Next steps?
-----------
Well, we need some further rtl hacking to handle fpc initialization code, that's 
beyond my knowledge. You can try to download a pdf with fpc internals 
(comparch.pdf) for more infos, but I think that an help from fpk & friends could 
be better ^_^
About prt0.s: the file provided works fine for our initial purposes, but someday 
we must use a startup file more advanced for handle all gba capabilities. 