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

68 lines
3.8 KiB
Markdown

Chapter 26\
VGA Write Mode 3 {#Heading1}
-----------------
### The Write Mode That Grows on You {#Heading2}
Over the last three chapters, we've covered the VGA's write path from
stem to stern—with one exception. Thus far, we've only looked at how
writes work in write mode 0, the straightforward, workhorse mode in
which each byte that the CPU writes to display memory fans out across
the four planes. (Actually, we also took a quick look at write mode 1,
in which the latches are always copied unmodified, but since exactly the
same result can be achieved by setting the Bit Mask register to 0 in
write mode 0, write mode 1 is of little real significance.)
Write mode 0 is a very useful mode, but some of VGA's most interesting
capabilities involve the two write modes that we have yet to examine:
write mode 1, and, especially, write mode 3. We'll get to write mode 1
in the next chapter, but right now I want to focus on write mode 3,
which can be confusing at first, but turns out to be quite a bit more
powerful than one might initially think.
### A Mode Born in Strangeness {#Heading3}
Write mode 3 is strange indeed, and its use is not immediately obvious.
The first time I encountered write mode 3, I understood immediately how
it functioned, but could think of very few useful applications for it.
As time passed, and as I came to understand the atrocious performance
characteristics of **OUT** instructions, and the importance of text and
pattern drawing as well, write mode 3 grew considerably in my
estimation. In fact, my esteem for this mode ultimately reached the
point where in the last major chunk of 16-color graphics code I wrote,
write mode 3 was used more than write mode 0 overall, excluding simple
pixel copying. So write mode 3 is well worth using, but to use it you
must first understand it. Here's how it works.
In write mode 3, set/reset is automatically enabled for all four planes
(the Enable Set/Reset register is ignored). The CPU data byte is rotated
and then ANDed with the contents of the Bit Mask register, and the
result of this operation is used as the contents of the Bit Mask
register alone would normally be used. (If this is Greek to you, have a
look back at Chapters 23 through 25. There's no way to understand write
mode 3 without understanding the rest of the VGA's write data path
first.)
That's what write mode 3 does—but what is it *for?* It turns out that
write mode 3 is excellent for a surprisingly large number of purposes,
because it makes it possible to avoid the bane of VGA performance,
**OUT**s. Some uses for write mode 3 include lines, circles, and solid
and two-color pattern fills. Most importantly, write mode 3 is ideal for
transparent text; that is, it makes it possible to draw text in 16-color
graphics mode quickly without wiping out the background in the process.
(As we'll see at the end of this chapter, write mode 3 is potentially
terrific for opaque text—text drawn with the character box filled in
with a solid color—as well.)
Listing 26.1 is a modification of code I presented in Chapter 25. That
code used the data rotate and bit mask features of the VGA to draw
bit-mapped text in write mode 0. Listing 26.1 uses write mode 3 in place
of the bit mask to draw bit-mapped text, and in the process gains the
useful ability to preserve the background into which the text is being
drawn. Where the original text-drawing code drew the entire character
box for each character, with 0 bits in the font pattern causing a black
box to appear around each character, the code in Listing 26.1 affects
display memory only when 1 bits in the font pattern are drawn. As a
result, the characters appear to be painted into the background, rather
than over it. Another advantage of the code in Listing 26.1 is that the
characters can be drawn in any of the 16 available colors.