Programming the NES : An Introduction

Most NES titles were written in 6502 assembly language. This allowed developers to fine tune each and every byte of their programs. You could code directly to the memory which was critical when fitting games into the small but (at the time) expensive ROM in the game cartridge. Assembly also allowed for clever optimisations which squeeze the most performance possible out of each frame of animation.

For those of you following this blog, the 6502 assembler will be no stranger to you. I hope that this becomes a gentle progressive series which shows what is under the hood of one of the most successful game consoles of all time.

Unlike modern consoles, there was no official development kit, third party developers hacked together their own. They often used a custom cartridge which was attached via a ribbon cable to a PC. Masahiro Sakurai revealed that when working for the game development company HAL Laboratory they would pair a NES console with the Disk System peripheral. Using a trackball to edit graphics and even to enter code.

Over time, cartridges became more sophisticated. Game studios expanded ROM sizes to 512 KB and included dedicated mapper chips to expand the console’s functionality. Some games even included additional audio chips.

When coding for the NES nowadays, developers usually write games in either assembler or the cc65 compiler toolchain. The cc65 toolchain uses the C programming language. This gives greater functionality per line of code. Admittedly it does have a lower performance and greater code size than writing in assembly, you can still write an extremely good game in C. Through this series I will attempt to look at both methods of coding to try and give a good overview of how to create some homebrew ‘masterpieces’.

sETTING UP THE dEVELOPMENT ENVIRONMENT

The first thing we need to install is the cc65 compiler. This is a cross platform compiler that includes a macro assembler, a C compiler, linker and several other tools. It supports systems such as the VIC 20, the C64, the Atari and of course the Nes.

  • Go to the cc65 webpage and click the ‘Getting started’ tab at the top.
  • Scroll down the page until you see the hyperlink to download the current cc65 snapshot. If you can’t find it click here. At the present time of writing this link should take you to the correct download.
  • Extract this file to a location like c:/cc65.

Now we can do a quick test of cc65 just to ensure that it is functional.

  1. Open your command window and change your directory to the location where you extracted cc65.
  2. Once inside this directory type cd bin to move to the bin folder inside cc65
  3. Type cc65 –help
  4. This should show the help files within the cc65 folder.

Creating Environmental Variables

We don’t want to have to direct our command line to this particular location. We want to run it from any location. To do that we set up environment variables.

  • Open a Folder and right click This PC. Then select properties.
  • Click on Advanced System Settings.
  • Click on Environment Variables.
  • Click the Edit button to add a Path Variable – please ensure that you have clicked Path so that you don’t modify any other user variable.
  • Click New and add the bin location of your cc65 folder.

Now when you open your command line prompt again, you can simply type cc65 –help and the help file will appear. This means that we can run the assembler from anywhere.

Testing this out – Building a NES file and running it on an emulator.

First grab a good NES emulator of your choice. I use NESTicle as I have been using it for over 15 years (and I am old fashioned that way) but there are a ton of great NES emulators out there and some that have good debugging facilities.

Next we download a test file – I have made one available via my Drop box. The file is called Blinktest.nes and can be obtained here. This is a file that was created by Michael Chiaramonte to ensure that programmers have a correct set up. Its a very simple file that changes colours on the NES background.

When you have downloaded this file to a location of your choice. We can build it into a form which the NES emulator can recognise.

  1. Open your command prompt window again.
  2. Type in the following

ca65 blinktest.asm -o blinktest.o -t nes
ld65 blinktest.o -o blinktest.nes -t nes

This makes a call to the assembler to create a file which is recognised by the NES. This file should appear in your previously specified folder.


Now you can open your NES emulator and open the file with the .NES extension. You should see the background alternate between Grey and blue.

Leave a Reply

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