abrash-black-book/47-01.md
2013-12-30 20:26:41 +11:00

120 lines
6.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Chapter 47\
Mode X: 256-Color VGA Magic {#Heading1}
----------------------------
### Introducing the VGA's Undocumented "Animation-Optimal" Mode {#Heading2}
At a book signing for my book *Zen of Code Optimization*, an attractive
young woman came up to me, holding my book, and said, "You're Michael
Abrash, aren't you?" I confessed that I was, prepared to respond in an
appropriately modest yet proud way to the compliments I was sure would
follow. (It was my own book signing, after all.) It didn't work out
quite that way, though. The first thing out of her mouth was:
"Mode X' is a stupid name for a graphics mode." As my jaw started to
drop, she added, "And you didn't invent the mode, either. My husband did
it before you did."
And they say there are no groupies in programming!
Well. I never claimed that I invented the mode (which is a 320x256-color
mode with some very special properties, as we'll see shortly). I did
discover it independently, but so did other people in the game business,
some of them no doubt before I did. The difference is that all those
other people held onto this powerful mode as a trade secret, while I
didn't; instead, I spread the word as broadly as I could in my column in
*Dr. Dobb's Journal,* on the theory that the more people knew about this
mode, the more valuable it would be. And I succeeded, as evidenced by
the fact that this now widely-used mode is universally known by the name
I gave it in *DDJ,* "Mode X." Neither do I think that's a bad name; it's
short, catchy, and easy to remember, and it befits the mystery status of
this mode, which was omitted entirely from IBM's documentation of the
VGA.
In fact, when all is said and done, Mode X is one of my favorite
accomplishments. I remember reading that Charles Schultz, creator of
"Peanuts," was particularly proud of having introduced the phrase
"security blanket" to the English language. I feel much the same way
about Mode X; it's now a firmly entrenched part of the computer lexicon,
and how often do any of us get a chance to do that? And that's not to
mention all the excellent games that would not have been as good without
Mode X.
So, in the end, I'm thoroughly pleased with Mode X; the world is a
better place for it, even if it did cost me my one potential female fan.
(Contrary to popular belief, the lives of computer columnists and rock
stars are not, repeat, *not*, all that similar.) This and the following
two chapters are based on the *DDJ* columns that started it all back in
1991, three columns that generated a tremendous amount of interest and
spawned a ton of games, and about which I still regularly get letters
and e-mail. Ladies and gentlemen, I give you...Mode X.
### What Makes Mode X Special? {#Heading3}
Consider the strange case of the VGA's 320x256-color mode—Mode X—which
is undeniably complex to program and isn't even documented by IBM—but
which is, nonetheless, perhaps the single best mode the VGA has to
offer, especially for animation.
We've seen the VGA's undocumented 256-color modes, in Chapters 31 and
32, but now it's time to delve into the wonders of Mode X itself. (Most
of the performance tips I'll discuss for this mode also apply to the
other non-standard 256-color modes, however.) Five features set Mode X
apart from other VGA modes. First, it has a 1:1 aspect ratio, resulting
in equal pixel spacing horizontally and vertically (that is, square
pixels). Square pixels make for the most attractive displays, and avoid
considerable programming effort that would otherwise be necessary to
adjust graphics primitives and images to match the screen's pixel
spacing. (For example, with square pixels, a circle can be drawn as a
circle; otherwise, it must be drawn as an ellipse that corrects for the
aspect ratio—a slower and considerably more complicated process.) In
contrast, mode 13H, the only documented 256-color mode, provides a
nonsquare 320x200 resolution.
Second, Mode X allows page flipping, a prerequisite for the smoothest
possible animation. Mode 13H does not allow page flipping, nor does mode
12H, the VGA's high-resolution 640x480 16-color mode.
Third, Mode X allows the VGA's plane-oriented hardware to be used to
process pixels in parallel, improving performance by up to four times
over mode 13H.
Fourth, like mode 13H but unlike all other VGA modes, Mode X is a
byte-per-pixel mode (each pixel is controlled by one byte in display
memory), eliminating the slow read-before-write and bit-masking
operations often required in 16-color modes, where each byte of display
memory represents more than a single pixel. In addition to cutting the
number of memory accesses in half, this is important because the
486/Pentium write FIFO and the memory caching schemes used by many VGA
clones speed up writes more than reads.
Fifth, unlike mode 13H, Mode X has plenty of offscreen memory free for
image storage. This is particularly effective in conjunction with the
use of the VGA's latches; together, the latches and the off-screen
memory allow images to be copied to the screen four pixels at a time.
There's a sixth feature of Mode X that's *not* so terrific: It's hard to
program efficiently. As Chapters 23 through 30 of this book
demonstrates, 16-color VGA programming can be demanding. Mode X is often
as demanding as 16-color programming, and operates by a set of rules
that turns everything you've learned in 16-color mode sideways.
Programming Mode X is nothing like programming the nice, flat bitmap of
mode 13H, or, for that matter, the flat, linear (albeit banked) bitmap
used by 256-color SuperVGA modes. (I't's important to remember that Mode
X works on *all* VGAs, not just SuperVGAs.) Many programmers I talk to
love the flat bitmap model, and think that it's the ideal organization
for display memory because it's so straightforward to program. Here,
however, the complexity of Mode X is opportunity—opportunity for the
best combination of performance and appearance the VGA has to offer. If
you do 256-color programming, and especially if you use animation,
you're missing the boat if you're not using Mode X.
Although some developers have taken advantage of Mode X, its use is
certainly not universal, being entirely undocumented; only an
experienced VGA programmer would have the slightest inkling that it even
exists, and figuring out how to make it perform beyond the write
pixel/read pixel level is no mean feat. Little other than my *DDJ*
columns has been published about it, although John Bridges has widely
distributed his code for a number of undocumented 256-color resolutions,
and I'd like to acknowledge the influence of his code on the mode set
routine presented in this chapter.