StuBS
Output in Text Mode

Video-RAM

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 (= Offset 0).

Output in text mode: the screen is divided into 80x25 characters, each of which is mapped to 2 bytes in the memory

Attributes

For each character, foreground color, background color and character blinking can be set individually using a byte whose bits have the following meaning:

Bit 7Bits 4-6Bits 0-3
blinkingbackgroundforeground

In CGA text mode the following 16 colors are available:

Fore- and background Foreground only
0

black

8 (dark) grey
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 grey

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.

Controlling the Hardware Cursor

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:

Index Register
0 Horizontal Total
1 Horizontal Displayed
2 Horizontal Sync Position
3 Horizontal Sync Width
4 Vertical Total
5 Vertical Total Adjust
6 Vertical Displayed
7 Vertical Sync Position
8 Interlace Mode
9 Maximum Scan Line Address
10 Cursor Start
11 Cursor End
12 Start Address (High Byte)
13 Start Address (Low Byte)
14 Cursor Address (High Byte)
15 Cursor Address (Low Byte)
16 Light Pen (High Byte)
17 Light Pen (Low Byte)

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. The actual access to the contents of the desired control register (read/write) is then performed via the data register.

I/O Port Register Access type
0x3d4 index register write only
0x3d5 data register read and write