CGA, 1981

IBM shipped the Color/Graphics Monitor Adapter with the 5150 in 1981: 16 KB of memory, four colours on screen, one MC6845 CRT controller. This era writes real bytes into a simulated framebuffer, so the limits push back the way they did.

Graphics track · 30 missions · boss mission, written exam and certificate · free, no signup. Everything below runs in the browser terminal on the SERVBG home page.

Open CGA in the terminal

What you will do

  1. render the reference scene through a 4-colour CGA pipeline ref

    IBM shipped the Color/Graphics Monitor Adapter with the 5150 PC in 1981: 16 KB of RAM, a Motorola MC6845 CRT controller, and four colours on screen out of sixteen the card could generate.

  2. read what IBM actually put on the card specs

    The dot clock is 14.31818 MHz, exactly four times the NTSC colour subcarrier. That one design choice gave the CGA both its composite output and, by accident, its artifact colours.

  3. list the modes the card can be put into modes

    Port 3D8h selects the mode. Two text modes, two graphics modes, and a colour-burst bit that quietly turns each of them into a second variant.

  4. switch the monitor to 320x200 in 4 colours mode 4

    Mode 4 is 320 by 200 at 2 bits per pixel: 4 pixels packed into every byte, 80 bytes per scanline, 16000 bytes for the whole screen.

  5. select the green/red/brown palette palette 0

    The colour-select register at 3D9h holds one palette bit. Palette 0 is green, red and brown. It is not what a mode set leaves behind: the BIOS writes 30h to 3D9h, which is palette 1 with the intensity bit already set.

  6. select the cyan/magenta/grey palette palette 1

    Palette 1 is cyan, magenta and light grey. One bit, two options, and no way to pick the three foreground colours individually. This is why so many DOS games look alike.

  7. raise the whole palette to high intensity intensity on

    RGBI carries one intensity line shared by all three colour lines, so it brightens every entry at once: cyan to light cyan, magenta to light magenta, light grey to white.

  8. clear the colour burst and reveal the third palette mode 5

    Mode 5 is mode 4 with the colour burst turned off. On an RGBI monitor that gives cyan, red and white, a third palette IBM never documented and plenty of games used anyway. On a composite monitor, where the burst is the colour, the same screen goes grey.

  9. read the 16 KB memory map at B800:0000 vram

    The framebuffer is mapped straight into the CPU address space at segment B800h. There is no blitter and no command queue: you write bytes, the CRTC reads them 60 times a second.

  10. find the byte and the bits that hold the top-left pixel addr 0 0

    Pixel (0,0) is the top two bits of byte 0. The leftmost pixel of a byte lives in the highest bits, because the shift register clocks them out most significant first.

  11. find the same pixel one scanline down and watch the address jump addr 0 1

    Scanline 1 starts at offset 8192, not 80. Even scanlines fill the first 8000 bytes, odd ones the 8000 bytes beginning at 2000h. That interleave is the signature of a CGA screen.

  12. write one byte and light four pixels at once poke 0 228

    228 decimal is 11100100 in binary: pixel values 3, 2, 1 and 0 left to right. One byte, four pixels, no way to touch one of them without reading the other three back first.

  13. read the byte back and decode its four pixel fields peek 0

    Reading video memory is the same as reading any other RAM, which is exactly why a single-pixel plot costs a read, a mask, an or and a write.

  14. write the same byte into the odd-scanline bank poke 8192 228

    Offset 8192 is the first byte of scanline 1. The two pokes you have now made are vertically adjacent on screen and 8 KB apart in memory.

  15. understand why the odd scanlines live 8192 bytes away bank

    The interleave came free: the MC6845 was built to scan text rows, so IBM let its internal line counter pick the bank. The card saved chips and handed the cost to every programmer.

  16. plot a single pixel in the centre of the screen pset 160 100 3

    PSET in GW-BASIC did exactly this: work out the byte, read it, clear two bits, or in the new value, write it back. In assembler it was about a dozen instructions.

  17. draw a straight segment with Bresenham line 40 20 280 20 1

    Jack Bresenham published his line algorithm in 1965 while at IBM. It walks the major axis with integer adds and one error term, no division and no floating point, which is why it outlived the hardware it was written for.

  18. draw a circle from one octant circle 160 100 40 2

    The midpoint circle algorithm computes one eighth of the curve and mirrors it eight ways. CGA pixels are not square, so a true circle renders slightly taller than it is wide.

  19. flood fill the inside of the circle paint 140 100 1

    PAINT spreads from a seed until it meets a different colour. Scanline flood fill handles a whole run at a time, so a full-screen fill needs a few hundred stack entries instead of 64000.

  20. compare hard banding against an ordered 4x4 dither dither none ;; dither ordered

    Ordered dithering perturbs each pixel by a fixed 4x4 Bayer matrix before snapping it to the palette. It needs no memory of the previous pixel, which made it the practical choice on an 8088.

  21. push quantisation error into the neighbouring pixels dither fs

    Floyd and Steinberg published their error-diffusion filter in 1976: 7/16 of the error goes right, then 3/16, 5/16 and 1/16 into the row below. More detail, and a buffer the size of a scanline.

  22. switch to 640x200 in 2 colours and requantise the scene mode 6 ;; ref

    Mode 6 trades colour for resolution: 640 by 200 at one bit per pixel, 8 pixels per byte, the same 80 bytes per scanline and the same odd/even interleave.

  23. feed the same bits to an NTSC composite monitor composite on

    On composite, luma and chroma share one wire. Four hi-res pixels fill one colour clock, so the on/off pattern inside that clock is read as a hue instead of as dots.

  24. read the pattern-to-colour table the artifacts follow artifact

    640x200 in two colours becomes 160x200 in sixteen. Nothing in the card computed those colours: they are phase relationships in a signal, and turning the tint knob rotates every one of them.

  25. switch to 40x25 text and read the character/attribute pairs mode text

    Text mode stores two bytes per cell: a character code and an attribute holding foreground in bits 0-3, background in bits 4-6, blink in bit 7. A 40x25 page is 1000 cells, 2000 bytes, so 16 KB holds eight of them.

  26. see the CGA snow bug and learn what caused it snow

    The card had no arbitration between the CPU and the CRTC. A write during active display stole a fetch cycle and put bus garbage on screen, so the BIOS polled port 3DAh and wrote only during blanking.

  27. return to mode 4 with palette 1 at high intensity, the classic CGA look mode 4 ;; palette 1 ;; intensity on

    Black, light cyan, light magenta and white. That combination is on the box art of half the DOS games ever sold, and nobody picked it: it is palette bit 1 with the intensity bit set.

  28. lay down six line segments to frame the drawing line 60 50 250 50 1 ;; line 250 50 250 160 1 ;; line 250 160 60 160 1 ;; line 60 160 60 50 1 ;; line 60 70 250 70 1 ;; line 150 90 150 150 2

    Six calls, a few thousand read-modify-writes, and every odd scanline landing 8192 bytes from its neighbour. This is what drawing meant before anything accelerated it.

  29. fill the region your lines enclose paint 100 120 3

    A fill only works if the outline is closed. One missing pixel and the colour escapes into the rest of the screen, which is the oldest bug in every paint program ever written.

  30. Boss missioncompose the era logo from the mode, palette and strokes you set up draw logo

    Four colours, 16 KB, two banks and one read-modify-write per byte. Everything after this era — planes, DACs, shaders — exists to take one of those limits away.

Certificate

This track is certifiable. Clear the boss mission in the terminal, then run EXAM CGA for the written paper: 20 server-graded questions drawn from our own bank, pass mark 14 of 20. The certificate is issued once both are done, and it carries a verification code.

Nearby eras

Previous
2004 · Blue Team Ops
Defensive operations on real log data: hunt failed logins in auth.log, cut fields with awk, read firewall rules, work an incident.
Next
1984 · EGA
Program the EGA: 640x350 in 16 colours out of 64, four bit planes, the latch registers, palette registers and hardware scrolling.

All 25 eras in the Terminal Academy

Open CGA in the terminal