Mark Hutchinson presents the first of a two part introduction to Atari graphics programming.
Program Antics
Mark Hutchinson presents the first of a two part introduction to Atari graphics programming
The Atari is an incomparable machine for games, but the trouble is, it's so good that it's all too easy to miss out on programming. This is the first of an occasional series of articles where I hope to delve into the quieter backwaters of the Atari 400 and 800. So if you'd rather learn how to write a space invaders game than just load it in, read on.
First, let's look at a few of the memory locations we'll be using in the future. When a key is pressed, the computer looks at a location in memory called a pointer. This is like a signpost which sends the computer to the part of the memory that holds the character set, which is held in ROM, and therefore cannot be changed.
But it we change the pointer by POKEing it with a number to make the computer look into an area of RAM, which we can change, then we can place data for a new character set here. Easy so far, but now come the dreaded mathematics.
Like most home micros, the Atari is what is termed an 8-bit machine. This means that each memory location is comprised of eight little switches that are either on (1) or off (0). Mathematically, the possibilities range from all off to all on which (work it out) means 256 numbers from 0 to 255. This isn't a great deal, so many locations use a two number pointer to overcome this. The first number, or Most Significant Byte (MSB), holds a number that you multiply by 256. The second, Least Significant Byte (LSB), you add to the first.
If you compare this to decimal, the first number is the equivalent of the tens, while the second number is like the units. Sometimes the pointer has only one number in which case it is the MSB and must be multiplied by 256.
The number stored here is terms pages, where one page equals 256 bytes, or four pages equals 1K. For example, one location we will be using a lot is for the Display List (DL), whose pointers are 560 and 561. To find where the DL currently resides (it changes according to memory size, graphic modes, etc) you use the following command:
PRINT PEEK(560)+256*PEEK(561)
The DL is a set of data that tells the Atari's Antic chip where the screen data is and how to use it. Again, if you reset the pointer you can set up your own custom graphics.
Another important location is 106. This is where the top of usable RAM is stored. As it is one number, it will be stored in pages. The computer looks at this location and knows how far up in RAM it is allowed to go. If you lower the number stored here the computer will not go above it, and you'll then have stored some 'safe' RAM for your own use. You can then use a pointer to go to this area whenever you have need of the data stored there. Using the DL like this allows the storage of high resolution pictures and recalls them instantaneously, fully drawn.
Atari graphics are instant, if not easy. You still have to do all the hard slogging, but once you've done it you won't be forced to wait around for hours as the drawing comes on screen. You'll be used to popping in the Star Raiders cartridge, pressing G and seeing the galactic chart appear without being drawn - well, now it's your turn.
Animation can be achieved without using player/missile techniques with PLOT and DRAWTO using COLOR 1, waiting for the human to admire the computer's handywork, then PLOT and DRAWTO the same points using COLOR 0, and finally PLOT and DRAWTO new points using COLOR 1. This is satisfactory for small designs such as BEMs (bug eyed monsters), but when drawing intricate designs such as maps, graphs and circuits it is far more professional to have the graphics appear ready drawn and ready to use.
Try program 1, which draws a design in a couple of seconds. Use the line numbers given, as you'll want to expand it later. Once you've seen the basic design in operation, you can pre-empt part of RAM for your own use. If you look at page 45 of the Basic Reference Manual you will see the memory requirements for the different graphic modes.
Remember that each K of memory is four pages, and to leave plenty of room. As we are in Graphics 8, 10K - or 40 pages - should be sufficient. Now add program 2 to the program you've already typed in.
Impressive, isn't it? If you LIST the program you will see that it is quite simple. First, work out how much memory you will need for each screen, then find RAM top by PEEKing into location 106. Draw out the first screen, find the display list pointer 1 by PEEKing locations 560 and 561, move RAMtop down a suitable number of pages and thus protect that part of RAM from being overwritten. Do exactly the same for the next screen, except that this time you POKE 106 with the number of pages needed for screen 1 plus the number needed for screen 2. Finally, POKE the DL locations with DL1 for page 1, DL2 for page 2 and so on.
Did you notice that line 350 is outside the DL pointer? When a graphics mode is called, a certain size of memory is allocated for it. What you do with it is irrelevant, as the pointer is already set up. All you need do is to lower RAMtop by enough pages to cover this memory plus your own program area. In case of misunderstanding, DLLOW is not a low memory pointer.
But I can hear you cry: "This man's mad. The computer is still drawing out the design on the screen." This is where we introduce ANTIC. Each time we call a graphics mode, ANTIC uses some of the 6502 cycle time to work out all the necessary details. What we do is to find the original data in location 559, POKE in a zero to switch ANTIC off, let the computer do what we want, then switch it back on by POKEing the original data into location 559. Remember to turn ANTIC off after each graphic command, and on again before the main sequence.
If you add the lines in program 3 you'll be able to see instant graphics. With this, try switching on a sound to relieve the boredom of a blank screen, then turning it off as your program starts.
Before you get carried away with location 559, you need to know a bit more about it. Some locations change their data so fast that it is impossible to write to them, but to make life easier, some of these locations have what is called a 'shadow', where you can write and store data. Location 559 is the shadow of 54272, the DMA control register, and is used when setting up PMG, so you need to be careful when using 559 for these two purposes in one program.