Difference between revisions of "GameBoy Advance"

From Lazarus wiki
Jump to navigationJump to search
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= Let's build a compiler for gba with fpc =
+
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.
  
== Tools needed ==
+
= GameBoy Advance port =
-FPC binaries for your platform, ver 2.0.0 ([http://www.freepascal.org/])
 
  
-FPC sources, ver 2.0.0 ([http://www.freepascal.org/])
 
  
-Gnu make (should be enough the one packed with fpc binaries, eg. in bin/i386-win32 directory)
+
== Status ==
 +
*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.
  
-An emulator with integrated debugger or, at least, a memory viewer, like Boycott Advance ([http://www.ngemu.com/gba/bca.php] main site seems down) or Visual Boy Advance Development version ([http://vba.ngemu.com/])
 
  
-fpc4gba package ([http://itaprogaming.free.fr/download/fpc4gba_v2.zip])
+
== 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:
 +
<pre>__attribute__ ((section (".mySection"))</pre>
 +
A workaround is declaring the variable in an external asm file:
 +
<pre>
 +
  @ external file ExtVars.s
 +
  .section .mySection
 +
  .global myVar
 +
...
 +
</pre>
 +
then declare it in pascal source code as external:
 +
<pre>
 +
...
 +
{$L ExtVars.o}  // You need to assemble ExtVars.s and link it
 +
var
 +
  myVar: integer; cvar; external;
 +
...
 +
</pre>
 +
* FPC does not allow <tt>volatile</tt> 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:
 +
<pre>
 +
// GCC preprocessor define...
 +
#define SCREEN_BASE(m) ((m) << 8)
  
 +
// ...in fpc becomes:
 +
function ScreenBase(m: integer): integer; inline;
 +
begin
 +
  ScreenBase := m shl 8;
 +
end;
 +
</pre>
  
== Foreword ==
+
== Building compiler ==
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)
+
''In this document I will refer to a windows system. It is possible to download a [http://itaprogaming.free.fr/download/build_fpc4gba.bat batch file] that could help in the building and installation process.''
  
== Preparing all stuff ==
+
In order to build fpc for gba you need a working copy of fpc binaries. Be sure that fpc path (eg. <tt>c:\fpc\bin\i386-win32</tt>) is at the first place in your search path (because compatibility problems with eg. Delphi <tt>make.exe</tt>)
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 ==
+
'''Step 1''' - Getting Cross Binutils
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
 
  
 +
You need cross binutils for arm-gba. You can get it (for win32) from http://itaprogaming.free.fr/download/arm-gba-binutils.zip. Extract them to some dir in the path on your machine. We will suppose that you extracted the cross binutils to: <tt>%FreePascal%\bin\arm-gba</tt> 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: http://www.freepascal.org/develop.html#svn
 +
 +
We will suppose that your Free Pascal 2.1.x source code is located here: <tt>%FreePascal%\sources</tt>
 +
 +
 +
'''Step 3''' - The Build process
 +
 +
In order to build the cross compiler it is necessary to have a correct <tt>PATH</tt> environment variable pointing to cross binutils. In Windows You should add <tt>%FreePascal%\bin\arm-gba</tt> to the PATH.
 +
Now open a dos prompt, go to <tt>%FreePascal%\source\compiler</tt> and run
 +
 +
<pre>
 
   make PPC_TARGET=arm
 
   make PPC_TARGET=arm
 +
</pre>
  
Go to %FreePascal%\bin, make a new subdirectory 'arm-gba' and copy there the new generated file ppcarm.exe. Extract and copy here 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.
+
At the end of building process, you should have a <tt>ppcarm.exe</tt> file in <tt>%FreePascal%\source\compiler</tt> directory.  
  
Now add '%FreePascal%\bin\i386-win32\arm-gba' in the search path.
+
Now copy these files to <tt>%FreePascal%\bin\arm-gba</tt>.  
  
 +
In the dos prompt go to <tt>%FreePascal%\source\rtl\gba</tt> and do
  
== FPCMake modification ==
+
<pre>
This utility is useful when you try to build the rtl, because it  generates all makefiles starting from a much simpler makefile.fpc.
+
  make CPU_TARGET=arm OS_TARGET=gba PP=ppcarm OPT="-Tgba"
 +
</pre>
  
Copy the files you found in "FPCMake" directory of fpc4gba package in the directory  
+
At the end of the compiling process, you can find a new directory called <tt>%FreePascal%\source\rtl\units\arm-gba</tt>
  %FreePascal%\source\utils\fpcm
+
Now copy the directory 'arm-gba' and all files inside in <tt>%FreePascal%\units</tt>
Go to %FreePascal%\source\utils\fpcm and run
 
  make
 
Go to %FreePascal%\bin\i386-win32 and copy there the new generated file fpcmake.exe.
 
  
  
 +
'''Step 4''' - Configuration file
 +
 +
Now you need to create <tt>fpc.cfg</tt> configuration file in <tt>%FreePascal%\bin\arm-gba</tt> folder in order to use <tt>ppcarm.exe</tt> easily.
 +
 +
Create an empty <tt>fpc.cfg</tt> file in <tt>%FreePascal%\bin\arm-gba</tt> folder and add the following lines to it:
 +
<pre>
 +
-Tgba
 +
-FuD:\freepascal\units\arm-gba
 +
-XParm-gba-
 +
-FDD:\freepascal\bin\arm-gba
 +
</pre>
 +
 +
Of course you should replace <tt>D:\freepascal</tt> with your fpc installation directory.
  
== 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!'''
  
== Done! ==
+
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 <tt>lnkscript</tt> and <tt>prt0.s</tt> (that you have found in <tt>%FreePascal%\source\rtl\gba</tt>) in your project directory. In order to make things a bit simpler, copy following text in a batch file:
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
 
   @echo off
   REM You should change "placeholder" according with your file name and
+
   REM "placeholder" should be changed according with your file name
  REM the path, according with your fpc installation directory
 
 
    
 
    
   ppcarm -Tgba -XS -Cn -FuC:\fpc\units\arm-gba -alnrt placeholder.pp
+
   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-as -o placeholder.o placeholder.s
   arm-gba-ld -Ttext 0x08000000 -Tbss 0x03000000 -L. link.res -o placeholder.elf
+
   arm-gba-ld -g --gc-sections -T lnkscript -L. link.res -o placeholder.elf
 
   arm-gba-objcopy -v -O binary placeholder.elf placeholder.gba
 
   arm-gba-objcopy -v -O binary placeholder.elf placeholder.gba
 
    
 
    
Line 89: Line 118:
  
  
== Next steps? ==
+
'''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.  
+
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.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 ([http://www.devrs.com/gba/files/crtls.zip]), 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.
+
About prt0.s, lnkscript and binutils: these files come from [http://www.devkitpro.org devkitPro].
 +
 
 +
== Binaries ==
 +
A zip package of arm-gba binaries is available [ftp://ftp.freepascal.org/pub/fpc/snapshot/v21/arm-gba/fpc4gba.zip here] or mirrored on my [http://itaprogaming.free.fr/download/fpc4gba.zip web site]
 +
 
 +
 
 +
== Documentation ==
 +
* [http://itaprogaming.free.fr/tutorial.html FPC 4 GBA Game Programming Tutorial]: a work-in-progress tutorial I'm writing about gba programming with fpc
 +
* [http://www.jharbour.com/gameboy/default.aspx Programming The Nintendo Game Boy Advance]: a free book for beginners about GBA game programming.  
 +
* [http://user.chem.tue.nl/jakvijn/tonc/ TONC]: if you want to program a game for gba, you MUST read this tutorial.
 +
* [http://fpc4gba.pascalgamedevelopment.com FPC4GBA initiative site]: Here you can find more infos on FPC 4 GBA.
 +
* [http://www.devrs.com/gba/files/gbadevfaqs.php GBA FAQ]: A lot of obscure GBA internal infos.
 +
 
 +
== Links ==
 +
* [http://itaprogaming.free.fr My home page] with some tools and demos.
 +
* [http://www.gbadev.org GBADev]: a great GBA developers community.
 +
* [http://www.devrs.com/gba GBA Devr's]: Jeff Frohwein's GBA related stuff.
 +
* [http://www.pascalgamedevelopment.com Pascal Game Development]: the biggest pascal game development community.
 +
* [http://www.devkitpro.org devkitPro]: the home page of the GBA development toolkit.
  
You can find more infos on FPC 4 GBA Initiative site ([http://fpc4gba.pascalgamedevelopment.com]) and on my personal page ([http://itaprogaming.free.fr])
+
[[Category: Video Game Consoles]]
 +
[[Category: Operating Systems and Platforms]]

Latest revision as of 22:20, 6 April 2013

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

Status

  • 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
var 
  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)

// ...in fpc becomes:
function ScreenBase(m: integer): integer; inline;
begin
  ScreenBase := m shl 8;
end;

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 http://itaprogaming.free.fr/download/arm-gba-binutils.zip. 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: http://www.freepascal.org/develop.html#svn

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:

-Tgba
-FuD:\freepascal\units\arm-gba
-XParm-gba-
-FDD:\freepascal\bin\arm-gba

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


Done!

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
 
 pause


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.

Binaries

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


Documentation

Links