GRAPHICS INDIRECTION
(COLOR REGISTERS AND CHARACTER SETS)

Indirection is a powerful concept in programming. In 6502 assembly language, there are three levels of indirection in referring to numbers. The first and most direct level is the immediate addressing mode In which the number itself is directly stated: The second level of indirection is reached when the program refers to a memory location that holds the number: The third and highest level of indirection with the 6502 is attained when the program refers to a pair of memory locations which together contain the address of the memory location that holds the number. In the 6502, this indirection is complicated by the addition of an index: Indirection provides a greater degree of generality (and hence power) to the programmer. Instead of trucking out the same old numbers every time you want to get something done, you can simply point to them. By changing the pointer, you can change the behaviour of the program. Indirection is obviously an important capability.

COLOR REGISTERS

Graphics indirection is built into the ATARI Home Computer in two ways: with color registers and with character sets. Programmers first approaching this computer after programming other systems often think in terms of direct colors. A color register is a more complex beast than a color. A color specifies a permanent value. A color register is indirect; it holds any color value. The difference between the two is analogous to the difference between a box-end wrench and a socket wrench. The box-end wrench comes in one size only but a socket wrench can hold almost any size socket. A socket wrench is more flexible but takes a little more skill to use properly. Similarly, a color register is more flexible than a color but takes more skill to use effectively.

There are nine color registers in the ATARI 400/800 Computer; four are for player- missile graphics and will be discussed in Section 4. The remaining five are not always used; depending on the graphics mode used, as few as two registers or as many as five will show up on the screen. In BASIC mode 0, only two and one-half registers are used because the hue value of the characters is ignored; characters take the same hue as playfield register 2 but take their luminance from register 1. The color registers are in CTIA at addresses $D016 through $D01A. They are "shadowed" from OS RAM locations into CTIA during vertical blank. Figure 3-1 gives color register shadow and hardware addresses.

For most purposes, the user controls the color registers by writing to the shadow locations. There are only two cases in which the programmer would write directly to the CTIA addresses. The first and most common is the display list interrupt which will be discussed in Section 5. The second arises when the user disables the OS vertical blank interrupt routines that move the shadow values Into CTIA. Vertical blank interrupts are discussed in Section 8.

Colors are encoded in a color register by a simple formula. The upper nybble gives the hue value, which is identical to the second parameter of the BASIC SETCOLOR command. Table 9-3 of the BASIC Reference Manual lists hue values. The lower nybble in the color register gives the luminance value of the color. It is the same as the third parameter in the BASIC SETCOLOR command. The lowest order bit of this nybble is not significant. Thus, there are eight luminances for each hue. There are a total of 128 colors from which to choose (8 luminances times 16 hues). In this book, the term `color' denotes a hue- luminance combination.

Once a color is encoded into a color register, it is mapped onto the screen by referring to the color register that holds it. In map display modes which support four color registers the screen data specifies which color register is to be mapped onto the screen. Since there are four color registers it takes only two bits to encode one pixel. Thus, each screen data byte holds data for four pixels. The value in each pair of bits specifies which color register provides the color for that pixel.

In text display modes (BASIC's GRAPHICS modes 1 and 2) the selection of color registers is made by the top two bits of the character code. This leaves only six bits for defining the character, which is why these two modes have only 64 characters available.

Color register indirection gives you four special capabilities. First, you can choose from 128 different colors for your displays. This allows you to choose the color that most nearly meets your needs.

Second, you can manipulate the color registers in real time to produce pretty effects. The simplest version of this is demonstrated by the following BASIC line:

This line simply cycles the border color through all possible colors. The effect is quite pleasing and certainly grabs attention. The fundamental technique can be extended in a variety of ways. A special variation of this is to create simple cyclic animation by drawing a figure in four colors and then cycle the colors through the color registers rather than redrawing the figure. The following program illustrates the idea: The third application of color registers is to logically key colors to situations. For example, a paged menu system can be made more understandable by changing the background color or the border color for each page in the menu. Perhaps the screen could flash red when an illegal key is pressed. The use of the color characters available in BASIC Graphics modes 1 and 2 can greatly extend the impact of textual material. An account sum could be shown in red if the account is in the red, or black if the account is in the black. Important words or phrases can be shown in special colors to make them stand out. The use of colors in map modes (no text) can also improve the utility of such graphics. A single graphics image (a monster, a boat, or whatever) could be presented in several different colors to represent several different versions of the same thing. It costs a great deal of RAM to store an image, but it costs very little to change the color of an existing image. For example, it would be much easier to show three different boats by presenting one boat shape in three different colors than three different boat shapes.

The fourth and most important application of color registers Is used with display list interrupts. A single color register can be used to put up to 128 colors onto a single screen. This very Important capability will be discussed in Sect ton 5.

CHARACTER SETS

Graphics indirection is also provided through the use of redefinable character set. A standard character set is provided in ROM, but there is no reason why this particular character set must be used. The user can create and display any character set desired. There are three steps necessary to use a redefined character set. First, the programmer must define the character set. This is the most time-consuming step. Each character is displayed on the screen on an 8x8 grid; it is encoded in memory as an 8-byte table. Figure 3-2 depicts the encoding arrangement.

A full character set has 128 characters in it, each with a normal and an inverse video incarnation. Such a character set needs 1024 bytes of space and must start on a 1K boundary. Character sets for BASIC modes 1 and 2 have only 64 distinct characters, and so require only 512 bytes and must start on a 1/2K boundary. The first 8 bytes define the zeroth character, the next 8 bytes define the first character, and so on. Obviously, defining a new character set is a big job. Fortunately, there are software packages on the market to make this job easier.

Once the character set is defined and placed into RAM, you must tell ANTIC where it can find the character set. This is done by poking the page number of the beginning of the character table into location $D409 (decimal 54281). The OS shadow location, which is the location you would normally use, is called CHBAS and resides at $2F4 (decimal 756). The third step in using character sets is to print the character you want onto the screen. This can be done directly from BASIC with simple PRINT statements or by writing numbers directly into the screen memory.

A special capability of the system not supported in BASIC is the four- color character set option. BASIC Graphics modes 1 and 2 support five colors, but each character in these modes is really a two-color character; each one has a foreground color and a background color. The foreground color can be any of four single colors, but only one color at a time can be shown within a single character. This can be a serious hindrance when using character graphics.

There are two other text modes designed especially for character graphics. They are ANTIC modes 4 and 5. Each character in these modes is only four pixels wide, but each pixel can have four colors (counting background) The characters are defined just like BASIC Graphics mode 0 characters, except that each pixel is twice as wide and has two bits assigned to it to specify the color register used. Unlike ANTIC modes 6 and 7 (BASIC modes 1 and 2), color register selection is not made by the character name byte but instead by the defined character set. Each byte in the character table is broken into four bit pairs, each of which selects the color for a pixel. (This is why there are only four horizontal pixels per character.) The highest bit (D7) of the character name byte modifies the color register used. Color register selection is made according to Figure 3-3:

Using these text modes, multicolored graphics characters can be put onto the screen.

Another interesting ANTIC character mode is the lowercase descenders mode (ANTIC mode 3). This mode displays 10 scan lines per mode line, but since characters use only eight bytes vertically, the lower two scan lines are normally left empty. If a character in the last quarter of the character set is displayed, the top two scan lines of the character will be left empty; the data that should have been displayed there will instead be shown on the bottom two lines. This allows the user to create lowercase characters with descenders.

APPLICATIONS OF CHARACTER SETS

Many interesting and useful application possibilities spring from character set Indirection. The obvious application is the modified font. A different font can give a program a unique appearance. It is possible to have Greek, Cyrillic, or other special character sets. Going one step further, you can create graphics fonts. The ENERGY CZAR(TM) computer program uses a redefined character set for bar graphs. A character occupies eight pixels; tints means that bar charts implemented with standard characters have a resolution of eight pixels, a rather poor resolution. ENERGY CZAR uses a special character set in which some of the less popular text symbols (ampersands, pound signs, and the like) have been replaced with special bar chart characters. One character is a one-pixel bar, another is a two-pixel bar, and so on to the full eight-pixel bar. The program can thus draw detailed bar charts with resolution of a single pixel. Figure 3-4 shows a typical display from this program. The mix of text with map graphics is only apparent; the entire display is constructed with characters.

In many applications, character sets can be created that show special images. For example, by defining a terrain graphics character set with river characters, forest characters, mountain characters, and so forth, It is possible to make a terrain map of any country. Indeed, with imagination a map of terrain on a different planet can be done just as easily. When doing this, it is best to define five to eight characters for each terrain type. Each variation of a single type should be positioned slightly differently in the character pixel. By mixing the different characters together, It is possible to avoid the monotonous look that Is characteristic of primitive character graphics. Most people won't realize that the resulting map uses character graphics until they study the map closely. Figure 3-5 shows a display of a terrain map created with character set graphics. The reproduction in black and white does not do justice to the original display, which has up to 18 colors.

You could create an electronics character set with transistor characters, diode characters, wire characters, and so forth to produce an electronics schematics program. Or you could create an architectural character set with doorway characters, wall characters, corner characters, and so on to make an architectural blueprint program. The graphics possibilities opened up by character graphics with personal computers have not been fully explored.

Characters can be turned upside down by pokeing a 4 into location 755. One possible application of this feature might be for displaying playing cards (as in a Blackjack game). The upper half of the card can be shown right side up; with a display list interrupt the characters can be turned upside down for the lower half of the card. This feature might also be of some use in displaying images with mirror reflections (reflection pools, lakes, etc.

Even more exciting possibilities spring to mind when you realize that It is quite practical to change character sets while the program is running. A character set costs either 512 bytes or 1024 bytes; in either case It is quite inexpensive to keep multiple character sets in memory and flip between them during program execution. There are three time regimes for such character set multiplexing: human slow (more than 1 second); human fast (1/60 second to 1 second); and machine fast (faster than 1/ 60 sec).

Human-slow character set multiplexing is useful for "change of scenery" work. For example, a space travel program might use one graphics character set for one planet, another set for space, and a third set for another planet. As the traveller changes locations, the program changes the character set to give exotic new scenery. An adventure program might change character sets as the player changes locales.

Human-fast character set multiplexing is primarily of value for animation. This can be done in two ways: changing characters within a single character set, and changing whole character sets. The SPACE INVADERS (trademark of Taito America Corp.) program on the ATARI Home Computer uses the former technique. The invaders are actually characters. By rapidly changing the characters, the programmer was able to animate them. This was easy because there are only six different monsters; each has four different Incarnations.

High-speed cyclic animation of an entire screen is possible by setting up a number of character sets, drawing the screen image, and then simply cycling through the character sets. If each character has a slightly different incarnation in each of the character sets, that character will go through an animated sequence as the character sets are changed. In this way a screen full of objects could be made to cyclically move with a very simple loop. Once the character set data is in place and the screen has been drawn, the code to animate the screen would be this simple:

Computer-fast character set animation is used to put multiple character sets onto a single screen. This makes use of the display list interrupt capability of the computer. Display list interrupts are discussed in Sect ton 5.

The use of character sets for graphics and animation has many advantages and some limitations. The biggest advantage is that it costs very little RAM to produce detailed displays. A graphics display using BASIC mode 2 characters (such as the one shown in Figure 3-5) can give as much detail and one more color than a BASIC mode 7 display. Yet the character Image will cost 200 bytes while the map image will cost 4000 bytes. The RAM cost for multiple character sets is only 512 bytes per set, so It Is inexpensive to have multiple character sets. Screen manipulations with character graphics are much faster because you have less data to manipulate. However, character graphics are not as flexible as map graphics. You cannot put anything you want anywhere on the screen. This limitation would preclude the use of character graphics in some applications. However, there remain many graphics applications for which the program need display only a limited number of predefined shapes in fixed locations. In these cases, character graphics provide great utility.