Mode 13h - Technical Layout

Technical Layout

Mode 13h is something of a curiosity, because the VGA is a planar device from a hardware perspective, and somewhat unsuited for chunky graphics operation. It was made possible by a hardware trick. In order to understand how Mode 13h was possible, one must consider the VGA's memory layout:

The VGA's 256KiB of video memory is not directly available to the programmer and is accessed via a 64 kB 'window' in the PC's real mode address space. The Video RAM is actually a bank of 4 planes, 64 kB each. By manipulating the VGA's control registers the programmer can decide how some or all of these 4 planes will be affected by each write or read operation. In 256-color mode each byte in a plane represents a single pixel. On the first line of the screen pixels 0, 4, 8, 12 etc. are all contained in memory plane 0, while pixels 1, 5, 9, 13 etc. are contained in memory plane 1 and so on. A single byte written to the 64 kB window, when all planes are enabled can write up to 4 pixels (of the same value) at once. Through a combination of the 64 kB window available, and the selection of one of the four planes (an effective extra two bits of address space), this allows the manipulation of the total 256 kB video memory.

Mode 13h, however, allows the programmer to access the VGA in "chunky" (linear) fashion, where each consecutive address represents a consecutive pixel on screen. Planes need not be manipulated to select the correct pixels to modify or read.

This is made possible because in Mode 13h the hardware uses the last two bits of the address (in the 64 kB window) to select the plane to write to. The upshot is that the programmer has a simple access model; the downside is that three quarters of the video memory is now inaccessible (we can now only access a total of 64 kB of display memory through the window, rather than 256 kB that is accessible indirectly by manipulating the planes). A simple bank switching method could have made the remaining memory available, and indeed bank switching was made available on Super VGAs to access the larger VRAMs these devices sported.

Although never documented by IBM, an alternative planar 256-color mode is available by "unchaining" the video ram; video modes created using this technique are collectively called Mode X.

Unlike SVGA and higher color depths, Mode 13h, part of the MCGA set of video modes, has 18 bits of color, 6 per channel. The palette data is not stored in memory per se, but rather modified, read and written to by low level I/O port calls, to the DAC registers.

The color palette data was often copied from the low level ports one at a time to a spot in memory after the screen data so the colors could be saved along with the pixels they belonged to. A mode 13h screen capture is merely 64000 bytes of values from 0 to 255, each an index of a color from a known palette of multiple colors. If the color palette is jumbled or totally wrong, a green leaf will appear blue, or any other possible color, depending on what that particular color was set for, which particular one out of 256.

Due to the way chunks of memory are allocated in such an environment, the bytes at high address &hA000 low address 64000 to 64768, can be written to with no negative effects. This lets the programmer save the additional bytes, 256 (colors) * 3 (color channels) = 768 bytes, at the end of the screen capture file. These have to be read back out again and written to the color port for them to change, when an image containing a footer of color info gets loaded. A method used with this was to have a global palette, and match the colors from an old color palette, to a new color palette. This meant changing the index of each pixel, where it would point to a near perfect match of one color from old to new color set. This also had drawbacks, as sometimes there were not enough gradients, and artifacts and such would appear.

It was also good sense to keep the first 16 colors, as they were, and modify colors 16 - 255. This would make sure any GUI or UI had reliable colors. Otherwise a user might be presented with a message or bit of text which was unreadable, or very difficult to read.

It is not essential to set the graphics mode through the BIOS; it is possible to switch graphics mode in 32-bit protected mode, if a little more complicated. It involves writing size data to the VGA controller. Set VGA graphics mode without BIOS in C

Read more about this topic:  Mode 13h

Famous quotes containing the word technical:

    In middle life, the human back is spoiling for a technical knockout and will use the flimsiest excuse, even a sneeze, to fall apart.
    —E.B. (Elwyn Brooks)