PPU palettes: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(Mention chroma and luma)
(Define "forced blanking" to make the last two sections more concise)
Line 1: Line 1:
The palette for the background runs from VRAM $3F00 to $3F0F; the palette for the sprites runs from $3F10 to $3F1F. Each color takes up one byte.
The palette for the background runs from VRAM $3F00 to $3F0F; the palette for the sprites runs from $3F10 to $3F1F. Each color takes up one byte.


{| border=1
{| class="wikitable"
! Address || Purpose
|-
| $3F00 || Universal background color
| $3F00 || Universal background color
|-
|-
Line 27: Line 29:
The choice of palette is in attribute 2 of each sprite (see [[PPU OAM]]).
The choice of palette is in attribute 2 of each sprite (see [[PPU OAM]]).


Addresses $3F04/$3F08/$3F0C can contain unique data, though these values are not used by the PPU when normally rendering (since the pattern values that would otherwise select those cells select the background color instead). They can still be shown using the background palette hack, explained below.
Addresses $3F04/$3F08/$3F0C can contain unique data, though these values are not used by the PPU when normally rendering (since the pattern values that would otherwise select those cells select the backdrop color instead). They can still be shown using the background palette hack, explained below.


Addresses $3F10/$3F14/$3F18/$3F1C are mirrors of $3F00/$3F04/$3F08/$3F0C. Note that this goes for writing as well as reading; a symptom of not having implemented this correctly in an emulator is the sky being black in Super Mario Bros (as it writes the background color palette entry through the mirror).
Addresses $3F10/$3F14/$3F18/$3F1C are mirrors of $3F00/$3F04/$3F08/$3F0C. Note that this goes for writing as well as reading.
A symptom of not having implemented this correctly in an emulator is the sky being black in ''Super Mario Bros.'', which writes the backdrop color through $3F10.


Another way of looking at it:
Thus, indices into the palette are formed as follows:
  43210
  43210
  |||||
  |||||
Line 37: Line 40:
  |++--- Palette number from attribute table or OAM
  |++--- Palette number from attribute table or OAM
  +----- Background/Sprite select
  +----- Background/Sprite select
Like most early color game consoles, the NES palette is based on Hue/Saturation/Value
As in some second-generation game consoles, values in the NES palette are based on [[wikipedia:HSL and HSV|hue and brightness]]:
  76543210
  76543210
  ||||||||
  ||||||||
Line 43: Line 46:
  ||++----- Value (voltage, determines NTSC/PAL luma)
  ||++----- Value (voltage, determines NTSC/PAL luma)
  ++------- Unimplemented, reads back as 0
  ++------- Unimplemented, reads back as 0
Hue $0 is light gray, $1-$C are blue to red to green to cyan, $D is dark gray, and $E-$F are black. The canonical code for "black" is $0F or $1D. $0D should not be used; it results in a "blacker than black" signal that may cause problems for some TVs.
Hue $0 is light gray, $1-$C are blue to red to green to cyan, $D is dark gray, and $E-$F are mirrors of $1D (black).
The canonical code for "black" is $0F or $1D.
$0D should not be used; it results in a "blacker than black" signal that may cause problems for some TVs.
It works this way because of the way colors are represented in an NTSC or PAL signal, with the phase of a color subcarrier controlling the hue. For details, see [[NTSC video]].
It works this way because of the way colors are represented in an NTSC or PAL signal, with the phase of a color subcarrier controlling the hue. For details, see [[NTSC video]].


Note that most VS Unisystem arcade PPUs have completely different palettes, and Playchoice-10 PPUs render hue $D as black.
The 2A03 RGB PPU used in the PlayChoice-10 and Famicom Titler renders hue $D as black, not dark gray.
The 2A04 PPUs used in many [[Vs. System]] arcade games have completely different palettes as a copy protection measure.


== Backdrop color (palette index 0) uses ==
== Backdrop color (palette index 0) uses ==


Provided rendering hasn't been completely disabled (i.e., either background or sprite rendering is enabled in [[PPU registers|PPUMASK ($2001)]]), the backdrop color will be used when background rendering is disabled, and also for the leftmost 8 pixels of the background when those are hidden via PPUMASK.
During forced blanking, when neither background nor sprites are enabled in [[PPU registers|PPUMASK ($2001)]], the picture will show the backdrop color.
 
If only the background or sprites are disabled, or if the left 8 pixels are clipped off, the PPU continues its [[PPU rendering|normal video memory access pattern]] but uses the backdrop color for anything disabled.
Internally, disabling either background or sprite rendering (but not both) causes 0 to be used as the palette index for the corresponding background and sprite pixels, but does not affect the PPU VRAM access pattern.


== The background palette hack ==
== The background palette hack ==


When rendering is ''completely'' disabled in PPUMASK (both background and sprite rendering disabled) and the current VRAM address points in the range $3F00-$3FFF, the color indicated by this palette location will be shown on screen instead of the backdrop color (looking at the relevant circuitry in [[Visual 2C02]], this is an intentional feature of the PPU and not merely a side effect of how rendering works). This can be used to display colors from the normally unused $3F04/$3F08/$3F0C palette locations.
If the current VRAM address points in the range $3F00-$3FFF during forced blanking, the color indicated by this palette location will be shown on screen instead of the backdrop color.
(Looking at the relevant circuitry in [[Visual 2C02]], this is an intentional feature of the PPU and not merely a side effect of how rendering works.)
This can be used to display colors from the normally unused $3F04/$3F08/$3F0C palette locations.
A loop that fills the palette will cause each color in turn to be shown on the screen, so to avoid horizontal rainbow bar glitches while loading the palette, wait for a real vertical blank first using an [[NMI]] technique.
A loop that fills the palette will cause each color in turn to be shown on the screen, so to avoid horizontal rainbow bar glitches while loading the palette, wait for a real vertical blank first using an [[NMI]] technique.

Revision as of 04:19, 22 July 2013

The palette for the background runs from VRAM $3F00 to $3F0F; the palette for the sprites runs from $3F10 to $3F1F. Each color takes up one byte.

Address Purpose
$3F00 Universal background color
$3F01-$3F03 Background palette 0
$3F05-$3F07 Background palette 1
$3F09-$3F0B Background palette 2
$3F0D-$3F0F Background palette 3
$3F11-$3F13 Sprite palette 0
$3F15-$3F17 Sprite palette 1
$3F19-$3F1B Sprite palette 2
$3F1D-$3F1F Sprite palette 3

Each palette has three colors. Each 16x16 pixel area of the background can use the backdrop color and the three colors from one of the four background palettes. The choice of palette for each 16x16 pixel area is controlled by bits in the attribute table at the end of each nametable. Each sprite can use the three colors from one of the sprite palettes. The choice of palette is in attribute 2 of each sprite (see PPU OAM).

Addresses $3F04/$3F08/$3F0C can contain unique data, though these values are not used by the PPU when normally rendering (since the pattern values that would otherwise select those cells select the backdrop color instead). They can still be shown using the background palette hack, explained below.

Addresses $3F10/$3F14/$3F18/$3F1C are mirrors of $3F00/$3F04/$3F08/$3F0C. Note that this goes for writing as well as reading. A symptom of not having implemented this correctly in an emulator is the sky being black in Super Mario Bros., which writes the backdrop color through $3F10.

Thus, indices into the palette are formed as follows:

43210
|||||
|||++- Pixel value from tile data
|++--- Palette number from attribute table or OAM
+----- Background/Sprite select

As in some second-generation game consoles, values in the NES palette are based on hue and brightness:

76543210
||||||||
||||++++- Hue (phase, determines NTSC/PAL chroma)
||++----- Value (voltage, determines NTSC/PAL luma)
++------- Unimplemented, reads back as 0

Hue $0 is light gray, $1-$C are blue to red to green to cyan, $D is dark gray, and $E-$F are mirrors of $1D (black). The canonical code for "black" is $0F or $1D. $0D should not be used; it results in a "blacker than black" signal that may cause problems for some TVs. It works this way because of the way colors are represented in an NTSC or PAL signal, with the phase of a color subcarrier controlling the hue. For details, see NTSC video.

The 2A03 RGB PPU used in the PlayChoice-10 and Famicom Titler renders hue $D as black, not dark gray. The 2A04 PPUs used in many Vs. System arcade games have completely different palettes as a copy protection measure.

Backdrop color (palette index 0) uses

During forced blanking, when neither background nor sprites are enabled in PPUMASK ($2001), the picture will show the backdrop color. If only the background or sprites are disabled, or if the left 8 pixels are clipped off, the PPU continues its normal video memory access pattern but uses the backdrop color for anything disabled.

The background palette hack

If the current VRAM address points in the range $3F00-$3FFF during forced blanking, the color indicated by this palette location will be shown on screen instead of the backdrop color. (Looking at the relevant circuitry in Visual 2C02, this is an intentional feature of the PPU and not merely a side effect of how rendering works.) This can be used to display colors from the normally unused $3F04/$3F08/$3F0C palette locations. A loop that fills the palette will cause each color in turn to be shown on the screen, so to avoid horizontal rainbow bar glitches while loading the palette, wait for a real vertical blank first using an NMI technique.