Overview
This is a step by step tutorial based on the example of my FPC port to the PlayStation 1.
To see the port in action look at the PlayStation 1 compilation manual.
This Guide assumes we are working under Linux.
Forking and Merging
The first step is to create an GitLab account and Fork the compiler sources from
https://gitlab.com/freepascal.org/fpc/source
when done, create a new branch. I called it PS1. Now when Your are done with the work, just do a merge request. The FPC Team will review Your changes and merge them into the main branch.
Preparing the Assembler and Linker
As we don't going to write our own internal Assembler and Linker we use GNU BinUtils. In our case we need the MIPSEL one.
Download a recent version from https://ftp.gnu.org/gnu/binutils/ . Then run shell commands:
./tar -xf <binutils.tar.gz>
./cd <binutils>
./mkdir build
./cd build
../configure --prefix=/usr/local/mipsel-unknown-elf --target=mipsel-unknown-elf --with-float=soft
make -j$(nproc)
sudo make install
export PATH="/usr/local/mipsel-unknown-elf/bin:$PATH"
Now you can go two ways:
1) Let the binaries as they are and always use the compiler option
-XP/usr/local/mipsel-unknown-elf/bin/mipsel-unknown-elf-
this means we going to build the compiler with the option CROSSOPT="-XP/usr/local/mipsel-unknown-elf/bin/mipsel-unknown-elf-"
2) Create symlinks
sudo ln -s /usr/local/mipsel-unknown-elf/bin/mipsel-unknown-elf-as /usr/bin/mipsel-ps1-as
sudo ln -s /usr/local/mipsel-unknown-elf/bin/mipsel-unknown-elf-ld /usr/bin/mipsel-ps1-ld
sudo ln -s /usr/local/mipsel-unknown-elf/bin/mipsel-unknown-elf-objcopy /usr/bin/mipsel-ps1-objcopy
this means we don't need any extra crosscompile options
Setup the make files
1) add the new build target to fpcmake:
- You need to extend the ./utils/fpcm/fpcmmain.pp:
insert the new Target in
type TOS=(..., ps1); (about line 100)
section.
don't forget to insert it also to OSStr(about line 135) and OSSufix(about line 144) constants.
add at the end of the table OSCpuPossible a new os/cpu entry (about line 207)
- now expand the ./packages/fpmkunit.pp
TOS=(.., ps1) (about line 166)
and in the OSCPUSupported table like with the fpcmmain above
2) rebuild your native compiler:
make all
sudo make install
now fpcmake can generate make files.
3) add to ./compiler/Makefile.fpc:
NO_NATIVE_COMPILER_OS_LIST (about line 44)
your target system, ps1 this indicates, there is no native compiler for your target
4) time to rebuild all make files:
you have to run the fpcmake in all folders of the fpc sources after that to build the rtl make files you have to execute ./regenmakefiles.sh in ./rtl to build the packages you have to execute ./regenmakefiles.sh in ./packages
to simplify all those steps execute my script:
#!/bin/sh
set -e
FPCMAKE=/usr/local/bin/fpcmake
find . -name Makefile.fpc | xargs $FPCMAKE -Tall
cd ./rtl
./regenmakefiles.sh
cd ./..
cd ./packages
./regenmakefiles.sh
cd ./..
cd ./utils/build/
$FPCMAKE -Tall -s -o Makefile.pkg Makefile.fpc
cd ./../..
cd ./packages/build/
$FPCMAKE -Tall -s -o Makefile.pkg Makefile.fpc
cd ./../..
Inform the compiler about the new Target
1) ppu signature
./compiler/utils/pputils/ppudump.pp (about line 252)
2) info screen of the compiler
./compiler/msg/errore.msg under option_help_pages (about line 4343)
3) to get the set of systems what support the new target add in
./compiler/systems.pas (about line 430, in our case after systems_jvm) the ps1 target
./compiler/systems.inc: (about line 218) in type tsystem
4) register new linker in ./compiler/systems.inc (about line 339) in tlink
ld_ps1
5) set to your target CPU (in this case MIPS) the new platform:
./compiler/mips/cputarg.pas (about line 58):
{$ifndef NOTARGETPS1}
,t_ps1
{$endif}
Create ./compiler/systems/i_ps1.pas
This unit implements support information structures for ps1.
unit i_ps1;
{$i fpcdefs.inc}
interface
uses systems;
const
system_mipsel_ps1_info : tsysteminfo =
(
system : system_mipsel_ps1;
name : 'PlayStation 1 for MIPSEL';
shortname : 'ps1';
flags : [tf_no_pic_supported, tf_smartlink_sections, tf_files_case_sensitive, tf_requires_proper_alignment, tf_emit_stklen];
cpu : cpu_mipsel;
unit_env : '';
extradefines : '';
exeext : '';
defext : '.def';
scriptext : '.sh';
smartext : '.sl';
unitext : '.ppu';
unitlibext : '.ppl';
asmext : '.s';
objext : '.o';
resext : '.res';
resobjext : '.or';
sharedlibext : '.so';
staticlibext : '.a';
staticlibprefix : 'libp';
sharedlibprefix : 'lib';
sharedClibext : '.so';
staticClibext : '.a';
staticClibprefix : 'lib';
sharedClibprefix : 'lib';
importlibprefix : 'libimp';
importlibext : '.a';
Cprefix : '';
newline : #10;
dirsep : '/';
assem : as_gas;
assemextern : as_gas;
link : ld_none;
linkextern : ld_ps1;
ar : ar_gnu_ar;
res : res_elf;
dbg : dbg_none;
script : script_unix;
endian : endian_little;
alignment :
(
procalign : 8;
loopalign : 8;
jumpalign : 8;
jumpalignskipmax : 8;
coalescealign : 0;
coalescealignskipmax: 8;
constalignmin : 0;
constalignmax : 8;
varalignmin : 0;
varalignmax : 8;
localalignmin : 0;
localalignmax : 8;
recordalignmin : 0;
recordalignmax : 8;
maxCrecordalign : 8
);
first_parm_offset : 0;
stacksize : 262144;
stackalign : 8;
abi : abi_default;
llvmdatalayout : 'e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32-S64';
);
implementation
initialization
{$IFDEF CPUMIPSEL}
{$IFDEF PS1}
set_source_info(system_mipsel_ps1_info);
{$ENDIF}
{$ENDIF}
end.