Converting Atari BASIC Programs to FastBasic
FastBasic reads like Atari BASIC, so it is tempting to paste an old program in and press compile. That rarely works first time, and a few differences can make a ported program misbehave quietly. This guide works through them using a real program: Star Catcher, the 44-line game from Writing a Complete Game in Atari BASIC, Step by Step. By the end you'll have the same game in FastBasic, a list of every change needed, and measured figures for how much faster it runs.
What you need: FastBasic (the examples were compiled with the 4.7 cross-compiler) and the original Atari BASIC listing. The ported game was run in atari800, and both versions were timed on the same emulated machine.
1. What stays the same
A lot carries over unchanged, which is what makes porting worthwhile:
GRAPHICS,SETCOLOR,COLOR,PLOT,DRAWTO,POSITION,PRINT(or?),SOUND,PEEK,POKE,STICKandSTRIGtake the same arguments.FOR/NEXTloops look the same.DIM X(4)still gives elements 0 to 4.- Screen codes, memory locations and hardware registers are the same machine underneath.
2. The conversion table
| Atari BASIC | FastBasic | Notes |
|---|---|---|
| Line numbers | none | Remove them. Keep a comment where a block started. |
GOTO 210 (loop back) |
DO … LOOP, REPEAT … UNTIL, WHILE … WEND |
Leave a loop early with EXIT. There is no GOTO. |
GOSUB 800 … RETURN |
PROC Score … ENDPROC, called with @Score |
Procedures can have parameters. |
IF A THEN B:C |
IF A / B : C / ENDIF |
THEN runs one statement only (see section 3). |
IF A THEN 330 (skip ahead) |
invert the test, or use ELSE/ELIF |
|
X=INT(RND(0)*40) |
X = RAND(40) |
Integer 0–39. |
IF RND(0)<0.3 |
IF RAND(10) < 3 |
Integer chances. |
PEEK(88)+256*PEEK(89) |
DPEEK(88) |
16-bit read. |
P=P-1 / P=P+1 |
DEC P / INC P |
Shorter and faster. |
PRINT #6;"TEXT" |
? #6, "TEXT" |
A comma after the channel, not a semicolon. |
DIM N$(20) |
not needed | Strings hold up to 255 characters without a DIM. |
A=1.5 |
A% = 1.5 |
Floating point needs % and the floating-point target. |
Two more rules apply everywhere:
- Numbers are 16-bit integers.
7/2gives 3.32767+1gives −32768. Addresses above 32767 print as negative numbers: on an XL with BASIC off,DPEEK(88)printed −17344 for screen memory at $BC40. They still work inPEEKandPOKE, so leave them alone. - Assign before you read. The compiler works from top to bottom and rejects a variable that is read before any assignment appears in the source. Initialise variables at the top of the program.
3. The THEN trap
This is the change most likely to break a port without any error message. In Atari BASIC, everything after THEN on the line belongs to the IF:
220 J=STICK(0):IF J=11 AND P>0 THEN POKE SC+922+P,0:P=P-1:GOSUB 700
FastBasic's manual is explicit: IF … THEN executes one statement. Typed in as it is, only the POKE depends on the joystick. P=P-1 and the catcher redraw would run every time, sliding the catcher left on its own. Every multi-statement THEN must become a block:
IF J = 11 AND P > 0
POKE SC+922+P, 0 : DEC P : @Catcher
ENDIF
Go through the original line by line and mark every THEN that is followed by more than one statement before you start.
4. Untangling the GOTOs
The heart of Star Catcher is a loop over four falling objects, steered by jumps:
260 Y(I)=Y(I)+1:IF Y(I)<1 THEN 330
270 IF Y(I)=1 THEN X(I)=INT(RND(0)*40):K(I)=10:IF RND(0)<0.3 THEN K(I)=3
280 IF Y(I)<23 THEN POKE SC+Y(I)*40+X(I),K(I):GOTO 330
290 IF X(I)<P OR X(I)>P+2 THEN 320
300 IF K(I)=10 THEN S=S+10:SOUND 0,40,10,8:GOSUB 800
310 IF K(I)=3 THEN L=L-1:SOUND 0,200,8,10:GOSUB 800:GOSUB 700
320 Y(I)=-INT(RND(0)*8)
330 NEXT I
Read it as a set of cases. A row below 1 does nothing more. Rows 1 to 22 draw the object. Row 23 is the catcher's row: score or lose a life if the catcher is under it, then send the object back above the screen either way. In FastBasic, that becomes one IF/ELIF with the catch test nested inside:
INC Y(I)
IF Y(I) = 1
X(I) = RAND(40) : K(I) = 10
IF RAND(10) < 3 THEN K(I) = 3
ENDIF
IF Y(I) > 0 AND Y(I) < 23
POKE SC + Y(I)*40 + X(I), K(I)
ELIF Y(I) = 23
IF X(I) >= P AND X(I) <= P+2
IF K(I) = 10
S = S + 10 : SOUND 0, 40, 10, 8 : @Score
ELSE
DEC L : SOUND 0, 200, 8, 10 : @Score : @Catcher
ENDIF
ENDIF
Y(I) = -RAND(8)
ENDIF
The condition X(I)<P OR X(I)>P+2 THEN 320 ("skip the scoring if missed") has been turned round into "score if caught". Inverting a skip like this is the usual way to remove a forward GOTO. The single-statement IF RAND(10) < 3 THEN K(I) = 3 is safe as it is, because only one statement follows THEN.
5. Loops, subroutines and the frame wait
The rest maps directly:
- The
GOSUB 700/800/900subroutines becomePROC Catcher,PROC ScoreandPROC Title. 200 REM MAIN LOOP…360 GOTO 210, with its exit atIF L=0 THEN 600, becomesREPEAT … UNTIL L = 0.GOTO 100after game over becomes an outerDO … LOOParound the whole game.- The waits for the fire button,
630 IF STRIG(0)=0 THEN 630, become one-line loops:REPEAT : UNTIL STRIG(0) = 1. - The frame wait
350 IF PEEK(20)=T THEN 350becomesREPEAT : UNTIL PEEK(20) <> T.
6. How much faster?
To compare, both versions were changed to skip the title, start with 999 lives, drop the frame wait and stop after 300 passes of the main loop, printing the time from the jiffy clock (locations 19 and 20):
| Version | 300 loop passes | Per pass |
|---|---|---|
| Atari BASIC (rev C) | 3,304 jiffies | about 11 frames |
| Altirra BASIC 1.58 | 1,347 jiffies | about 4.5 frames |
| FastBasic 4.7 port | 284 jiffies | about 0.95 frames |
FastBasic ran the same loop about 11.6 times faster than Atari BASIC and almost 5 times faster than Altirra BASIC. In Atari BASIC each pass takes about 11 frames, so the frame wait in the original never actually waits. The FastBasic version finishes a pass in less than one frame, so with the frame wait back in, the game runs at a steady 60 steps a second (50 on PAL). That speed is a design change, of course: you may want to move objects every second or third frame, or add more of them.
7. The complete port
' STAR CATCHER - ported from Atari BASIC to FastBasic
DIM X(4), Y(4), K(4)
SC = 0 : S = 0 : L = 0 : P = 0 : T = 0 : J = 0
PROC Catcher
POKE SC+920+P, 60 : POKE SC+921+P, 63 : POKE SC+922+P, 15
ENDPROC
PROC Score
POSITION 2, 0 : ? "SCORE "; S; " LIVES "; L; " ";
ENDPROC
PROC Title
GRAPHICS 2+16 : SETCOLOR 4, 9, 2
POSITION 3, 2 : ? #6, "STAR CATCHER"
POSITION 2, 5 : ? #6, "catch the stars"
POSITION 1, 6 : ? #6, "DODGE THE ROCKS"
POSITION 2, 9 : ? #6, "PRESS FIRE"
REPEAT : UNTIL STRIG(0) = 0
REPEAT : UNTIL STRIG(0) = 1
ENDPROC
@Title
DO
' New game
GRAPHICS 0 : POKE 752, 1 : SETCOLOR 2, 0, 0 : SETCOLOR 1, 0, 14
SC = DPEEK(88) : S = 0 : L = 3 : P = 18
FOR I = 1 TO 4 : Y(I) = -I * 6 : NEXT I
@Score : @Catcher
REPEAT
T = PEEK(20)
J = STICK(0)
IF J = 11 AND P > 0
POKE SC+922+P, 0 : DEC P : @Catcher
ENDIF
IF J = 7 AND P < 37
POKE SC+920+P, 0 : INC P : @Catcher
ENDIF
FOR I = 1 TO 4
IF Y(I) > 0 THEN POKE SC + Y(I)*40 + X(I), 0
INC Y(I)
IF Y(I) = 1
X(I) = RAND(40) : K(I) = 10
IF RAND(10) < 3 THEN K(I) = 3
ENDIF
IF Y(I) > 0 AND Y(I) < 23
POKE SC + Y(I)*40 + X(I), K(I)
ELIF Y(I) = 23
IF X(I) >= P AND X(I) <= P+2
IF K(I) = 10
S = S + 10 : SOUND 0, 40, 10, 8 : @Score
ELSE
DEC L : SOUND 0, 200, 8, 10 : @Score : @Catcher
ENDIF
ENDIF
Y(I) = -RAND(8)
ENDIF
NEXT I
SOUND 0, 0, 0, 0
REPEAT : UNTIL PEEK(20) <> T
UNTIL L = 0
' Game over
POSITION 15, 10 : ? "GAME OVER";
POSITION 8, 12 : ? "PRESS FIRE TO PLAY AGAIN";
REPEAT : UNTIL STRIG(0) = 1
REPEAT : UNTIL STRIG(0) = 0
LOOP
It compiles to a 2,056-byte executable. Play it in an emulator or on a real Atari from DOS.
8. Tuning the result
A faithful port is the right first step: get it working the same way, then change it. With the speed-up, Star Catcher became much harder. Left alone, the faithful port reached game over in under 30 seconds. Replacing the frame wait with PAUSE 4 (one step every four frames) brought back a playable pace, and frees time for more objects, smoother movement or player/missile graphics (see Writing a Game in FastBasic, Step by Step).
Troubleshooting
parse error, expected: variable name: a variable is read before it is assigned anywhere above. Give it a value at the top.- Something happens when it shouldn't: a multi-statement
THENwas copied unchanged. Convert it to a blockIF … ENDIF. - Random numbers out of range:
RAND(n)returns 0 to n−1. Don't addINT()or multiply. - Wrong numbers after a division: FastBasic divides integers. Reorder the sum (
A*10/3rather thanA/3*10), or use floating-point%variables. - Text in GRAPHICS 1 or 2 doesn't appear: use
? #6, "TEXT"with a comma. - A negative address: that's normal for locations above 32767.
POKEandPEEKstill work.
Further reading
- FastBasic Manual: the sections on
IF,DIM,RAND,DPEEKandPAUSE. - On this site: Getting Started with FastBasic, Writing a Complete Game in Atari BASIC, Step by Step, Writing a Game in FastBasic, Step by Step, Converting Microsoft BASIC and Applesoft Listings to Atari BASIC.
Credits
- Picture: Star Catcher after porting to FastBasic: the same game, with a much faster main loop. — Program authors; screenshot by the site
- FastBasic Manual (MANUAL.md, FastBasic 4.7) — IF/THEN executes one statement; DIM; RAND; DPEEK; PRINT # comma rule; DO/LOOP, REPEAT, WHILE/WEND, EXIT; PAUSE; 16-bit integers — GitHub project authors
- Star Catcher original listing — site guide writing-a-basic-game-step-by-step (original program written for this site)
- Mapping the Atari — RTCLOK 18-20, SAVMSC 88-89 — AtariArchives.org
