Once a line the PPU reads the sprite list (object attribute memory, OAM, 64 sprites of
4 bytes; see PPU registers) and prepares the
sprites to draw on the next line. This is separate from drawing them. In
the text below OAM[n][m] is byte m (0 to 3) of sprite n
(0 to 63), at offset 4n + m.
The four steps
- Clear the list of sprites to draw (secondary OAM, 32 bytes).
- Read through OAM and keep the first eight sprites that are on this line.
- If eight were found, look for a ninth to decide whether to set the sprite overflow flag. The wiki says this check is “wrongly implemented”.
- Using the chosen sprites, work out which pixels each has and where to draw them.
What the PPU does on each dot
| Dots | Action |
|---|---|
| 1 to 64 | Secondary OAM is set to $FF. A read of $2004 during this time returns $FF. Inside the chip this is an ordinary read from OAM and write to secondary OAM, with a signal that forces the read value to $FF. |
| 65 to 256 | Evaluation (below). On odd dots a byte is read from primary OAM. On even dots a byte is written to secondary OAM, or, when it is full, the value in secondary OAM is read instead. |
| 257 to 320 | Sprite fetches, 8 dots per sprite. Dots 1 to 4 read the Y, tile, attribute and X bytes from secondary OAM. Dots 5 to 8 read the X byte four times while the PPU fetches the tile pattern. The first empty slot reads sprite 63’s Y followed by three $FF; later empty slots read four $FF. |
| 321 to 340 and dot 0 of the next line | Reads the first byte of secondary OAM while the PPU fetches the first two background tiles of the next line. |
The wiki says this pattern was found by carefully timed reads of $2004 with different sets of sprites, and was then confirmed by simulating the chip in Visual 2C02.
The evaluation loop (dots 65 to 256)
- Start at n = 0. Read the Y coordinate
OAM[n][0]and copy it to the next free slot in secondary OAM (the write is ignored once eight sprites are found).- If the Y coordinate puts the sprite on the next line, copy
OAM[n][1]toOAM[n][3]as well.
- If the Y coordinate puts the sprite on the next line, copy
- Add 1 to n.
- If n wrapped to 0 (all 64 sprites done), go to step 4.
- If fewer than eight sprites have been found, go to step 1.
- If exactly eight have been found, writes to secondary OAM are disabled because it is full. Sprites further back in OAM drop out.
- Start at m = 0 and treat
OAM[n][m]as a Y coordinate.- If it is in range, set the sprite overflow flag in
$2002, read the next three OAM entries (adding 1 to m after each, and to n when m overflows); if m = 3, add 1 to n. - If it is not in range, add 1 to n and to m (without carry). If n wrapped to 0, go to step 4, otherwise repeat step 3. Adding to m is the hardware bug.
- If it is in range, set the sprite overflow flag in
- Try, and fail, to copy
OAM[n][0]into the next slot, adding 1 to n, until horizontal blank is reached.
The sprite overflow bug
After eight sprites are found, the logic keeps scanning OAM for one more. The first test checks the Y coordinate of the next entry correctly. After that it also steps m, so it scans OAM “diagonally”, reading the tile, attribute and X bytes of later entries as if they were Y coordinates. The flag can therefore be set when it should not (false positive) or stay clear when more than eight sprites are on a line (false negative).
The cause, found in the Visual 2C02 simulation, is the signal that turns the secondary OAM writes into reads once it is full. The value then read is the Y coordinate of the first sprite copied. It is always in range for the line, and a logic error lets this comparison affect how the sprite address is advanced. Once one more in-range sprite has been found, another signal stops the comparison having this effect and the bug ends. The wiki lists the games that use the quirk on a separate page, “Sprite overflow games”.
Notes
- Evaluation does not happen on the pre-render line. Nothing is drawn on line 0, and this is why a sprite’s Y coordinate is one less than the line it appears on.
- Evaluation runs when either the sprite layer or the background layer is enabled. If only the sprite layer is off, this merely hides the sprites. If both are off, rendering stops at once.
- Sprite evaluation does not cause the sprite 0 hit. Sprite rendering does.
- On the 2C02G and 2C02H, if OAMADDR (
$2003) is not 0 when evaluation starts, an OAM refresh bug copies the 8 bytes starting at OAMADDR &$F8over the first 8 bytes of OAM. - If rendering is switched off during an active line before evaluation finishes, the OAMADDR logic can be corrupted and sprites lost in the OAM DMA of the next frame. The wiki says this can be avoided by making the write after evaluation: at pixel x = 192 for lines with no sprites, x = 240 for lines with at least one. Turning rendering back on in the same frame may corrupt more, at least on the first line it is re-enabled.
Sources
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.