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]

  As you have learned, in text modes no character images are stored in video
  memory. Instead, each character is represented in the video buffer by a
  pair of bytes containing the character's ASCII value and display
  attributes. The pixels that make up the character are drawn on the screen
  by a character generator that is part of the display circuitry. The Color
  Graphics Adapter has a character generator that produces characters in an
  8 x 8 pixel block format, while the Monochrome Display Adapter's character
  generator uses a 9 x 14 pixel block format. The larger format is one of
  the factors that makes the MDA's display output easier to read.

  The standard ASCII characters (01H through 7FH [decimal 1 through 127])
  represent only half of the ASCII characters available in the text modes.
  An additional 128 graphics characters (80H through FFH [decimal 128
  through 255]) are available through the same character generator. More
  than half of them can be used to make simple line drawings. A complete
  list of both the standard ASCII characters and the graphics characters
  provided by IBM is given in Appendix C.

  The graphics modes can also display characters, but they are produced
  quite differently. Graphics-mode characters are drawn, pixel by pixel, by
  a ROM BIOS software character generator, instead of by a hardware
  character generator. (ROM BIOS interrupt 10H provides this service; see
  Chapter 9.) The software character generator refers to a table of bit
  patterns to determine which pixels to draw for each character. The ROM of
  every PC and PS/2 contains a default table of character bit patterns, but
  you can also place a custom bit pattern table in RAM and instruct the BIOS
  to use it to display your own character set.

  In CGA-compatible graphics modes (640 x 200, 2-color and 320 x 200,
  4-color), the bit patterns for the second 128 ASCII characters are always
  found at the address stored in the interrupt 1FH vector at 0000:007CH. If
  you store a table of bit patterns in a buffer and then store the buffer's
  segment and offset at 0000:007CH, the ROM BIOS will use the bit patterns
  in the buffer for ASCII characters 80H through FFH (decimal 128 through
  255. In other graphics modes on the EGA, MCGA, and VGA, the ROM BIOS
  provides a service through interrupt 10H that lets you pass the address of
  a RAM-based table of character bit patterns for all 256 characters.

  Mapping characters in text modes

  In text modes, the memory map begins with the top left corner of the
  screen, using 2 bytes per screen position. The memory bytes for succeeding
  characters immediately follow in the order you would read them--from left
  to right and from top to bottom.

  Modes 0 and 1 are text modes with a screen format of 40 columns by 25
  rows. Each row occupies 40 x 2 = 80 bytes. A screen occupies only 2 KB in
  modes 0 and 1, which means the CGA's 16 KB memory can accommodate eight
  display pages. If the rows are numbered 0 through 24 and the columns
  numbered 0 through 39, then the offset to any screen character in the
  first display page is given by the following BASIC formula:

  CHARACTER.OFFSET = (ROW.NUMBER * 80) + (COLUMN.NUMBER * 2)

  Since the attribute byte for any character is in the memory location next
  to the ASCII character value, you can locate it by simply adding 1 to the
  character offset.

  Modes 2, 3, and 7 are also text modes, but with 80 columns in each row
  instead of 40. The byte layout is the same, but each row requires twice as
  many bytes, or 80 x 2 = 160 bytes. Consequently, the 80 x 25 screen format
  uses 4 KB, and the 16 KB memory can accommodate four display pages. The
  offset to any screen location in the first display page is given by the
  following BASIC formula:

  CHARACTER.OFFSET = (ROW.NUMBER * 160) + (COLUMN.NUMBER * 2)

  The beginning of each text display page traditionally starts at an even
  kilobyte boundary. Because each screen page in the text modes actually
  uses only 2000 or 4000 bytes, some unused bytes follow each page: either
  48 or 96 bytes, depending on the size of the page. So, to locate any
  screen position on any page in text mode, use the general formula shown on
  the next page.

  LOCATION = (SEGMENT.PARAGRAPH * 16)
             + (PAGE.NUMBER * PAGE.SIZE) + (ROW.NUMBER * ROW.WIDTH * 2)
             + (COLUMN.NUMBER * 2) + WHICH
  LOCATION  is the 20-bit address of the screen information.
  SEGMENT.PARAGRAPH  is the location of the video display memory
  (for example, B000H or B800H).
  PAGE.NUMBER  is in the range 0 through 3 or 0 through 7.
  PAGE.SIZE  is 2000 or 4000.
  ROW.NUMBER  is from 0 through 24.
  ROW.WIDTH  is 40 or 80.
  COLUMN.NUMBER  is from 0 through 39 or 0 through 79.
  WHICH  is 0 for the display character or 1 for the display attribute.

  Mapping pixels in graphics modes

  When you use a graphics mode, pixels are stored as a series of bit fields,
  with a one-to-one correlation between the bit fields in memory and the
  pixels on the screen. The actual mapping of bit fields in the video buffer
  depends on the video mode.

  In CGA-compatible graphics modes, the display is organized into 200 lines,
  numbered 0 through 199. Each line of pixels is represented in the video
  buffer in 80 bytes of data. In 640 x 200, 2-color mode, each bit
  represents one pixel on the screen, while in 320 x 200, 4-color mode, each
  pixel is represented by a pair of bits in the buffer. (See Figure 4-13.)
  Thus there are eight pixels to each byte in 640 x 200, 2-color mode, and
  80 x 8, or 640, pixels per row. Similarly, there are four pixels to each
  byte in 320 x 200, 4-color mode, and 80 x 4, or 320, pixels per row.

  The storage for the pixel rows is interleaved:

  .  Pixels in even-numbered rows are stored in the first half of the video
     buffer, starting at B800:0000H.

  .  Pixels in odd-numbered rows are stored starting at B800:2000H.

  For example, in 640 x 200, 2-color mode, the first pixel in the first row
  (in the upper-left corner of the screen) is represented by the leftmost
  bit (bit 7) in the byte at B800:0000H. The second pixel in the row is
  represented by bit 6 of the same byte. Because of the interleaved buffer
  map, however, the pixel immediately below the first pixel is represented
  in bit 7 of the byte at B800:2000H.

  640 x 200, 2-color mode            320 x 200, 4-color mode

            bit                                bit
      7 6 5 4 3 2 1 0                    7 6 5 4 3 2 1 0
   +-------------------+              +-------------------+
   |  1 0 0 0 1 1 1 1  |              |  1 1 0 0 0 1 0 1  |
   +--+-+-+-+-+-+-+-+--+              +-------------------+
      | | | | | | | |                    +-+ +-+ +-+ +-+
  +---|-|-|-|-|-|-|-|-------         +----|---|---|---|--------
  |+--|-|-|-|-|-|-|-|-------         |+---|---|---|---|--------
  ||  . . . . . . . .                ||   .   .   .   .
  ||  # . . . # # # #                ||   #   .   #   #
  ||                                 ||
  ||                                 ||
  ||                                 ||

  Figure 4-13.  Pixel mapping in CGA-compatible graphics modes.

  In all other graphics modes, the buffer map is linear, as it is in text
  modes. Pixels are stored from left to right in each byte, and one row of
  pixels immediately follows another in the video buffer. On the MCGA and
  VGA, for example, the 1-bit pixels in 640 x 480, 2-color mode and the
  8-bit pixels in 320 x 200, 256-color mode are stored starting at
  A000:0000H and proceeding linearly through the buffer.

  The catch is that pixel bit fields are not always mapped linearly in all
  video modes. On the EGA and VGA, the video buffer in 16-color graphics
  modes is arranged as a set of four parallel memory maps. In effect, the
  video memory is configured to have four 64 KB memory maps spanning the
  same range of addresses starting at A000:0000H. The EGA and VGA have
  special circuitry that accesses all four memory maps in parallel. Thus in
  16-color EGA and VGA graphics modes, each 4-bit pixel is stored with 1 bit
  in each memory map. (See Figure 4-14.) Another way to visualize this is
  that a 4-bit pixel value is formed by concatenating corresponding bits
  from the same address in each memory map.

  There is a good reason why the EGA and VGA were designed to use parallel
  memory maps in graphics modes. Consider the situation in 640 x 350,
  16-color mode: With 4 bits per pixel, you need 640 x 350 x 4 (896,000)
  bits to store one screenful of pixels. That comes out to 112,000 bytes,
  which is bigger than the 64 KB maximum size of one 8086 segment. If you
  organize the pixel data in parallel, however, you only need 112,000 . 4
  (28,000) bytes in each memory map.

  With this variety of memory maps and pixel sizes, it's fortunate that the
  ROM BIOS provides services that let you read and write individual pixels
  regardless of the video mode. (Chapter 9 describes these services.)
  Unfortunately, these ROM BIOS pixel-manipulation services are pretty slow.
  If you're working in graphics modes, you'll probably find that the
  graphics drawing functions provided in your programming language (such as
  the PSET, LINE, and CIRCLE functions in BASIC) are the best tools for
  creating graphics-mode screens.

              +-------------------+
  Parallel    |  1 0 1 1 0 1 0 1  | Map 3
  memory      +-------------------+
  maps        +-------------------+
              |  1 0 0 0 0 0 0 0  | Map 2
              +-------------------+
              +-------------------+
              |  1 0 1 1 0 1 1 1  | Map 1
              +-------------------+
              +-------------------+
              |  1 0 1 1 0 0 0 1  | Map 0
              +-------------------+
       +---------+ | |++ ++| | +---------+
       |    +------Pixel values-----+    |
       |    |    +---+|   |+---+    |    |
     +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+
     1111 0000 1011 1011 0000 1010 0010 1011
       |    |    +---+|   |+---+    |    |
       |    +------+ |++ ++| +------+    |
       +---------+ | | | | | | +---------+
             +---|-|-|-|-|-|-|-|-------
             |+--|-|-|-|-|-|-|-|-------
             ||  . . . . . . . .
             ||  # . # # . # # #
             ||

  Figure 4-14.  Pixel mapping in 16-color EGA and VGA graphics modes.

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