Home > Previous Page >  Ace Programming - Alien Swarm - 1K Space Invaders game.
Archive Search  
Popular Computing Weekly 20 January page 22

Jupiter Ace Revisited
Martyn Sudworth re-examines the Jupiter Ace and presents
Alien Swarm - a 1K Space Invaders game.

the language.

Inside Ace
If you look at the memory map of the Ace, you can see that there are two copies of the television screen next to each other above the 8K ROM. The screen scroll routine (see below) uses the second copy, because this gives a steady clear display whereas manipulation of the first copy produces white dots flickering over the screen. This is presumably akin to the Slow and Fast commands on the ZX81, although the effect is not so drastic.
Above the video screen is the pad, an area for manipulating text (strings are stored temporarily in the pad to allow string arithmetic). The pad is followed by two copies of the character set memory and four copies of the dictionary and stacks. The dictionary contains the new words you have defined, the return stack contains the return addresses and the data stack contains the numbers you wish to store there for use in these words.
One of the main drawbacks of the Ace is the lack of memory for the dictionary. The problem is not as bad as on the 1K ZX81, since the Ace's memory is used far more efficiently. But advertisements for the Ace state The Jupiter Ace is your answer' if you have a computer and problems with your memory. This is not true unless you fix a 16K RAM pack (the ZX 16K Ram packs will fit with some modification) when the memory will, in effect, be upwards of 50K compared with Basic systems.

The manual for the Ace is good with many useful word definitions clearly laid out. A section on hardware add-ons, describing two circuits which are an interesting addition to games (one circuit gives a circuit with three LEDs which can be used to indicate fuel levels or the end of a game) is most welcome.

Alien Swarm
This program can just be fitted in the 1K of memory on the Ace. I have used some fairly long titles for some of the words, but these can be reduced to one or two letters if you want to conserve memory. However, do not use the letters / or J because these are used by the Ace as loop counters. Also, the letters A to F should not be used if you are going to work in hexadecimal, as they could then be both commands and numbers.
The listing is in two separate parts first the program to define the
t first sight, the Jupiter Ace is an unimpressive plastic box strongly reminiscent of the ZX80. The Ace keyboard is a slightly improved version of the Spectrum keyboard. Both of these features betray the origin of the basic design. The Ace is, however, a totally different machine from these computers by virtue of the language, Forth.
When the Ace is turned on, you will be pleasantly surprised by the dark screen which is much easier to use than a ZX81 'bright' screen. The cursor is a small white pixel which can easily be changed to suit all tastes (the cursor is Chr$ 151).
If you have just bought an Ace, after using a ZX81, then two features will strike you very quickly - it is very, very, fast and your commands do not work. Although Forth uses many commands found in Basic, the order of the command, and any numbers associated with it, are reversed. This reverse notation is awkward to use at first, but a few weeks' use will soon make you feel at home. To give an example, the Basic line:
FOR I = 0 TO 1000 : NEXT I
is replaced in Forth by:

1001 0 DO LOOP

Notice the fact that the do-loop never reaches the upper limit. The Forth equivalent is clearly shorter and on the Ace takes 0.125 seconds to run, about eight time faster than the BBC micro. A further example of speed is given by the word Type where:
100 100 TYPE
will print out the first 100 characters after location 100 in the Ace (equivalent to For I = 100 To 200 Print Chr$ (Peek (1)) : Next 1) and takes a remarkable 0.04 seconds.
The first question asked by Basic users about the Ace is how you write programs without pro-
gram lines. To understand this you must understand how to define words. Words in Forth can be commands like CLS, Then or Print for instance. Instead of program lines, Forth arranges these words to produce the program. For instance, if you wanted to use the equivalent of the Basic line:
100 PRINT AT 10,20 ;
we would define a word Hello as:
: HELLO 10 20 AT . :
This has exactly the same effect. Now, if you want to write hello in the middle of the screen, you type in the word Hello and press Enter. If you want to clear the screen before printing hello, you could define a word Clear as:
If you now type Clear, the Ace will perform the CLS command then the Hello command. Simply by extending this idea, - you can build up larger and larger words (or words which do more and more) until you type in one word and the computer plays space invaders.
Word definitions start with a colon and end with a semi-colon. After the colon you must print a space, then the new word you wish to define. The use of spaces is very important in Forth as it tells the computer where one word ends and another begins.
Next, print the commands which your new word will perform - note that these commands must already be defined so that the compiler can work correctly. When your word definition is complete, a semicolon tells the Ace that you have finished.
After building up a number of words, you type the master word which runs the program by calling up other words in the same way as subroutines work in Basic. This short overview gives an idea of how Forth works, but there is much more, such as the use of data stacks to store numbers which are vital to the running of
Popular Computing Weekly 20 January page 23
graphic characters and then the program itself. In Forth remarks are contained in brackets. These have no affect on the program and can be omitted.
A peculiarity of the Ace is that you cannot Save the contents of the character set memory (or rather you can not Save it accurately). So, I think the best alternative is to Save the data in a separate program.
You must first use a graphics word to define your characters. The manual gives a suitable word (page 71):
: GRAPHICS 8 * 11263 + dup
8 + do i c! -1 +loop ;
Now you must select the data to use. For this program I suggest defining words as follows:
: ship F0 3F 1F 0F 1F 3F F0 00
1 graphics ;
: ship2 00 00 C0 FC C0 00 00
00 3 graphics ;
Note that the data is in hexadecimal (base 16). A useful feature of the Ace is that you can change number bases in the middle of words by use of the square brackets which change you from immediate (ie normal) mode to defining mode (as in word definitions) and back again.
The space ship can be tested by:
(immediate mode ) invis 1
emit 3 emit (enter )
which should print out the ship as the word Emit is equivalent to Basic's "Print Chr$". The word lnvis stops the input line being printed onto the screen (useful for graphic games).
Other words to give different graphics are:
: misslle 06 FF 06 00 06 FF 06
00 4 graphics ;
: alien1 0E 38 70 56 70 38 0E
00 5 graphics ;
: alien2 18 3C 7E FF 7E 3C 18
00 2 graphics ;
And for the explosions:
: ex6 00 00 00 18 18 00 00
00 6 graphics ;
: ex7 00 00 18 24 24 18 00
00 7 graphics ;
: ex8 00 3C 42 42 42 42 3C
00 8 graphics ;
: ex9 7E 81 81 81 81 81 81
7E 9 graphics ;
: ex10 00 3C 42 42 42 42 3C
00 A graphics ;
: ex11 00 00 18 24 24 18 00
00 B graphics ;
: ex12 00 00 00 18 18 00 00
00 C graphics ;
This program should be tested, saved and verified. A word which uses all the word definitions should be written beforehand, so the program can be Loaded and run to Load the character data for the main program.
Use Forget Graphics after running. This last instruction makes room for the rest of the program.
The main program must be typed in the following order because the Ace will not accept words inside a word definition unless they are already in the Dictionary.
First you must initialise the variables:
1 constant y ( y co-ordinate of ship)
10 variable x ( x co-ordinate of ship)
0 variable sc ( score)
The game requires a random number generator. A routine is included in the Ace manual. The words Seed, Seedon, Rnd and Rand are needed.
To start the program we must reset the variables:
: INIT 0 sc ! 10 x ! cls ;
A word to draw and erase the ship must now be written:
: ship x @ y @ at 1 emit
3 emit ;
: m x @ y @ at ."    ";
( there are three spaces in the quotes)
To allow movement, define the following Words:
: up m (erase ship) x @
1- x! ;
: down m x ((-1) 1+ x!;
: move inkey dup 101 = if up
then 99 = if down then ;
The word Move uses the word Up if the 'E' key is being pressed and the word Down if the 'C' key is being pressed.
In order to make the aliens move, I have used a word which Scrolls the screen to the left. Some idea of the speed of Forth can be seen here in that it can perform a screen scroll without resorting to machine code:

: scr 9898 9216 do 32 4 do
i j + dup c @ swap 1- c!
loop 32 +loop ;
To draw random aliens in the last column use;

: LL 22 0 do i 31 at 32 rnd
?dup 0= if 2 emit else 3 <
if 5 emit else ."  "
(2 spaces) then then loop ;
Now you can set up the means of shooting at the aliens.

First, you need a simple delay loop.
: wait 100 0 do loop ;
(This is directly equivalent
to Basic's; For I = 0 TO 99
: Next I)
: score sc dup @ 10 + dup rot
! 0 0 at . ;
: bang 15388 @ 12 0 do wait
dup i swap c! loop 32 swap
c! score ;
: fire x @ dup 3 at 32 3 do
15388 @ c@ 32 = if 4 emit
wait dup i at ."  " ( 2 spaces ) else bang leave then loop drop ;
A simple lnkey routine to detect if the (fire) button is being pressed;
: ?F inkey 107 = if fire then :
Lastly, the word to actually play the game is Go:
: go init 100 0 do move ship
?f LL scr ?f x @ ."  "
(2 spaces ) loop ;
If you used my suggestion to use short word titles, then you might be able to fit in the word;
: g go cls 10 10 at ." score"
sc @ . ;
which ends the program by printing the score.
The game is quite simple to play. You are part of the Earth Defence Fleet when you come upon an armada of alien fighters. You have just 100 seconds (Galactic Seconds by the way) to destroy as many enemy fighters as possible.
The program is as complex as the limitations of memory allowed. The only possible way of stopping the program before the end is to lower your ship into the 'Input' line (so don't do it!).
Once you have ensured the program works properly, you can use the Fast command which almost doubles the speed of the game. A score of 1000 is very good (my record is 1060). As the manual states, Fast is very dangerous as errors tend to lead to crashes, so be careful.
This program should be Saved and Verified after the previous graphics program. To play the game, first Load and Run the graphics program (to get the user-defined characters) then rub out that program and Load and play the game.

Possible problems
The most likely problem that will occur is missing-out/putting in a space where it should/should not be. If the Ace will not accept a word, then check to see if you have defined it (for example, it will not accept the word Move until Up and Down have been defined). The next most likely problem is running out of memory (until you get a 16K Ram pack fitted). This is caused by the dictionary being either too large (which can only be cured by Forgetting words) or by improperly defining a word so that it leaves unwanted numbers on the stack which will eventually fill up the memory.