|Table of Contents
At a book signing for my book Zen of Code Optimization, an attractive young woman came up to me, holding my book, and said, Youre Michael Abrash, arent 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 didnt 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 didnt 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 320×256-color mode with some very special properties, as well 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 didnt; instead, I spread the word as broadly as I could in my column in Dr. Dobbs 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 thats a bad name; its short, catchy, and easy to remember, and it befits the mystery status of this mode, which was omitted entirely from IBMs 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; its now a firmly entrenched part of the computer lexicon, and how often do any of us get a chance to do that? And thats not to mention all the excellent games that would not have been as good without Mode X.
So, in the end, Im 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.
Consider the strange case of the VGAs 320×256-color modeMode Xwhich is undeniably complex to program and isnt even documented by IBMbut which is, nonetheless, perhaps the single best mode the VGA has to offer, especially for animation.
Weve seen the VGAs undocumented 256-color modes, in Chapters 31 and 32, but now its time to delve into the wonders of Mode X itself. (Most of the performance tips Ill 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 screens 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 ratioa slower and considerably more complicated process.) In contrast, mode 13H, the only documented 256-color mode, provides a nonsquare 320×200 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 VGAs high-resolution 640×480 16-color mode.
Third, Mode X allows the VGAs 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 VGAs latches; together, the latches and the off-screen memory allow images to be copied to the screen four pixels at a time.
Theres a sixth feature of Mode X thats not so terrific: Its 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 youve 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. (Its 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 its the ideal organization for display memory because its so straightforward to program. Here, however, the complexity of Mode X is opportunityopportunity 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, youre missing the boat if youre 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 Id like to acknowledge the influence of his code on the mode set routine presented in this chapter.
|Table of Contents