[ Class Overview ]
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–3 | Foreground color |
Bits 4–6 | Background color |
Bit 7 | Blinking (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 | |||
---|---|---|---|
0 | Black | 8 | Dark gray |
1 | Blue | 9 | Light blue |
2 | Green | 10 | Light green |
3 | Cyan | 11 | Light cyan |
4 | Red | 12 | Light red |
5 | Magenta | 13 | Light magenta |
6 | Brown | 14 | Yellow |
7 | Light gray | 15 | White |
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:
Index | Register | Meaning |
---|---|---|
14 | Cursor (high) | Character offset of the cursor position |
15 | Cursor (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:
Port | Register | Access mode |
---|---|---|
0x3d4 | Index register | Write only |
0x3d5 | Data register | Read 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: