Setting the 8 bit palette from Linux


Thanks to RobB for figuring out how to set the 8 bit palette.

There are two palette registers, a palette address register (MLC_STL_PALLT_A) and a palette data register (MLC_STL_PALLT_D). These are used to access a table of 512 16 bit values. Word 0 is the green and blue values for colour 0, word 1 is the red value for colour 0. Word 2 is the green and blue values for colour 1 and so on:

word high byte low byte
0 green value, colour 0 blue value, colour 0
1 0 red value, colour 0
2 green value, colour 1 blue value, colour 1
3 0 red value, colour 1
... ... ...

The procedure for writing an entry to a table is to write the entry number to the address register before writing the data for that entry to the data register. When data is written the address is incremented automatically so the whole table can be filled in one go by writing 0 to the address register, then writing all 512 values to the data register in a loop.

Here's an example of how to do this from Linux.

        volatile uint8 palette_red[255];
        volatile uint8 palette_green[255];
        volatile uint8 palette_blue[255];

	memdev = open("/dev/mem", O_RDWR);
	gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);

	gp2x_memregs[0x28da>>1]=0x02ab; // set 8 bit mode

	gp2x_memregs[0x2958>>1]=0;  // palette table address 0
	for(int i=0; i<=255; i++) {

Personal tools