How do you get 1024 colors in CGA? And are there really 1024?

Launched in 2015, the translation “New graphics mode: CGA in 1024 colors” impressed me very much, but from it I still did not understand how this trick works, because the article was written by a professional in the field of retro-computer graphics, and suggests that the reader already has some knowledge in this area. Although my first computer ( “Search” was a Soviet clone of IBM PC / XT) had CGA, I didn’t find monitors with a composite input, so I had to google it and figure out how the composite video worked. For those who are also interested in the “inside” technologies of 1981, in which unexpected undocumented opportunities were discovered in 2015, I put together the material I found.

1. Native CGA Features


For a layman, CGA is usually associated with 320x200 graphics and a four-color palette with acid-creepy colors, such as in the 1983 games Alley Cat , J-bird , Lode Runner and Tapper :




But the 320x200x2bpp limit is imposed only by the amount of video memory available on the adapter (16 Kb) , in fact, it is capable of producing an image of 640x200x4bpp - with pixels strongly elongated vertically.This is used, for example, in 80x25 text mode with 16-color characters and a background, and a character matrix of 8x8 pixels. (We can assume that the screen buffer in this case only takes 80x25x2 = 4000 bytes.) But if the graphics are output in this “high resolution” 640x200, then 16 KB of video memory is only enough for a black and white image. Such graphic mode was supported, for example, by Windows up to version 3.0 inclusive:



At the output, the CGA produced a digital signal for which the RGBI interface was specially invented: two synchronization lines signal the beginning of the frame and the beginning of the scan line, and four lines of the video signal transmit the four-bit color of the current pixel. But monitors capable of receiving an RGBI digital signal at the input were rare in 1981; IBM itself began to produce such digital monitors only in 1983. For home users, CGA had a second conclusion - the very composite video signal with which the masters of the demo scene conjured. It could be connected via an RF modulator to the antenna input of a conventional American TV, plus some televisions had a special composite input (for connecting, for example, a VCR) that allowed you to input a video signal bypassing the tuner and avoid unnecessary modulation / demodulation.



The part of the video adapter strapping, which forms the composite NTSC signal from the digital output (this is the American standard for color television), was intended only for the transitional stage until all PC users got digital monitors. The next video adapter from IBM - EGA (1984) - supported only digital (already six-bit) output, using a pair of previously unused contacts in the RGBI interface. In addition, the “high resolution” modes were not recommended for use with composite video output, because 640 pixels in the scan line exceeded the capabilities of NTSC. The next section explains why; and Wikipedia illustrates how text was blurred in 80-column CGA mode on a composite input monitor:




2. TV features


Black-and-white television was arranged in a very straightforward manner: an electron beam goes around the screen line by line, and the video signal level directly controls the power of the beam. During the flyback between the lines and between frames, the beam is turned off, and the video signal transmits synchronization pulses. Since the signal is completely analog, its horizontal resolution is not defined: the brightness can smoothly change throughout the line. On the other hand, the vertical resolution is hard-set by the horizontal scanning frequency (243 lines for NTSC).

The illustration, carefully drawn in MS Paint, shows how the video signal looks for a frame consisting of two vertical stripes: wide dark and narrow bright. At the top, a signal for one line is schematically shown, at the bottom, a signal for a sequence of lines. A sync pulse that is “blacker than black” is transmitted before each line . When color television was created, it was important that existing black-and-white televisions accept acceptable color video. The Americans came up with the following trick: color information is transmitted by a subcarrier harmonic signal



added to the luminance signal. Then a black-and-white TV that does not know how to isolate and analyze the subcarrier harmonic will highlight each pixel with a brightness corresponding to the average signal value during the pixel transmission time. If the subcarrier frequency is large enough, then the additional signal will not affect the average value, i.e. on the displayed brightness. Now we agree that the phase shift of the subcarrier corresponds to the hue of the color, and its amplitude corresponds to the saturation; the division of color into brightness, hue and saturation corresponds to the YIQ color model .

The second illustration schematically shows the video signal for one line of the frame, consisting of colored vertical stripes. An additional color burst is transmitted between the clock and the beginning of the line., which sets the receiver to the phase from which the subcarrier shifts will be measured. Now the flat sections of the signal (i.e., with zero amplitude of the subcarrier) will correspond to the gray bars (zero saturation); areas where the subcarrier corresponds in color burst phase to yellow bands with two different saturation values ​​depending on the subcarrier amplitude; plot where the subcarrier is in antiphase relative to the color burst - the blue bar. Since the average signal level is the same in all areas, a black-and-white television receiver displays all the bands with the same shade of gray. NTSC requires that the auxiliary signal be sinusoidal, but this is not so important: the square wave shown in the illustration ( meander


) consists of harmonics with the desired frequency and phase, as well as higher-frequency additives ignored by the television receiver. The amplitude of this harmonic is greater than the meander amplitude in4/π times, but since the TV uses the amplitude of the subcarrier not in absolute value, but relative to the amplitude extracted from color burst, the color display is not affected by the replacement of a sinusoidal additive with a rectangular one. CGA and other primitive digital devices generate exactly square waves.

In order to simplify the separation of the main signal (brightness) and the secondary (color), NTSC standardized the subcarrier frequency, 227.5 times higher than the horizontal frequency - i.e. for each row, along with the spacing between lines, there are 227.5 subcarrier periods. Thus, the American color TV has a certain horizontal resolution: the brightness can still change smoothly throughout the line, but the frequency of the color hue is limited by the subcarrier frequency. If a television receives a video signal with a horizontal resolution of 640 pixels, then approximately one third of the subcarrier period corresponds to each pixel, and a primitive hardware filter does not have time to correctly identify the phase shift and amplitude of the subcarrier from the signal for each pixel. This leads to unexpected results - from blurring,as shown in the example above, before creating"Artifact" colors , unattainable by regular means of the video adapter. (I really don't like the name "spurious colors" usedSLY_Gin translation of an article about 1024 colors; The main meaning of the English word artifact - “man-made, created artificially” - contrasts these colors with the “natural” for CGA, and has nothing to do with parasitism.)

Exactly with the horizontal resolution, a hardware bug in CGA was connected: in 640x200 modes (i.e. h. in 80-column text) the image on the composite monitor turned out to be black and white. The workaround for this bug was well known: you need to set a brown frame around the image. The fact is that the strapping that forms the clock signals sets their duration in clock cycles of the video adapter; in 640x200 modes, these clocks are half as long as in 320x200 modes, so the clock between the lines of the frame is also twice as short as NTSC requires. As a result, color burst just does not have time to be transmitted. But if there is a brown frame around the image (brown has almost zero phase shift, low brightness, and saturation corresponds to an approximate amplitude for color burst), then the monitor takes the left side of the frame as color burst, does not draw it on the screen, and is correctly set to color subcarrier phase.



3. CGA composite signal device


Although the digital video signal produced by CGA can have a horizontal resolution of up to 640 pixels (ie 640 changes in the levels of digital signals in one line), a composite video signal can be thought of as having a resolution of 1280 “subpixels” regardless of the video mode used, i.e. for each pixel in 640x200 modes there are two subpixels, in 320x200 modes - four. The signal level (in volts) output by the video adapter for the current subpixel is calculated by the formulaU=0.41+I⋅0.3+C⋅0.75 , where theI(intensity)bitis taken directly from the color of the current pixel, and theC(chroma)signalis selected by the multiplexer depending on the other three color bits (R, G, B):
RGBCColour
00000000000
the black
00100001111
blue (~ 180 °)
01011100001
green (~ 45 °)
01111000011
turquoise (~ 90 °)
10000111100
red (~ 270 °)
10100011110
magenta (~ 225 °)
11011110000
yellow (~ 0 °)
11111111111
white
Chromatic colors correspond to a chroma signal with a period of eight subpixels and with a phase shift close to a multiple of 45 °, i.e. to an integer number of subpixels. Thanks to the ingenious delay schemes in CGA, the phase shifts of the colors are slightly (within ± 10 °) different from the multiples of 45 ° to be closer to the standard NTSC colors .

Thus, out of 227.5 subcarrier periods in a line, 160 are in the output image, and the rest are in the interval between lines (clock, color burst, and frame around the image). It is important that a non-integer number of subcarrier periods passes from color burst to the first pixel of the line: the first pixel starts to be transmitted at 45 °, so if the line starts with four yellow pixels, the chroma signal will take values ​​during the transmission of the corresponding eight sub-pixels 11100001.

Pixel brightness (luma), i.e. the average value of the signal level for the period of the subcarrier, getL=0.72⋅Cavg+0.28⋅I , whereCavg=R∧G∧B+R∹G∹B2 : 0.5 for chromatic colors, 0 for black and 1 for white. This means that on a black and white composite monitor, the first CGA (1981) model could only display six shades of gray. In later CGA models (since 1983), the non-periodic addition to the subcarrier began to depend on all four color bits, so that the brightness corresponded to a more complex formulaL=0.29⋅Cavg+0.1⋅R+0.22⋅G+0.07⋅B+0.32⋅I , which allowed us to display our own shade of gray on a black and white monitor for each of the 16 color values. As a result of this refinement, colors that differ in brightness and are closer to those displayed on the digital one also began to be displayed on the color composite monitor: Hardware bugs and flaws in the harness forming the composite signal from the digital output indicate that the CGA developers were not too worried about its use with a composite monitor, and even less worried about how the “high resolution” modes would be displayed on the composite monitor. However, it was this unsupported combination that allowed the demoscene masters to squeeze the unimaginable from the CGA!






4. Unexpected CGA Features ...


4a) ... in graphic modes


As explained above, the part of the line, shaded in one color, in the composite video signal turns into a meander with a period of eight subpixels. The converse is also true: any composite signal repeating with a period of eight subpixels will be displayed as a single-color part of the line, because the harmonic subcarrier extracted from it will have a constant phase shift and constant amplitude. What exactly the phase shift will be used is more difficult to predict; The article about 1024 colors shows the result without any explanation: The Nybble column shows a repeating sequence of four pixels in 640x200x1bpp mode, i.e. of eight subpixels. Two of these sequences ( and



01011010) correspond to meanders with a period of four subpixels: there is no harmonic subcarrier in such a wave at all, so the resulting color is gray. Four more sequences ( 0011, 0110, 1001, 1100) correspond to pure subcarrier waves - with a shift of 135 °, 225 °, 45 °, and 315 °, respectively. Two of these waves set the standard purple and green colors, the remaining two - blue and orange, unattainable by regular means of CGA. Signal filling at 75% (sequence0111, 1011, 1101, 1110) corresponds to a phase subcarrier shift by half a pixel, i.e. on one subpixel; but the result does not exactly match the phase of the standard colors - because of the very delay schemes that “pull” standard CGA colors to standard NTSC colors. Thus, shades with a slight shift are obtained: turquoise blue, greenish turquoise, reddish brown, purple red. A filling of 75% increases the average signal level, i.e. color brightness; but does not affect saturation, because the wider “edges” of the meander are created by high-frequency harmonics ignored by the television receiver. Finally, a signal with a 25% fill corresponds to the same hues as for 75%, but less brightness.

It is even more difficult to predict what color will turn out when the sequence is repeated not from black and white pixels, but from color ones. In 640x200x1bpp mode, the background color is always black, but the second color can be selected from any of the 16 standard ones. In the 320x200x2bpp mode, on the contrary, the background color can be selected from any of the 16 standard ones, and the other three colors are determined by the palette selected from four options. The article on 1024 colors shows two such combinations, again without explanation. I asked for comments directly to reenigne , responsible for the technical part of the video output in 8088 MPH, as well as implementing CGA emulation in DOSBox; and he advised me cgaart- autonomous CGA emulator written by him, capable of working in batch mode. Adding a simple Python binding to cgaart, I generated a table from all possible 16-color sets of “artifact colors”:

Full table

The code for each set consists of the number of the graphic mode ( 1Afor 640x200x1bpp, 0Afor 320x200x2bpp), the number of the palette ( 0–3), and the number of freely selectable color ( 0–F). Each row of the table consists of 16 squares corresponding to a repeating sequence of pixels; each square consists of five bands - digital output, composite signal level and output on the composite monitor with the first CGA model, output on the composite monitor and composite signal level with the corrected CGA model. The composite signal level is shown as the average for two subpixels - cgaart calculates it only with such accuracy. Let us consider in more detail the lines corresponding to the two sets shown in the article about 1024 colors - these are sets 1A0Cand0A01; but before that, consider a simpler set 1A04:



A set 1A04- red pixels on a black background - corresponds to a signal with a 50% fill and a shift of 270 °, modulated by a periodic sequence of four pixels. The high level of the signal falls within the first three pixel sequences, so it is the fourth bit does not affect the resulting color: for example, 000xand 001xturn into pure black, and 111x- in pure red. Sequences zeroed first pixel ( 010xand 011x) correspond to the phase shift by one subpixel, i.e. -45 ° to magenta; Resettable second pixel ( 100xand 101x) - shift of 45 ° to orange.

Set1A0C- light red pixels on a black background - corresponds to the same signal, but with a non-periodic addition, so its low level is now higher than black. As a result of this sequence 0001, 0010, 0011are converted to the same colors (green, turquoise, blue and biryuzovato blue) as in the case of white pixels, but now with a much lower brightness, because a low level signal source plays a role of a high level for the resulting. The remaining sequences maintain a high level of the original signal, and the corresponding color shades are obtained close to the set 1A04, although the brightness is now higher due to a higher average level.

Finally set0A01- a blue background, a green-red-brown palette, four subpixels in each pixel, two pixels in a repeating sequence - contains four standard colors (sequences 00, 11, 22, 33do not cause artifacts) and their pairwise mixtures: if you add two meanders, then the harmonic subcarrier shift in the resulting signal will be in the middle between the shifts of the terms. Nevertheless, the brightness and saturation of the result depend on the order of the terms: for example, red has a high level of subpixels 1–4, green has 6–1, so that the sequence 12completely cuts off the high level of the red term, and half green ; whereas the sequence21fully retains a high level of the red term, and half - green. The hue in both cases turns yellow, but for the sequence 12- saturated and very dark, while for 21- unsaturated and very light.

So, there are 80 sets of 16 simultaneously available colors. What is the actual resolution? Color can only be changed between blocks of 8 subpixels, i.e. 160 times per line; but brightness is set for each pixel independently. An article about 1024 colors warns that these modes cannot be called “16-color 160x200,” because “horizontal resolution is an open question, it depends on the sampling and filtering of the signal, and varies depending on what forms of the color signal you use” ( transferSLY_G) Nevertheless, it is easy to make sure that even with a constant color tone, the composite monitor does not display brightness fluctuations within 8 sub-pixel blocks: pay attention, for example, to the set 0A0Ain the full table, and in it to the sequences 01and 10corresponding to green pixels to light green background. Both the brightness and the hue of the color are analyzed block by block with the composite monitor, so instead of alternating green pixels of two brightness levels, monotonous pixels with an average brightness level are obtained. For a sequence, the 10result is less saturated than the terms, because this sequence cuts off the low level of the dark green term. Therefore, in my opinion, when using artifact colors, there is every reason to consider 160x200 as actual resolution.

4a) ... in text modes


As mentioned at the very beginning, CGA supports an 80x25 16-color text mode with an 8x8 pixel character matrix, and only a quarter of the video memory is used in this mode. Craftsmen found an undocumented wayswitch the output to a new line of text every two lines of pixels, i.e. output from each character only the top two lines of pixels. In the resulting 100-line “half-text” mode, all 16 KB of video memory is used, and the image is 16-color with a resolution of 640x200 pixels. But in comparison with graphic modes, in semi-text there are quite strict restrictions: firstly, in each familiarity of 8x2 pixels, only two different colors can be used; secondly, the sequence of "main" and "background" pixels in each familiarity should correspond to the two upper lines of one of 256 characters, flashed in the CGA ROM. Even with these limitations, combining the inconvenience of pixel art and text pseudo-graphics, the wizards manage to create incredible 16-color drawings:



(In the article about 1024 colors, only a quarter of this picture is shown, but its full version was published separately - directly in the form of a video memory dump.)

Without a 100-line hack - i.e. if the matrix of each character was completely rendered, the middle part of the picture would look like this:



On the right is a character table stitched in ROM, where the top two lines of each character available for use in semi-text mode are highlighted in yellow.

For the first time, Macrocom decided to combine a 100-line hack and text pseudo-graphics, releasing the game "ICON: The Quest for the Ring" in 1984 with a resolution of 320x200x4bpp that seemed incredible for CGA. However, even static screensaversMacrocom was drawn much worse than VileR; and during the game the graphics were used even more primitive.




(The paradox is that although this is a text mode, displaying text in it is very nontrivial: you have to collect each character from the “scraps” of firmware in ROM.)

Another significant limitation of the semi-text mode is that the image on the composite monitor is blurred to a rather ugly appearance . For example, a VileR drawing would look like this:



But you can benefit from this blurring - to generate a huge number of artifact colors in semi-text mode! As explained in the previous section, any repeating sequence of pixels with a period of 8 subpixels turns into a solid artifact color half the character width in 80-column mode. This means that in semi-text mode - when both the main and background colors for each character can be selected arbitrarily from 16 standard colors - to create artifact colors you need characters that have

  1. the top two rows of pixels are the same;
  2. the left and right halves of these two lines are the same.

CGA ROM found characters suitable for two periodic sequences - 1100and 0110. The first sequence allows you to get artifact colors corresponding to any two repeating pixels in 320x200 mode, i.e. any colors from the rows 0Axxin the full table; the second is the colors from the squares No. 6 and No. 9 in the lines 1A0x, when the sequence 0110is displayed with colored symbols on a black background or black symbols on a colored background, respectively. In this case, the actual resolution is 80x100: the minimum area for which you can set the color corresponds to the "trimmed" familiarity.

So, how many artifact colors can be displayed in semi-text mode at the same time? All combinations of 16 primary colors, 16 background colors, and two matching characters are available, so the author of this trick exclaims: “512 colors! Of course, there are actually fewer of them, there are duplicates and very slightly different shades. ” When the main and background colors coincide, it is obvious that with any symbol the result will be the same, therefore, at the digital output we have 496 different periodic sequences of eight pixels. Converting this output to composite leads to the fact that some sequences turn into the same video signal; for example, 0110gray on black, gray on purple, and light green on purple - turn into the same dark lilac shade:



Compared to the full table, here the squares are doubled for clarity, which allowed us to sign the value of each strip inside the squares directly on the diagram. It can be seen that the sequence 0110completely cuts off the high level of the magenta background, so that the background becomes indistinguishable from black. Green color differs in phase from magenta by 180 °, so this sequence cuts off a high level of green characters in the same way, and light green characters become indistinguishable from gray ones. On the color chartIn the article about 1024 colors, you can see that lines No. 0 and No. 5, corresponding to the black and purple background, are almost indistinguishable; columns 8 and 10, corresponding to gray and light green symbols, are also almost indistinguishable. According to cgaart calculations, dark lilac shades corresponding to the three named combinations of parameters coincide completely; with the corrected CGA model, as can be seen at the bottom of the squares on the diagram, these three shades are different.

In addition to these three combinations, the first CGA model equally displays the sequence in 0110black in light magenta and gray; gray in light magenta gives the standard gray, and black in magenta gives the standard black. Finally, as shown on the right side of the diagram, the sequence1100gray on light red gives the same - almost standard - shade of gray as the sequence is 0110light green in light purple. In total, with the first CGA model, 490 different colors are available, but with the corrected one - all 496.

The climax of the article about 1024 colors is the invented reenigne way to display in semi-text mode only one top row of pixels for each character, but repeat this line twice. It's hard to call a “new graphics mode”, Because it requires a constant — at the beginning of each scan line — change of the CGA settings, which occupies the 8088 processor completely, and leaves no opportunity to do anything else in parallel with displaying a 1024-color image, for example, to prepare an animation. But thanks to this trick, it becomes possible to use sequences 0010and 0101in addition to the two shown above. Using four different symbols, it is possible to obtain 976 different periodic sequences of eight pixels at the digital output. And how many different colors will be obtained after conversion to a composite video signal? I counted 950 with the first CGA, and 973 with a fixed one.

In conclusion, I give a table of all the colors that can be obtained in semi-text mode. VileR has a similar table, but grouped by monitor type, and not by the output sequence of pixels, like mine. His table is convenient for an artist working with a specific device; mine is for the theorist who wants to understand how this or that color appears on the monitor.

Full table

All Articles