Monday, August 23, 2010

Plotting Pixels (In Theory)

Start at the Beginning
Table of Contents

The VGA stores the contents of the screen in memory in segment 0xA0000. In theory this means we could just write directly into 0xA0000 to draw on the screen as easily as by assigning variables.

...What exactly segment 0xA0000 is, unfortunately, can only be answered by explaining the x86 architecture's archaic and obsolete 16-bit memory layout. By default, our compiler inserts code into our program that, at start up, lets us use a much more modern, 32-bit memory layout introduced with the 386 processor. This has many perks, but unfortunately it also makes difficult to write directly into video memory the way we could in 16-bit mode.

There are a few solutions. The first is to take advantage of a BIOS function that plots a pixel, much in the same way that we switched video modes before. Here's the code:

(click here for slowpix.c)

The only problem with this is that it's slow. Plotting pixels is something we'll be doing a lot of, so our pixel plotting routine needs to be as fast as possible. This solution just won't fly.

Another approach is to use a number of tricks our compiler provides to write directly into video memory, as hard as that is in the 32-bit world. This introduces a lot of complexity I'd like to avoid - because it's hard to understand, and because I don't want to have to explain all of it!

Instead, we'll be using a technique called double buffering.

No comments:

Post a Comment