Authors
Publication
Pub Details
Date
Pages
The Problem
You are piloting a spaceship when suddenly appears an onslaught of meteorites which must be avoided at any cost. Your warp system is malfunctioning and can be used, but its duration varies. The situation seems hopeless!
Sound challenging? How would you like to try to save the ship on the unexpanded ZX81? It may sound impossible, but it is not. Here is a technique that will allow arcade-style games on the ZX81—even in 1K.
The Solution
Often clouded in mystery is the display file—the place in memory which stores the picture you see on your screen. In general, it is 24 lines by 33 characters (32 columns + ENTER) and occupies 793 bytes of memory. The address of the display file is held in two bytes with a special name: D-FILE. The first character in the display file is always ENTER. This is why it is 793 bytes long and not 792. The address of any position in the display file can be calculated by finding its start, adding 33 for each line down it is, and adding its column number. Somewhat tedious, isn’t it?
Notice that I said, “in general.” This is because, when the computer finds it has less than 3 1/4 K RAM available (according to RAMTOP, another system variable), the display file is left with just ENTERs to serve as an end-of-line character. Thus, the system is thoughtfully designed so that each of the 24 lines can contain a different number of characters. How, then, can you calculate the address of a certain screen position? The truth is, you do not even need to. The PRINT AT subroutine in the ROM is required to find the address of a particular position held in registers B (line) and C (column) and set the PRINT position there. The current PRINT position is held as two bytes in DF-CC. These are the bytes the command PRINT AT will modify.
To find the character at a certain position, you PRINT AT the position and PEEK the address contained in DF-CC. This technique is illustrated in lines 60 and 70 of Listing 1. That is all there is to it; you do not even need a full display file to do memory mapping.
Do not be surprised to find an ENTER character (code 118) at the position. This simply indicates there is nothing there yet. What CLS does is to look at RAMTOP to determine if there is sufficient memory to fill the screen with spaces. If not, it just places 25 ENTERs in the display file. There must always be these 25 ENTER characters present. POKEing an ENTER character could very well crash the system.
So our fantasy of dodging meteors hurtling through space is now within reach. Examine Listing 1. Once you understand how it works, you can create fast-moving games in just 1K RAM.
The first thing you should notice is how “outer space” is printed. I could have used a FOR-NEXT loop, but the method used here is more economical and just as fast. The bytes called S-POSN store the number of positions to the left of and including the current position and the number of lines below and including the current PRINT position (see Table 1 for a list of all bytes referred to). The second byte of S-POSN is useful for determining when to use CLS to avoid the out-of-screen error. If you add a line
IF PEEK 16442=2 THEN CLS
you will not run out of screen and have to use CONT. You can see how this technique was used in line 20.
Lastly, the address at 16418 (called DF-SZ) holds the number of lines at the bottom of the screen. If this is POKEd to zero, you can use all 24 lines! Whenever any report code is given, though, its value will return to two.
Also, there is a warning when POKEing DF-SZ: if its value is less than two, SCROLLing or INPUTing will crash the system (if you do not know what that is, try it and see what happens).
So POKEing this will not do much good in our program, but it will remove the 22-line restriction and allow a greater amount of data to be shown on the screen at one time.
Meteors
Type in the program as listed using the graphics notes. To begin, make sure you are in SLOW mode; type RUN and ENTER. The display represents space. You avoid the meteors by pressing the 8 key to move left and the 5 key to move right. Pressing the O key activates the warp, which makes your ship invulnerable to the meteors, but lasts only a random amount of time. The game is over when any part of your spaceship collides with a meteor. After your score is displayed, press R to play again.
Program Notes
10: Inverse space (10).
90: Inverse quotation marks; inverse V; inverse quotation marks.
150: Inverse space (10); inverse asterisk.
180: Double quotation marks (on Q key); double quotation marks; inverse quotation marks; inverse V; inverse quotation marks.
These are just a handful of ideas for which the display file may be used. If you have discovered some others, drop us a line.
Table 1: Important Address/System Variables
| Address | # bytes | Name | Description |
|---|---|---|---|
| 16388 | 2 | RAMTOP | Stores address of first nonexistent byte; varies with amount of RAM available. |
| 16396 | 2 | D-FILE | Start of display file; first character is always ENTER. Warning: Never POKE an ENTER. |
| 16398 | 2 | DF-CC | Holds current PRINT position; PEEK after PRINT AT to find character at that position. |
| 16442 | 1 | S-POSN | Holds number of lines left on screen; line number is equivalent to 24-PEEK 16442. |
| 16418 | 1 | DF-SZ | Number of lines on bottom of screen; can be POKEd to zero to use all 24 lines. Warning: Do not use SCROLL or INPUT if value is less than 2. |
| 08F5h | PRINT AT | 8K monitor routine to set PRINT position as held in registers B (row) and C (col). Useful for machine language programs. |
Listing 1
10 PRINT " "
20 IF PEEK 16442<>2 THEN RUN
30 LET P=4
40 LET S=0
50 LET W=S
60 PRINT AT 0,0;
70 LET Z=PEEK 13698+256*PEEK 16399
80 IF NOT W AND PEEK (Z+P)+PEEK (Z+P+1)+PEEK (Z+P+2)<>384 THEN GOTO 170
90 IF NOT W THEN PRINT AT 0,P;""V""
100 LET S=S+1
110 IF INKEY$="0" THEN LET W=1
120 IF RND>RND THEN LET W=0
130 LET P=P-(INKEY$="5" AND P)+(INKEY$="8" AND P<7)
140 SCROLL
150 PRINT " ";AT 21,9*RND;"*"
160 GOTO 80
170 PRINT AT 0,16;"SCORE "+STR$ S
180 PRINT AT 0,P;"""V""";AT 0,P;"""V"""
190 IF INKEY$<>"R" THEN GOTO 180
200 PRINT AT 0,0;
210 RUN
Products
Downloadable Media
Image Gallery
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
