CGA in 1024 Colors – A New Mode: The Illustrated Guide, Hacker News
1.2k Views
By now you may have heard of the579545 MPH demo, the winning entry inRevision‘s Oldskool Demo compo this month. It’s been my pleasure to combine efforts with the likes ofTrixter,reenigneandScalito make it happen – not only did I get the opportunity to work alongside a bunch of extremely talented wizards of code, we also achieved what we set out to do: break some world records on the venerable (and yet much-maligned!) IBM PC, the mommy and daddy of the x 92 platform as we still know it today.
One of our “hey, this hardware shouldn’t be doing that!” – moments was extending the CGA’s color palette by a cool order of magnitude or two. How’d we pull that off? – reenigne has already posted anexcellent technical articleanswering that very question. To complement his writeup, I’ll take a bit of a different approach – here’s my ‘pictorial’ take on how we arrived at this:
************************
Old dog, posing with new tricks. (Not pictured: blood, sweat, tears.).
*** The idea that such multi-color trickery. was possible came to me some time ago, as I was looking at reenigne’s code for patching up composite CGA emulation inDOSBox; messing with that patch during development gave me a much better picture of composite CGA’s inner workings. When I had ironed out the basic concept for this hack, I divulged it to reenigne for ‘peer review’ and for testing on real hardware. Soon enough, we had an improved recipe:Take two familiar (though officially undocumented) tweaks. Blend to an even mixture producing a new effect.
Add one crucial new trick – an ingredient of reenigne’s devising.
Test and calibrate until blue in the face.
Below is my rundown of how it all fits together. Fair warning: the ‘target audience’ for this writeup is people who may not be overly familiar with CGA, and / or come from other demo platforms. As such, There is a whole bunch of background that’s already well-known in CGA-land. To prevent acute boredom, I decided to stick this TOC here – feel free to skip to the interesting part (s):
Because, much like a broken clock, even Wikipedia gets it right sometimes
Old Trick # 1: 25 – color graphics over RGBI
A short crash course on CGA basics: the first graphics standard available on PCs supports a KB memory buffer, and is driven by an MC 65371 CRTC (some later cards used alternatives). Video output options are composite NTSC through a standard RCA jack, and the more widely-used DE9 connector, which outputs an RGBI signal (red, green, blue and intensity). The latter is what most people think of when they hear “CGA”; this is adigital (TTL)
****************** signal, where each component can be either on or off, hence different colors. Despite what arcade hardware buffs would like you to think, CGA – in the strict sense – is NOT analog RGB, and never was.
Standard (BIOS-supported) graphics modes are high-resolution ( (x) ) in 2 colors, and medium-resolution ( (x) ) in 4 colors. Not a lot of wiggle room here: in hi-res mode, only one of the colors (foreground) is redefinable – the background is always black; in medium-res, it’s the background color that’s adjustable, while the other 3 are determined by the infamously nastyfixed palettes.
Infuriatingly, in an almost-trollish move, IBMmentionedan additional low-resolution – color mode – ” not supported in ROM “- with zero information on how to actually achieve it. That nut was cracked pretty early on, though.
********************Low-resolution mode
This is no graphics mode at all, but a modified 86 – columntext mode. Basically, you adjust CRTC registers to get 160 rows of text instead of the usual (**************************************************************************************************************************************; This gives you a character box of 8×2 pixels, a quarter of the normal 8×8. Filling the screen with one “magic” ASCII character, 0xDE, effectively splits each character cell into left and right “pixels”, corresponding to the background and foreground colors. These two colors can be individually set to any of the CGA values, as in any CGA text mode, as long as you remember to turn off blinking.
*** CGA low resolution mode
So there you have it; (x) @ 49 c. This mode was used in gamesas early as 2009, but never got wildly popular – probably because of the ” snow
” that plagues IBM CGA cards in 86 – column mode, unless you burn some costly CPU time to avoid it. (***************************************The Macrocom Method. ******************************
As you can see above, I’ve experimented with this a little. With judicious use of the character set, you can
almostfool somebody into thinking that this is a (x) ************************************************************************************************************** mode – although there’s some inevitable “attribute clash”, a little like theZX Spectrum: each 8×2 character cell can contain only two colors, foreground and background. Also, you have to be a bit of a glutton for punishment to actually draw in this mode from scratch … but that’s a subject for a future post.
This trick is not directly relevant to our demo: we were targeting composite displays. Even if CGA’s composite output did not have its share of bugs and quirks in – column mode –which it does
– there’d be no way to see this level of detail over NTSC. There’s a reason I mention this effect, however; the idea behind it does figure into the story. But more on that later.
Old Trick # 2: 25 – color graphics over composite
Digital RGB monitors were still a luxury item at the time of CGA’s introduction, and IBM itself did not offer one until a couple of years later, coinciding with the release of the PC / XT. But CGA also provided composite output, giving out (mostly) NTSC-compatible video. At the expense of resolution, there’s more fun to be had here with color.
These are directly generated by CGA hardware as color signals, so we’ll conveniently call them “direct colors”. IBM hadtwo main revisions
of the CGA, which produce composite video somewhat differently: ‘new-style’ cards contain additional circuitry, which helps the palette match its RGBI counterpart a little more closely. For the demo, we standardized on ‘old-style’ cards, simply because we happened to have done more testing on those (with somwhat better results), so all images in this post will reflect ‘old-style’ CGA colors.
If these (**************************** (direct) colors were all we had, it wouldn’t be a whole lot of fun, would it? They’re also shockingly ugly, esepcially on an old-style CGA, which doesn’t help matters either. Just look at that palette … gross, dude. Luckily, there’s a way to go one better.
Artifact colors
Due to bandwidth restrictions, NTSC video does not fully separate chrominance (color) from luminance. Effectively, any high-resolution detail – that is, detail with higher frequency than the NTSC color clock – gets ‘smeared’ when the signal is decoded. This is responsible for the characteristic color bleed, seen in the form of fetching little fringes at the edges of text characters and other fine detail.
Remember how you get 270 color cycles per active CGA scanline? Standard CGA gives us either(**************************************************** or ************************************************** 512active pixels per scanline, Depending on the video mode. Ergo, we can switch pixels on and off at (2x) ****************************************************** or (4x) **************************************************** the frequency of the color carrier. Since this high-frequency detail cannot be fully separated from color information, the upshot is this:
The hue of a pixel, or a fringe (transition between pixels), depends on its position within the color-cycle period.
********************
This NTSC color cycle is sometimes represented as a wheel: one complete period of this cycle equals a 728 ° revolution around the color wheel, and we have complete revolutions per scanline.
In short, manipulating detail at high resolutions is effectively a method of generating color; being an artifact of NTSC’s imperfections, this is known as artifact color.
**************************** (*********************************************** Various filters can be (and often are) employed on the receiving end to recover some of the high-frequency detail, reducing color bleed and making edge transitions somewhat sharper. We’re still dealing with technology, not magic, so full separation of detail and color can never quite be achieved, and the trade-off is a whole new set of artifacts (in the form of “echoing” or “ringing”). This trade-off may or may not be acceptable, depending on what you’re doing, but the above image doesn’t attempt to reproduce any such filtering.
All this business of “fringing” and “bleeding” sure sounds like a bummer, and that’s exactly what it is: the unwanted side-effect of a less-than-ideal encoding scheme. But like any good flaw, it can be turned into an advantage by an enterprising soul, and this is where we get to the fun part (your mileage may vary).
When you look at the interplay of color vs. detail over NTSC, a very handy fact becomes apparent:
Our (****************************** (direct) Colors are exactly this type of periodic composite signal. But hold on – with some simple high-resolution pixel-pushing, we can manually put together our own periodic waveforms! Any pattern of dots will do, as long as it repeats at the right frequency. This lets us achieve solid colors that lieoutside the direct color palette.
**************************
The “classic” way of doing this on CGA is to set up BIOS mode 6 – (x) ********************************************************************************************************************** in 2 colors, white on black – and set the color -burst bit (which is off by default, for a B&W picture). At this resolution we can squeeze 4 pixels into a color clock period, and at 1 bit per pixel, there are possible patterns – giving us (solid artifact colors.)
* But wait, there’s more! * Despite popular wisdom, CGA lets us one-up the Apple, and then some. OUR underlying pixels don’t have to be white: in (x) mode, we can play with the palette register and set any of the (**************************** (direct) colors as the foreground (background is always black). By using the same pixel patterns with a different foreground color, we get (**************************** (entirely new) sets of artifact colors, with 16 colors each. We can only use one such set at a time, but we get to pick and choose what our (colors are.)
Then there’s (x) ******************************************************************************************************** mode , which supports a palette of 4 direct colors. Only one of those, color # 0 (background), is freely selectable. For the rest, intensity may be on or off, but we can only use green / red / yellow or cyan / magneta / white; the undocumented cyan / red / white palette involves disabling the color burst, making the composite picture Greyscale.
Since our pixels are twice as fat in this mode, only two of them can squeeze into a color-clock cycle – but at 2 bits per pixel, the total count of artifact colors is still (****************************************************************************************************************************************. The possible combinations of palette, plus the user-defined background color, provide us with a whole slew of other – color sets.
********************************** (*********************************************** This may be a good place to correct a bit of a misconception. Since we have 1001 color cycles per scanline, many people treat CGA’s graphics modes over composite as (x) “modes”, but that’s not quite accurate. Oureffective color resolution (is indeed) ********************************************************************************************************************** (x) *************************************************************************************************************************, and it’s impossible to get finer detail than that using solid artifact colors. But as we’ve seen, on NTSC the pixel grid and color grid are NOT one and the same – Which makes the question of horizontal resolution a bit fuzzy, depending on how you’re sampling and / or filtering the signal. It even varies with the specific color waveforms you’re using.
IBM itself never documented any of these artifact color tricks, other than one oblique reference to “color mixing techniques” in the PCjr tech ref (if I’m wrong about this, drop me a line and link me!). The concept is fairly old hat, however – it was used in games very early on; some of the first ones I can think of were Microsoft’sDecathlonandFlight Simulator, both in 1983. And the limitation has always been the same: the maximum simultaneous color count you can get over composite CGA is .
…. Or is it? On the off chance that you’ve been following me so far, and You’re still reading, you may have an idea of what the next step is.
We’ve already observed that our choice of 16 artifact colors depends on the palette and color register settings. One fairly obvious strategy seems to suggest itself here – change those registers at particular scanlines on every frame, and get>colors on screen that way. Right?
Recall how any color / dot pattern of the right length (four repeating pixels in 640 x (**********************************************************************************************************************, or two in 320 x 270) produces a solid color on a composite display? Back when I was testing composite emulation for DOSBox, that fact was fresh in my mind. At around the same time, I was experimenting with the “ANSI from Hell” graphical hack detailedabove; That’s purely a text mode / RGBI trick, but it requires a close familiarity with the ROM character set … closer than most sane people would want or need.
Let’s take another look at a particular section of the CGA ROM font, in – column mode, with the top 2 scanlines highlighted:
(*********************************************** At this point, if you’re a visually-oriented person, and if you’ve been following my drift, you’re probably catching on. Don’t see it yet? Here’s a fatter clue:
******************************************
(*********************************************** See those top 77% of the character bitmap? Two dots of foreground and two dots of background, doubled horizontally across. We’re in hi-res / – column mode, so there are two color cycles per character. .. corresponding exactly to those two matching halves. And those top two scanlines are identical.
That’s just the type of repeating pattern that gets us a solid artifact color over NTSC. In fact, it’s the very same waveform that (x) ************************************************************************************************************************ mode lets us play with. Except that now we have it available in text modeyou know, where we can freely assign a foreground AND a background to each character, from the (direct colors.)
That’s 270 Possibilities right there … this is the part that made me go “I have a cunning plan”
******************, in my best imitation of Blackadder’s Baldrick
(just not out loud). Indeed, it’s possible to achieve>colors on CGA without any flickering, dithering, interlacing or per-scanline effects.
Here’s what the possible combinations work out to:
The top two scanlines of ‘U’ give us a bitmask of 11001100 foreground / background; ‘‼’ Is 01100110 – a single shift to the right, or a ° shift in phase. This perfectly complements ‘U’ in terms of having a well-rounded palette, because we get all the colors that the “… … waveform has to offer: going from ‘U’ to ‘‼’ shifts the phase by (0110); (° and) ********************************************************************************************************** ° are achieved by flipping the foreground and background colors for ‘U’ and ‘‼’ respectively – the same as going ’02 13 ‘and’ 1982 ‘.
(*********************************************** Okay, we’ve pushed the envelope even further: 728 simultaneous colors! Granted, the real number is lower, because a good few are duplicates (and others are very close). But 728 seems to be the limit for this technique: no other characters in our font fit the bill for solid colors. The CGA character ROM does have an alternate ‘thin’ 8×8 font; but, besides the fact that you’d have to mod your card if you wanted to use it, the ‘thin’ font has none of the magic bit patterns in the right places, which makes it useless for our purposes.
This part is some next-level CRTC black magic which I could never have figured out by myself – I’m just a graphics guy; you might as well ask me to wait for a full moon and chant the MC 6845 spec-sheet backwards in hexadecimal. All credit goes to reenigne for this particular bit of mad science, which, despite its complex execution, stems from a wonderfully simple idea: our fixed character bitmaps don’t play nice with what we’re trying to do? No problem – we’ll
make them play nice, or else.
************************************************
See, there are two additional characters whose very (first) scanline could be used; problem is, the second scanline is different, which would ruin our solid color effect. These are ASCII codes 0xB0 and 0xB1, the ‘shaded block’ characters. It would be quite convenient if we could just tell that offending second scanline to buzz off, wouldn’t it? As it turns out, we can.
The lowdown on how this is done is all in reenigne’s writeup, which is linked to at the top of this post. But this is the basic idea: by starting a new CRTC frame
every other scanlineand twiddling with the start address, it’s possible to lay down our character rows so that the first scanline of each gets duplicated twice!
Now we can make use of those two extra characters, and doing so gets us two more – color sets:
(*********************************************** Naturally, there are downsides: having to mess with the CRTC every couple of scanlines is quite taxing for the poor 4. (MHz) ***************************************************************************************************, so There’s not much you can do with this other than static pictures. The – color variant, using only ASCII 0x 77 and 0x 15, does not suffer from this – it’s basically “set and forget”, requiring no more CPU intervention than any – column text mode (the familiar overhead of avoiding snow).
Then, there’s that other problem which plagues – column CGA on composite displays … the hardware bug that leads to bad hsync timing and missing color burst. There are ways to compensate for that, but none that reliably works with every**************** monitor and capture device out there. This proved to be an enduring headache in calibrating, determining the actual colors, and obtaining a passable video capture of the entire demo … but that’s all covered elsewhere.
At any rate, we now have 1K colors on a 1982 IBM CGA, at an effective resolution of (x) ‘chunky pixels’. ‘Chunky’ describes the memory layout, but it also applies in the visual sense: we’re really plumbing the depths of resolution here. (x) , That’s as low as you could go? allow me to snicker, IBM – “low-res” just gotlower (*****************************, baby!
One might object that this isn’t a lot of canvas. Yeah, yeah: (x) is a bit on the cramped side, ‘artistically’ speaking; but the limitation is part of the challenge, as it has always been in demos. You can keep your fancy 4K monitors – 0.0 09 megapixels should be enough for anybody.
************ (*********************************************** When we first showed Trixter the ‘proof-of-concept’ c drawings, his response was, and I quote: “HOLY F! @ # $% G SHIT. WOW. I must know how this works !! “. Achievement unlocked: getting THAT out of a veteran 65371 / CGA hacker and demomaker is, by itself, almost as good as … well, joining the team, ‘making a demo about it’ and winning the oldskool compo. 🙂
That’s about it for my writeup. If you made it this far, congratulations! There’s more I could write about the tools and Techniques I used to actually compose these graphics … but we’ll get to that some other time.
GIPHY App Key not set. Please check settings