StuBS
|
Each PC graphics card has its own memory area, the video RAM (VRAM), with which the screen content can be programmed. In graphics mode, one or more bits in the Video RAM must be set or deleted for each pixel, depending on the number of possible colors. In text mode, however, 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 autonomously by the graphics card.
In the first assignments of StuBS, all output should be done in CGA text mode. This mode is supported by almost all graphics cards, is easy to program and completely sufficient for our operating system.
In CGA text mode, each screen position occupies two bytes in the video RAM. The first of the two bytes (even offset address) takes the ASCII code of the character to be displayed, the second (odd offset address) takes the desired foreground and background color. The mapping of the screen positions to the entries in the Video RAM is done by rows and columns. At 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 the right occupies bytes 2 and 3 and the character at the end of the first line occupies bytes 158 and 159. Counting continues with the first character of the second line.
The Video RAM is mapped into the main memory of the PC and can therefore easily be written using normal memory accesses. The Video RAM starts at address 0xb8000
(see Memory Mapped I/O).
For each character, foreground color, background color and character blinking can be set individually using a byte whose bits have the following meaning:
Bit 7 | Bits 4-6 | Bits 0-3 |
---|---|---|
blinking | background | foreground |
In CGA text mode the following 16 colors are available:
0 | black | 4 | red | 8 | dark gray | 12 | light red |
1 | blue | 5 | magenta | 9 | light blue | 13 | light magenta |
2 | green | 6 | brown | 10 | light green | 14 | yellow |
3 | cyan | 7 | light gray | 11 | light cyan | 15 | white |
Since only three bits are available for the background color in the attribute byte, only the first eight colors can be used for the background.
The memory-mapped CGA buffer can be accessed directly with a pointer (see Memory Mapped I/O). We recommend using the TextMode::Cell to access the memory for a single cell (char + attribute). A working but not that excellent solution would be the following:
To be able to retrieve or set the current cursor position, the video controller of the graphics card must be programmed. The video controller of the CGA card provides a total of 18 control registers (with 8-bit word length).
Only control registers 14
and 15
are required to control the cursor in StuBS.
However, unlike the Video RAM, these registers cannot be addressed directly – indirect access is possible via an index and a data register using I/O Ports (using the in and out instructions). For this purpose, the number of the control register to be accessed is first written into the index register (I/O Port 0x3d4
). The actual access to the contents of the desired control register (read/write) is then performed via the data register (I/O Port 0x3d5
).