# VGA/SVGA Video Programming--VGA Text Mode Operation
Pure VGA/SVGA hardware programming (registers, identification, and other low-level stuff.)
Author: Joshua Neal
Keywords: VGA SVGA hardware video programming

Home Intro Memory Attributes Fonts Cursor Back

Hardware Level VGA and SVGA Video Programming Information Page
VGA Text Mode Operation

- Introduction -- gives scope of this page.
- Display Memory Organization -- details how the VGA's planes are utilized when in text mode.
- Attributes -- details the fields of the attribute byte.
- Fonts -- details the operation of the character generation hardware.
- Cursor -- details on manipulating the text-mode cursor.

Introduction
This section is intended to document the VGA's operation when it is in the text modes, including attributes and fonts. While it would seem that the text modes are adequately supported by the VGA BIOS, there is actually much that can be done with the VGA text modes that can only be accomplished by going directly to the hardware. Furthermore, I have found no good reference on the VGA text modes; most VGA references take them for granted without delving into their operation.

Display Memory Organization
The four display memory planes are used for different purposes when the VGA is in text mode. Each byte in plane 0 is used to store an index into the character font map. The corresponding byte in plane 1 is used to specify the attributes of the character possibly including color, font select, blink, underline and reverse. For more details on attribute operation see the Attributes section below. Display plane 2 is used to store the bitmaps for the characters themselves. This is discussed in the Fonts section below. Normally, the odd/even read and write addressing mode is used to make planes 0 and 1 accessible at interleaved host memory addresses.

Attributes
The attribute byte is divided into two four bit fields. The field from 7-4 is used as an index into the palette registers for the background color which used when a font bit is 0. The field from 3-0 is used as an index into the palette registers for the foreground which is used when a font bit is 1. Also the attribute can control several other aspects which may modify the way the character is displayed.
If the Blink Enable field is set to 1, character blinking is enabled. When blinking is enabled, bit 3 of the background color is forced to 0 for attribute generation purposes, and if bit 7 of the attribute byte for a character is set to 1, the foreground color alternates between the foreground and background, causing the character to blink. The blink rate is determined by the vertical sync rate divided by 32.
If the bits 2-0 of the attribute byte is equal to 001b and bits 6-4 of the attribute byte is equal to 000b, then the line of the character specified by the Underline Location field is replaced with the foreground color. Note if the line specified by the Underline Location field is not normally displayed because it is greater than the maximum scan line of the characters displayed, then the underline capability is effectively disabled.
Bit 3 of the attribute byte, as well as selecting the foreground color for its corresponding character, also is used to select between the two possible character sets (see Fonts below.) If both character sets are the same, then the bit effectively functions only to select the foreground color.

Fonts
The VGA's text-mode hardware provides for a very fast text mode. While this mode is not used as often these days, it used to be the predominant mode of operation for applications. The reason that the text mode was fast, much faster than a graphics mode at the same resolution was that in text mode, the screen is partitioned into characters. A single character/attribute pair is written to screen, and the hardware uses a font table in video memory to map those character and attribute pairs into video output, as opposed to having to write all of the bits in a character, which could take over 16 operations to write to screen. As CPU display memory bandwidth is somewhat limited (particularly on on older cards), this made text mode the mode of choice for applications which did not require graphics.

For each character position, bit 3 of the attribute byte selects which character set is used, and the character byte selects which of the 256 characters in that font are used. Up to eight sets of font bitmaps can be stored simultaneously in display memory plane 2. The VGA's hardware provides for two banks of 256 character bitmaps to displayed simultaneously. Two fields, Character Set A Select and Character Set B Select field are used to determine which of the eight font bitmaps are currently displayed. If bit 3 of a character's attribute byte is set to 1, then the character set selected by Character Set A Select field, otherwise the character set specified by Character Set B Select field is used. Ordinarily, both character sets use the same map in memory, as utilizing 2 different character sets causes character set A to be limited to colors 0-7, and character set B to be limited to colors 8-15.
Fonts are either 8 or 9 pixels wide and can be from 1 to 32 pixels high. The width is determined by the 9/8 Dot Mode field. Characters normally have a line of blank pixels to the right and bottom of the character to separate the character from its neighbor. Normally this is included in the character's bitmap, leaving only 7 bit columns for the character. Characters such as the capital M have to be squished to fit this, and would look better if all 8 pixels in the bitmap could be used, as in 9 Dot mode where the characters have an extra ninth bit in width, which is displayed in the text background color, However, this causes the line drawing characters to be discontinuous due to the blank column. Fortunately, the Line Graphics Enable field can be set to allow character codes C0h-DFh to have their ninth column be identical to their eighth column, providing for continuity between line drawing characters. The height is determined by the Maximum Scan Line field which is set to one less than the number of scan lines in the character.
Display memory plane 2 is divided up into eight 8K banks of characters, each of which holds 256 character bitmaps. Each character is on a 32 byte boundary and is 32 bytes long. The offset in plane 2 of a character within a bank is determined by taking the character's value and multiplying it by 32. The first byte at this offset contains the 8 pixels of the top scan line of the characters. Each successive byte contains another scan line's worth of pixels. The best way to read and write fonts to display memory, assuming familiarity with the information from the Accessing the Display Memory page, is to use standard (not Odd/Even) addressing and Read Mode 0 and Write Mode 0 with plane 2 selected for read or write.
The following example shows three possible bitmap representations of text characters. In the left example an 8x8 character box is used. In this case, the Maximum Scan Line field is programmed to 7 and the 9/8 Dot Mode field is programmed to 0. Note that the bottom row and right-most column is blank. This is used to provide inter-character spacing. The middle example shows an 8x16 character. In this case the Maximum Scan Line field is programmed to 15 and the 9/8 Dot Mode field is programmed to 0. Note that the character has extra space at the bottom below the baseline of the character. This is used by characters with parts descending below the baseline, such as the lowercase letter "g". The right example shows a 9x16 character. In this case the Maximum Scan Line field is programmed to 15 and the 9/8 Dot Mode field is programmed to 1. Note that the rightmost column is used by the character, as the ninth column for 9-bit wide characters is assumed blank (excepting for the behavior of the the Line Graphics Enable field.) allowing all eight bits of width to be used to specify the character, instead of having to devote an entire column for inter-character spacing.

Cursor
The VGA has the hardware capability to display a cursor in the text modes. Further details on the text-mode cursor's operation can be found in the following section:

- Manipulating the Text-mode Cursor -- details controlling the appearance and location of the cursor.

Notice: All trademarks used or referred to on this page are the property of their respective owners.
All pages are Copyright © 1997, 1998, J. D. Neal, except where noted. Permission for utilization and distribution is subject to the terms of the FreeVGA Project Copyright License.