In March 2008 Blargg reverse-engineered the power-up and reset state and behaviour of the NTSC NES PPU. The registers it names are on PPU registers; the rendering timeline is on PPU rendering sequence. The second half of this page is the wiki’s errata table.
Initial register values
| Register | At power | After reset |
|---|---|---|
PPUCTRL ($2000) | 0000 0000 | 0000 0000 |
PPUMASK ($2001) | 0000 0000 | 0000 0000 |
PPUSTATUS ($2002) | +0+x xxxx | U??x xxxx |
OAMADDR ($2003) | $00 | unchanged (see note 1) |
$2005 / $2006 latch | cleared | cleared |
PPUSCROLL ($2005) | $0000 | $0000 |
PPUADDR ($2006) | $0000 | unchanged |
PPUDATA ($2007) read buffer | $00 | $00 |
| odd frame | no | no |
| OAM | unspecified | unspecified |
| palette | unspecified | unchanged |
| nametable RAM (in the control deck) | unspecified | unchanged |
| CHR RAM (in the game pak) | unspecified | unchanged |
Key: ? unknown, x irrelevant, + often set, U unchanged. Note 1: OAMADDR is unchanged by reset but is changed during rendering and cleared at the end of normal rendering, so assume its contents are random.
- The PPU comes out of power and reset at the top of the picture.
- Writes to PPUCTRL, PPUMASK, PPUSCROLL and PPUADDR are ignored until about 29658 CPU clocks after reset, which also means the PPUSCROLL/PPUADDR latch does not toggle. The other registers work at once: PPUSTATUS, OAMADDR, OAMDATA (
$2004), PPUDATA and OAMDMA ($4014). An internal reset signal clears PPUCTRL, PPUMASK, PPUSCROLL, PPUADDR, the PPUSCROLL/PPUADDR latch and the PPUDATA read buffer (clearing PPUSCROLL and PPUADDR corresponds to clearing the VRAM address latch T and the fine X scroll; the VRAM address V itself is not cleared). The signal is set on reset and cleared at the end of VBlank by the same signal that clears the VBlank, sprite 0 and overflow flags; writing to a register while it is being cleared has no effect, which is why writes are “ignored”. - If the NES is powered on again after being off for less than 20 seconds, register writes are ignored as after a reset and the starting values differ: PPUSTATUS
$80(VBlank set), OAMADDR$2For$01, PPUADDR$0001. - The VBlank flag (PPUSTATUS bit 7) is random at power and unchanged by reset; it is next set around 27384 CPU cycles and then around 57165.
- Preliminary tests on a PAL NES show writes ignored until about 33132 CPU clocks after power and reset, 9 clocks less than 311 scanlines; the first VBL flag setting is conjectured, and confirmed by further testing from nocash, to be close to 241 PAL scanlines (241 times 341 / 3.2 CPU cycles).
- After power and reset it is as if the APU’s
$4017were written 10 clocks before the first code runs; this delay is probably the source of the 9-clock difference in the ignored-write time, and likely comes from the 2A03’s reset sequence reading the reset vector. - On front-loaders (NES-001) the Reset button resets both CPU and PPU. On top-loaders (Famicom, NES-101) it resets only the CPU.
Some of this state is unspecified: different lots of chips have different initial values because of the relative strengths of pull-down and pull-up elements in each bit cell, and some bits may vary from one power-on to the next with temperature or noise. OAM contents are unspecified at power-on and at reset because of DRAM decay. The palette is unspecified at power-on and unchanged at reset; during the warm-up the PPU outputs a solid colour screen from the value at $3F00. Nametable RAM and CHR RAM are unspecified at power-on and unchanged at reset. In almost all mappers CHR bank values are unspecified at power-on and unchanged at reset; the exceptions, if any, are on each mapper’s page.
Best practice: the warm-up loop
The easiest way to be sure 29658 cycles have passed, used by commercial games, is two wait loops in the init code:
bit PPUSTATUS ; clear the VBL flag if it was set at reset time
vwait1:
bit PPUSTATUS
bpl vwait1 ; about 27384 cycles have passed
vwait2:
bit PPUSTATUS
bpl vwait2 ; about 57165 cycles have passed
Because of the $2002 race condition, the alignment between CPU and PPU clocks at reset can make the NES miss an occasional VBL flag setting; the only effect is one more frame before startup. Mapper and RAM initialisation can go between the two loops.
Famicom and Dendy
- Famicom: on the NTSC NES the PPU and CPU are reset at the same time; on the Famicom the PPU does not respond to the reset button, only the CPU is reset. At power-on on the Famicom PPU initialisation begins about one frame before the CPU reset, because PPU /reset is tied to 5 V while CPU /reset sits on a 0.47 microfarad capacitor (exact timing not measured, may vary). The Famicom game Magic John waits only 9217 CPU cycles before enabling NMI, before the 29658 the NTSC NES needs, so it will not boot there (a Game Genie lets it boot); the international release Totally Rad fixed this, and The Lord of King (Astyanax) is affected too; both were by Aicom for Jaleco.
- Dendy: reading
$2002at the exact start of vblank clears the flag without reading it back as 1. Usually the alignment shifts so this does not repeat, but Dendy-style PAL famiclones have a frame of exactly 113.667 by 312 = 35464 cycles, a multiple of 8, and abit/bplloop that crosses a page boundary (as in Eliminator Boat Duel) lasts 8 cycles, so on some alignments it hits the start of vblank every time and never advances. Do not make a$2002wait loop whose length in cycles divides the frame length evenly.
Errata: video
The wiki’s errata page lists aspects of the hardware that may be considered mistakes in the implementation or are just unintuitive. Many are poorly emulated and cause compatibility problems, especially for homebrew not tested on hardware. The PPU revision letters (letterless, A, B, C, D, E, G, H) are as on the wiki.
| Area | Type | Problem | Workaround |
|---|---|---|---|
| Emphasis | design | On RGB PPUs, emphasis maximises the emphasised colour (even for $0F black) instead of dimming the other two; with all three bits set the screen is entirely white. | Offer an option to disable emphasis, detect RGB PPUs and disable it, or avoid it where information is critical. |
| Emphasis | design | On PAL and Dendy the red and green emphasis bits of PPUMASK are swapped relative to NTSC and RGB PPUs. | Swap which bits are used if a PAL or Dendy PPU is detected. |
| EXT | design | Setting PPUCTRL bit 6 (EXT output) makes the PPU drive pins that are grounded in every NTSC and PAL console, causing near-constant bus conflicts that may damage the PPU or the pin drivers over time. | Never set the bit. |
| Palettes | design | Colour $0D is “blacker than black” and makes the image unstable on some TVs, worse with emphasis. | Use $0F for black. |
| Palettes | design | When v points at a palette address while rendering is off, the PPU draws the colour at that address, so palette updates outside vblank flash stripes. Writing PPUADDR usually causes it, but it also happens if rendering ends while v looks like a palette address, which can flash colours in the bottom border for some scroll positions. | Write palettes in vblank; avoid scroll positions that leave v in palette RAM at the bottom of the screen (for example in a horizontally scrolling game that moves the background down one scanline to line up with sprites, start on the last scanline of nametables 2 and 3 instead of 0 and 1). |
| Palettes | bug | If the low 4 bits of the palette address change while the PPU is reading palette RAM, data may corrupt: turning rendering off mid-screen with v in palette RAM when the rendering pipeline’s palette address differs (turning it off during an attribute fetch widens the range to v AND $3C00 equal to $3C00); turning it on mid-screen with v in palette RAM (the pipeline address is then $3F00). On early revisions (letterless, A, B, D) it can corrupt in other, not understood, circumstances (perhaps when rendering starts with v in palette RAM, or because t to v copies after the second PPUADDR write are not synchronised). | Test mid-screen toggling on hardware across many power cycles; do not turn rendering off with v in a vulnerable range; when turning it on make the low nybble of v zero if v points into palette RAM; on early revisions end vblank palette updates with the low nybble of v zero or write to the last palette mirror so v wraps to $0000, and update v in vblank so a stale value does not point into palette RAM. |
| Palettes | bug | Reading palette RAM through PPUDATA is unreliable on G and H PPUs (corrupt data on one alignment) and impossible on earlier PPUs (added in revision G). | Keep a copy of the palettes in CPU RAM. |
| Register access | bug | Some 6502 write instructions make an extra read or write (all read-modify-write instructions and indexed stores such as STA $2000,X), which causes extra increments of the PPU’s internal address with OAMDATA and PPUDATA and can corrupt data if PPUDATA is written too fast; accessing PPUDATA on consecutive cycles probably skips every second increment, and consecutive reads may return garbage on every second read. | Do not use instructions with dummy reads on PPUDATA or dummy writes on PPUDATA or OAMDATA. |
| Register access | bug | On NTSC CPUs DMC DMA during reads of PPUDATA can cause extra reads, each incrementing the VRAM address so bytes are skipped, and can make polling PPUSTATUS miss the vblank flag. | Disable DPCM during PPUDATA reads or time the reads so they cannot collide with DMA. |
| Reset | design | On NES-001 front-loaders the PPU refuses data written to PPUCTRL, PPUMASK, PPUSCROLL and PPUADDR for about one frame after power-on or reset. | Wait for the vblank flag twice in init code (read it once first, since it may start set). |
| Scroll | design | Setting the VRAM address through PPUADDR clobbers the scroll position because they share an internal register. | Reset scroll with PPUCTRL and PPUSCROLL after background updates. |
| Scroll | design | Background Y scroll positions 240 to 255 are “negative” and render the attribute table as two rows of tiles; incrementing the scroll past 255 wraps to 0 of the same nametable without carrying into the Y nametable bit. | Skip those positions when scrolling vertically (below 0 wraps to 239, above 239 to 0). |
| Scroll | bug | Writing PPUCTRL, PPUSCROLL or PPUADDR at the exact start of horizontal blanking (dot 257) can make the PPU use the wrong scroll for the next scanline, because t briefly takes the CPU open bus value at the start of the write while the PPU may reload the horizontal scroll. It most often hits games that write PPUCTRL to enable NMI at the end of the gameplay frame, making the next scanline show the left nametable instead of the right. | Do not write PPUCTRL outside vertical or forced blanking except in a timed raster effect, and lock out re-entrant NMI with a variable instead of PPUCTRL; write PPUCTRL through $2000 or its mirror $2100 depending on the low bit of the value; use the PPU open bus mitigation; time writes away from the dot; or use vertical arrangement or one-screen mirroring so both nametable positions are identical. |
| Scroll | bug | A first write to PPUSCROLL mid-scanline makes fine x briefly take the CPU open bus value, possibly selecting the wrong pixel for one dot, and on some alignments corrupting the background pixel for one dot. | Use the open bus mitigation or write to the PPUSCROLL mirror whose page number ($2x05, x 0 to 7) matches the new fine x in its low 3 bits. |
| Scroll | bug | If v is updated both by an increment and by a copy from t at the same moment, the incremented parts of both t and v become the bitwise AND of the two sources: when the t to v copy of a second PPUADDR write coincides with one of the horizontal increments during rendering or the vertical increment at dot 256, or when the increments of a PPUDATA access fall on dot 257 or on pre-render dots 280 to 304. | Make second PPUADDR writes for raster effects in hblank during dots 257 to 327, which do not increment v (the copy happens about 3 dots after the write begins; leave a margin). |
| Sprites | design | OAMDATA is readable only on G and H PPUs (not on the common E PPU of Japan and North America). | Do not rely on reading OAMDATA. |
| Sprites | design | Sprite evaluation and fetching happen on the line before the sprites are drawn, so turning rendering on mid-screen may show wrong sprite data for one scanline. | Keep sprites off for one scanline after turning rendering on. |
| Sprites | design | OAM is DRAM and decays unless refreshed: accessing a byte refreshes its 9-byte row (8 bytes of OAM1 and 1 of OAM2) and all OAM is accessed on each rendered scanline; with rendering off OAM can decay if it stays off too long (vblank itself is short enough), and only the row at OAMADDR persists because the PPU accesses it every dot. PAL avoids decay in its long vblank by forcing a refresh during scanlines 265 to 310. | Perform OAM DMA in the vblank before the first rendered frame after rendering was off (on lag frames avoid OAM DMA, as OAM keeps its state and the RAM buffer may be incomplete); when vblank is extended, do the DMA late enough that the time from DMA start to rendering start is not longer than a normal vblank (21 scanlines); on PAL do it early enough to avoid the forced refresh, which starts after any NTSC-fitting DMA would have finished. |
| Sprites | bug | Sprite 0 hit does not occur at X=255. | Do not rely on it in that column. |
| Sprites | bug | Sprite overflow detection is wrong: the PPU increments both the high 6 bits and the low 2 bits of the OAM1 address separately instead of just the high 6 bits, so it checks tile, attribute and X bytes of some sprites as Y coordinates, giving false positives and negatives. | Make the 8th and 9th sprites on a line adjacent in OAM and make sure no earlier line has 8 or more sprites. |
| Sprites | bug | Changing the OAM address to another row while the PPU is accessing OAM corrupts all 9 bytes of the newly selected row, typically copying the old row into it but sometimes by analogue corruption (no corruption if the rows are equal). It happens when OAMADDR is written during use (unsynchronised; the early write glitch means one write can corrupt 2 rows), when rendering is turned off while OAM2 is being accessed, when rendering is turned on during a dot at which sprite rendering would access OAM2, and on C, E, G and H PPUs when rendering begins automatically at pre-render dot 0, where the OAM1ADDR row is reliably copied to the OAM2ADDR row (not on letterless, A, B, D). | Write OAMADDR only to set it to 0 before OAM DMA; avoid manual OAMDATA updates unless all bytes of a corrupted row are rewritten and the address is back to 0 before rendering; turn rendering off mid-screen at about dots 320 to 340 (both addresses 0), or at about dots 65 to 256 if the next scanline has no sprites, to avoid corruption. |
| vblank flag | design | Enabling NMI through PPUCTRL bit 7 while the vblank flag is set triggers an NMI at once, which can be later than the start of vblank or a second NMI in one vblank. | Read PPUSTATUS shortly before enabling NMI. |
| vblank flag | bug | Reading PPUSTATUS at the exact moment the flag becomes 1 stops it from ever becoming 1 that frame, or stops NMI from being asserted long enough for the CPU to see it. | Outside init code, wait for vblank with NMI; on Dendy make sure each PPUSTATUS poll loop’s cycle length does not divide the frame length evenly (a loop crossing a page boundary can hang forever). |
Errata: input
On the 2A03, DMC DMA during a controller read ($4016 or $4017) clocks the register twice, so bits of the report are skipped; a common symptom is spurious presses of Right. Workarounds: read a standard controller or Power Pad several times until two reads agree; check a Four Score’s signature; for the Arkanoid controller prevent a sudden one-frame acceleration; if a DMC IRQ is already used, read the controller in its handler; or run an OAM DMA before reading to align the CPU clock to the even or odd phase of the APU clock.
Errata: audio
- Pulse channels: in sweep decrease mode the carry input differs between the two channels, giving slightly different sweep rates. Channels at low frequencies can be silenced by the sweep unit in increase mode even when it is disabled (write
$08to$4001and$4005to use decrease mode while disabling sweep). Writing$4003or$4007to change the high period byte while a note plays resets the phase and clicks (write only when changed, or use the hardware sweep clocked at once by a write to$4017, an advanced technique). - Triangle: the length counter and linear counter share a control bit in
$4008, so both run at once; the linear counter’s shorter maximum makes longer length-counter periods unusable. Unlike the pulse channels the triangle does not mute ultrasonic frequencies (the mixer usually attenuates them); several games “silence” it by setting period 0, which pops. - DMC: the length counter makes a sample read one byte past the end of an otherwise 16-byte-aligned sample, so 15 bytes of padding are needed between samples. The 16 playback pitches of the rate table are tuned to an A-440 scale for wavetable-like looped samples, but the +1 on the sample length detunes the wavelength (workaround: a 17-byte loop in the key of B major). The PAL rate table has two small tuning errors (entries
$4and$C). Playback conflicts with controller reads and PPUDATA reads, so code must work around them. Each sample byte plays least significant bit first, the opposite of how the PPU scans out background tiles.
Errata: CPU
Gotchas, mostly inherited from the MOS 6502: JMP ($xxyy) does not advance pages when the low address byte is $FF (the high byte is fetched from $xx00); zero page addressing modes wrap within the zero page ($xx,x, $xx,y and ($xx,x) count 254, 255, 0, 1); the ($xx),y mode wraps when the low byte of the pointer is at $FF (the high byte comes from $0000, not $0100); BRK, IRQ and NMI can mask each other in certain conditions (not all can happen on the NES); decimal mode was disconnected from the ALU in the NES’s second-source 6502 to save royalties (some famiclones use a real 6502 with working decimal mode; do not use SED, convert binary numbers for display); the unofficial opcodes reliably exist and some games rely on them but code using them is not portable to other 6502 variants such as the SNES CPU; page wrapping is reliable on the Famicom and NES but not portable.
PPU glitches: early writes and internal bus conflicts
Early writes. When the CPU writes, it tells the bus that a write is starting before it puts the data on the bus, so for a short time at the start of the write the data lines hold open bus. The PPU assumes the data is valid for the whole write, and for many registers it briefly uses the open-bus value, which can give visible glitches. The open-bus value is normally the high byte of the register address, because the CPU fetched that byte on the cycle before the write, so the early value is normally $20. The 6502 (and so the 2A03) keeps R/W and the address stable while M2 is high, but not the data bus; the wiki has a timing diagram for the 2A03G (about 40 ns per step) that shows the data lines floating at the start of a write.
Most early writes do no harm: most PPU writes happen outside rendering, and even those during rendering matter only on certain dots. The problem is mostly the case where the old and new value of a register are equal, because then the early value can make the register differ for a moment. When the values differ, the early write only shifts when the change happens (earlier if the early value matches the old value, later if it matches the new one), but different bits of one write can change at different times, because different bits match one value or the other. Two ways to make the early value match the value written:
- PPU open bus. A read of a write-only PPU register returns the last value on the PPU’s internal bus (the last value the CPU read or wrote; it decays only over times of 1 ms or more). Put the wanted value on that bus, then have the CPU read it on the cycle before the write. An indexed write (
STA reg,X, five cycles) reads the target address on its fourth cycle, so the sequence is: load the value into A, set X to 0,STA $2002(this primes the bus through a read-only register) and thenSTAto the target register indexed by X. It works for PPUCTRL, PPUMASK, OAMADDR, PPUSCROLL and PPUADDR but not for PPUDATA, which has no early-write problem, can be read, has side effects on reads and needs more than one CPU cycle for an access. - Register mirrors. Because the early value is the high byte of the address, a write to a mirror of the register in
$2000-$3FFFwhose high byte matches the value fixes the low five bits (the top three bits are always001). Example: to turn rendering on in mid-screen,LDA #$1EthenSTA $3E01(a mirror of PPUMASK). Use the open-bus method if all eight bits matter or several bits must change together.
Registers affected (the wiki also says which chip revision):
- PPUCTRL. On the 2C02G the
$C3bits are processed asynchronously. Bit 0: a write that starts on dot 257 and ends on dot 258 of an active scanline can draw the left nametable on the next line (the PPU sees$20at dot 257); work around it by writing to the PPU address mirror whose high byte has the wanted low two bits. Bit 1: a write starting on dot 304 of the pre-render scanline and ending on dot 305 can make the next field draw the top nametable (same workaround). Bit 6 (the EXT output): any write in the active field can disable the colour output on the EXT pins for a pixel (believed to cause some HDNES bugs). Bit 7: any write in vertical blank can assert or clear the NMI output for about 80 ns, which is invisible because the 6502 ignores NMI then. On the 2C02A the$18bits of PPUMASK are known to be asynchronous too and all other bits are suspected to be: bit 2 should do nothing, bit 3 may fetch one bitplane of one sprite sliver from the wrong pattern table when written in horizontal blank, bit 4 may do the same for one background sliver during active redraw, and bit 5 may draw one sprite sliver wrongly (the details are unclear). - PPUMASK. On the 2C02G the
$81bits are asynchronous, with unimportant effects: any write can turn off monochrome for one pixel, and can turn off blue emphasis for half of one pixel whatever the sub-pixel phase. On the 2C02A and 2C03B the$38bits are known to be asynchronous: on the 2C02A any write during active redraw can switch rendering off for one pixel, with all the usual hazards of disabling rendering, and on the 2C03B any write can switch red emphasis off for a whole pixel; on the 2C02A and 2C02B the$60bits are believed to turn red and green emphasis off for half a pixel. The$06bits would be hidden by the first effect. - OAMADDR. On the 2C02G a change of the OAM address can copy the row of memory at the first address to the row at the second. Writes to OAMADDR can start it in several ways, one being early writes; fixing the early write makes OAMADDR writes work on some CPU and PPU alignments but not on all.
- PPUADDR and PPUSCROLL. On the 2C02G a write that starts on dot 257 and ends on 258 and changes coarse X in the temporary address t can cause the PPUCTRL symptoms. Every second PPUADDR write sets the low three bits of coarse X and five bits of coarse Y of the working address to the open-bus value at once and fixes them on the next pixel (other than in the dot 257 and 258 case), which is usually invisible but can give one wrong sliver. Every first PPUSCROLL write sets fine X to the open-bus value at once and corrects it on the next pixel.
PPU-internal bus conflicts. When the PPU increments the working address v and reloads v from t on the same dot, the two inputs to v conflict, and both v and t become the bitwise AND of them. Only the parts that were being incremented (X, Y or both) are affected. This can happen (at least) two ways: a second write to PPUADDR, which loads a new scroll position, so that the copy from t to v falls on dot 256 (where Y is incremented) or on the last dot of each group of four background fetches (where coarse X is incremented); or a read or write of PPUDATA, whose increments of fine Y and coarse X fall on the dot where the PPU reloads v from t (dot 257 of each rendering scanline, or dot 304 of the pre-render scanline). Source: NESdev Wiki, PPU glitches.
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. Not copied: the forum threads and test ROM references each entry cites, and the timing diagram of the early write on the 2A03G.