Well. Imagine that you have to paint a picture using oil paints. Imagine that you have your canvas, your brush and your talent, and also you have a palette where there is place for 256 different colours. Depending on what you are gonna paint, you select carefully each of the 256 colours.
That's how VGA palette works. Actually, there are 262144 different colours, but only 256 can be displayed at once on screen. That selection of 256 colours among the 262144 possible is called "palette". To choose a colour, you just especify its three luminic components, red, green and blue. Each one of those components is specified giving a 6 bits number, that is, a number which ranges from 0 to 63 (64 different intensities). That's why we have 64*64*64=262144 possible colours. To associate a colour to one of our slots, we can use the (slow) PALETTE command...
PALETTE slot, red + 256*green + 65536*blue
Or we can use OUTs to the DAC ports of the VGA (fast)...
OUT &H3C8, slot
OUT &H3C9, red
OUT &H3C9, green
OUT &H3C9, blue
When you start mode 13h (after a SCREEN 13 command) the DAC registers (that is how those slots are called) are set to defaults values. That arrangement is (IMHO) really ugly and unusable for real GFX. The trick is to choose a better palette, and make all your graphics using it. Programs like neopaint help you converting images from one palette to another.
What I do is beginning with a BLACK palette (in Deluxe Paint, the proggie I use to do my GFX) and I create the colours within the graphics editor when I need them. For example, if I begin doing some grass tiles, I create some green shades. Then, when I need drawing rocks, I create gray shades... That way, your palette will be just like you want. Then, in your program, you just have to find a way to load up the palette and alter the DAC registers as I mentioned above.
How does it work (the OUT stuff)
Let me explain what are "ports". Ports are 65536 kinda mailboxes that your PC uses to communicate with some devices (keyboard, loudspeaker or VGA, among others). In some operations, you just put values in some ports waiting for the device to read them and act consequently (OUT port, value). In other cases you just read a value that some device has located in a port ( value = INP(port) ).
Ports &H3C7, &H3C8 and &H3C9 are use to communicate with VGA's DAC registers. If the VGA finds a value in &H3C7, it will return the colour values in &H3C9 (like some kind of queue: when you read red, it will put green, and so on). When the VGA finds a value in &H3C8, it just waits for you to give it the red, green and blue thru &H3C9.
My 4 OUTs just say:
OUT &H3C8, slot ' PC says: Hey, VGA, I wanna change this slot in the DAC registers
' The VGA thinks: Ącool! Let me wait for 3 values. First "red".
OUT &H3C9, red ' PC says: Here's the red value.
' The VGA thinks: nice. Now I am waiting for "green".
OUT &H3C9, green ' PC says: Here's the green value.
' The VGA thinks: well, now the last one, "blue".
OUT &H3C9, blue ' PC says: And here's blue.
' The VGA thinks: well done, now I have red, green and blue.
' Time to change the DAC registers :)
I know it looks like Sesame Street, but I liked how it looked like :D
Cheerz :)= Nathe.