[Archive] Programming for the NES


Editor’s Note: This is a repost of an old post on August 3, 2015 that you can find here, I was 15 so please excuse my immaturity in my post. Also, I’ve unfortunately lost all my source code from this project, though I don’t imagine it was any good anyway.


The Nintendo Entertainment System, otherwise simply known as the NES, or in Japan and some other asian countries, Nintendo Famicom, is this thing :

Image courtesy of wiki media commons.

Image courtesy of wiki media commons.

Why program for an 80’s gaming console, you ask? For the fun of it.

Don’t think that programming for an older console necessarily mean it is easier (though this IS sometimes the case, such as when comparing 3d and 2d games). Older devices sometimes used difficult-to-understand ‘tricks’ that were used as to save memory and CPU cycles. Nowadays, we have loads of RAM, memory, very fast computers that make all these easier for us. Indeed, some young coders probably take for granted all these technological accomplishments.

Fun Fact : Remember that I said earlier that it was known as Nintendo Famicom in some asian countries. Sadly, in more than a few asian countries, including my own, the device was not even known. Instead, there were knock-off consoles that imitated the device, known as ‘Famiclones’, with the most popular being a Micro Genius console. Though I suppose programming for these ‘Famiclone’ devices would be a fun exercise, there is little to no documentation on those devices on the internet, though it would probably be very similiar in terms of hardware to the Famicom itself. I do recall hearing in a forum of someone who tried to program on a Famiclone however, though I do not remember whom or where.

Now, having decided to program for the NES, I’ve decided to set some goals, I decided to make a simplistic Pong game.

Pong is one of the easiest game to program, generally speaking. Most developers start off making Pong whenever they’re tackling a new platform, just to get familiar with the platform. Now, I’m not looking for a polished product, what I’m looking for is a quick and super simplistic Pong. It doesn’t need to have a title screen, it doesn’t have to have artificial intelligence, it doesn’t have to have excellent code, I just want it to work.

Fun Fact : Some smart guy (understatement) somehow managed to make an artificial intelligence to play NES games, through, to quote, ‘lexicographic orderings and time travel’. All this is way over my head and I am still uncertain if it is a joke when he says it uses ‘time travel’

Now, searching around the web, I find some nice resources to work with.

  1. NesDev Wiki – this is an indispensable source as it covers everything NES-related ; except for maybe beginner-friendly tutorials.
  2. Nerdy Night’s tutorial – while, indeed being one of the better tutorials for the NES, it does have several errors in the beginning, that really confused me when I was just starting out a few days ago.
  3. Patater’s tutorial – I recall reading this and Patater’s GBA tutorial a while back for fun. While I find it great, as it also adds a human element, the feeling that a human learning along with you while he tells you what he knows, it is said to not be worth following as it is largely outdated (it was made in 2002, and while that was a loong time after the NES was released, it was still not fully understood).

And for the emulators, I only really looked at two popular ones, though I know there are many more.

  • Nestopia – Plays commercial games, which as far as I know, is what it’s purpose is.
  • FCEUX – Designed to be as accurate and similiar to the NES as possible (though it failed that one) and developer-friendly. Has a debugger, PPU (Picture-Processing-Unit) Viewer, along other things. This is the emulator I used and is the only one of the two that plays my game.

I also used NESST to make sprites and palettes, though this isn’t much since I only made a solid block, a ball, and some numbers.

Now, having read a bit of Nerdy Night’s tutorial, I attempted to make my Pong game. The first thing I did was to load a square onto the screen, and move that with my A and B buttons. I quickly discovered on doing so that the default size for a sprite, 8×8 pixel, was too small. As such, I made 3 separate sprites and moved them together.

This was most definitely not a pretty solution as it messes up my code, but it works. I’ve learned it’s better to have messy code in the beginning and than rewrite that in cleaner code later on (much later on, when you don’t feel bored of it). Later on, I got a blue ball onto the screen. At this point, I was worried. If I had to bounce the ball around, that would mean I would need to check for collision every frame. Was the NES technically advanced enough it could do that? Did NES games use a ‘trick’ such as dividing the screen into parts and checking each separately to save time?

Turns out that was not the case. I managed to have a ball up and bouncing.

pong1

Next, I decided to add another paddle. I also added a simple AI for it to move where the ball is headed. I noted that the ball will hit the AI’s paddle every time (though it will pass right through it, as I haven’t implemented Collision Detection yet). As a result, I decided not to make three separate sprites to give it the appearance of being longer.

Now was the most difficult and frustrating part : Collision Detection. Adding the collision detection for the ball when colliding with the walls was easy enough, since the position of the walls remained constant, but the paddle? Perhaps a more experience programmer would have managed it in less time than I did, and probably add some neat tricks to make it faster or something. But still, I managed to get collision detection after some time. This was also the time I changed the player’s paddle to have a ball in the middle, so I could debug some collision-related stuff. I kept it because it looked cool. After handling the collision, everything else seemed to fall into place.

I threw drew up some quick palette drawings of the numbers 0 through 5. This was going under the assumption the game point was 5 of course.

I did some polishing. Added the scores, made the ball’s position reset every time it hit a goal. And voilà, a simple pong game.

Note how stupid the game looks

Note how stupid the game looks

Of course, this was not meant to be a polished product at all. It was meant to teach me a bit of how the Nintendo Entertainment System Worked. And It was made in less than 2 hours, so there’s that. If I were to continue this onwards, this is what I would do (in order)

  1. Destroy assembly code I wrote
  2. Draw stuff
  3. Code Stuff
  4. Optimize Code
  5. Back to step 2 until done.
  6. Add states (Win/Lost/GameMenu)

I wouldn’t exactly call this project a success, seeing as I don’t remember exactly how to code for the system, though I guess I can blame that on needing to rely on memorizing register addresses and exactly what to put in them in what order instead of having descriptive names. Basically blaming it on having to write it in assembly language and not being able to use preprocessor definitives for some reason.

But still, it was fun. Frustrating, demotivating and time consuming. But yeah, fun. I was originally looking to making another game with the NES, but perhaps I should hold that off for a while, at least until I can look at 6502 (the NES’s CPU) assembly code without feeling queasy.

Naavin Ravinthran
Naavin Ravinthran
Computer Science Graduate

My interests include cybersecurity, osdev, and graphics programming.