Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  Text and graphics modes use the same color-decoding circuitry, but differ
  in the way they store the color attribute data in the video buffer. In
  text modes, no matter what video subsystem you use, the foreground and
  background colors of each character are specified by two 4-bit fields in a
  single attribute byte. (See Figure 4-7.) Together, the foreground and
  background attributes describe all of a character's pixels: All foreground
  pixels are displayed with the character's foreground attribute, and all
  background pixels assume the background attribute.

        Bit
  7 6 5 4 3 2 1 0          Use
  --------------------------------------------------------------------------
  1 . . . . . . .          Blinking of foreground character or intensity
                           component of background color
  . 1 . . . . . .          Red component of background color
  . . 1 . . . . .          Green component of background color
  . . . 1 . . . .          Blue component of background color
  . . . . 1 . . .          Intensity component of foreground color
  . . . . . 1 . .          Red component of foreground color
  . . . . . . 1 .          Green component of foreground color
  . . . . . . . 1          Blue component of foreground color
  --------------------------------------------------------------------------

  Figure 4-7.  The coding of the color attribute byte.

  In graphics modes, each pixel's attribute is determined by the contents of
  a bit field in the video buffer. The size and format of a pixel's bit
  field depend on the video mode: The smallest bit fields are only 1 bit
  wide (as in 640 x 200, 2-color mode), and the largest bit fields are 8
  bits wide (as in 320 x 200, 256-color mode).

  The reason for having both text and graphics modes becomes clear if you
  think about how much data it takes to describe the pixels on the screen.
  In graphics modes, you need between 1 and 8 bits of data in the video
  buffer for every pixel you display. In 640 x 350, 16-color mode, for
  instance, with 4 bits per pixel, you need 640 x 350 x 4 . 8 (112,000)
  bytes to represent one screenful of video data. But if you display 25 rows
  of 80 characters in a text mode with the same resolution, you need only 80
  x 25 x 2, or 4000, bytes.

  The tradeoff is clear: Text modes consume less memory and require less
  data manipulation than do graphics modes--but you can manipulate each
  pixel independently in graphics modes, as opposed to manipulating entire
  characters in text modes.

  Setting color in text modes

  Let's take a closer look at how you control colors in text modes. (We'll
  get back to graphics modes later in this chapter.) In text modes, each
  character position on the display screen is controlled by a pair of
  adjacent bytes in the video buffer. The first byte contains the ASCII code
  for the character that will be displayed. (See Appendix C for a chart of
  characters.) The second byte is the character's attribute byte. It
  controls how the character will appear, that is, its colors, brightness
  (intensity), and blinking.

  We've already mentioned two attributes that affect a character's
  appearance: color and intensity (brightness). You can assign several other
  attributes to text characters, depending on which video subsystem you're
  using. With all IBM video subsystems, text characters can blink. On
  monochrome-capable subsystems (the MDA, EGA, and VGA), characters can also
  be underlined. Also, on some non-IBM subsystems like the Hercules Graphics
  Card Plus, characters can have attributes such as overstrike and boldface.

  In all cases, you assign these alternate attributes by using the same
  4-bit attributes that specify color. A case in point is the blinking
  attribute. Character blinking is controlled by setting a bit in a special
  register in the video subsystem. (On the CGA, for example, this
  enable-blink bit is bit 5 of the 8-bit register mapped at input/output
  port 3D8H.) When this bit is set to 1, the high-order bit of each
  character's attribute byte is not interpreted as part of the character's
  background color specification. Instead, this bit indicates whether the
  character should blink.

  If you have a CGA, watch what happens when you run the following BASIC
  program:

  10 DEF SEG = &HB800             ' point to start of video buffer
  20 POKE 0,ASC("A")              ' store the ASCII code for A in the buffer
  30 POKE 1,&H97                  ' foreground attribute = 7 (white)
                                  ' background attribute = 9 (intense blue)

  You'll see a blinking white letter A on a blue background. If you add the
  following statement to the program, you'll clear the enable-blink bit and
  cause the CGA to interpret the background attribute as intense blue:

  40 OUT &H3D8,&H09               ' clear the "enable-blink" bit

  The default attribute used by DOS and BASIC is 07H, normal white (7) on
  black (0), without blinking, but you can use any combination of 4-bit
  foreground and background attributes for each character displayed in a
  text mode. If you exchange a character's foreground and background
  attributes, the character is displayed in "reverse video." If the
  foreground and background attributes are the same, the character is
  "invisible."

  Setting attributes in the monochrome mode

  The monochrome mode (mode 7) used by the Monochrome Display Adapter has a
  limited selection of attributes that take the place of color. Like the
  CGA, the MDA uses 4-bit foreground and background attributes, but their
  values are interpreted differently by the MDA attribute decoding
  circuitry.

  Only certain combinations of foreground and background attributes are
  recognized by the MDA. (See Figure 4-8.) Other useful combinations, like
  "invisible" (white-on-white) or a reverse-video/underlined combination,
  aren't supported by the hardware.

  Like the CGA, the MDA has an enable-blink bit that determines whether the
  high-order bit of each character's attribute byte controls blinking or the
  intensity of the background attribute. On the MDA, the enable-blink bit is
  bit 5 of the register at port 3B8H. As on the CGA, the enable-blink bit is
  set by the ROM BIOS when it establishes monochrome text mode 7, so you
  must explicitly clear this bit if you want to disable blinking and display
  characters with intensified background.

  With the EGA, MCGA, and VGA, text-mode attributes work the same as with
  the MDA and CGA. Although the enable-blink bit is not in the same hardware
  register in the newer subsystems, the ROM BIOS offers a service through
  interrupt 10H that toggles the bit on an EGA, MCGA, or VGA. (See Chapter
  9, page 178 for more information about this service.)


  Attribute  Description
  --------------------------------------------------------------------------
  00H        Nondisplayed

  01H        Underlined

  07H        Normal (white on black)

  09H        High-intensity underlined

  0FH        High-intensity

  70H        White background, black foreground ("reverse video")

  87H
Not displayed by all monochrome monitors
       Blinking white on black (if blinking enabled)
             Dim background, normal foreground (if blinking
             disabled)

  8FH
Not displayed by all monochrome monitors
       Blinking high-intensity (if blinking enabled)
             Dim background, high-intensity foreground (if blinking
             disabled)

  F0H        Blinking "reverse video" (if blinking enabled)
             High-intensity background, black foreground (if blinking
             disabled)
  --------------------------------------------------------------------------


  Figure 4-8.  Monochrome text-mode attributes. The appearance of some
  attributes depends on the setting of the enable-blink bit at I/O port
  3B8H.

  Setting color in graphics modes

  So far, we've seen how to set color (and the monochrome equivalent of
  color) in text modes. Setting color in graphics modes is quite different.
  In graphics modes, each pixel is associated with a color. The color is set
  the same way attributes are set in text mode, but there are important
  differences. First, since each pixel is a discrete dot of color, there is
  no foreground and background--each pixel is simply one color or another.
  Second, pixel attributes are not always 4 bits in size--we've already
  mentioned that pixel attributes can range from 1 to 8 bits, depending on
  the video mode being used. These differences give graphics-mode programs a
  subtly different "feel" than they have in text modes, both to programmers
  and to users.

  The most important difference between text-mode and graphics-mode
  attributes, however, is this: In graphics modes you can control the color
  of each pixel. This lets you use colors much more effectively than you can
  in text modes. This isn't so obvious with the CGA and its limited color
  capabilities, but with an MCGA or VGA it's quite apparent.

  Let's start with the CGA. The CGA's two graphics modes are relatively
  limited in terms of color: In 320 x 200, 4-color mode, pixel attributes
  are only 2 bits wide, and you can display only four different colors at a
  time. In 640 x 200, 2-color mode, you have only 1 bit per pixel, so you
  can display only two different colors. Also, the range of colors you can
  display in CGA graphics modes is severely limited.

  In 320 x 200, 4-color mode, pixels can have value 0, 1, 2, or 3,
  corresponding to the 2-bit binary values 00B, 01B, 10B, and 11B. You can
  assign any one of the CGA's 16 color combinations to zero-value pixels,
  but colors for nonzero pixels are derived from one of three built-in
  palettes. (See Figure 4-9.) In 640 x 200, 2-color mode, nonzero pixels
  can be assigned any one of the 16 color combinations, but zero-value
  pixels are always black. In both modes, you can assign palette colors
  using ROM BIOS interrupt 10H services described in Chapter 9.

  The EGA, MCGA, and VGA are considerably more flexible in terms of color
  management, because you can assign any color combination to any palette or
  video DAC color register. Equally important is the fact that you have
  larger pixel values and therefore more colors to work with on the screen.
  The most frequently used graphics modes on the EGA and VGA are the
  16-color modes with pixels that require 4 bits to define the colors. In
  most applications, 16 colors are adequate, because you can select those 16
  colors from the entire range of color combinations the hardware can
  display (64 colors on the EGA and 262,144 colors on the MCGA and VGA).
  Again, the ROM BIOS provides services that let you assign arbitrary color
  combinations to the palette and video DAC color registers on the EGA,
  MCGA, and VGA. See Chapter 9 for details.

  Pixel Bits               Pixel Value             Pixel Color
  --------------------------------------------------------------------------
  Mode 4, palette 0:
  0 1                      1                       Green
  1 0                      2                       Red
  1 1                      3                       Yellow or brown

  Mode 4, palette 1:
  0 1                      1                       Cyan
  1 0                      2                       Magenta
  1 1                      3                       White

  Mode 5:
  0 1                      1                       Cyan
  1 0                      2                       Red
  1 1                      3                       White
  --------------------------------------------------------------------------

  Figure 4-9.  Palettes in CGA 320 x 200, 4-color graphics mode.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson