PSP Programming/Text Menu

Feel free to use or edit any part of this page and/or code, but please leave credit where credit is due.

This is a simple program i wrote to show how you would use controls in your game, and how to code an extremely simple menu, it is thoroughly commented but there may be easier / faster ways of doing things than im aware of, if you see anything you could improve on, please feel free.

To compile this, you will need a few files(that i did not write) that you can download at http://www.psp-programming.com/tutorials/c/lesson04.zip I believe theese codes were written by Yeldarb, anyway nice work.

Somebody please do a public service and wiki format this, looks fine during edit if you want to copy/paste, like i said, just give me some kind of credit.

Feel free to comment/edit this code or more thoroughly describe what's happening, just leave credit where credit is due.

Note: using recursion (i.e. in the startMenu function) is a very good way to crash your system. The reason is that when a function is called the system does so by pushing a pointer to the function onto the system stack followed by any arguments to that function in reverse order (reversed from how they appear in the source code). It's normally not a problem because programs do not usually make many successive function calls. But consider what would happen in the case of the startMenu function: Every time the cursor goes off the end of the menu list a new function call is pushed onto the stack ( (4byte function pointer) + (4byte integer) = 8 bytes ). You say that's not a problem? Well let me ask you- what would happen if the user just holds down the up/down d-pad key and lets the cursor scroll through the menu list for a while? Those 8 bytes will be added to the stack every four startMenu calls. So after a while the system stack will outgrow the available memory on the PSP(32mb on the phatties, 64mb on the slims). When that happens the system will barf and hang. Then the user must reset his PSP to recover. Now, maybe you think that's unlikely to happen, and in this case that's true, but consider what would happen in similar situations if you were pushing more/bigger arguments onto the stack. What would happen if this forced the user to reboot his PSP while doing something to the firmware? It would brick the PSP.

Even if things do not get quite that out of hand code like this can still be slow because the PSP's MIPS32 CPUs use what is called a Register Window to store function calls and arguments instead of an orthodox stack like the IA32 architecture. When the window of registers fills up the CPU will dump the overflow out to memory until it needs it again. That is a (relatively) slow operation even if your program never totally fills up all available RAM.

The point is that recursion is logically elegant, but a very dirty practical solution. I would recommend, instead of recursively calling the startMenu function when the cursor runs off the end of the menu list, that the startMenu function be changed to return an invalid menu position (i.e. -1) when the cursor goes needs to loop. Then, wherever the startMenu function is called, the code should check the return value to see if it's invalid. If so, then simply call startMenu again. For example:

There are more functions in this code, but it's not the absolute number of function calls that's the problem with recursion- it's the depth of the function calls that causes problems. On a further note, main has always been required to return an integer value on POSIX systems. The only system/compiler which doesn't require it is Windows and Visual Studio.