abrash-black-book/28-03.md
2014-01-04 15:15:18 +11:00

68 lines
3.5 KiB
Markdown

---
title: Michael Abrash's Graphics Programming Black Book, Special Edition
author: Michael Abrash
date: '1997-07-01'
isbn: '1576101746'
publisher: The Coriolis Group
category: 'Web and Software Development: Game Development,Web and Software Development:
Graphics and Multimedia Development'
chapter: '28'
pages: 530-531
---
By the way, the code in Listing 28.1 is intended only to illustrate read
mode 0, and is, in general, a poor way to perform animation, since it's
slow and tends to flicker. Later in this book, we'll take a look at some
far better VGA animation techniques.
As you'd expect, neither the read mode nor the setting of the Read Map
register affects CPU *writes* to VGA memory in any way.
> ![](images/i.jpg)
> An important point regarding reading VGA memory involves the VGA's
> latches. (Remember that each of the four latches stores a byte for one
> plane; on CPU writes, the latches can provide some or all of the data
> written to display memory, allowing fast copying and efficient pixel
> masking.) Whenever the CPU reads a given address in VGA memory, each of
> the four latches is loaded with the contents of the byte at that address
> in its respective plane. Even though the CPU only receives data from one
> plane in read mode 0, all four planes are always read, and the values
> read are stored in the latches. This is true in read mode 1 as well. In
> short, whenever the CPU reads VGA memory in any read mode, all four
> planes are read and all four latches are always loaded.
### Read Mode 1 {#Heading4}
Read mode 0 is the workhorse read mode, but it's got an annoying
limitation: Whenever you want to determine the color of a given pixel in
read mode 0, you have to perform four VGA memory reads, one for each
plane, and then interpret the four bytes you've read as eight 16-color
pixels. That's a lot of programming. The code is also likely to run
slowly, all the more so because a standard IBM VGA takes an average of
1.1 microseconds to complete each memory read, and read mode 0 requires
four reads in order to read the four planes, not to mention the even
greater amount of time taken by the `OUT`s required to switch between
the planes. (1.1 microseconds may not sound like much, but on a 66-MHz
486, it's 73 clock cycles! Local-bus VGAs can be a good deal faster, but
a read from the fastest local-bus adapter I've yet seen would still cost
in the neighborhood of 10 486/66 cycles.)
Read mode 1, also known as *color compare mode*, provides special
hardware assistance for determining whether a pixel is a given color.
With a single read mode 1 read, you can determine whether each of up to
eight pixels is a specific color, and you can even specify any or all
planes as "don't care" planes in the pixel color comparison.
Read mode 1 is selected by setting bit 3 of the Graphics Mode register
(Graphics Controller register 5) to 1. In its simplest form, read mode 1
compares the cross-plane value of each of the eight pixels at a given
address to the color value in bits 3-0 of the Color Compare register
(Graphics Controller register 2), and returns a 1 to the CPU in the bit
position of each pixel that matches the color in the Color Compare
register and a 0 for each pixel that does not match.
That's certainly interesting, but what's read mode 1 good for? One
obvious application is in implementing flood-fill algorithms, since read
mode 1 makes it easy to tell when a given byte contains a pixel of a
boundary color. Another application is in detecting on-screen object
collisions, as illustrated by the code in Listing 28.2.