# 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

## NES 2A03 CPU memory map

| Address Range (Hexadecimal) | Size | Notes (Page size is 256 bytes) | Notes (Page size is 256 bytes) |
| $0000–$00FF | 256 bytes | Zero Page — Special Zero Page addressing modes give faster memory read/write access | Zero Page — Special Zero Page addressing modes give faster memory read/write access |
| $0100–$01FF | 256 bytes | Stack memory | Stack memory |
| $0200–$07FF | 1536 bytes | RAM | RAM |
| $0800–$0FFF | 2048 bytes | Mirror of $0000–$07FF | $0800–$08FF Zero Page $0900–$09FF Stack $0A00–$0FFF RAM |
| $1000–$17FF | 2048 bytes | Mirror of $0000–$07FF | $1000–$10FF Zero Page $1100–$11FF Stack $1200–$17FF RAM |
| $1800–$1FFF | 2048 bytes | Mirror of $0000–$07FF | $1800–$18FF Zero Page $1900–$19FF Stack $1A00–$1FFF RAM |
| $2000–$2007 | 8 bytes | Input/Output registers | Input/Output registers |
| $2008–$3FFF | 8184 bytes | Mirror of $2000–$2007 (multiple times) | Mirror of $2000–$2007 (multiple times) |
| $4000–$401F | 32 bytes | Input/Output registers | Input/Output registers |
| $4020–$5FFF | 8160 bytes | Expansion ROM — Used with Nintendo's MMC5 to expand the capabilities of VRAM. | Expansion ROM — Used with Nintendo's MMC5 to expand the capabilities of VRAM. |
| $6000–$7FFF | 8192 bytes | SRAM — Save Ram used to save data between game plays. | SRAM — Save Ram used to save data between game plays. |
| $8000–$FFFF | 32768 bytes | PRG-ROM | PRG-ROM |
| $FFFA–$FFFB | 2 bytes | Address of Non Maskable Interrupt (NMI) handler routine | Address of Non Maskable Interrupt (NMI) handler routine |
| $FFFC–$FFFD | 2 bytes | Address of Power on reset handler routine | Address of Power on reset handler routine |
| $FFFE–$FFFF | 2 bytes | Address of Break (BRK instruction) handler routine | Address of Break (BRK instruction) handler routine |

## 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

## External links

* Super NES Programming
* More info can be found on IRC, on the EFnet server, in channel #nesdev
* NesDev, contains detailed technical documents, tools, and examples.
* P65 Assembler, an assembler written in Perl for the 6502 family of processors with support for programming the NES.