So this is the next part in the “Coding for Playstation 2” article series. I’m really sorry for the long delay on this one but I will try to keep the phase up now (even if I have a small game called Battlefield 3 to finish as well :)

Before going on I will recommend that you are familiar with using gnu toolchains and the tools that comes along with it (gcc, make, etc) as these will be needed when using the compiler.

Anyway, lets get started:

So you have your shiny Playstation 2 that you want to program for but there are two issues.

1. How do you run your own code on it?
2. How do you compile the code to run on it?

Lets start with 1.

How to run you own code on Playstation 2

This section is a bit tricky. I will list you some of the options you have.

Get a mod-chip

This is by far the easiest solution but will cost you some money (and will likely require some soldering
skills if you want to install it yourself) There are (at least in Sweden) plenty of places that allows you to hand in your ps2 and they will install the chip for you. If you decide to go for mod-chip make sure you get one that can boot from memory card (most new ones can) The upside with this is: Faster boot time (no need for DVD spin up/etc). One problem is that mod-chip  may be illegal in your country so that you might need to go for one of the options listed below.

Playstation 2 Independence Exploit

Notice that this exploit will not work with slim PS2 and after v10 of the machine here

Ps2Link

There are several other tricks to get code running on your Playstation 2 but it’s beyond the scope of this article series to go into all of them because some of them may or may not be legal depending on where you are living (reverse engineering, DMCA, ..)

Your end goal (whatever method you pick) is to get Ps2Link running on your machine. Ps2link is a tool that allows you to execute elf’s over network (so we can compile offline and send it over to the machine using a TCP/IP connection) This is typically known as cross-compilation (more on how to compile your code in section 2)

You can download a binary version of Ps2Link here : http://www.mediafire.com/?qyij2d3yfoz

How to compile code for Playstation 2

The toolchain we will be using is gcc 3.2.2 (there has been some work on porting the ps2 patches to 4.x but it’s still in experimental state so using 3.2.2 which is fairly stable will do fine for this article series)

How do I obtain the compiler? I have made it easy for you. You can just grab the toolchain here https://github.com/emoon/ps2dev_tools for both Windows and Mac OS X. Notice that this toolchain has only been tested with C code (so C++ might not work, but I won’t be using any C++ in this tutorial series, if you really want C++ support you will need to build the toolchain yourself with C++ enabled but be prepared for some pain trying to build it) You can grab the build toolchain script here If you really want to build it yourself (you will likely run into issues but if you want to use Linux/Other nix* this is the only way as distributing binaries for Linux is a pain in the a** so I haven’t even tried)

Ok… I got it. Now what?

First step is to grab the open PS2SDK which can be obtained from here Next step is to compile the sdk.

Compiling the SDK

You could get a prebuild version of the SDK but I would argue that it’s better to build it yourself as will be a good indicator that you have setup everything correct.

Windows

On Windows you will need either Cygwin or MinGW as a small “unix” environment. I will quickly go over here on how to setup MinGW as it’s usually faster and more lean than Cygwin. If you really want to use Cygwin (or have it installed already) there are several tutorials on the net on how to set it up.

You will need to install

MinGW (I grabbed 20110802 but most versions should be fine) Pick all the defaults and then pick “MSYS Basic System” at the “Select Components” screen. The installer will now grab a few packages of the net and install them so just sitt  back and let it do it’s thing. Once it’s done we move to the next one.

MSYS (I grabbed the 1.0.11 version) I picked all defaults and when asked to continue with post-install select y. Select y that you have installed MinGW and then write the path to the installation (in my case c:/MinGW) and then your are done with that part.

Now start MSYS (clicking icon on desktop or just just write MSYS <enter> in start menu fast launch window (Vista and Win 7) You should get a window that looks something like this

Lets run some commands to make sure it’s working correct

If you see something like the output above you are ready to go.

Mac OS X

As Mac OS X already got a proper *nix env we can skip the instructions above for Windows as we already have whats needed.

Getting the toolchain

Next step is to get the toolchain (that includes the the C compiler, ps2link (used to on host side to send ps2 executables (elf files) over to target (ps2)) I have made it easy (and thanks to Tobias Pflug for fixing the broken OS X toolchain) for you as all you need to do is to goto ps2dev_tools and either do a regular git clone or just press the “Downloads” button and pick tar.gz or zip

Now create a directory called /usr/local/ps2dev (or some other path that you think i’s better) and extra the version of choice in there. You should have something that looks like this afterwards

/usr/local/ps2dev/bin
/usr/local/ps2dev/dvp

Environment setup

Next step is to set up the correct environment variables.

export PS2DEV=/usr/local/ps2dev
export PATH=$PATH:$PS2DEV/bin
export PATH=$PATH:$PS2DEV/ee/bin
export PATH=$PATH:$PS2DEV/iop/bin
export PATH=$PATH:$PS2DEV/dvp/bin
export PS2SDK=$PS2DEV/ps2sdk
export PATH=$PATH:$PS2SDK/bin

Alright. Now try to write:

ee-gcc
iop-gcc

You should get output that looks like this:

ee-gcc(.exe): no intput files

iop-gcc(.exe): no input files

If that is the case everything is fine and we can move on to the next section of building the SDK

Getting the SDK and building it

The best way to get the SDK is to grab it from here 

Now when you have the PS2, SDK and toolchain setup it’s time to run some code. As this article is running long the best thing is to try out some example from the SDK.

For example try samples/draw/cube

Enter the directory and run “make” then you should have a nice little cube.elf ready for action.

To show it on the actual PS2 you do:

ps2client -h <ip to the ps2> execee host:cube.elf

and you should see a nice fancy cube on your screen.

That’s it for part two. In part 3 I will start going over more of the details of the hardware, the DMA and so on.

Meanwhile some useful links:

forums.ps2dev.org