NES Programming



This book is an open work designed for people interested in learning to program for the NES (Nintendo Entertainment System).

Hardware specifications

 * CPU - Ricoh 2A03 CPU which is a MOS 6502 without a decimal mode.
 * Clocked 1.789773Mhz for NTSC (System 21.47727Mhz / 12) and 1.773447Mhz for PAL (System 26.601171Mhz / 15)
 * Memory - 2 KB
 * Video
 * Ricoh 2C02 PPU (Picture Processing Unit)
 * Resolution - 256 x 240 pixels (Only 256 x 224 pixels are visible on NTSC)
 * Colors - Fixed 52 (44 colors, 5 grays and 3 duplicated)
 * Palette
 * Background: 4 Palettes x 3 Colors + 1 Back Color
 * Sprites: 4 Palettes x 3 Colors
 * Tiles - 8 x 8 pixels tiled background
 * Sprites - Up to 64 8x8-pixel or 8x16 pixel sprites but only 8 can be displayed per scan-line
 * Sound
 * 2 Pulse wave channels
 * 1 Triangle wave channel
 * 1 Noise channel
 * 1 DPCM channel

Tutorials

 * Introduction: A few words on assembly
 * /Initializing the NES/

Appendices

 * The NES memory map

Hardware I/O ports
The NES has I/O ports located at addresses $2000-$2007 (hexadecimal) and $4000-$4017.

PPU

 * $2000 - write only - PPU Control Register 1
 * $2001 - write only - PPU Control Register 2
 * $2002 - read only - PPU Status Register

Video control

 * Sprites
 * $2003 - write only - Sprite Memory Address
 * $2004 - read / write - Sprite Memory Data
 * $2005 - write only - Background Scroll

PPU (continued)

 * $2006 - write only - PPU Memory Address - indexing into PPU memory location
 * $2007 - read / write - PPU Memory Data - data to read from a PPU memory location or write to a PPU memory location

Sound

 * Square wave 1
 * $4000 - Register 1 - Bits:
 * 0-3: volume / envelope decay rate
 * 4: envelope decay disable
 * 5: length counter clock disable / envelope decay looping enable
 * 6-7: duty cycle type (unused on noise channel)
 * $4001 - Register 2 - Bits:
 * 0-2: right shift amount
 * 3: decrease / increase (1/0) wavelength
 * 4-6: sweep update rate
 * 7: sweep enable
 * $4002 - Register 3 - Bits:
 * 0-7     Eight LSB of wavelength
 * $4003 - Register 4 - Bits:
 * 0-2: 3 MSB of wavelength (unused on noise channel)
 * 3-7: length counter load register
 * Square wave 2
 * $4004 - Register 1 - Same as Square wave 1
 * $4005 - Register 2 - Same as Square wave 1
 * $4006 - Register 3 - Same as Square wave 1
 * $4007 - Register 4 - Same as Square wave 1
 * Triangle
 * $4008 - Register 1 - Bits:
 * 0-6 	linear counter load register
 * 7	       length counter clock disable / linear counter start
 * $4009 - Unused Register
 * $400A - Register 3 - Same as Square wave 1
 * $400B - Register 4 - Same as Square wave 1
 * Noise
 * $400C - Register 1 - Same as Square wave 1
 * $400D - Unused Register
 * $400E - Register 3 - Bits:
 * 0-3	 playback sample rate
 * 4-6	 unused
 * 7	random number type generation
 * $400F - Register 4 - Same as Square wave 1

DMC

 * $4010 - Register 1 - Bits:
 * 0-3: sample rate
 * 4-7: unused?
 * $4011 - Register 2 - Bits:
 * 0-7: volume
 * $4012 - Register 3 - Bits:
 * 0-7: voice table start address(N*0x40+0xc000)
 * $4013 - Register 4 - Bits:
 * 0-7: voice table byte size(N*16+1)

Sprites (continued)

 * $4014 - write only - DMA Access to the Sprite Memory

Sound (continued)

 * $4015 - read / write - enable/disable individual sound channels

Joystick

 * $4016 - read / write - Joystick 1
 * $4017 - read / write - Joystick 2