GOING TO TOWN PROJECT
by Geoff Corry
GOING TO TOWN PROJECT
by Geoff Corry
In the last issue, we introduced a new project for this column. A graphic adventure game that will be built over the next several issues, covering the various techniques that are used to produce interesting displays and a smooth running program. This project will produce a program that will work with a 16K memory computer and tape storage or a 48K machine with disk storage. We will use some special routines to hold the player's interest, while the next section of the program is loading. During the building of this program, we will find out how to use special programming techniques such as scrolling, page flipping, player/missile graphics, etc. to produce a professional style to this program.
How Is Your Memory?
This question might strike you as being strange. But a significant characteristic of successful people is, that they have all had excellent memories. For the same reason, computers have also been successful, because they never forget. That is, if you don't forget to save things before you turn off the power.
Let's look at our own computer's memory, not to see how good it is, but to make sure we have enough when we need it. If your computer has 16K bytes of useable memory, we must be extra careful in how we design our program to get it to work well.
Your computer has printed circuit cards with rows of black rectangular chips which house the electrical memory units. The 400 and 600XL have memory boards, permanently wired inside, that are equipped with 16x1024 or 16384 memory cells that can each hold one byte (any number from 0 to 255) of information. You can upgrade the 400 by having the board replaced by a 48K board, whereas the 600XL just requires the addition of a memory expansion unit that plugs in the back. The Atari 800 computer uses three 16K modules, for a maximum of 48K of Random Access Memory.
The two sets of numbers refer to the memory cells available to a 16K memory computer with tape storage or a 48K memory computer with disk drive. The disk operating system requires a considerable amount of memory.
Here is a brief memory map of the main areas that we need to store the Basic program and the special graphic features:
... Read Only Memory ...
... 40960 ...
... 16383 40959 ...
... Screen Memory ...
... 15424 40000 ...
... 15423 39999 ...
... Display List ...
... 15392 39968 ...
... 3041 8443 ...
... Small Basic Program ...
... (See below) ...
... 1792 7420 ...
... 1791 7419 ...
... Operating System ...
... Random Access Memory ...
... 0000 0000 ...
Here is a short program that we will use from time to time to check on our memory usage. The second area from the bottom in the above memory map shows the memory required to operate this program. Here it is:-
30000 REM This MEMADDR utility looks at the starting addresses of blocks of memory used in BASIC programs.
30120 ? "THE TOP END OF USABLE MEMORY=";RAMTOP
30140 ? "START OF SCREEN MEMORY=";SCREEN
30160 ? "START OF DISPLAY LIST=";DLIST
30220 ? "TOP OF YOUR BASIC PROGRAM=";MEMTOP
30240 ? "START OF STATEMENT TABLE=";STMTAB
30260 ? "START OF VARIABLE VALU TABLE=";VVTP
30280 ? "START OF VARIABLE NAME TABLE=";VNTP
30300 ? "START OF BASIC MEMORY BUFFER=";LOWMEM:?
30310 ? "SUBSTITUTE YOUR LOW & HIGH NUMBERS IN THE NEXT LINE, TO SEE WHAT IS IN THAT AREA OF MEMORY:-"
30320 ? "FOR A=(LOWNUM) TO (HINUM):? CHR$(PEEK(A));CHR$(32);:NEXT A"
Type in this memory address program and LIST it to tape(LIST"C:"), or to disc(LIST"D:MEMADDR"). Please use the line numbers as shown, as we will be adding the missing lines later. We have used high numbers so that we can merge it with our main program sections without writing over existing lines.
Now that you have stored it, type NEW and press RETURN. Retrieve it by rewinding the tape and typing ENTER"C:" or for those with disc drives, type ENTER" D:MEMADDR". When a program is stored in this manner, it can be entered on top of an existing program, whereas the LOAD command wipes out any previous program that was in memory.
Now run it and you will see a list of numbers for each line. If you look at the above memory map, some or most of these numbers will be there depending on the amount of memory in your computer, and what type of storage device is connected. These numbers are actual storage locations in your computers memory, and in most cases are the start of a block of memory assigned for a certain function. At present these blocks are sized to take care of the program we entered. A larger program would increase the size of these blocks and change the starting locations. The list should look like this. If you have some other combination of memory size and storage device, you will get a different set of numbers.
16K & TAPE 48K & DISK
THE TOP END OF USABLE MEMORY
START OF SCREEN MEMORY 15424
START OF THE DISPLAY LIST 15392
TOP OF YOUR BASIC PROGRAM 3041
START OF STATEMENT TABLE 2156
START OF VARIABLE VALUE TABLE
START OF VARIABLE NAME TABLE 2048
START OF BASIC MEMORY BUFFER
As you can see, a 16K memory size has a lot less free memory between the top of this BASIC program and the start of the display list. The areas between each of the above memory locations will vary, depending on the size of the program and what it does. The only things that remain fixed, are the TOP END OF USABLE MEMORY and the START OF BASIC MEMORY BUFFER. All your BASIC programs use more or less of this space.
Maybe you are getting curious as to what the names of these memory locations are all about. Well, here goes anyway. Starting at the top is the last memory location you can change in a BASIC program. Everything above that is fixed in place at the factory or is used by your plug-in BASIC cartridge. From the START OF SCREEN MEMORY to the TOP END OF USABLE MEMORY is the actual information that creates the screen display. This information changes with each change of picture or text. It has to be remembered, because the T.V. is not intelligent and has to be reminded 60 times a second of what goes where.
Below the screen memory is the display list, which is a set of instructions to a special microprocessor hidden deep inside the computer, known as ANTIC. (Yes, it's a magazine too!) The ANTIC chip controls the function of each graphic mode. Right now we are in GR. 0 text mode. If you take your cursor up the screen with the CTRL + arrow keys, you can modify that last line in the display. Before you do anything, please jot down the set of numbers at the right of each line. I will tell you more about that later. Now let's type over '(LOWNUM)' with the number beside START OF DISPLAY LIST. Then CTRL DELETE, 3 times to get rid of `UM)'. NOW CTRL right arrow along to `(HINUM)' and type in the number beside START OF SCREEN MEMORY. Type 2 more CTRL DELETE's to get rid of `M)', and CTRL right again to `CHR$'. CTRL DELETE four times, to get rid of this for now. Don't worry, it will come back when we rerun the program. Now hit RETURN. Hey presto! - our display list. It should look like this;
16K & TAPE
112 112 112 66 64 60 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 65 32 60 0
48K & DISK
112 112 112 66 64 156 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 65 32 15
These cryptic instructions tell ANTIC to leave three sets of 8 blank lines at the top of the screen. Most of this is hidden above the T.V. window. The next instruction we will hurry through for now, except to say that the 66 is actually 64 plus the ANTIC display mode. More confusion, ANTIC has 16 display modes of which BASIC uses 8 (not counting GTIA modes 9 to 11). ANTIC mode 2 is the same as GRAPHIC mode 0 in BASIC. Whew! I'm glad that works out. THe next two numbers make up the starting address of the screen memory (at least we know all about that!). If you look at line 30130 of the program listing, you will see that we took the first number and added it to the product of 256 times the second number. Well ANTIC does the same and comes up with:
16K & TAPE 64+(256*60)=15424
48K & DISC 64+(256*156)=40000
Well, at least that worked out. Now if you count the number of 2's and add the one buried in the 66 that we talked about, you should come up with the number of GR.0 lines on the screen, that's right- 24. Next, the 65 is a jump command to start putting the next set of lines on the screen. The last two numbers, 32 and the split 156 are combined as above, to give the starting address of the display list. That 0 at the end is the first byte of the screen memory, showing that nothing special goes up at the left top of the screen (not counting dust!). If you want to delve into the mysteries of the display list, try Chapter 2 of DE RE ATARI, Page 294 of YOUR ATARI COMPUTER, or Chapter 2 of ADVANCED PROGRAMMING FOR YOUR ATARI, a 1983 TAB book by Linda Schreiber.
There is a large area below the display list, right down to the top of BASIC memory, that BASIC uses as a scratch pad to handle each instruction in turn. The 6502 microprocessor, which does all the work, doesn't understand BASIC. So the BASIC cartridge has an interpreter inside that converts all the statements into machine language subroutines as it executes the program. For those who wish to dig into this, see THE ATARI BASIC SOURCE BOOK from COMPUTE! Books, by Bill Wilkinson, Kathleen O'Brien, and Paul Laughton. A quick example; if your program was running along and came to a PLOT command, it would first see it as a token number 44, which is how PLOT is stored. It then executes it by jumping to a position routine. Now load in the color of the point, find out where you want to plot it, screen or printer, and it sends it to screen memory (or to the printer buffer). All this is done in several machine language steps, `9' for the position subroutine, '1' to get the color, `1' to get the output device, and `9' more to print it. Also within the position and print subroutines, it will branch down into other subroutines. So you see, it is much simpler to say PLOT in BASIC, than to go through all the minute steps of machine language, but we pay the price in extra memory required for BASIC to interpret and execute each command as it comes along.
Now that you have looked at the higher memory locations used during the running of a program, let's now look at the area that stores the BASIC program itself. Run the progam again. Remember the list of numbers I asked you to jot down. Compare this list with what you see on the screen. You will see that the numbers beside the top of BASIC memory, start of statement table, and variable value table have all increased. This is because when we went into the last line of the display, changed it and hit RETURN, converting the line from a printed line, (see line 30320 in the listing), to an immediate command. This is now added to the BASIC program and it had to find room for the FOR and NEXT command, and added the variable `A'.
Go back and modify our immediate mode line again. Substitute the number against START OF STATEMENT TABLE for (LOWNUM). Get the number against TOP OF BASIC PROGRAM, subtract 1, and substitute it for (HINUM). DON'T hit RETURN yet, but CTRL uparrow to the top of the screen. Now SHIFT DELETE a few times carefully until the immediate action line is at the top of the screen. When you press return, you will see the program scroll up. Some of the program will be recognizable, but there are some funny graphic symbols mixed in. These are the ATASCII versions of the BASIC command tokens for the program. The FOR and NEXT commands are now tokens 8 and 9, which are the ATASCII triangle and small box symbols respectively. Each letter and token is separated by a space not normally in memory, but put in by our immediate action line (CHR$(32)) to stop everything from running together.
You have now seen where this BASIC program is stored. Now let's skip the variable value table for the moment and look at the variable name table. First, we have to get the program back, so type RUN and press RETURN. Now we are back to normal. Notice that the numbers have not increased again. Modify the immediate action line again using the number next to the START OF VARIABLE NAME TABLE for (LOWNUM), and take 1 off the number against the START OF VARIABLE VALUE TABLE for (HINUM). Now go over to CHR$(32); and CTRL delete 9 times to get rid of it. Now hit RETURN. There is the list of all the variables used in the program. BASIC knows where each variable ends, because it adds 132 (sets bit 8) to the last letter which gives us the inverse display.
I hope this has been an interesting look `under the hood' of BASIC storage. We will have further use for this information as we continue with our 'GOING TO TOWN' project. That's enough to byte on for now. See you again in the next issue!