Lets talk VGA, Part 1

When I say VGA, most people these days will think about that funny blue connector that used to be so common for connecting computers to display devices.

That thing on the back of my monitor next to the HDMI port?

Why would I wanna talk about that you might wonder? Well I won’t really be talking about that. What I’m talking about is the Video Graphics Array, a graphics card standard introduced in 1987 by IBM, the connector pictured above was only one part of that standard. VGA was important, really important. Before VGA, PCs could only display 16 colours on screen at a time and most of the time programmers and artists had no choice what those colours were. With VGA you could have 256 colours on screen at the same time! Not only that but you could also pick those colours from a massive set of over 200 thousand colours, this was truly groundbreaking. Since the a VGA card would have been the most common type of thing to see in PCs in the era Chuck Jones targets (around 1992), this is what I have chosen to support. Lets get down to the nitty gritty then shall we?

VGA Graphics Modes

For the uninitiated graphics programming for DOS can seem quite daunting, there’s no API, you just write data directly to the address of the video memory as if its just one big array. The mode you choose determines how that data is interpreted. Most games that used VGA used its 320×200 pixel 256 colour mode (mode 13h). Mode 13h is really fast and dead easy to program, one byte is one pixel and the next pixel is at the address of that pixel plus one. You might wonder then why Chuck Jones doesn’t use it, in fact it doesn’t use any documented mode at all. Chuck Jones uses what is commonly known a Mode X, popularized by the genius programmer and talented writer Michael Abrash (he describes how to use it here).

The Planet Orlokis in glorius 320×240

I don’t use mode 13h because it has a couple drawbacks, firstly the pixel aren’t square so drawing art using a modern image editor would be tough. The second thing is that the VGA card has a minimum 256k of ram but 13h only gives you 64k of that (just enough for 320×200 pixels) meaning that the pixels you write to are always the same ones that are being displayed so the user is gonna see terrible tearing all the time, unless of course you manage to make all your changes to the screen during the vertical retrace (the time it takes for an electron beam in a CRT to go back to the top at the end of a frame, retained in LCDs for compatibility). In practice most games managed this alright but there was no way to guarantee that tearing would not occur. Mode X allows a technique called page flipping where you can write the next frame to a part of the video memory that is off screen and then point the graphics card to that at the end of the current frame. Mode X also gives a resolution of 320×240 which is slightly higher and has square pixels.

Mode X isn’t perfect however, to understand why you’ll need to understand why mode 13h only gives you access to 64k of video memory. The VGA card organizes it’s memory 256k of memory into 4 64k planes, each pixel on the plane having a 16bit address (216 = 65536 = 64k). In order for mode X to give you 320×240 it actually has to put the screen across multiple planes, meaning that the next pixel isn’t the next byte in memory, its actually on the next plane! This complicates things more than a little bit. You actually need to not just write to the pixel you want (actually pixel address / 4) but actually select which planes you want to write to. Notice how I said planes and not plane? Well the interesting thing is that you can actually write to multiple planes at the same time. I’ll talk more about this and how this has some interesting performance characteristics when I talk about my code in Part 2.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s