CGA Screen

[ Class Overview ] [ Assembler Crash Course ]

Video RAM

Most PC graphics cards have their own memory, the video RAM, which is used to define the screen contents. In graphics mode, one or more bits must be set or cleared in the video RAM for each pixel, depending on the number of possible colors. In text mode, on the other hand, it is sufficient to transfer the ASCII code of the desired character together with its display attributes into the video RAM. The conversion of the ASCII code into a pixel matrix is then performed independently by the graphics card.

In the OSC lab exercises, all output should be made in CGA text mode. This mode is supported by almost every graphics card, is easy to program, and is perfectly adequate for this lecture.

In CGA text mode, each screen position occupies two bytes in video RAM. The first of the two bytes (at an even offset address) takes the ASCII code of the character to be displayed, the second (with an odd offset address) the desired foreground and background color. The mapping of the screen positions to the entries in the video RAM is done by lines and columns. With a resolution of 80 characters per line and 25 lines, the character in the upper left corner occupies bytes 0 and 1, the character to its right occupies bytes 2 and 3, and the character at the end of the first line occupies bytes 158 and 159. Byte-counting then continues with the first character of the second line.

Since video memory is mapped to the main-memory address space of the PC, it can be written to using normal memory accesses. Offset 0 in the video RAM is addressed with (hexadecimal) b8000.

Character Attributes

For each character the attributes foreground color, background color and blinking can be defined individually. These attributes are encoded in the attribute byte as follows:

Attributes
Bits 0–3Foreground color
Bits 4–6Background color
Bit 7Blinking (Note: QEMU does not emulate blinking. It works on the real hardware, though.)

In CGA text mode, the following 16 colors are available:

Color Palette
0Black 8Dark gray
1Blue 9Light blue
2Green 10Light green
3Cyan 11Light cyan
4Red 12Light red
5Magenta 13Light magenta
6Brown 14Yellow
7Light gray 15White

As the attribute byte only budgets 3 bits for the background color, only the first 8 colors can be chosen as a background color (at least, by default).

Controlling the Cursor

To query or to set the current cursor position, the video controller of the graphics card must be programmed. CGA's video controller provides a total of 18 control registers (each 8 bits wide). Only control registers 14 and 15 are of interest here for cursor control:

IndexRegisterMeaning
14Cursor (high) Character offset of the cursor position
15Cursor (low)

However, the control registers cannot be accessed directly. Instead, access goes through the indirection of an index register and a data register. For this purpose, the number of the control register to be read or written is first written into the index register. The actual access to the content of the control register (read/write) is then done via the data register. Data and index registers can be addressed directly by means of the in and out CPU instructions, since they were assigned independent port addresses:

PortRegisterAccess mode
0x3d4Index registerWrite only
0x3d5Data registerRead and write

References

If you want to know more about CGA or VGA programming (this is not necessary reading for task 1), follow these links: