famicom party

3. Getting Started

Let's get started actually programming for the NES! In this chapter, we're going to set up a development environment, installing all of the tools that you will need to work through this book, and then we will build and run the most basic game possible just to make sure everything is working.

Setting Up Your Development Environment

Here are all of the tools that we will be installing. Some of these will be used right away (and all the time), while others are more specialized and won't come into play until much later. For each category, I'm including the specific software I will be using in this book; there are many other choices, so feel free to experiment with other tools once you get comfortable with my recommendations.

  • A text editor (your choice)
  • An assembler/linker (ca65 and ld65)
  • An emulator (Nintaco)
  • A graphics tool that can read/save NES formatted images (NES Screen Tool)
  • A music composition tool (FamiTracker)

Text Editor

First, you will need a text editor. I assume that you have previous programming experience and that, as a result, you already have a favorite text editor. If not, here are a few programs that you may want to try.

  • Sublime Text. Cross-platform, popular with web developers, easy to get started with and powerful tools once you get familiar with the basics.
  • Atom. Basically GitHub's answer to Sublime Text. Cross-platform, highly configurable.
  • Visual Studio Code. Microsoft's robust text editor platform. Tailored for web development but extensible for any kind of programming. Also cross-platform, not limited to Windows.
  • Vim, emacs, nano, etc. Command-line text editors of yore. (I personally use Vim, but your mileage may vary.)

Assembler and Linker

An assembler compiles your assembly code (what we will be writing in this book) into machine code, the raw stream of bytes that the processor reads. A linker takes a group of files that have been run through the assembler and turns them into a single program file. Since each processor has its own machine code, assemblers usually target only one type of processor. There are many assemblers and linkers to choose from for the 6502, but for this book we will be using ca65 and ld65. They are open-source and cross-platform, and have some very useful features for developing larger programs. ca65 and ld65 are part of the larger "cc65" suite of programs, which include a C compiler and more.

Mac

To install ca65 and ld65 on a Mac, first install Homebrew, a Mac package manager. Copy and paste the command from the homepage into a terminal and press enter; follow the instructions and Homebrew will be ready to use. Once you have Homebrew, just type brew install cc65 and press enter.

Windows

On Windows, you'll need to download ca65 and ld65 to a specific directory on your computer. Download the latest "Windows Snapshot" from the main cc65 project page. Unzip the contents to C:\cc65. You'll also need to update your system path to make ca65 and ld65 available from any directory. The process for doing this varies depending on which version of Windows you are using. On most newer versions of Windows, you can right-click "My Computer", select "Properties", then "Advanced System Settings" and finally "Environment Variables". You'll want to find the entry for %PATH% and add C:\cc65\bin to the end of it.

Linux

You will need to build cc65 from source. Thankfully, this is a fairly simple process. First, make sure you have git and a basic build environment - on Ubuntu, for example, sudo apt-get install git build-essential should do it. Then, navigate to the directory where you want to install cc65, clone the cc65 repository, and build it:

git clone https://github.com/cc65/cc65.git
cd cc65
make

Finally, make the cc65 programs available from any directory with sudo make avail. This will add symlinks from your cc65 folder to /usr/local/bin.

Emulator


Nintaco.
An emulator is a program that runs programs intended for a different computer system. We will use an NES emulator to run the programs that we create on the same computer used to develop them, instead of requiring a hardware NES. There are a number of NES emulators available (and, once you have a solid grasp of NES development, it's fun to try to make your own!), but for this book we will be using Nintaco. It is cross-platform and one of the few emulators to feature debugging tools, which will be useful as we write programs.

Installing Nintaco is the same for all platforms - download it from the Nintaco website and unzip. To run Nintaco, double-click Nintaco.jar. Nintaco requires Java to run; if you do not have Java installed on your computer, download a "Java Runtime Environment" from java.com.

Graphics Tools

The NES stores graphics in a very different format from common image types like JPEG or PNG. We will need a program that can work with NES images. There are plugins for large graphics packages like Photoshop or GIMP, but I like using a smaller, purpose-built tool for this. For this book, we will be using Shiru's NES Screen Tool.
NES Screen Tool.

Windows

Just download NES Screen Tool from the link above, and double-click "nesst.exe" to run it.

Mac

NES Screen Tool is a Windows program, but thankfully it runs well on Mac with a Windows compatibility layer. Download my Mac wrapper, unzip it and place it in your Applications folder. Alternatively, you can use Homebrew to install WINE (brew install wine), then launch it from the command line. E.g. wine ~/Downloads/NESst/nesst.exe.

Linux

Use your distribution's package manager to install WINE (e.g. apt-get install wine), then launch NES Screen Tool from the command line as above under "Mac". On some distributions, installing WINE automatically associates it with .exe files; you may be able to just double-click nesst.exe.

Music Composition Tools

As with graphics, NES audio is a set of instructions to an audio processor rather than something like an MP3. The most popular program for creating NES audio is FamiTracker, which we will use later in this book.
FamiTracker.

Windows

Download FamiTracker 0.4.6 from the link above and unzip it. Double-click "FamiTracker.exe" to open it.

Mac

As with NES Screen Tool, I have made a Mac wrapper for FamiTracker, or you can run it using WINE directly.

Linux

Use WINE to launch FamiTracker, as with NES Screen Tool above.


Putting It All Together

Now that you have all of the tools installed, it's time to make sure that they work. We are going to create the "Hello World" of NES games: filling the entire screen with one color.

Any code listing, such as the one here, has a "Copy" link in the top-right corner if you hover over it. This will copy the entire code listing to your clipboard, ready to paste into a text editor. Open your text editor and create a new file, helloworld.asm. Copy and paste the following code into the file:

.segment "HEADER"
.byte "NES", 26, 2, 1, 0, 0

.segment "CODE"
.proc irq_handler
  RTI
.endproc

.proc nmi_handler
  RTI
.endproc

.proc reset_handler
  SEI
  CLD
  LDX #$00
  STX $2000
  STX $2001
vblankwait:
  BIT $2002
  BPL vblankwait
  JMP main
.endproc

.proc main
  LDX $2002
  LDX #$3f
  STX $2006
  LDX #$00
  STX $2006
  LDA #$29
  STA $2007
  LDA #%00011110
  STA $2001
forever:
  JMP forever
.endproc

.segment "VECTORS"
.addr nmi_handler, reset_handler, irq_handler

.segment "CHARS"
.res 8192
.segment "STARTUP"

Next, we need to use our assembler. In the directory where you saved helloworld.asm, run ca65 helloworld.asm. The result should be a new file, helloworld.o. This is an object file - machine code. But it is not in a format that is ready to plug into an emulator just yet. To do that, we need to run the linker. In the same directory, run ld65 helloworld.o -t nes -o helloworld.nes. This should result in a new file, helloworld.nes - a "ROM" file for the emulator.

The green screen here is an actual, running NES emulator in your browser! I am using the amazing jsnes by Ben Firshman. Whenever we compile a .nes file, I will include a running demo like this one. (It's hard to tell in this case, but the emulator is actually running at 60fps.) Open Nintaco and choose "Open" from the "File" menu. Select the helloworld.nes file you just created and click Open. The result should be a green screen.

Next Steps

If you were able to see the green screen in Nintaco, congratulations! Your development environment is ready to use. In the next chapter, we will discuss what the code you copied and pasted is actually doing, and learn a bit about how the NES hardware works.