Hello all. I’m Doug (@nesdoug2, dougeff). Welcome to my tutorial – How to program an NES game in C. You can make an original Nintendo game that plays on a real NES console (or emulator).
The original NES games were written in Assembly / ASM. If you prefer that route, then you could start with the Nerdy Nights tutorial like I did.
But, I think C is easier. You could develop a game in half the time in C.
Another option, you could use NESmaker ($36), which has all the code for a game written, and all you need to do is design the graphics and the levels.
Let’s talk about the NES.
Released in Japan (Famicom), 1983, released in US, 1985.
CPU, Ricoh 2A03, 1.79 MHz, is a 6502 clone (missing decimal mode) with audio circuitry. 6502 was a very popular chip at the time. It is the same chip that Apple II and Atari 2600 used.
Screen resolution = 256×240 pixels
1 background layer, scrollable
Colors available = 56
64 sprites (8×8 or 8×16), freely moving graphic objects
5 channels of audio. 2 square, 1 triangle, 1 noise, and 1 for small samples
Here’s the memory map for the CPU.
That’s only 2048 bytes of RAM standard. Not a lot to work with.
The PPU (produces a video image) is a separate chip, that has its own memory. It is only accessible from the CPU through a slow 1 byte at a time transfer, using hardware registers.
Here’s the memory map for the PPU
Each tileset holds 256 tiles. Each tile is 8×8 pixels.
Nametable is a technical word that basically means tilemap, or background screen.
There is also another RAM chip dedicated to Sprites (called OAM). It holds 256 bytes.
It may look like there are 4 usable nametables (background screens), but actually there is only enough internal VRAM for 2 screens. The cartridges are hardwired to either be in “horizontal mirroring” or “vertical mirroring”. More advanced cartridges can switch between these options.
Vertical mirroring (or horizontal arrangement) is for sideways scrolling games, and the lower 2 nametables are just a mirror of the upper 2 nametables, like this…
Horizontal mirroring (or vertical arrangement) is for vertically scrolling games, and the right 2 nametables are just a mirror or the left 2 nametables, like this…
A few games had extra RAM on the cartridge to actually have 4 different nametables (Gauntlet is one example). For more detailed information, check out the nesdev wiki.
Lastly, the game cartridges usually have 2 ROM chips inside. One PRG-ROM (executable code), and one CHR-ROM (graphic tiles). But some games, instead of a CHR-ROM chip, have a CHR-RAM chip. The graphics are located somewhere in the PRG-ROM, and the program has to transfer the bytes from there to the CHR-RAM chip.
My tutorials will exclusively deal with CHR-ROM style games. It’s easier. With this style, the graphic tiles are automatically connected to the PPU and you don’t have to transfer them to the PPU. With the simplest cartridge (NROM-256) you get 2 sets of 256 tiles (each tile = 8×8 pixels). Usually, one for background tiles and one for sprite tiles.
You might want to read up on hexadecimal numbers. 8 bit numbers are much easier to read and understand in hex. I usually use $ to indicate hex, but sometimes I use 0x. $ is used in assembly languages, and 0x is used in C like languages. You don’t need to be a math expert, but it will help if you know what I’m talking about.