The PPU scrolls by changing the address it fetches background tiles from. Four internal registers do the work. The ordinary case needs only the registers on PPU registers. This page is for programs that split the screen mid-frame, and for emulators.
The common case
- Take the 9-bit X and Y position of the top left corner of the visible area.
- Write the low 8 bits of X, then the low 8 bits of Y, to PPUSCROLL (
$2005). - Write the 9th bit of X and of Y to bits 0 and 1 of PPUCTRL (
$2000): this selects the nametable seen at the top left.
Do this in the NMI handler, before vblank ends: the position is applied just before rendering begins. A write to PPUADDR ($2006) overwrites the scroll position, so the scroll writes must come after any VRAM update that uses PPUADDR. To scroll over more than the two screens held in VRAM, write the column or row that is about to appear (the “seam”) before setting the scroll. A handler that runs too long misses the end of vblank, and the scroll is then wrong for that frame; most games write no more than about 64 bytes to a nametable per NMI.
The internal registers
- v: the current VRAM address, 15 bits. Used for reads and writes through
$2007, and, while drawing, as the address of the tile being fetched. - t: the temporary VRAM address, 15 bits. It can be thought of as the address of the top left tile on screen.
- x: the fine X scroll, 3 bits.
- w: the first-or-second-write toggle, 1 bit, shared by
$2005and$2006.
During rendering t and v are laid out like this:
yyy NN YYYYY XXXXX
||| || ||||| +++++-- coarse X scroll
||| || +++++-------- coarse Y scroll
||| ++-------------- nametable select
+++----------------- fine Y scroll
The PPU address space is 14 bits wide, so the top bit of v is not used for access through $2007.
What each register access does
d is the byte written, with bits named A (bit 7) to H (bit 0). A dot means the bit is unchanged.
| Access | Effect |
|---|---|
$2000 write | t: ...GH.. ........ <- d: ......GH (nametable select). The other bits of d are used elsewhere. |
$2002 read | w <- 0 |
$2005 first write (w = 0) | t: ....... ...ABCDE <- d: ABCDE...; x: FGH <- d: .....FGH; w <- 1 |
$2005 second write (w = 1) | t: FGH..AB CDE..... <- d: ABCDEFGH; w <- 0 |
$2006 first write (w = 0) | t: .CDEFGH ........ <- d: ..CDEFGH (bits 7 and 6 of d are unused); bit 14 of t is cleared; w <- 1 |
$2006 second write (w = 1) | t: ....... ABCDEFGH <- d: ABCDEFGH; w <- 0; then, after 1 to 1.5 dots, v <- t (all bits) |
$2007 read or write | Outside rendering, adds 1 or 32 to v (PPUCTRL bit 2). During rendering (pre-render line and lines 0 to 239, with background or sprites enabled) it makes a coarse X increment and a Y increment at the same time, whatever the increment bit says. The Young Indiana Jones Chronicles and Burai Fighter use this. If the access coincides with a normal increment, the wiki presumes there is no double increment. |
The first $2006 write clears bit 14 for reasons the wiki says are not known. Because $2005 sets t and not v, it can be done at any time before the frame starts, and v picks it up at the right moment.
What the PPU does to v while rendering
All of these happen only if rendering is enabled.
| When | Action |
|---|---|
| Every 8 dots from dot 328 of a line (328 and 336), and on the next line at 8, 16, … 256 | Increment coarse X of v. |
| Dot 256 of each line | Increment Y of v. |
| Dot 257 of each line | Copy the horizontal bits from t: v: ....A.. ...BCDEF <- t: ....A.. ...BCDEF |
| Dots 280 to 304 of the pre-render line | Repeatedly copy the vertical bits from t: v: GHIA.BC DEF..... <- t: GHIA.BC DEF..... |
Wrapping when v is incremented
Coarse X
if ((v & 0x001F) == 31) // coarse X == 31
v &= ~0x001F // coarse X = 0
v ^= 0x0400 // switch horizontal nametable
else
v += 1
Y
if ((v & 0x7000) != 0x7000) // fine Y < 7
v += 0x1000
else
v &= ~0x7000 // fine Y = 0
y = (v & 0x03E0) >> 5 // coarse Y
if (y == 29)
y = 0
v ^= 0x0800 // switch vertical nametable
else if (y == 31)
y = 0 // nametable not switched
else
y += 1
v = (v & ~0x03E0) | (y << 5)
Row 29 is the last row of tiles in a nametable. Coarse Y can be set above 29: the PPU then reads the attribute table as tiles. From 31 it wraps to 0 without switching nametable. A Y of 240 or more written to $2005 therefore looks like a negative scroll, with one or two rows of attribute data above the tiles. Some games use that to hide the top of the picture in the overscan area.
Tile and attribute addresses from v
tile address = 0x2000 | (v & 0x0FFF)
attribute address = 0x23C0 | (v & 0x0C00) | ((v >> 4) & 0x38) | ((v >> 2) & 0x07)
The attribute address is made of: nametable select (2 bits), 1111 (the 960-byte offset), the high 3 bits of coarse Y, and the high 3 bits of coarse X. Fine Y is not part of either address; it supplies the row in the pattern fetch.
A worked example
The wiki follows these writes, starting with t, v and x clear and w = 0:
| Instruction | t after | x | w | Note |
|---|---|---|---|---|
LDA #$00 / STA $2000 | ...00.. ........ | ... | 0 | nametable bits set |
LDA $2002 | ...00.. ........ | ... | 0 | w reset |
LDA #$7D / STA $2005 | ...00.. ...01111 | 101 | 1 | coarse X = 15, fine X = 5 |
LDA #$5E / STA $2005 | 1100001 01101111 | 101 | 0 | fine Y = 6, coarse Y = 11 |
LDA #$3D / STA $2006 | 0111101 01101111 | 101 | 1 | bit 14 cleared |
LDA #$F0 / STA $2006 | 0111101 11110000 | 101 | 0 | v becomes 0111101 11110000 |
Split scrolling
Single scroll
Write $2000 once and $2005 twice before vblank ends, after any $2006 writes. If the state of w is unknown, read $2002 first.
Split X scroll
Horizontal bits of v are reloaded from t at dot 257, so writes to $2000 and $2005 made before the end of a line change X for the next line.
- The first
$2005write changes fine X at once, and coarse X in t (which counts at dot 257). - A second
$2005write has no effect on the picture but resets w for later splits. - Write
$2000if the high bit of X must change; its other bits change as well. - Write
$2005as late as possible but before horizontal blank begins. About four pixels of jitter are normal, so write a little early, and hide the glitch with a one-colour pixel run or a sprite. - To hide the glitch fully: before dot 257 write the new coarse X combined with the old fine X to a mirror of
$2005, then between dot 257 and the end of the line write the whole new X. On some alignments the PPU takes fine X from open bus for one pixel, so use the mirror whose address high byte has the same low 3 bits as the value written (%xxxxx000to$2005,%xxxxx001to$2105, and so on).
Split X and Y scroll
Four writes set the full position mid-frame. In order:
- nametable number × 4 (
$00,$04,$08or$0C) to$2006; - Y to
$2005; - X to
$2005; - the low byte of the nametable address,
((Y & $F8) << 2) | (X >> 3), to$2006.
The last two writes should fall in horizontal blank (dot 256 or later; dot 252 may do because of the right overscan and the pixel pipeline). The final $2006 write copies t to v immediately. Only the last write to each bit of t counts, which is why the first write can be as simple as the nametable number × 4. For the wiki’s example (nametable 1, X = $7D, Y = $3E) the full values are $2005 = $7D, $2005 = $3E, $2006 = $14, $2006 = $EF, and the simplified first write is $04.
Because Y of v is incremented on dot 256, set the intended Y minus 1 before dot 256, or set Y after dot 256. Hblank runs from after dot 256 to dot 320. Because the method sets v at once it can change the scroll in the middle of a line, but the timing is hard and the wiki does not recommend it. A visible effect is delayed by 1 or 2 tiles because of the pattern pipeline.
Quick coarse X/Y split
Only two $2006 writes (the second copies t to v) can change coarse X, coarse Y, the nametable and the low 2 bits of fine Y. Fine Y bit 2 is cleared and fine X is unchanged. The first write gives 0 0yy NN YY and the second YYY XXXXX, in the order of t.
Sources
- NESdev Wiki: PPU scrolling, which cites loopy’s 1999 document “The skinny on NES scrolling”
The NESdev Wiki text is available under CC BY-SA 3.0. This page is written in our own words from the raw wikitext kept in samples/nes/sources/hardware.