The picture processing unit (PPU) draws the screen. The CPU talks to it through eight
registers at $2000–$2007. The addresses are only partly decoded,
so they repeat every 8 bytes up to $3FFF: a write to $3456 is a
write to $2006. After power-on or reset the PPU starts drawing at once but
ignores writes to $2000, $2001, $2005 and
$2006 until the pre-render line of the next frame (about 29,658 NTSC CPU
cycles or 33,132 PAL cycles). Where a bit pattern is shown, bit 7 is on the left. This
page follows the NESdev Wiki pages named under Sources.
Summary
| Name | Address | Bits | Access | Purpose |
|---|---|---|---|---|
| PPUCTRL | $2000 | VPHB SINN | write | NMI enable, master/slave, sprite height, pattern tables, VRAM increment, nametable |
| PPUMASK | $2001 | BGRs bMmG | write | Colour emphasis, sprite and background enable, left-column enable, greyscale |
| PPUSTATUS | $2002 | VSOx xxxx | read | Vblank, sprite 0 hit, sprite overflow |
| OAMADDR | $2003 | AAAA AAAA | write | Sprite memory address |
| OAMDATA | $2004 | DDDD DDDD | read, write | Sprite memory data |
| PPUSCROLL | $2005 | XXXX XXXX, then YYYY YYYY | write twice | X scroll, then Y scroll |
| PPUADDR | $2006 | ..AA AAAA, then AAAA AAAA | write twice | VRAM address, high byte then low byte |
| PPUDATA | $2007 | DDDD DDDD | read, write | VRAM data |
| OAMDMA | $4014 | AAAA AAAA | write | Copy a CPU page to sprite memory (in the CPU, not the PPU) |
The PPU has an internal data bus that acts as a latch. Writing any PPU port fills it, even PPUSTATUS. Reading PPUSTATUS, OAMDATA or PPUDATA fills it with the bits read. Reading a write-only register, or the unused bits of PPUSTATUS, returns the latch. At least one bit of it fades after 3 to 30 ms. (NESdev Wiki, “PPU registers”.)
PPUCTRL, $2000 (write)
| Bits | Name | Meaning |
|---|---|---|
1–0 (NN) | Nametable | Base nametable address: 0 is $2000, 1 is $2400, 2 is $2800, 3 is $2C00. These are also bit 8 of the scroll position (bit 0 adds 256 to X, bit 1 adds 240 to Y). |
2 (I) | Increment | VRAM address step per PPUDATA access: 0 adds 1 (across), 1 adds 32 (down). |
3 (S) | Sprite table | Pattern table for 8×8 sprites: 0 is $0000, 1 is $1000. Ignored for 8×16 sprites. |
4 (B) | Background table | Background pattern table: 0 is $0000, 1 is $1000. |
5 (H) | Sprite height | 0 gives 8×8 sprites, 1 gives 8×16. |
6 (P) | Master/slave | 0 reads the backdrop from the EXT pins, 1 outputs colour on them. Never set it on a stock console: the EXT pins are grounded and it may damage the PPU. |
7 (V) | NMI enable | 1 calls the NMI handler at the start of vertical blank (line 241, dot 1). |
Switching NMI from 0 to 1 while the vblank flag is already 1 fires an NMI at once. Read PPUSTATUS first to clear the flag. The nametable bits share the internal t register with PPUSCROLL, so write them together. A write that starts exactly on dot 257 can draw only the next line from the wrong nametable (the “bit 0 race”); it matters with vertical mirroring or four-screen VRAM. (NESdev Wiki, “PPU registers”.)
PPUMASK, $2001 (write)
| Bit | Name | Meaning |
|---|---|---|
0 (G) | Greyscale | 1 forces greyscale: the colour is ANDed with $30. |
1 (m) | Background left | 1 shows the background in the leftmost 8 pixels, 0 hides it. |
2 (M) | Sprites left | 1 shows sprites in the leftmost 8 pixels, 0 hides them. |
3 (b) | Background | 1 enables background rendering. |
4 (s) | Sprites | 1 enables sprite rendering. |
5 (R) | Emphasise red | Green on PAL and Dendy. |
6 (G) | Emphasise green | Red on PAL and Dendy. |
7 (B) | Emphasise blue |
Rendering is on while either bit 3 or bit 4 is set. A disabled part is treated as transparent. With both off the PPU shows the backdrop colour and the CPU may use VRAM and OAM freely. Most programs write $00 outside play and $1E during play. Turn rendering on or off during vblank where possible. The effect comes about 3 to 4 dots after the write. Sprite 0 hit never happens where sprites or background are off. Emphasis darkens the other two colour components. Emphasising all three dims the picture. On RGB PPUs emphasis works differently. (NESdev Wiki, “PPU registers”.)
PPUSTATUS, $2002 (read)
| Bits | Name | Meaning |
|---|---|---|
| 4–0 | Open bus | The PPU latch. The arcade 2C05 PPUs return an identifier here instead: the 2C05-02 returns $3D (mask $3F), the 2C05-03 $1C and the 2C05-04 $1B (mask $1F). |
5 (O) | Sprite overflow | Meant to be set when a line has more than 8 sprites; the check is buggy and gives false positives and negatives. |
6 (S) | Sprite 0 hit | Set when an opaque pixel of sprite 0 overlaps an opaque background pixel. Not at X = 255 or where sprites or background are disabled (including the hidden left 8 pixels). |
7 (V) | Vblank | Set at line 241, dot 1. Cleared by a read and at dot 1 of the pre-render line. |
A read also clears the internal write toggle w shared by PPUSCROLL and PPUADDR. The three flags clear at dot 1 of the pre-render line. Reading the vblank flag is not a reliable way to see vblank: a read one dot before the flag is set returns 0 and suppresses that frame’s NMI. Use the NMI. (NESdev Wiki, “PPU registers”.)
OAMADDR, $2003 and OAMDATA, $2004
OAMADDR takes an 8-bit address into sprite memory (OAM). OAMDATA reads or writes the byte there. A write adds 1 to OAMADDR; a read does not. Most programs set OAMADDR to 0 and use OAM DMA instead. OAMADDR is set to 0 during dots 257–320 of every rendering line. On the 2C02G, writing OAMADDR can corrupt OAM; writing all 256 bytes (through DMA) hides this. During rendering writes to OAMDATA do not change OAM but bump OAMADDR in a glitchy way, so ignore them. Early PPUs could not read OAMDATA. On the 2C07 (PAL) sprite evaluation can never be switched off and starts 24 lines after vblank starts, so OAM must be updated inside those 24 lines. (NESdev Wiki, “PPU registers”.)
Sprite memory format
OAM holds 64 sprites of 4 bytes. (NESdev Wiki, “PPU OAM”.)
| Byte | Meaning |
|---|---|
| 0 | Y position of the top of the sprite, minus 1 (sprites are delayed by one line). Values $EF–$FF hide the sprite. |
| 1 | Tile number. In 8×16 mode bit 0 chooses the pattern table ($0000 or $1000) and bits 7–1 the top tile. |
| 2 | VHP. .CC: bits 1–0 palette (4 to 7), bit 5 priority (0 in front of the background, 1 behind), bit 6 flip horizontally, bit 7 flip vertically. Bits 4–2 are not implemented and read 0. |
| 3 | X position of the left side. |
PPUSCROLL, $2005 (write twice)
The first write is the X scroll bits 7–0, the second is the Y scroll bits 7–0. Bit 8 of each lives in PPUCTRL bits 0 and 1. Which write comes next is tracked by the toggle w, shared with PPUADDR, so write PPUSCROLL after any PPUADDR writes and clear the toggle by reading PPUSTATUS first. X runs 0–255. Y normally runs 0–239; 240–255 make the PPU read attribute data as tiles. A Y change during rendering takes effect only on the next frame. (NESdev Wiki, “PPU registers”.)
PPUADDR, $2006 (write twice)
Write the high byte first, then the low byte of a 14-bit address ($0000–$3FFF; bits 14 and 15 are ignored). Each access to PPUDATA then adds 1 or 32 to it. After a palette update, set the address to $3F00 (or $3F10, $3F20) before pointing it elsewhere, to avoid palette corruption. A second write that ends at the wrong time during rendering can AND with the current scroll value; only finish it in blanking. (NESdev Wiki, “PPU registers”.)
PPUDATA, $2007 (read and write)
Reads and writes VRAM at the current address and then advances it. Only use it in vblank or with rendering off. Reads are delayed by one: the register returns an internal read buffer and then refills it, so the first read after setting the address is a dummy read. The buffer changes only on PPUDATA reads. Palette reads ($3F00–$3FFF) are different on later PPUs (2C02G, 2C02H and PAL): they return the palette byte at once, with open bus in the top two bits, and the underlying nametable byte goes into the buffer. They are unreliable, so do not depend on them. A DPCM fetch that lands on a PPUDATA read adds an extra read and a byte is skipped. (NESdev Wiki, “PPU registers”.)
OAMDMA, $4014 (write)
Write the high byte of a source page. The 2A03 halts the CPU and copies 256 bytes from that page to OAM by writing OAMDATA. It takes 513 or 514 CPU cycles. See DMA. On PAL the OAM is refreshed by the PPU from 24 lines after vblank starts and cannot be written then, so DMA belongs at the start of vblank. OAM decays if it is not refreshed; on NTSC vblank is short enough to avoid that. Do not copy on lag frames. (NESdev Wiki, “PPU registers”.)
Internal registers
v(15 bits): the current VRAM address, and the scroll position while rendering.t(15 bits): the temporary address, loaded by PPUSCROLL, PPUADDR and PPUCTRL.x(3 bits): the fine X scroll.w(1 bit): the first-or-second-write toggle; a read of PPUSTATUS clears it.
Writing the high byte of PPUADDR forces bit 14 of t to 0. (NESdev Wiki, “PPU registers”.)
The PPU address space
The PPU has its own 14-bit address space. (NESdev Wiki, “PPU memory map”.)
| Addresses | Size | What | Mapped by |
|---|---|---|---|
$0000–$0FFF | $1000 | Pattern table 0 | Cartridge (CHR ROM or RAM, often banked) |
$1000–$1FFF | $1000 | Pattern table 1 | Cartridge |
$2000–$2FFF | $1000 | Four nametables of $03C0 bytes, each followed by $40 bytes of attributes ($23C0, $27C0, $2BC0, $2FC0) | Cartridge: usually the console’s 2 KiB of VRAM arranged by mirroring; some boards map ROM, RAM or four screens |
$3000–$3EFF | $0F00 | Usually a mirror of $2000–$2EFF; not used for drawing | Cartridge |
$3F00–$3F1F | $0020 | Palette RAM | Inside the PPU |
$3F20–$3FFF | $00E0 | Mirrors of $3F00–$3F1F | Inside the PPU |
Sources
The NESdev Wiki text is available under CC BY-SA 3.0. This page is written in our own words.