|Dͻ
|D |5The Happy Hacker |D
|Dͼ

^CIBM-PC VIDEO PRIMER
^Cby:  Joel Ellis Rea

^CPart Four:

   Last time we discussed the basics behind the operation of the IBM Monochrome 
Display Adapter (MDA) and its compatibles.  This installment will discuss the 
IBM Color Graphic Adapter (CGA), the matching IBM Color Display RGBI Monitor 
and compatibles of both. 

   Two very obvious differences between the CGA and the MDA are, of course, 
that the CGA has color and graphics capabilities, while the MDA does not.  
There are, of course, many more differences than that.  First let's summarize 
what we learned last time about the operation of the MDA: 

   The informtion needed to generate the text display resides in two areas of 
memory:  a 4K block of RAM accessible to the CPU (called the DISPLAY BUFFER) 
which holds the CHARACTER CODES and their associated ATTRIBUTES, and an 8K 
block of ROM accessible only to the MDA board itself (called the CHARACTER 
GENERATOR) which holds the shapes of all 256 available characters, each mapped 
into a 9x14 grid of pixels. 

   The CGA differs in that the 4K Display Buffer RAM is replaced by a 16K 
Display Buffer.  This is enough to hold four separate 80x25 text screen images, 
or eight separate 40x25 text screen images, or one graphics screen image.  
Also, the CGA needs to be able to generate a signal which more closely 
approximates that used by TV sets in the United States, called the NTSC 
standard.  Thus, instead of 350 scan lines, it only has 200.  That means only 
eight scan lines per text line, rather than fourteen.  Since the maximum 
horizontal resolution is reduced from 720 pixels to 640, the width of each 
character is only 640/80, or eight pixels.  So the Character Generator ROM now 
forms each character in an 8x8 grid of pixels instead of 9x14.  This is the 
main reason why text quality is so much better on the MDA than on the CGA -- 
the characters themselves have 49% less resolution on the CGA. 

   The attributes have been changed.  While the MDA used only a few bits of the
Attributes byte for each character to get such effects as Bold, Inverse, 
Underscore, Flashing or even Invisible, the CGA uses all eight bits.  It splits 
this byte into two NYBBLES (a general term for four bits, or a half-byte).  Each
nybble can hold a number from 0 to 15, for a total of 16 values.  These 
generally represent the 16 colors that the CGA can display.  The first nybble 
controls the color of the "off" pixels in the associated character, while the 
second nybble controls the color of the "on" pixels.  Each nybble has 4 bits, 
with numeric values of 1, 2, 4 and 8.  These control Blue, Green, Red and 
Intensity, respectively.  So if you want a bright (high-Intensity) Yellow (a 
combination of Red and Green), you would add Green (2), Red (4) and Intensity 
(8) together. So the color code for Bright Yellow is 2+4+8, or 14.

   A special flag bit in a CGA register allows the video circuitry to give up 
half the background colors by making the Background Intensity bit instead 
control Flashing.  In standard IBM text modes this flag bit is always set, so 
you can have sixteen foreground colors on eight (dim) background colors, either 
steady or flashing.  You may also pick one of sixteen colors for the border.  
This nybble is stored in a special register on the CGA itself.  Underscore is 
not possible, since there is neither place to have an attribute code for it, nor
space in the mere 8x8 character grid to put the physical underscore.  Inverse is
done by swapping the foreground and background attribute nybbles.  Invisible is 
done by setting both nybbles to the value. 

   The CGA also has some modes that the MDA just plain can't do.  By cutting 
its speed in half, the CGA can display 40 columns of double-wide text.  Each 
screen in this mode needs only 40x25, or 1,000, bytes for the characters 
themselves, plus another 1,000 for the attribute bytes.  This means the active 
display buffer is only 2,000 bytes.  Advantages of this mode include wider text 
that is easier to read from a distance (or for young children), plus a 
generally faster display since the CPU only has to update half as much display 
buffer to generate a display.  There is another advantage which I will get to 
later. 

   The most important modes that the CGA has over the MDA are two PIXEL-MAPPED 
GRAPHICS MODES.  In these modes, the Character Generator ROM is not used.  The 
display is NOT divided into characters and attributes.  Instead, it is divided 
into 200 scan lines of either 320 or 640 pixels, depending on the mode.  If it 
is 640 pixels, each pixel is one bit.  If it is 320 pixels, each pixel is two 
bits, so both modes require 200 rows of 640 pixels, 640/8 = 80 bytes per row, 
200x8 = 16,000 bytes for the whole display.  Once again, the next largest size 
that computer RAM comes in is 16,384 bytes, or 16K.  That is why the CGA needs 
a 16K display buffer. 

   In the 640-pixel ("high resolution", or "hi-res") mode, each "1" bit 
displays in the foreground color, held in a special register on the CGA itself.
Each "0" bit displays in black.  So, you can have pixels in any color on a 
black background.  However, all "on" pixels must be the SAME foreground color 
at any one time.  You may not have more than one foreground color on the 
screen.  In the 320-pixel ("medium resolution", or "medium-res") mode, each 
pixel consists of TWO bits.  There are four possible combinations of two bits:  
00 (0), 01 (1), 10 (2) and 11 (3).  Depending on the current "palette" 
(selected by bits in another special register on the CGA itself), colors #1, #2 
and #3 are set to certain fixed colors, while color #0 (the "background" color) 
may be any of the 16 CGA colors, as selected by the same special register which 
holds text border colors or hi-res foreground colors.  There are 6 palettes, as 
follows: 

^1 PALETTE                COLOR #1 (01)   COLOR #2 (10)   COLOR #3 (11)

^1 Dim "Color" #1:         Dim Red         Dim Green       Dim Yellow (Brown)
^1 Dim "Color" #2:         Dim Magenta     Dim Cyan        Dim White (Light Gray)
^1 Dim "B&W":              Dim Red         Dim Cyan        Dim White

^1 Bright "Color" #1:      Bright Red      Bright Green    Yellow (Bright)
^1 Bright "Color" #2:      Bright Magenta  Bright Cyan     Bright White
^1 Bright "B&W":           Bright Red      Bright Cyan     Bright White

   The CGA sends its information to the monitor display in much the same way as 
the MDA does.  The electron beam scans the screen from the upper left corner, 
across lines from left to right, each line getting slightly lower, until it 
reaches the lower-right corner.  The beam is blanked between lines, and again 
at the end of the screen.  Whereas the MDA does complete scans at 50Hz, the CGA 
does them at 60Hz, the standard United States AC voltage frequency.  Whereas the
MDA scans individual lines at 18.432 kHz, the CGA does it at 15.75 kHz.  

   It is important to notice that, in 80x25 text mode, the CGA has to access 
more information to generate a display.  It needs to access the Display Buffer 
RAM for character codes and attributes, as well as the Character Generator ROM 
for the character shapes themselves, much like the MDA.  But instead of simply 
determining the off/on status of one electron beam, it must control three:  
Red, Green and Blue, plus one "pseudo-gun", Intensity, usually implemented by 
turning all three guns on at a low intensity and adding the resulting dark Gray 
into whatever color is being displayed, thus producing Bright colors.

   The CGA simply cannot keep up with all of this in the time allotted it on the
bus, so it must also spend some of the time in which the CPU is supposed to have
the bus.  This means that when the CPU is updating or reading the display 
buffer, the CGA may be trying to access it at the same time.  When this occurs, 
the CPU has priority (of course -- otherwise the system would crash) and the CGA
suddenly finds itself with totally erroneous information.  It goes ahead and 
displays this as "digital snow."  This problem is so severe on the CGA that the 
IBM Video BIOS routines would rather blank the screen during scrolls (scrolling 
is a very memory-intensive operation!) rather than allow the "snow" to be 
visible.  They figure that a flickering, blinking screen is preferable to the 
"blizzard."  Let me assure you, they are right.

   Most CGA "clone" cards use faster circuitry than does the IBM CGA, thus 
eliminating this problem.  Also, the 40x25 text mode is immune since it is 
operating at half the speed and can thus get its job done during the times that 
the CPU is off the bus.  The graphics modes are also immune, since the CGA 
doesn't have to do as much.  (It doesn't have to access the Character Generator 
ROM, for instance.)  There are third-party CON-driver replacements which write 
text to the screen and scroll only during the Vertical and/or Horizontal 
Blanking intervals, thus reducing flicker considerably and eliminating the need 
for "blinking" while scrolling. 

   Next time we will discuss a particular chip which is instrumental in the 
workings of both the MDA and the CGA (but not the EGA).  This chip is called 
the 6845 Cathode-Ray Tube Controller, or CRTC.  By playing around with it, you 
can get amazing effects.  See you next issue.
