top of page

Written 2nd July 2018, by S.A.Ray

MD Dev part 2: Colour Palettes

The Megadrive produces its graphics with a chip called the VDP, or Visual Display Processor.   It has many features including three planes for sprites, back and foregrounds and four colour palettes.  Today we are just concerned about the four colour palettes.

The make up of a palette

According to the site Sega Retro each of the VDP's four palettes can store up to 16 colours, and does so by storing them in the VDP's internal CRAM.  Now, with 4 lots of 16, thats a total of 64 colours that can be held at any one time, in the CRAM.

​

Each colour in a palette is stored in 2 bytes, giving 16 bits to store red, green and blue values.  Each of those RGB values are given 3 bits to play with,  but they are encoded in a rather odd way...

​

EXERCISE:  Pause for a moment and make a trip over to Sega Retro...

​

https://segaretro.org/Sega_Mega_Drive/Palettes_and_CRAM

​

...and consider the section called Format.  If you choose to manually create your own colours then you can use "bit shifting" to encode each colour channel, and therefore you just need that page as a guide.

​

Once you have your 16-bit value, you can pop it in the CRAM using SGDK's VDP_setPaletteColor() function.  To make it easier to select one of the four palettes and one of its 16 colour entries, I've whipped up a quick helper function for you...

​

void setPaletteColour( u16 pal, u16 entry, u16 colour )
{
    u16 index = ( pal*16 ) + entry;

    VDP_setPaletteColor( index, colour );
}

So, by providing the first argument a value of 0-3( four palettes ) you can select the desired colour palette.  The second argument is the colour entry within that palette and so you need to provide a value of 0-15( 16 colours ).  The last argument will be the encoded colour value you wish to store.

Encoding the 16-bit colour

Although we will not need to manually encode our palettes( they will be loaded in from our bitmaps, in a later tutorial ), I will at least provide you with a function that returns an encoded 16-bit colour value...

u16 makeColour( u16 blue, u16 green, u16 red )
{
    u16 colour = 0;

    colour = blue << 9;
    colour = colour | (green << 5);
    colour = colour | (red << 1);

    return colour;
}

 

...where each of the given blue, green and red parameters must be a value of 0-7( 8 shades ). 

 

EXERCISE:  Take a peak at the contents of the VDP.   If you are using the correct version of the GENS emulator, then you can inspect the contents of the four colour palettes held in the VDP.  There appears to be a fifth, but I'm not sure what it represents.  Depending on which palette you chose for your text( in part 1 ), you will find SGDK uses the last colour in the selected palette. So, with the above makeColour() function you can provide any colour you desire for your text. 

 

To bring up the VDP dialog box in GENS, use CPU->DEBUG->GENESIS->VDP from the menu bar.  Remember the debug menu is only avaliable in the Kmod version of GENS.

​

With the colour palettes you can make some interesting graphical effects just by rotating the colour entries.  However, our main concern is knowing just enough to load in a bitmap image into our programs and display them on screen.  Each pixel in those images will refer to a colour in one of those palettes, so thats why its important to understand the palette first before attempting to load in an image...

​

In the next part we shall look at how to add a "resource" to our Megadrive programs.  If you haven't done this before then its basically adding a file( for example, a bitmap file ) in to our executable file.  So instead of two separate files you just have the executable.

​

Have fun!  ^_^

​

...

bottom of page