Wiki

Clone wiki

ultima-exodus / CGA

This section describes the CGA video mode 0x04 as used by the Ultima games.

CGA Palette

CGA data consists of four colors across two palettes:

Index Data Color Palette 0 Color Palette 1
0 00 Background Background
1 01 Green Cyan
2 10 Red Magenta
3 11 Yellow White

Where the background color could be either black or blue. In the Ultima games we of course use a black background with palette 1.

CGA Data

Pixels are grouped by row such that each successive pixel represents a column in the current row. Once the end of a row is reached, the next byte begins the next row (though rows are interleaved; see the CGA Video Buffer section below).

CGA pixels are 2 bits wide, so CGA data is represented as four pixels per byte. Each byte has pixels organized sequentially as follows:

p0-p1-p2-p3

For example, the following byte from CGA data shown in binary:

00010011

Can be broken down into four pixels:

00-01-00-11

Which in decimal is color indices 0,1,0,3 and corresponds to CGA colors black, cyan, black, and white.

CGA Video Buffer

The CGA video buffer during mode 0x04 is located at segment 0xB800. It is a 16kB buffer that's divided into two row-interlaced frames. The first frame outputs the even rows; the second frame the odd rows. The video buffer is structured according to the following table:

Offset Size Description
0x0000 8000 bytes first frame - even rows (0-198)
0x1F40 192 bytes buffer, unused
0x2000 8000 bytes second frame - odd rows (1-199)
0x3F40 192 bytes buffer, unused

If we indexed each byte in the buffer, it would look like this:

Frame 1:

row 0   - 0000 0001 0002 0003 0004 ... 0048 0049 004A
row 2   - 004B 004C 004E 004F 0050 ... 0093 0094 0095
row 4   - 0096 0097 0098 0099 009A ... 00DE 00DF 00E0
...
row 198 - 1EF5 1EF6 1EF7 1EF8 1EF9 ... 1F37 1F38 1F39

Frame 2:

row 1   - 2000 2001 2002 2003 2004 ... 2048 2049 204A
row 3   - 204B 204C 204E 204F 2050 ... 2093 2094 2095
row 5   - 2096 2097 2098 2099 209A ... 20DE 20DF 20E0
...
row 199 - 3EF5 3EF6 3EF7 3EF8 3EF9 ... 3F37 3F38 3F39

Updated