A cartridge only makes sense next to the console it plugs into. This page describes the chip in the console that runs the program: the Ricoh 2A03 (2A07 on PAL consoles), a 6502 core with the sound unit, the two DMA units and the controller ports built in. Everything here is written from the NESdev Wiki pages named in each section; the plain text of those pages is kept in the project’s samples folder (see Sources).
The address map
The CPU has a 16-bit address space. (NESdev Wiki, “CPU memory map”.)
| Addresses | Size | What is there |
|---|---|---|
$0000–$07FF | $0800 | 2 KiB of internal RAM |
$0800–$1FFF | $1800 | Three mirrors of $0000–$07FF ($0800, $1000, $1800) |
$2000–$2007 | $0008 | The PPU registers |
$2008–$3FFF | $1FF8 | Mirrors of $2000–$2007, repeating every 8 bytes |
$4000–$4017 | $0018 | The APU and I/O registers (OAM DMA at $4014, controllers at $4016–$4017) |
$4018–$401F | $0008 | APU and I/O functions that are normally disabled (CPU test mode) |
$4020–$FFFF | $BFE0 | Unmapped: the cartridge decides. Usually RAM at $6000–$7FFF ($2000 bytes) and ROM with mapper registers at $8000–$FFFF ($8000 bytes) |
$0000–$00FFis the zero page, which is cheaper to reach.$0100–$01FFholds the stack; it usually starts at$01FFand grows downwards. The rest is for the game.$0200–$02FFis most often the OAM buffer, copied to the PPU every frame.- A cartridge can watch every read and write on the bus. The one exception is a read of
$4015, which stays inside the CPU. A cartridge can put writable registers anywhere. Readable memory must not clash with other readable hardware. The wiki advises putting readable cartridge memory only in$4020–$FFFF, because a quirk of the 2A03 can make DMA misbehave if the CPU is halted while reading$4000–$401F. - Reading an address that nothing answers returns open bus (the last value on the bus). The cartridge can pull bits high or low across the whole address space.
Vectors
The CPU reads three 16-bit little-endian vectors from the top of the cartridge space. The cartridge supplies them. Unless the mapper fixes the top of memory to one bank, the vectors (and a reset stub) must be in every bank. (NESdev Wiki, “CPU memory map”.)
| Addresses | Vector | Used for |
|---|---|---|
$FFFA–$FFFB | NMI | The non-maskable interrupt: the start of vertical blank, when PPUCTRL bit 7 is set |
$FFFC–$FFFD | Reset | Where the program starts |
$FFFE–$FFFF | IRQ and BRK | Mapper interrupts, the APU frame and DMC interrupts, and the BRK instruction |
DPCM sample data must lie at $C000–$FFFF. Playback wraps from $FFFF to $8000. (NESdev Wiki, “CPU memory map”.)
Controller ports
Two registers serve both ports. (NESdev Wiki, “Standard controller”.)
| Address | Access | Bits | Meaning |
|---|---|---|---|
$4016 | write | xxxx xxxS | S is the strobe (latch) for the controller shift registers. While S is 1 the shift registers keep reloading from the buttons, and reads return the state of button A. Write 1 then 0 to capture the buttons. Bits 2–0 are also kept in latches inside the CPU (the expansion port uses them). |
$4016 | read | xxxx xMES | Controller 1. S is the primary controller bit. E is the expansion port bit (Famicom). M is the microphone of the second Famicom controller ($4016 only). |
$4017 | read | xxxx xxES | Controller 2, same layout (no microphone bit). The same address on write is the APU frame counter, see APU registers. |
Each read returns one button. The eight reads come out in this order: A, B, Select, Start, Up, Down, Left, Right. A pressed button reads as 1. After eight reads an official controller returns 1; some third-party controllers return other values. The upper bits are not driven, so they keep the value last on the bus, usually the high byte of the address ($40). A few games, such as Paperboy, depend on reading exactly $40 or $41. A port with no controller reads 0.
Famicom details: the hard-wired second controller has no Select or Start button (they read 0) and has a microphone. The microphone gives an instant one-bit report in bit 2 of $4016. Controllers on the expansion port report through bit 1 instead of bit 0, so games often OR bits 0 and 1. The later AV Famicom has two normal detachable controllers and no microphone.
The controller is a 4021 shift register. OUT ($4016 bit 0) loads the buttons in parallel. Each read pulses CLK to shift the next bit out on D0. Ground feeds the serial input, which is why the report ends in 1s once inverted. Some PAL consoles have diodes on the port that need pull-up resistors in the controller (model NES-004E). A DPCM sample fetch can delete one bit of a controller read; see the DMC DMA section below.
DMA
The 2A03 has two DMA units: one copies sprite data to the PPU, the other feeds the DMC sound channel. (NESdev Wiki, “DMA”.)
Cadence
The CPU alternates between get cycles (DMA may read) and put cycles (DMA may write). They are the first and second halves of the APU clock. Whether the first cycle after power-on is a get or a put is random. If DMA wants to read on a put cycle it waits one cycle (an alignment cycle). DMA can only halt the CPU on a read cycle. If the CPU is writing, the halt fails and DMA tries again on the next cycle, so a halt can be delayed by up to three cycles (a read-modify-write instruction has two consecutive writes, an interrupt has three). While the CPU is halted it repeats its last read. On a 2A03 those repeats can be seen on the bus, so a register with side effects can lose data. When the DMA ends, the CPU repeats the read it was making.
OAM DMA ($4014)
Write a page number to $4014 and the CPU copies the 256 bytes of that page, starting at the page boundary, to the PPU’s sprite memory through $2004. It halts on the cycle after the write, performs an optional alignment cycle, then 256 get and put pairs. It takes 513 or 514 CPU cycles. A read-modify-write such as INC $4014 copies from the second page written. Write 0 to OAMADDR first. OAM DMA has a lower priority than DMC DMA: a DMC get pauses it briefly. See also PPU registers.
DMC DMA
DMC DMA copies one byte of sample data into the DMC sample buffer. It is scheduled when sample playback is on, bytes are left and the buffer is empty. It performs a halt cycle, a dummy cycle, an optional alignment cycle and one get, taking 3 or 4 cycles in the usual cases. A load DMA (after $4015 bit 4 is set) halts on a get cycle during the second APU cycle after the write, so it normally takes 3 cycles. A reload DMA (the buffer has just emptied) halts on a put cycle, so it normally takes 4. Bugs triggered by stopping the sample at certain moments can add more cycles.
Register conflicts: on the 2A03 a DMC DMA that halts the CPU while it reads a joypad port or $2007 adds an extra read. That can skip a byte of PPU data and delete a bit of a controller report, usually seen as a false Right press. The 2A07 (PAL) avoids this by forcing the halt onto the first cycle of an instruction (the opcode fetch). Software works around it by reading the controller until two reads agree. (NESdev Wiki, “DMA”, “APU DMC” and “Standard controller”.)
Sources
The NESdev Wiki text is available under CC BY-SA 3.0. This page is written in our own words.