MIPS, Snake, PrimLib

Note: This post is rather out of date now. There’s a page on this over here. Feel free to read on, but note that one or two of the links to elsewhere will not work.


Today it’s all about MIPS. This post’ll get a shout out over on the Console Development page I just created, but I felt I should keep all the MIPS stuff concentrated in a post since it didn’t really warrant its own page and I didn’t want to pollute the Console Dev page.

MIPS is a RISC (reduced instruction set computer) architecture used in CPUs such as the 32-bit MIPS Allegrex R4000 found in Sony’s PlayStation Portable. Throughout the first semester of Console Development we’ve been programming simple applications using MIPS assembly language. Personally, I find programming in assembly language very relaxing when compared to regular programming, and I hope to soon find the time to look into 6502 assembly as a step towards a long-standing dream.

Following is the description of my main two achievements to date using MIPS: Snake, and PrimLib.

A few days after being introduced to the MARS MIPS simulator I dropped on the Bitmap Display and Keyboard and Display Simulator tools included with it. Being primarily a games programmer, the first thought that occurred to me was ‘I can use this’. Move ahead a few hours and I had the basis of a working snake game written in assembly. Another few days and a recode later the game was near complete, but there was one error that reared it’s head, whereby the entire MARS simulator would randomly crash while running snake.

As it turns out, the fault wasn’t in my code, but in a threading issue with the Keyboard and Display Simulator. An hour or so of poking around in the java source provided with MARS enabled me and my friend to make a quick fix to stop the lock-up from occurring, and I sent a quick email off to KenVollmar@missouristate.edu about the issue.

My MIPS Snake game is now complete and I’ll provide the assembly for it should you wish to try it out,. I warn you though: it won’t run for long unless you care to fix the same problem I did. I will not distribute the modified version of MARS I’m using – that’s not my place.

How Snake Works
The main issue I ran into when programming snake was how to store data about all the entities. But at some point I discovered that the two alpha bytes in the colours used for the display were simply being wasted, so I opted to store only the colour data for the display, the address of the head and tail of the snake, and store all other information in the alpha bytes using different numbers for different meanings. As the snake moves around a new head segment is placed at the front, and the old head position is replaced with a block of the same colour, but with a code in the alpha bytes indicating the direction of the new head. When that segment is at the address in the tail register the direction stored is recovered and used to swap the address in the tail register to the next segment along. The old segment is then replaced with the background colour so that the snake’s tail follows its path. Information on food and walls is also stored in the alpha bytes, and this system could potentially be used to identify an even larger number of entities.

DOWNLOAD (*.asm)

PrimLib was written for the first Console Development assignment. It’s a collection of functions which draw primitives to the Bitmap Display included in MARS. The assignment was marked for efficiency and the algorithms implemented. Out of personal preference I chose to prioritize reliability at the sacrifice of some performance by ensuring that all of my functions performed boundary checks and clipping where necessary. Going out of bounds in the Bitmap Display tool doesn’t usually crash MARS, but it does wrap around and produce undesirable results.

I won’t go into much detail about PrimLib, and I will not distribute the source so soon after handing in an assignment (lest I be accused of plagiarizing myself o_O). Most of the algorithms are fairly easy to implement and/or are based on existing algorithms such as the Bresenham’s Line Drawing and Cohen-Sutherland Line Clipping algorithms. The screenshot seen above is running the test data I implemented to stress test all of the algorithms implemented. What remains then is to list the functionality of PrimLib:

EDIT 27/02/2012: Uploaded the assembly code for PrimLib here:
Download (*.asm)
EDIT 14/06/2012: Oh, I gave this it’s own page a while ago. Nevermind.

setpixel(int x, int y, int colour)
drawline(int x1, int y1, int x2, int y2, int colour)
drawrectangle(int x, int y, int width, int height, int colour)
drawfilledrectangle(int x, int y, int width, int height, int colour)
drawpolygon(point[] points, int count, int colour)
drawcircle(int x, int y, int radius, int colour)
drawfilledcircle(int x, int y, int radius, int colour)
clipline(int x1, int y1, int x2, int y2)

That’s all for now. I’ll go sort out that Console Development page, drink some milk, and get some sleep before tomorrow’s early start.


2 thoughts on “MIPS, Snake, PrimLib

  1. Pingback: MIPS code, 저는 너무 바빠요! | Confect's Codex

  2. Pingback: See-Through, PSPudding | Confect's Codex

Your thoughts:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s