Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 10, NO. 12 / DECEMBER 1984 / PAGE 209

Outpost: Atari. (Screen memory and regular memory) Sandy Small; David Small.

Last month we delved into mysteries of screen memory on the Atari. This month we continue that discussion and conclude with some specifics of assembly language.

Picking up where we left off, let's try a few more experiments. Type: SCREEN = PEEK (88) +256*PEEK (89). If you then type PRINT SCREEN, you will get a number from 0to 64,000 which tells you where "screen memory" begins. (My machine gives the value 40,000; if you have a 64K machine, you will probably get the same thing).

What is screen memory? It is just like regular memory, with an added plus; anything that is written there shows up on the TV screen as well. It is the section of memory that the Atari uses to store what goes to the screen.

On your Atari display, in graphics 0 (the default typing mode for you beginners) there are 24 lines of 40 characters. There are thus 24 x 40 or 960 characters on the screen. The Atari stores a number corresponding to what letter shows up on screen in what position in screen memory, sometimes called display memory. This is because memory can hold only numbers, not letters; the designers get around this by assigning a numeric code for each letter.

For instance, clear the screen (with CTRL-CLEAR keys), and then type POKE SCREEN, 33.

You will notice a difference on your TV screen: there will be a letter A in the upper lefthand corner. What happened? Well, we wrote the code for an A character into the memory location that the Atari uses to store the upper-leftmost character on the screen. The Atari then faithfully displayed the A in the screen position corresponding to our memory location.

If we move up one character in memory, we will move one character to the right; for instance, POKE SCREEN + 1,33 and you will get two A's displayed on screen.

If you PEEK, (SCREEN+2), you will find what a "blank" or space looks like to the Atari: a 0. If you change this 0 to anything else, you will get a character on screen.

Everything the Atari outputs to the screen comes by way of this memory. In other words, when the Atari wants to send you the READY prompt when it first starts up, it writes a series of numbers into screen memory. You can duplicate the effect yourself: POKE SCREEN+2, 50 POKE SCREEN+3, 37 POKE SCREEN+4, 33 POKE SCREEN+5, 36 POKE SCREEN+6, 57 and READY will appear at the top of the screen, because you POKEd the right numbers for those characters. That is the exact same way the Atari gets the READY printed; it just stuffs numbers into memory. See Figures 1 and 2.

If you will imagine a post office for a moment, perhaps we can extend our analogy. There are 64,000 postal boxes there, and you are the postman. Looking at the wall from the inside, you see that some of the boxes are unused. Some just hold mail for a long time, and are rarely looked at. Some are very busy, and have mail going in and out all the time. And finally, there are special postal boxes which have an immediate effect, just as though the person the mail was addressed to was standing right in front of his postal box waiting for his mail. The instant that mail is put in the box, he takes it out and takes action on it.

That's a pretty good analogy of how the Atari works. You have 64,000 memory locations to POKE into or PEEK from. Some have immediate effects and others are just temporary memory locations.

We can have some fun with this knowledge. Let's run one memory location through every number it can hold and let the screen display what is in there at high speed: 5 SCREEN=PEEK (88) + 256*PEEK (89) 10 FOR A=0 to 255 20 POKE SCREEN, A 30 NEXT A 40 GOTO 10

What you will see is a rapidly spinning character in the upper leftmost portion of the screen, which corresponds to the contents of memory for that location at any split second. It is whirling fast because the memory locations are changing fast.

What you are seeing is what the Atari does with a screen memory that is changing rapidly. The Atari makes the screen image reflect what is in memory 60 times per second. So 60 times per second it reads the number in the SCREEN location and sends a character that corresponds to that number to the TV.

Now let's change a whole chunk of memory at once. It will slow things down so you can see what is happening. This program takes a given number and puts it into several different screen locations, changing them all to the same thing. (For instance, if the number is a 65, then A's will appear all over the screen): 10 FOR A=0 to 255 20 FOR B=SCREEN to SCREEN +100 30 POKE B, A 40 NEXT B 50 NEXT A 60 GOTO 10 You will will see the first 100 locations of your screen (the first two lines of 40 characters each and the third line for 20 characters, totalling 100) all changing to the same letter. And remember, we are doing this by directly altering display memory without a single PRINT, POSTION, or PUT statement. Pretty near, right?

Other mysterious things happen in memory. Try: PRINT PEEK (20). You will get a number (0-255). Type it again. Whoops! The number changed. Indeed, that number is constantly changing; it is counting from 0 to 255 every 1/60th of a second (so it completes one cycle about every four seconds).

Hey, this could be the foundation of a good clock. Just take the value found in location 20 and divide it by 60; that is the number of seconds gone by. And sure enough; there are several clock demonstration programs on the market that use this effect. The Atari keeps very good time.

To get a higher speed look at location 20. Try this: 10 PRINT PEEK (20) 20 GOTO 10 RUN and stop the program after a few lines have written. You will see the counter counting up to 255, then going back to 0 and starting all over.

What this all boils down to is that unknown to you, the Atari is altering and using memory locations on its own. That little computer, which you thought was idle when you first started it up, is busily working away at memory and doing things even when you are not touching it. Just turning the Atari on sets in motion a large number of processes, some interlocking, that are quite complex to follow.

We could get a neat bird's eye view of this whole process by having the Atari use a different location for display memory, namely, the area around location 20. Right now, what you see on the screen reflects the contents of screen memory, up around 40,000. Let's change that so it reflects what is down at 20: 10 DL!PEEK (560) + 256*PEEK (561) 20 POKE DL+4, 0: POKE DL+5, 0

All we have done is to tell the Atari to start displaying what is in memory starting at location 0. And my, what a strange display we get.

Remember, the Atari thinks it is displaying characters. What it is displaying, however, is the contents of low memory. So it interprets the numbers it finds down there as characters, and some of them are quite weird. What we are going to look for is motion, not a particular character; there are all sorts of strange numbers down in low memory, and they show up as strange characters. But if the characters move or change, we know that memory has changed.

About the middle of the first line will be location 20. It should be a whirling character, showing us that location 20 is indeed changing rapidly. On its left will be a character that changes every four seconds or so; it "ticks" every time location 20 goes over 255 back to 0.

Now, press a key and hold it down. All sorts of furious activity will occur. There will be a counter that starts up, telling the Atari how many 1/60ths of a second you have held the key down. The Atari "stack" will jump back and forth; you'll see this as a high speed flicker six lines down. And various other things in memory will change. (This is a really neat effect.)

For a really entertaining time, try running a short Basic program with the above lines as the first lines. Rather than seeing the output from Basic, you will see what the Atari is doing in some of its businest memory locations, namely, PageZero. The activity that goes on in even the simplest Basic program will amaze you. An example? Okay, just add 30 PRINT "HELLO": GOTO 30 to the above program. To get back to a normal screen, press SYSTEM RESET.

In summary, consider that everything going on in the Atari happens because you either read or write to a memory location. There are more than 64,000 of these locations, each containing a number from 0 to 255. Altering these contents produces spectacular effects.

Obviously, a map of all these memory locations, especially the ones with spectacular effects, is a pretty handy thing to have. There are several good ones on the market, and nearly every Atari book has such a map in an Appendix. Even the Atari Basic reference manual, written without mentioning many of the neatest features of the Atari, has a good little map in the back.

Next, before we quit for the day, I would like to tell you of a special language developed to help us talk to these 64,000 locations without so much hassle. We frequently see strange numbers to POKE into or PEEK from; wouldn't it be nice if the numbers made some sort of sense? For instance, if you are driving around town and need to find a house at 236 Dexter, you know to look between streets with addresses 200 and 300 on Dexter; those numbers "make sense." But the Atari numbers don't really seem to make any sense: 0 to 255, 64,000 memory locations, PEEKing to 53246.

However, if you can learn to speak a special language--almost a code--they will make more sense, and much of the mystery of this machine will disappear. It is a bit like touring a foreign country whose language you do not speak; much of the culture and history is inaccessible to you. But if you take the time to learn the language, much of what the country is about becomes clear to you; you understand why things are as they are.

Fortunately, in the case of the Atari, we are not talking about something as difficult as a foreign language; we are talking about hexadecimal (hex) notation, a special code for identifying the 64,000 memory locations. In hex, everything comes out evenly. It is convenient to use, and provides a really understanding of the machine. Many of the subtleties of the Atari will become clear just because you are working in hex.

True, it takes a little work to learn hex and to speak it comfortably. But if you do, you will know it for any computer, because everyone else uses hex, too. And you will be on far more intimate terms with more advanced computer articles, and on your way to understanding even some complex hardware principles. Finally, if you learn hex and really understand it, you will find that assembly language--that dreaded mystery for beginners--is positively easy. (The only hard part of assembler is learning memory concepts.)

Why bother with assembler? For speed. Assembler gives you absolute control over the computer and lets you accomplish things as fast as possible. As a sneak preview of what we will be getting into, remember our Basic program of a few paragraphs back, which flipped 100 letters on the screen over and over? Well, try the program in Listing 1, which does the same thing with assembly language, and compare the speed.

See you next time for an introduction to hex. I enjoy playing around with the Atari and hope you do too; it is far and a way the most powerful graphics computer you can buy for anywhere near the price.

Incidentally, for our assembler course, you will need an Assembler cartridge or another assembler (MAC/65 or AMAC are fine) eventually. But there is no rush; we will stick with Basic for a while and use assembler only for a few bits and pieces.