How to : use the VGA card (320x200x256 colors)

Written by : Peter Quiring (Jan 11/97)

Well the VGA card has become a very popular standard. So in this tutorial I'm going to teach you how to do stuff like, getting into VGA mode (320x200 256 colours), setting the palette without snow. This is just a simply tutorial.

Setting up VGA mode

To setup VGA mode all you need to do is make one simple BIOS call. Which is like this:
mov ax,13h  ;this is mode number
int 10h     ;this calls the video BIOS
After this call you are in 320x200 256 color mode. After your grafix program quits you can simply load ax with 3 and then call int 10h again to return to text mode (80x25). After setting up the grafix mode the following is setup. Each pixel in VGA mode is 1 byte (hence the 256 colors), but each of these numbers represents an index into a palette held on the video card which will describe the exact red, green and blue intesity of that color. The total amount of RAM needed for 320x200 is 64000 bytes so at segment 0a000h for 64000 bytes represent the monitor display. To access any pixel you simply read or write to the following address using this formula:
segment = 0a000h
offset = y*320+x (where the top left corner is 0,0)
And if you are using a linear address from 0:0 then the formula would be:
linear addr = 0a000h*10h + y*320+x
Once in the grafix mode you can tweak the VGA registers to setup funky mode such as 320x400 or 360x400 but this will be all explained in the modeX tutorial (coming soon).

Setting and reading the palette

To set or get the palette you simply write the index number to port 3c8h and then read or write three bytes from port 3c9h. You can also continue to read or write from port 3c9h which will access the next index (incrementally). So lets say you had a complete palette loading into memory at the location PALETTE the following code would be using to put it into the VGA card:
  mov si,offset PALETTE
  mov dx,3c8h
  xor al,al  ;clear al
  out dx,al
  inc dx
  mov cx,768
  rep outsb
The PALETTE should be a array of 256 color each 3 bytes for the red, green and blue values. Each value can only be from 0-63 (6 bits) the other 2 bits are ignored. So the entire PALETTE will be 768 bytes.
When the VGA card sends the current image to the monitor it reads the image from it's RAM and then looks up the indexs from it's palette. If you change the palette as it is doing this most cards will through junk on the screen, called 'snow'. To avoid this you can determine when the VGA card is performing a vertical retrace, which is when it is moving the lasers in the CRT tube back upto the top of the screen. Once this is 'properly' detected you can dump the entire palette to the VGA quickly. Older dox say that there is not enough time during this retrace to update the entire palette, but they are old and video cards are fast enough now. Here is the 'proper' way to detect the beginning of a retrace:
  mov dx,3dah
  in al,dx
  test al,8
  jnz L1
  in al,dx
  test al,8
  jz L2
  ;retrace has just begun
This 'proper' method is hardly used because so many people use other methods which are 1/2 as good as this is. Many games out there still don't use this technique and you can quickly spot them. (Ultima 8)
Basically bit 8 at port 3dah is high (1) while the VGA card is retracing. Many programmer thought it was good enough just to make sure this bit was 1 and then reprogram the palette. But that's wrong cause the retrace could be just finishing. The best way is to wait till this bit is 0 and then wait till it is 1. Although this may be a little slower it pays to have a nice clean image in your games, espicially if you use the pallette and do palette rotation to make flames glow and such.

Well that's it. Hope this helps. TTYL.
Copyright © 1995-2005 Nexus Systems Privacy Logo