CONTENTS

Home
Updates
Software
Electronics
Android / iOS
Videos
Music
Resume
Contact



HTTPS VERSION


Michael Kohn

Playstation 2 Programming

Posted: March 16, 2006
Updated: March 23, 2017

Introduction

11 year ago I got a Playstation 2 with a modchip in it so I could play around with Playstation 2 development writing code in C using the PS2DEV. Recently I added to naken_asm the full Playstation 2 Emotion Engine R5900 and vector unit instruction sets. I made a quick demo of drawing a triangle and placed it in the naken_asm git repository: /samples/playstation2/. I also added some Python tools to help with debugging and a playstation2.inc include file to make coding simpler.

I left all the old PS2DEV programs at the bottom of this page and I am repurposing this page with information on how the sample assembly program works so hopefully help anyone else looking to do Playstation 2 programming.

Most of the information I used for learning to write this simple program came from http://lukasz.dk/playstation-2-programming/an-introduction-to-ps2dev/. Lukasz has links to serval old Playstation 2 demos including Duke's 3stars.asm demo. I lifted a lot of code from the 3stars.asm demo, reorganizing it a bit, to get the Playstation 2 video initialization working. There are several PDFs that are also important: EE_Core_Instruction_Set_Manual.pdf, EE_Core_Users_Manual.pdf, EE_Overview_Manual.pdf, EE_Users_Manual.pdf, and GS_Users_Manual.pdf. These PDFs document the R5900 instruction set, the vector units instruction set, DMA, and Graphic Synthesizer (GS) modules.

The sample program I wrote simply initializes the graphics display and then in a while(1) loop it will draw the entire screen to a really dark red, draw a triangle, and wait for vsync (vsync meaning the TV screen's video beam has drawn the entire screen and now it shuts off for a short time while the video beam goes back up to the top). A sample of what it draws is below:

The program itself I think is pretty decently commented so there probably isn't much use going into too much detail on what every little piece of code there does. So just as an high level explanation, the first part is going to initialize the GS turning on read circuit 2. I'm actually still a little confused on what that means exactly. It seems there are two display buffers (context 1 and 2) that are separately set up and when drawing primitives (triangles, lines, sprites, etc) the user selects which context to draw the primitives onto. What confused me is all the demos I see sets up read circuit 2 but draws on context 1. I tried drawing to context 2 in my demo but the screen stayed blank. I would have thought the read circuit was connected to the context.

To draw primitives, a GIF packet is sent from the main CPU to the GS over a DMA channel (always channel 2). GIF packets are a set of 128bit (16 byte) chunks of memory where the first chunk tells the GS what kind of packet is being sent and the following 16 bytes are the commands. So in the case of the triangle, the GIF packet says there will be 7 chunks of data of type A+D (address and data). So all the chunks that follow have an address in the GS along with the data that gets written into that register. the RGBAQ is color information and XYZ2 is a point on a triangle. As soon as 3 points are sent to the GS, the GS will draw it.

The GIF packet data is a pain to read so I wrote a python script included in the repository called show_gif.py. It will read in a naken_asm .lst file and given an address it will show the GIF packet in text format. From the repository an example of this can be shown by typing: make show_gif

PS2DEV Homebrew (March 2006)

I've recently started learning how to do programming on Sony's Playstation 2 console. This has been some of the most fun programming I've done in a while. This is my first time even dealing with 3D graphics on this level and such. For anyone who wants to try out any of the sample programs I've been working on, a Playstation 2 with a modchip is needed (which will allows the burning of a CDROM with the following programs and boot them on your console), a USBExtreme type kit, a Naplink kit, or an exploit. These demos also run on the PCSX2 emulator.

The Hardware Development Kit

I started by buying premodded Playstation 2 with a DMS4 modchip from Modchip World (http://www.modchipworld.com/). I've also found the USBExtreme kit can also be used to boot homebrew programs off a USB CompactFlash card. The PGEN Genesis Emulator boots and runs just fine like this, although it won't find any ROMS from the CF card. My homebrew programs however do not work using this method. I'll find out why later :). Also, I've found that the USBExtreme is a bit picky which USB flash memory it will recognize. The USBExtreme kit can also be purchased from Modchip World.

The SDK and Documentation

I decided to start with simple C until I understand the hardware better and then move to assembly language. I downloaded a script from Dan Peori's http://www.oopo.net/consoledev/ site that downloaded all the components and patches I needed and installed the development kit. This guy has tons of great examples and documentation on programming the Playstation 2. Another great site for Playstation 2 programming is http://www.ps2dev.org/.

Test2 Program

This is my first simple test program which draws 3 moving triangles on the screen and my name spinning in 3D in the center of the screen. This is for the most part a modified version of Dan Peori's introduction to 3D on the PS2.

http://www.youtube.com/watch?v=DhdJ39Y01nU. Kinda a crappy video :(. I should have moved the camera closer... my name and weird handdrawn smily face in there are hardly visible. The Toxic logo came from the modchip and wasn't a part of my programming.

Download Source and ELF: test2-2006-03-16.tar.gz

ps2mandel mandelbrot generator

I think I'm too obsessed with mandelbrots :). This program will draw a mandelbrot on the screen using code from my old Mandel Server program. I wrote this because a few years ago I tried using my mandelserver program on Playstation2 Linux, but for some reason it was extremely slow. I think the floating point may have been done in software or something. This time it's quite fast actually, altho not quite fast enough to be real time. After booting this program, the the joystick pad and left joystick can be used to rotate the image. The right joystick will move inside the mandelbrot and the X button will zoom in.

http://www.youtube.com/watch?v=QbjIsUzE9cA. The Toxic logo came from the modchip.

Download Source and ELF: ps2mandel-2006-03-16.tar.gz

Copyright 1997-2017 - Michael Kohn