Thread: PSP C programming tutorials: Episode 3

Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1 PSP C programming tutorials: Episode 3 
    Senior Member PSP Elite Hacker
    Join Date
    Jul 2006
    Posts
    1,794
    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
    Attached Files
    Reply With Quote  
     

  2. #2  
    Senior Member I Modded My PSP dark_thug's Avatar
    Join Date
    Oct 2006
    Posts
    102
    nice tut man but can u pls atlst tell the other users what tool is needed like

    pspsdk
    gcc compiler
    cygwin and other stuff





    u gonna be logged out
    Reply With Quote  
     

  3. #3  
    Retired Moderator PSP Elite Hacker Julie's Avatar
    Join Date
    Oct 2005
    Posts
    12,160
    he listed the software in episode 1.

    hopefully when this is all complete, we can make a master thread instead of having 3 individual threads for the homebrew coding project
    R.I.P Zoidberg
    Reply With Quote  
     

  4. #4  
    Senior Member I Modded My PSP lead2gold's Avatar
    Join Date
    Aug 2006
    Posts
    359
    once you get into cgywin, you need to get the toolchain script.
    It checks to see if you have the proper libraries installed, if those requirements have been met then it downloads the entire ps2/psp sdk library.
    It literally takes about 4-5 hrs to complete (no joke).
    As short part of that time is the downloading, and the lengthy part is the compiling of the libraries.

    After that you can rock through this tutorial without a problem.
    Reply With Quote  
     

  5. #5  
    Senior Member PSP Elite Hacker
    Join Date
    Jul 2006
    Posts
    1,794
    yeah, that was all included in episode 1. and you can start merging them now if you like, it'll be one long ass thread, lol.
    Reply With Quote  
     

  6. #6  
    Senior Member I Modded My PSP lead2gold's Avatar
    Join Date
    Aug 2006
    Posts
    359
    ah sorry about that!
    Reply With Quote  
     

  7. #7  
    Senior Member PSP Elite Hacker
    Join Date
    Jul 2006
    Posts
    1,794
    lol, it's cool, oh yeah, and lead, I remember you saying somethinng about not being able to find a functions list or something to that effect, I noticed in the pspsdk src there's a shitpot of .c and .h files for a plethora of different functions, and they're even organized as to what they do (gui, irda, wifi, etc.), you might wanna poke around in there and see what you can find, I'm sure the src is commented pretty well, that's what I plan on doing as soon as I get time, lol.

    EDIT: the pspsdk comes with doxygen documentation in /usr/local/pspdev/psp/sdk/doc/html. Pretty sweet actually, lists just about every function in every header file. and there's also a samples directory with a crapload of examples on how to use them.
    Reply With Quote  
     

  8. #8  
    Senior Member I Modded My PSP lead2gold's Avatar
    Join Date
    Aug 2006
    Posts
    359
    thanks for that edit... i didn't actually know you added that until now.
    here is another great url, but after i check what you posted, it will probably get me the same thing: http://psp.jim.sh/pspsdk-doc/

    For some reason the toolchain i loaded didn't install a doc dir... i'm using the one dated: 20060120
    Reply With Quote  
     

  9. #9  
    Senior Member I Modded My PSP dark_thug's Avatar
    Join Date
    Oct 2006
    Posts
    102
    ned help i got a prob with Cygwin in my desktop i got the error that the GNU make is not installed need help sorry kinda a nooby i knw only codes and thats it i dnt knw how to config this crap pls help or wer can i get the GNU make?





    u gonna be logged out
    Reply With Quote  
     

  10. #10  
    Senior Member I Modded My PSP lead2gold's Avatar
    Join Date
    Aug 2006
    Posts
    359
    you may need to run the cygwin installer again, your missing some of the dev tools. They don't come installed by default.

    I will run through my config and see if i can take note of what you need to install... (i'll get back to you on this in a bit)
    Reply With Quote  
     

  11. #11  
    Senior Member PSP Elite Hacker
    Join Date
    Jul 2006
    Posts
    1,794
    you familiar with linux? just get gnu make from here: http://ftp.gnu.org/pub/gnu/make/make-3.81.tar.gz . use winrar to extract it to C:\cygwin (or wherever you installed it), then open up cygwin and type the follwing commands, with enter after each line:

    cd /make-3.8.1
    ./configure
    make && make install

    and that should install it, you might need to get automake too, on second thought, it would prolly be easier for you to just reinstall cygwin following my first tutorial, making sure you set "web" and "devel" to install.
    Reply With Quote  
     

  12. #12  
    Senior Member I Modded My PSP lead2gold's Avatar
    Join Date
    Aug 2006
    Posts
    359
    Once you complete all of the tutorials,
    download this source code (available to the public):
    http://dl.qj.net/A-Farmer-s-Life-Sou...1163/catid/203

    Some guy's creation... i haven't looked at it myself yet (so it may be spaghetti code). But there is no better way of teaching yourself further after you get the basics then viewing someone who is kind enough to share their advance code.

    Make sure if you intend to use (some/all of) his code or libraries you give this individual proper respect and credit!

    Happy programming!
    Reply With Quote  
     

  13. #13  
    Senior Member I Modded My PSP dark_thug's Avatar
    Join Date
    Oct 2006
    Posts
    102
    anyway ill stick with the real linux ^^ fedora core 5 is better than an emulation





    u gonna be logged out
    Reply With Quote  
     

  14. #14  
    Junior Member
    Join Date
    Nov 2006
    Posts
    1
    ive followed all 3 of your tutorials, and they have worked fine up to now.

    When i tried making the eboot, i open up the cygwin and go to the projects folder and type make, this is what i get

    "make: psp-config: Command not found
    Makefile:12: /lib/build.mak: No such file or directory
    make: *** No rule to make target '/lib/build.mak'. Stop."

    i looked for build.mak in the lib folder, and it actually isnt there..where exactly can i get it from?
    Reply With Quote  
     

  15. #15  
    Senior Member PSP Elite Hacker
    Join Date
    Jul 2006
    Posts
    1,794
    hey man, sorry I missed your message, so hopefully you see this. looks like your problem is that you didn't add the paths to your cygwin.bat (you're using cygwin, correct?) if you're using cygwin, right click on cygwin.bat, go to edit and replace:

    Code:
    @echo off
    
    C:
    chdir C:\cygwin\bin
    
    bash --login -i
    with this:

    Code:
    @echo off
    
    C:
    chdir C:\cygwin\bin
    
    set path=%path%;C:/cygwin/usr/local/pspdev/bin
    set PSPSDK=C:/cygwin/usr/local/pspdev
    
    bash --login -i
    if you're not using cygwin, just add this to your paths:

    Code:
    set path=%path%;/usr/local/pspdev/bin
    set PSPSDK=/usr/local/pspdev
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Similar Threads

  1. Star wars episode 1 doesnt work ?
    By KKinsane in forum PSP Emulation
    Replies: 0
    Last Post: 11-25-2008, 01:04 AM
  2. PSP C programming tutorials: Episode 1
    By n8thegr8 in forum PSP Homebrew
    Replies: 20
    Last Post: 04-13-2007, 02:15 PM
  3. PSP C programming tutorials: Episode 2
    By n8thegr8 in forum PSP Homebrew
    Replies: 10
    Last Post: 10-18-2006, 06:58 PM
  4. Episode 19 of PSP Hacking 101
    By markcapo in forum PSP Homebrew
    Replies: 5
    Last Post: 09-01-2006, 12:23 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •