Making Atari BASIC Faster (and Smaller)
Atari BASIC is a pleasant language to write in, but nobody ever called it quick. The reasons are structural, and once you know them you can often double the speed of a program without touching machine code.
You will learn: what makes Atari BASIC slow, a checklist of speed-ups in order of effectiveness, how to measure the effect with the system's jiffy clock, a matching list of memory-saving tricks, and when it is time to change tools.
You need: Atari BASIC on a real machine or emulator. (In Altirra, turning off "warp" speed matters when you benchmark.)
1. Why it is slow
De Re Atari, Atari's own technical manual, is frank about the interpreter's weaknesses:
- There are no integers. Every number — loop counters, screen coordinates, POKE values — is a six-byte binary-coded-decimal floating-point value, and the maths package that handles them is slow.
- Line numbers are found by searching. Your program is stored as a linked list of tokenized lines in ascending order. When BASIC executes
GOTO 2000orGOSUB 2000, it starts at the first line of the program and walks forward, line by line, until it finds 2000. A jump near the end of a long program is measurably slower than one near the start. - Loops and subroutines use a software stack.
FORpushes a 16-byte entry andGOSUBa 4-byte entry onto BASIC's runtime stack, and BASIC returns to the stored line number when the loop repeats or the subroutine returns. - The display steals time. ANTIC, the display chip, takes cycles from the 6502 to fetch screen data.
2. The speed-up checklist
De Re Atari lists its recommendations "in order of decreasing effectiveness". Here they are with examples.
Restructure and check the algorithm. Unstructured code gets worse with every revision, and a better algorithm beats any micro-trick. A shell sort instead of a bubble sort will matter more than everything below.
Put hot subroutines and loops at the top. Since line searches start from the beginning, the classic Atari layout jumps over its fast routines at the start:
10 GOTO 1000
20 REM *** FREQUENTLY CALLED ROUTINE ***
30 X=X+DX:Y=Y+DY:RETURN
1000 REM *** MAIN PROGRAM STARTS HERE ***
1010 GOSUB 20
Use in-line code in tight loops. A GOSUB costs a line search plus stack work. If a routine is called inside an inner loop, consider copying its statements into the loop.
Make the busiest loop the innermost. In nested loops, the inner loop's FOR entry is pushed and popped least often per iteration if it is the one with the most iterations.
Simplify the arithmetic. If you multiply a constant by a loop counter, add the constant instead:
100 REM SLOWER: A MULTIPLY EVERY PASS
110 FOR I=0 TO 23:POKE SC+I*40,128:NEXT I
200 REM FASTER: ONE ADDITION EVERY PASS
210 A=SC:FOR I=0 TO 23:POKE A,128:A=A+40:NEXT I
(SC here would be the screen address, PEEK(88)+PEEK(89)*256.)
Keep loops on one line. If the FOR, the body and the NEXT are all on one line, BASIC never has to fetch another line to continue the loop — as in line 210 above.
Switch the screen off during heavy work. POKE 559,0 turns off ANTIC's display DMA. The saving is up to 30 percent. The screen goes black, so save the old value and restore it afterwards — the default is 34:
100 D=PEEK(559):POKE 559,0
110 REM ... LONG CALCULATION OR DISK LOAD HERE ...
120 POKE 559,D
This is also a neat way to make a picture appear "instantly" after drawing or loading it.
Use a cheaper display. If you do not need a full screen, a less demanding graphics mode or a shorter display list can save up to 25 percent (Atari's own estimate in De Re Atari).
Move the inner loop to machine language. The final item on Atari's list: code the critical loop in assembler and call it with USR. See our USR guide for how arguments are passed.
Let strings move memory for you. BASIC copies strings with a fast internal routine, so a string assignment moves a block of bytes far faster than a FOR/POKE loop. The self-copy fill (A$(1)="X":A$(1000)="X":A$(2)=A$) and the string-pointer technique for player/missile animation are both described in our string guide.
3. Measure, don't guess
The operating system keeps a three-byte counter called RTCLOK at locations 18, 19 and 20. Location 20 increases every vertical blank — 60 times a second on NTSC machines, 50 on PAL — carrying into 19 and then 18. You may POKE these registers for your own use. A tiny harness lets you time alternatives:
10 GOSUB 900:T0=T
20 FOR I=1 TO 500:X=I*3:NEXT I
30 GOSUB 900:PRINT "JIFFIES: ";T-T0
40 END
900 T=PEEK(18)*65536+PEEK(19)*256+PEEK(20):RETURN
Run it, then change line 20 to try another approach. Because the three bytes are read separately, a reading taken just as the low byte rolls over can occasionally be off; repeat a test a couple of times before trusting a surprising number. Also remember that moving the timing subroutine from line 900 to line 2 is itself an optimisation — it is a GOSUB target.
4. Saving memory
The same chapter of De Re Atari gives a parallel list for squeezing programs into less RAM — useful on 16K machines or when a big array needs room:
- Remove REMs. Remarks are stored as plain text in the running program.
- Replace a constant used three or more times with a variable. A numeric constant occupies seven bytes in the tokenized line; a variable reference occupies one.
- Name your common line numbers. If
GOSUB 100appears fifty times, settingZ100=100and writingGOSUB Z100saves around 300 bytes. (It also makes renumbering harder, so do it last.) - Use few, short variable names. Each name adds about 8 bytes to the variable value table plus its spelling to the name table. The limit is 128 names in any case.
- Clean out dead variables. Names stay in the tables even after you delete every line that used them.
LIST "D:PROG.LST",NEW, thenENTER "D:PROG.LST"rebuilds the tables with only the names still in use. - Pack lines together. Joining two lines into one saves three bytes each time.
- Use strings for small numbers. If array values never exceed 255, a string stores them at one byte each instead of six.
- POKE instead of SETCOLOR, and use cursor-control characters inside PRINT strings instead of POSITION statements.
- Chain programs. Run an initialisation program first and have it
RUNthe main program from disk.
5. When to change tools
There is a ceiling to all this. When you reach it:
- Turbo-BASIC XL (Frank Ostrowski, 1985, XL/XE only) loads from disk, runs Atari BASIC programs largely unchanged, and is described in its documentation as about three times faster; its companion compiler claims a further large speed-up, with compiled programs running from a small runtime file.
- FastBasic is a modern, line-number-free BASIC with integer variables that compiles to fast bytecode, either on the Atari or with a PC cross-compiler.
- Action! compiles a structured language to machine code and is often within reach of hand-written assembler.
We cover each of these in their own guides.
Troubleshooting
- Speed-ups made no difference. Measure first. The bottleneck is often a single inner loop, a disk access, or PRINTing to the screen.
- The screen did not come back. Restore 559 with the value you PEEKed, not a guess; programs that use players and missiles set extra bits.
- The program got bigger every time I loaded and saved it. That is not your code — revision B of Atari BASIC (built into many 600XL/800XL machines) adds 16 bytes on each LOAD. Save with
LISTand reload withENTERoccasionally, or use a revision C cartridge.PRINT PEEK(43234)tells you which revision you have: 162 = A, 96 = B, 234 = C.
Further reading
- Atari BASIC Faster and Better — Carl M. Evans (IJG, Inc., 1983).
- De Re Atari — Chris Crawford, Lane Winner and the Software Development Support Group (Atari, Inc., 1982).
- The Atari BASIC Source Book — Bill Wilkinson, Kathleen O'Brien and Paul Laughton (Small System Services, 1983), for the definitive explanation of how the interpreter spends its time.
Credits
- De Re Atari, Chapter 10 — Atari BASIC (Improving Program Performance) — Lane Winner (Section 10 of De Re Atari)
- Mapping the Atari — locations 18-20 (RTCLOK) and 559 (SDMCTL) — AtariArchives.org
- Turbo-BASIC XL Expanded Documentation (Ron Fetzer) and Compiler notes (Dave Arlington) — Turbo-BASIC XL Expanded Documentation (Ron Fetzer); AtariAge forum members
- Atari BASIC Faster and Better (Carl M. Evans, IJG 1983) — Atari8bit.net, 1983
- comp.sys.atari.8bit FAQ (Michael Current) — 7.2.1 What is Atari BASIC? — Michael Current (comp.sys.atari.8bit FAQ)
