Tutorial – 7 – C Programming in 6502 – Colour Setting for NES


Previously on writing NES files in CC65 compiler, we have introduced interacting with joysticks, accessing Video RAM etc. This tutorial will tell you the basics of setting colours for background & text by accessing the VRAM (Video RAM).

Since the NES FC has a limited RAM, we cannot map all the resources (Video RAM, Sound, Joysticks) to RAM, therefore, what it does is to map the address of a few ports (e.g., control and data). For example, the control for the first joystick is mapped to address 0x4016 and the second joystick is mapped to address 0x4017. [see herefor more]; the screen (32 x 30 characters) is mapped to 0x2000, the address is written to 0x2006 with first 8 bit written first and then the lower 8 bits, 0x2007 has the value to put. [see more here]

1
2
3
4
5
// specify the address is $2100
address(0x2006) = 0x21;
address(0x2006) = 0x01;
// put character 'A' on $2100
address(0x2007) = 65;
// specify the address is $2100
address(0x2006) = 0x21;
address(0x2006) = 0x01;
// put character 'A' on $2100
address(0x2007) = 65;

The FC has a 2K RAM, 6502 CPU and a graphic resolution 256 X 240 (32 X 30 blocks of 8 X 8). Maximum 62 colours can be supported by only 25 colours can be shown per screen.

The 62 colours are represented by the following indices.

colortable Tutorial - 7 - C Programming in 6502 - Colour Setting for NES 6502 8 bit c / c++ code implementation Nintendo Entertainment System programming languages

If you use the modern NES emulator (e.g. Virtual NES), under the tools – view, you can open up the Palette Viewer, which is like this.

palette Tutorial - 7 - C Programming in 6502 - Colour Setting for NES 6502 8 bit c / c++ code implementation Nintendo Entertainment System programming languages

The yellow line is marked to make it clear. The table actually consists of two rows. The first row is the Palette for Background, and the second row is the Palette for Spirits (a block of a character image, e.g., Super mario).

Each palette consists of 4 x 4 = 16 colours, so there are 32 colours maximum. Each groups has four colours (0, 1, 2 and 3). The first (zero index) is background, and it is synchronised in all groups, so that will give you 32 – 8 + 1 = 25 colours maximum.

Let’s verify this step by step:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "conio.h"
 
#define addr(_addr)     (*(unsigned char*) (_addr))
 
void main() {
    addr(0x2006) = 0x3f;
    addr(0x2006) = 0x00;
    addr(0x2007) = 5;  // put color 5 to address 0x3f00, which is the first in the above table
 
    addr(0x2006) = 0x3f; 
    addr(0x2006) = 0x03;
    addr(0x2007) = 2;  // put color 2 to address 0x3f0x, which is the third in the above table.
 
    cputc('A');
    for(;;);
}
#include "conio.h"

#define addr(_addr)     (*(unsigned char*) (_addr))

void main() {
	addr(0x2006) = 0x3f;
	addr(0x2006) = 0x00;
	addr(0x2007) = 5;  // put color 5 to address 0x3f00, which is the first in the above table

	addr(0x2006) = 0x3f; 
	addr(0x2006) = 0x03;
	addr(0x2007) = 2;  // put color 2 to address 0x3f0x, which is the third in the above table.

	cputc('A');
	for(;;);
}

The above C code first changes the first group – index 0 color to 5, which controls the background. And then changes the first group – index 2 color to 2, which controls the text color. And outputs a character ‘A’ at last.

colortest Tutorial - 7 - C Programming in 6502 - Colour Setting for NES 6502 8 bit c / c++ code implementation Nintendo Entertainment System programming languages

Let us view the status of the Palette again:

palette2 Tutorial - 7 - C Programming in 6502 - Colour Setting for NES 6502 8 bit c / c++ code implementation Nintendo Entertainment System programming languages

The first thing to notice is that all the first color in all groups (index 0) are updated to colour 5 (synchronised).  The text color (blue, index 3) is updated.

Therefore, we can use  the following function to change a particular colour in the Palette table.

1
2
3
4
5
6
7
8
9
10
/*
   n - Palette group number
   m - Index number (0 to 3)
*/
void set_color(uint8 n, uint8 m, uint8 color)
{
    address(0x2006) = 0x3f;  
    address(0x2006) = 0x00 + n * 4 + m;   
    address(0x2007) = color;   
}
/*
   n - Palette group number
   m - Index number (0 to 3)
*/
void set_color(uint8 n, uint8 m, uint8 color)
{
    address(0x2006) = 0x3f;  
    address(0x2006) = 0x00 + n * 4 + m;   
    address(0x2007) = color;   
}

To be continued.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
704 words
Last Post: C Programming in 6502 - CC65 Compiler Does Not Support Float Type
Next Post: Coding Exercise - Timus Online Judge - 1264. Workdays - C++ solution

The Permanent URL is: Tutorial – 7 – C Programming in 6502 – Colour Setting for NES

Leave a Reply