Episode 3: Revenge of the….eh, screw it, it’s just Episode 3
It’s the moment you’ve all been waiting for….actually, I don’t even know if anyone cares, but it sounded like the right thing to say. Anyways, it’s time to build our first PSP program! If you haven’t read the past two tutorials, now would be a very good time to do so, otherwise, you won’t know what the hell is going on.
Ok, so now you know the basics of programming in C, we can apply that for a hello world on the PSP! Now, when we code anything, it’s gotta be in notepad, lame, I know, but that’s how it goes. First, to avoid future complications, we need to disable the hiding of file extensions. To do this, open up my computer, go to tools>folder options, and click on the “view” tab, the uncheck “hide extensions of known file types”

Next, right click on your desktop, then go to new>text document. Name the file “main.c”. Do this again to create another file, and name that one “Makefile” with no extension.

Now you should have 2 files on your desktop named main.c and Makefile. Let’s work on main.c first. Right click on it, then go to open/open with, and select notepad. Now you should have a blank notepad document open. It’s time to start typing our code! First we need to include our header files:
Code:
#include <pspkernel.h>
#include <pspdebug.h>
Pspkernel.h is absolutely necessary for every psp program. If you don’t include it, it won’t work, so just do it. Pspdebug.h includes information helpful for debugging, but more importantly, contains the information needed for us to print to console. With bigger and different programs, you’ll obviously have more/different header files that perform functions needed by your program. The next part:
Code:
PSP_MODULE_INFO(“Hello World”, 0, 1, 1);
This tells the psp a little about your program, like the name (hello world), major version, and minor version (1, 1). This isn’t really that important to understand, it’s just good practice to throw it in there. Moving right along…
Code:
#define printf pspDebugScreenPrintf
If you want to use standard C/C++ syntax, you’re going to have to define most things. This basically just tells the compiler that any time you use “printf”, you really mean “pspDebugScreenPrintf” (the psp’s equivalent of printf()). You could of course just use pspDebugScreenPrintf everytime, but we’re lazy, that’s why we’re programmers. Prepare to be blown away:
Code:
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
} Woah, confusing eh? Well guess what? This is another chunk of code you don’t need to understand. This is what we call cookie cutter code. It’s in every psp program. It basically just makes the program more stable, and allows you to exit via the home button and such. Ok, here’s where we get to the familiar stuff:
Code:
int main() {
pspDebugScreenInit();
SetupCallbacks();
printf(“Yay! I 4m teh 1337!!!111!!!one!”);
sceKernelSleepThread();
return 0;
} That looked pretty familiar didn’t it? Let’s break down the new stuff:
Code:
pspDebugScreenInit();
SetupCallbacks();
This sets up the debug screen (looks like a command prompt) and calls the function declared above to setup the callbacks (the stuff for the home button I talked about).
Code:
printf(“Yay! I 4m teh 1337!!!111!!!one!”);
This just prints out whatever you put in parentheses, just like on our last hello world in episode 2.
Code:
sceKernelSleepThread();
This pauses the output so that it stays on screen. If you forget this part, it’ll pop up the output and disappear so fast, you’ll never see it. So now you’re whole main.c file should look something like this:
Code:
#include <pspkernel.h>
#include <pspdebug.h>
PSP_MODULE_INFO(“Hello World”, 0, 1, 1);
#define printf pspDebugScreenPrintf
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
int main() {
pspDebugScreenInit();
SetupCallbacks();
printf(“Yay! I 4m teh 1337!!!111!!!one!”);
sceKernelSleepThread();
return 0;
} Now just save it, and that’s all there is for main.c! Now we have to write the Makefile. Now the makefile is what tells the compiler how to build the program. You see this mostly in linux, and any linux nerd will be intimately familiar with the makefile and make command, but don’t worry, we’ll talk about that in a moment. Now open up the Makefile in notepad in the same way you did with the main.c. This is a little less detailed:
Code:
TARGET = hello
OBJS = main.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Hello to teh w0rldz!
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
All you makefiles will pretty much look like this, although when you start bigger programs, you’ll need to include the libraries needed by that program (such as png, jpeg, etc.) The PSP_EBOOT_TITLE is where you set what the eboot will say when you view it in the xmb. Save changes, and now it’s time to compile your eboot! Copy the main.c and Makefile files to your projects folder you created in the first episode (either in my documents, or somewhere in C:\Cygwin). Open up cygwin and use the command cd to change to you projects directory (i.e. cd /home/Nate/projects, or whatever the location of your projects folder is). Once in that directory, type ls. You should now see those 2 files listed. Next, all you have to do is type make, and gcc will build your eboot for you! It’s as simple as that! Now all you have to do is browse to you project folder in explorer, and copy EBOOT.PBP over to your psp like you would any other homebrew, and run it! Marvel at the glorious hello worldness! Congrats on making your first psp program!
To me, this tut seems a tad shittier than my others, but if you can think of anything you’d like me to clarify, just lemme know, and I’ll edit it. I’ve also attached a zip file including the source and eboot in case you’re too lazy to build it yourself, or you’re just curious what it looks like. Not sure what I’m planning for the next episode, maybe getting input from the controls or graphics or something, but I dunno, so it’ll be a surprise