CGA in 1024 Colours – a New Mode: the Illustrated Information
Half #1 of the 8088 MPH writeup collection • continued in 8088 MPH Final: Old vs. New CGA (and Other Gory Details)
By now you will have heard of the 8088 MPH
demo, the successful entry in
Revision 2015‘s Oldskool Demo compo
this month. It has been my pleasure to mix efforts with the likes of
Trixter,
reenigne and
Scali to make it occur – not solely did
I get the chance to work alongside a bunch of extraordinarily gifted
wizards of code, we additionally achieved what we got down to do: break some
world data on the venerable (and but much-maligned!) IBM PC, the
mommy and daddy of the x86 platform as we nonetheless comprehend it at present.
Considered one of our “hey, this {hardware} should not be doing that!”-moments was
extending the CGA’s coloration palette by a cool order of magnitude or two.
How’d we pull that off? – reenigne has already posted an excellent
technical
article
answering that very query. To enhance his writeup, I will take a
little bit of a special method – here is my ‘pictorial’ tackle how we
arrived at this:
The concept such multi-color trickery was potential got here to me some
time in the past, as I used to be taking a look at reenigne’s code for patching up composite
CGA emulation in DOSBox; messing with that
patch throughout improvement gave me a significantly better image of composite
CGA’s internal workings. After I had ironed out the fundamental idea for this
hack, I divulged it to reenigne for ‘peer overview’ and for testing on
actual {hardware}. Quickly sufficient, we had an improved recipe:
- Take two acquainted (although formally undocumented) tweaks. Mix to
an excellent combination producing a brand new impact. - Add one essential new trick – an ingredient of reenigne’s devising.
- Take a look at and calibrate till blue within the face.
Beneath is my rundown of the way it all suits collectively. Honest warning: the
‘target market’ for this writeup is individuals who will not be overly
accustomed to CGA, and/or come from different demo platforms. As such,
there’s a complete bunch of background that is already well-known in
CGA-land. To stop acute boredom, I made a decision to stay this TOC right here –
be at liberty to skip to the attention-grabbing half(s):
As a result of, very similar to a damaged clock, even Wikipedia will get it proper generally
Outdated Trick #1: 16-color graphics over RGBI
A brief crash course on CGA fundamentals: the primary graphics normal
accessible on PCs helps a 16KB reminiscence buffer, and is pushed by an
MC6845 CRTC (some later playing cards used options). Video output choices
are composite NTSC by a regular RCA jack, and the extra widely-used
DE9 connector, which outputs an RGBI sign (purple, inexperienced, blue and
depth). The latter is what most individuals consider after they hear
“CGA”; this can be a digital (TTL) sign, the place every element will be
both on or off, therefore 16 totally different colours. Regardless of what arcade
{hardware} buffs would love you to assume, CGA – within the strict sense – is
NOT analog RGB, and by no means was.
Normal (BIOS-supported) graphics modes are high-resolution (640×200)
in 2 colours, and medium-resolution (320×200) in 4 colours. Not plenty of
wiggle room right here: in hi-res mode, solely one of many colours (foreground) is
redefinable – the background is all the time black; in medium-res, it is the
background coloration that is adjustable, whereas the opposite 3 are decided by
the infamously nasty fixed
palettes.
Infuriatingly, in an almost-trollish transfer, IBM
mentioned
a further low-resolution 16-color mode – “not supported in ROM” –
with zero info on easy methods to really obtain it. That nut was
cracked fairly early on, although.
Low-resolution mode
That is no graphics mode in any respect, however a modified 80-column text
mode.
Principally, you regulate CRTC registers to get 100 rows of textual content as a substitute of
the standard 25; this provides you a personality field of 8×2 pixels, 1 / 4 of
the traditional 8×8. Filling the display screen with one “magic” ASCII character,
0xDE, successfully splits every character cell into left and proper
“pixels”, equivalent to the background and foreground colours. These
two colours will be individually set to any of the 16 CGA values, as in
any CGA textual content mode, so long as you bear in mind to show off blinking.
So there you’ve got it; 160×100 @ 16c. This mode was utilized in video games as
early as
1983,
however by no means bought wildly in style – most likely due to the
“snow”
that plagues IBM CGA playing cards in 80-column mode, except you burn some
pricey CPU time to keep away from it.
The Macrocom Methodology
It’s possible you’ll ask: since that is textual content mode, what’s stopping you from utilizing the
total ASCII character set? Aside from a wholesome respect on your personal
sanity, nothing actually! This was first tried across the mid-’80s by
just a few courageous souls at
Macrocom, who
mixed the 100-rows trick with ASCII artwork, to create what Trixter as soon as
succinctly known as “ANSI from hell”.
As you’ll be able to see above, I’ve experimented with this a bit. With
even handed use of the character set, you’ll be able to virtually idiot any person into
considering that this can be a 640×200 mode – though there’s some inevitable
“attribute conflict”, a bit just like the ZX
Spectrum: every 8×2
character cell can include solely two colours, foreground and background.
Additionally, it’s important to be a little bit of a glutton for punishment to really draw
on this mode from scratch… however that is a topic for a future submit.
This trick is not instantly related to our demo: we had been focusing on
composite shows. Even when CGA’s composite output did not have its share
of bugs and quirks in 80-column mode – which it
does
– there’d be no solution to see this stage of element over NTSC. There is a
cause I point out this impact, nonetheless; the concept behind it does determine
into the story. However extra on that later.
Outdated Trick #2: 16-color graphics over composite
Digital RGB displays had been nonetheless a luxurious merchandise on the time of CGA’s
introduction, and IBM itself did not provide one till a few years
later, coinciding with the discharge of the PC/XT. However CGA additionally offered
composite output, giving out (largely) NTSC-compatible video. On the
expense of decision, there’s extra enjoyable available right here with coloration.
Direct colours
On the composite output, the acquainted 16-color CGA palette is
represented by a collection of coloration indicators, whose hue is decided by
their part relative to a reference sign (the NTSC coloration
burst). The frequency of the NTSC coloration clock (3.579545 MHz) works
out to precisely 160 coloration cycles per energetic CGA scanline.
These are instantly generated by CGA {hardware} as coloration indicators, so we’ll
conveniently name them “direct colours”. IBM had two main
revisions
of the CGA, which produce composite video considerably in a different way:
‘new-style’ playing cards include further circuitry, which helps the palette
match its RGBI counterpart a bit extra intently. For the demo, we
standardized on ‘old-style’ playing cards, just because we occurred to have
carried out extra testing on these (with somwhat higher outcomes), so all pictures
on this submit will mirror ‘old-style’ CGA colours.
If these 16 direct colours had been all we had, it would not be a complete lot
of enjoyable, wouldn’t it? They’re additionally shockingly ugly, esepcially on an
old-style CGA, which doesn’t assist issues both. Simply take a look at that
palette… gross, dude. Fortunately, there is a solution to go one higher.
Artifact colours
Resulting from bandwidth restrictions, NTSC video does not totally separate
chrominance (coloration) from luminance. Successfully, any high-resolution
element – that’s, element with greater frequency than the NTSC coloration clock
– will get ‘smeared’ when the sign is decoded. That is liable for the
attribute coloration bleed, seen within the type of fetching little fringes
on the edges of textual content characters and different wonderful element.
Bear in mind the way you get 160 coloration cycles per energetic CGA scanline? Normal
CGA offers us both 320 or 640 energetic pixels per scanline,
relying on the video mode. Ergo, we will swap pixels on and off at
2x or 4x the frequency of the colour provider. Since this
high-frequency element can’t be totally separated from coloration info,
the upshot is that this:
The hue of a pixel, or a fringe (transition between pixels), will depend on its place inside the color-cycle interval.
This NTSC coloration cycle is typically represented as a wheel: one full
interval of this cycle equals a 360° revolution across the coloration wheel,
and we’ve 160 full revolutions per scanline.
For instance we’re in hi-res (640×200) mode, the place 4 pixels match into one
such coloration cycle: shifting one pixel left or proper interprets to shifting
90° alongside the wheel, in both path, and accordingly shifts the hue
by 90°. Likewise, in 320×200 mode, we transfer in 180° increments of
hue-shift.
In brief, manipulating element at excessive resolutions is
successfully a way of producing coloration; being an artifact of NTSC’s
imperfections, this is named artifact coloration.
Varied filters will be (and sometimes are) employed on the receiving finish to
get better a few of the high-frequency element, lowering coloration bleed and
making edge transitions considerably sharper. We’re nonetheless coping with
expertise, not magic, so full separation of element and coloration can by no means
fairly be achieved, and the trade-off is a complete new set of artifacts (in
the type of “echoing” or “ringing”). This trade-off might or will not be
acceptable, relying on what you are doing, however the above picture does not
try to breed any such filtering.
Strong artifact colours
All this enterprise of “fringing” and “bleeding” positive seems like a
bummer, and that is precisely what it’s: the undesirable side-effect of a
less-than-ideal encoding scheme. However like all good flaw, it may be
became a bonus by an enterprising soul, and that is the place we
get to the enjoyable half (your mileage might range).
Whenever you take a look at the interaction of coloration vs. element over NTSC, a really
helpful reality turns into obvious:
Any periodic composite sign, with the identical frequency as the colour provider (160 per line), will probably be decoded as a strong, steady coloration.
Our 16 direct colours are precisely this kind of periodic composite
sign. However maintain on – with some easy high-resolution pixel-pushing,
we will manually put collectively our personal periodic waveforms! Any sample of
dots will do, so long as it repeats on the proper frequency. This lets
us obtain strong colours that lie exterior the direct coloration palette.
The “traditional” approach of doing this on CGA is to arrange BIOS mode 6 –
640×200 in 2 colours, white on black – and set the color-burst bit (which
is off by default, for a B&W image). At this decision we will
squeeze 4 pixels right into a coloration clock interval, and at 1 bit per pixel,
there are 16 potential patterns – giving us 16 strong artifact colours.
That is just about the identical
technique
utilized by Steve Wozniak to generate coloration on the Apple ][. Actually, on
an old-style CGA card, these 16 colours are an identical to the 16 low-res
Apple colours (though you could not get them on a poster, like Apple
house owners
could).
Extra to the purpose: the pixels themselves are white, which carries no
coloration info; it is the element that does the deed.
*However wait, there’s extra!* Regardless of in style knowledge, CGA lets us one-up
the Apple, after which some. OUR underlying pixels do not need to be white:
in 640×200 mode, we will play with the palette register and set any of
the 16 direct colours because the foreground (background is all the time black).
By utilizing the identical pixel patterns with a special foreground coloration, we
get 16 fully new units of artifact colours, with 16 colours every. We
can solely use one such set at a time, however we get to choose and select what
our 16 colours are.
Then there’s 320×200 mode, which helps a palette of 4 direct colours.
Solely a kind of, coloration #0 (background), is freely selectable. For
the remainder, depth could also be on or off, however we will solely use
inexperienced/purple/yellow or cyan/magneta/white; the undocumented cyan/purple/white
palette entails disabling the colour burst, making the composite image
greyscale.
Since our pixels are twice as fats on this mode, solely two of them can
squeeze right into a color-clock cycle – however at 2 bits per pixel, the full
depend of artifact colours continues to be 16. The potential combos of
palette, plus the user-defined background coloration, present us with a complete
slew of different 16-color units.
This can be place to right a little bit of a false impression. Since we
have 160 coloration cycles per scanline, many individuals deal with CGA’s graphics
modes over composite as 160×200 “modes”, however that is not fairly correct.
Our efficient coloration decision is certainly 160×200, and it is inconceivable
to get finer element than that utilizing strong artifact colours. However as we have
seen, on NTSC the pixel grid and coloration grid are NOT one and the identical –
which makes the query of horizontal decision a bit fuzzy, relying
on the way you’re sampling and/or filtering the sign. It even varies with
the precise coloration waveforms you are utilizing.
IBM itself by no means documented any of those artifact coloration tips, different
than one indirect reference to “color mixing
techniques”
within the PCjr tech ref (if I am flawed about this, drop me a line and hyperlink
me!). The idea is pretty previous hat, nonetheless – it was utilized in video games
very early on; a few of the first ones I can consider had been Microsoft’s
Decathlon
and Flight
Simulator,
each in 1982. And the limitation has all the time been the identical: the utmost
simultaneous coloration depend you may get over composite CGA is 16.
….Or is it? On the off likelihood that you’ve got been following me thus far, and
you are still studying, you will have an thought of what the following step is.
256 colours
We have already noticed that our alternative of 16 artifact colours will depend on
the palette and coloration register settings. One pretty apparent technique
appears to counsel itself right here – change these registers at specific
scanlines on each body, and get >16 colours on display screen that approach.
Proper?
This has been carried out earlier than on CGA, and also you can really exploit this
for 256 colours (as confirmed by reenigne – see the picture to the left), however
that is not how we did our multi-color hacking within the demo. We had been
really toying with the concept of together with a static display screen that makes use of
this method, however I did not have the time to pursue this; if anybody
manages to compose some good art work utilizing this technique, I would like to see
it – that is gotta be a little bit of an inventive problem. However no, the best way we
wrangled extra coloration out of CGA is a complete different shenanigan… which I got here
throughout by equal components likelihood and morbid curiosity.
Recall how any coloration/dot sample of the fitting size (4 repeating
pixels in 640×200, or two in 320×200) produces a strong coloration on a
composite show? Again once I was testing composite emulation for
DOSBox, that reality was recent in my thoughts. At across the similar time, I used to be
experimenting with the “ANSI from Hell” graphical hack detailed
above;
that is purely a textual content mode / RGBI trick, but it surely requires an in depth
familiarity with the ROM character set… nearer than most sane individuals
would need or want.
Let’s take one other take a look at a selected part of the CGA ROM font, in
80-column mode, with the highest 2 scanlines highlighted:
At this level, when you’re a visually-oriented individual, and when you’ve been
following my drift, you are most likely catching on. Do not see it but?
This is a fatter clue:
See these high 25% of the character bitmap? Two dots of foreground and
two dots of background, doubled horizontally throughout. We’re in
hi-res/80-column mode, so there are two coloration cycles per character…
corresponding precisely to these two matching halves. And people high two
scanlines are an identical.
That is simply the kind of repeating sample that will get us a strong artifact
coloration over NTSC. Actually, it is the exact same waveform that 320×200 mode
lets us play with. Besides that now we’ve it accessible in textual content mode:
you realize, the place we will freely assign a foreground AND a background to
every character, from the 16 direct colours.
That is 256 potentialities proper there… that is the half that made me go
“I’ve a crafty plan”, in my greatest imitation of Blackadder’s
Baldrick
(simply not out loud). Certainly, it is potential to attain >16 colours on
CGA with none flickering, dithering, interlacing or per-scanline
results.
This is what the potential combos work out to:
512 colours
Oh, we’re not carried out but: as soon as that lightbulb went off over my head, I had
one other take a look at the CGA ROM font to see if some other helpful bit
sequences emerge. There are just a few character bitmaps that give us the
very same waveform as ‘U’ does – ‘H’, ‘V’, ‘Y’ and ‘¥’ – however just one
with a totally different appropriate bit sequence proper the place we want it: 0x13,
the double exclamation mark (‘‼’).
The highest two scanlines of ‘U’ give us a bitmask of 11001100 for
foreground/background; ‘‼’ is 01100110 – a single shift to the fitting, or
a 90° shift in part. This completely enhances ‘U’ by way of having
a well-rounded palette, as a result of we get all the colours that the “…1100…”
waveform has to supply: going from ‘U’ to ‘‼’ shifts the part by 90°
(0110); 180° and 270° are achieved by flipping the foreground and
background colours for ‘U’ and ‘‼’ respectively – the identical as going
‘0011’ and ‘1001’.
Okay, we have pushed the envelope even additional: 512 simultaneous colours!
Granted, the actual quantity is decrease, as a result of few are duplicates
(and others are very shut). However 512 appears to be the restrict for this
method: no different characters in our font match the invoice for strong
colours. The CGA character ROM does have an alternate ‘thin’ 8×8
font; however, moreover the very fact
that you just’d need to mod your card when you wished to make use of it, the ‘skinny’
font has not one of the magic bit patterns in the fitting locations, which makes
it ineffective for our functions.
My kingdom for redefinable characters… alas, if you’re coping with
previous PC {hardware}, IBM’s penchant for cost-cutting over innovation can
all the time sneak up from behind and wreck your day – even in essentially the most
uncommon of locations.
Nonetheless, I used to be happy with my little discovery: extending the palette by
an element of 32 has to depend for one thing, proper? At this level, I
shared my concepts with reenigne. Little did I do know that he’ll promptly
provide you with a brand new devious scheme to double our coloration depend but
once more…
1024 colours
This half is a few next-level CRTC black magic which I may by no means have
found out on my own – I am only a graphics man; you would possibly as nicely ask
me to attend for a full moon and chant the MC6845 spec-sheet backwards in
hexadecimal. All credit score goes to reenigne for this specific little bit of mad
science, which, regardless of its advanced execution, stems from a splendidly
easy thought: our fastened character bitmaps do not play good with what we’re
attempting to do? No downside – we’ll make them play good, or else.
See, there are two further characters whose very first scanline
may very well be used; downside is, the second scanline is totally different, which might
wreck our strong coloration impact. These are ASCII codes 0xB0 and 0xB1, the
‘shaded block’ characters. It will be fairly handy if we may simply
inform that offending second scanline to buzz off, would not it? Because it
seems, we will.
The lowdown on how that is carried out is all in reenigne’s writeup, which is
linked to on the high of this submit. However that is the fundamental thought: by
beginning a brand new CRTC body each different scanline and playing with the
begin handle, it is potential to put down our character rows in order that the
first scanline of every will get duplicated twice!
Now we will make use of these two further characters, and doing so will get us
two extra 256-color units:
Naturally, there are downsides: having to mess with the CRTC each
couple of scanlines is sort of taxing for the poor 4.77MHz 8088, so
there’s not a lot you are able to do with this apart from static footage. The
512-color variant, utilizing solely ASCII 0x55 and 0x13, does not undergo from
this – it is mainly “set and neglect”, requiring no extra CPU
intervention than any 80-column textual content mode (the acquainted overhead of
avoiding snow).
Then, there’s that different downside which plagues 80-column CGA on
composite shows… the {hardware} bug that results in unhealthy hsync timing and
lacking coloration burst. There are methods to compensate for that, however none
that reliably works with each monitor and seize system on the market.
This proved to be a permanent headache in calibrating, figuring out the
precise colours, and acquiring a satisfactory video seize of the complete
demo… however that is all lined elsewhere.
At any fee, we now have 1K colours on a 1981 IBM CGA, at an efficient
decision of 80×100 ‘chunky pixels’. ‘Chunky’ describes the reminiscence
structure, but it surely additionally applies within the visible sense: we’re actually plumbing
the depths of decision right here. 160×100, that is as little as you might go?
permit me to snicker, IBM – “low-res” simply bought decrease, child!
One would possibly object that this is not plenty of canvas. Yeah, yeah: 80×100 is
a bit on the cramped facet, ‘artistically’ talking; however the limitation
is a part of the problem, because it has all the time been in demos. You may preserve
your fancy 4K displays – 0.008 megapixels must be sufficient for
anyone.
Once we first confirmed Trixter the ‘proof-of-concept’ 1024c drawings, his
response was, and I quote: “HOLY F!@#$%G SHIT. WOW. I have to know the way
this works!!”. Achievement unlocked: getting THAT out of a veteran
8088/CGA hacker and demomaker is, by itself, virtually nearly as good as… nicely,
becoming a member of the workforce, ‘making a demo about it’ and successful the oldskool
compo. 🙂
That is about it for my writeup. In case you made it this far,
congratulations! There’s extra I may write in regards to the instruments and
strategies I used to really compose these graphics… however we’ll get to
that another time.
Continued partly 2: 8088 MPH Final: Old vs. New CGA (and Other Gory Details)