Sega Mega Drive/Palettes and CRAM
From Sega Retro
The Sega Mega Drive stores four palettes of 16 colours, totalling 64 ($40) colours, in 128 ($80) bytes of CRAM. The CRAM is a separate area of memory on the VDP from VRAM. Each colour consists of two bytes which contain 9-bit RBG values. This gives the Mega Drive a total of 512 colours to choose from for its 64 colour palette.
In practice only 61 colours are visible because four are used for transparency, although one of those is visible as the background colour. It is possible to display more than 61 colours by changing the CRAM mid-frame, or by using shadow and highlight mode.
Format
- B, G, R: 3-bit values each for blue, green and red.
A simpler way of expressing this is as a word value with the format $0BGR. The 2nd, 3rd and 4th nybbles contain the brightness levels for blue, green and red respectively. Possible values are 0, 2, 4, 6, 8, $A, $C and $E.
3-bit value | Nybble value | Brightness (0-255) |
---|---|---|
000 | 0 | 0 ($00) |
001 | 2 | 52 ($34) |
010 | 4 | 87 ($57) |
011 | 6 | 116 ($74) |
100 | 8 | 144 ($90) |
101 | A | 172 ($AC) |
110 | C | 206 ($CE) |
111 | E | 255 ($FF) |
CRAM mid-frame changes
Normally a game would transfer new data to the CRAM between two frames, during the VBlank period, and any in-game changes to the palette are buffered in RAM before the transfer. The contents of CRAM can also be changed mid-frame, in order to display a new set of colours on the lower part of the screen. One example of this is in water-based levels of Sonic the Hedgehog games, where a new palette is loaded partway down the screen to show the change between over- and underwater areas. To modify the CRAM at a specific scanline, new data is loaded during a horizontal interrupt.
A drawback of CRAM mid-frame changes is that data sent to the CRAM interferes with the screen drawing process, which results in dots appearing on the screen 32 pixels apart for every word sent to the CRAM. This bug is known as "CRAM dots".
Background colour
The background colour is a single colour selected from the palette which is displayed as the screen border, and also where there are no opaque pixels displayed from any plane or sprite. The background colour is controlled by VDP register $07. By choosing a transparent colour as the background colour, the number of visible colours can be extended from 60 to 61.
Below is an example of Sonic the Hedgehog using a transparent colour (dark blue) for the borders and in the background water.