Profiling Atari BASIC Programs: Finding the Slow Lines with Bottleneck Breaker
Every slow BASIC program has a few lines that use most of the time, and they are rarely the ones you would guess. Tips for speeding up Atari BASIC are plentiful (see Making Atari BASIC Faster), but applying them everywhere wastes effort. A profiler tells you where to look. Antic published one for Atari BASIC in April 1989: Bottleneck Breaker by Stan Lackey, three short BASIC programs and a 124-byte block of machine code. This guide runs it, explains how it works, and shows how to use it on your own programs.
What you need: Atari BASIC, 48K, and a disk drive (or an emulator's host-folder device). The programs are on the April 1989 Antic disk (they appear in Antic's software archive under 89-04a). We ran the original files from that disk in atari800, with a real Atari BASIC revision C ROM and the emulator's built-in AltirraOS, loading them from the H: host device instead of D:.
1. Sampling: how a profiler can be cheap
An exact profiler would time every line, which would slow the program enormously. Bottleneck Breaker samples instead. A machine-language routine runs on POKEY's Timer 1 interrupt, about 250 times a second. Each time, it looks at which line BASIC is executing and adds one to a counter for that line. Over a run of several seconds the counts become a good statistical picture of where the time goes. The article puts the cost at about 25% extra running time.
Atari BASIC makes this easy. Its pointer STMCUR (locations 138–139) always points to the start of the line being executed, and the first two bytes of every tokenised line are its line number. The interrupt routine just reads those two bytes.
2. The three programs
10 REM BASIC PROFILER, PART 1
20 REM BY STAN LACKEY
30 REM (c)1988, ANTIC PUBLISHING
40 X=PEEK(106)-32:REM RESERVE 8K FOR COUNTERS
50 POKE 106,X:POKE 740,X
60 GRAPHICS 0:? "Working..."
70 FOR X=40704 TO 40827:REM LOAD PROFILER INTO PAGE 9F
80 READ D:POKE X,D:NEXT X
90 DATA 0,173,0,159,208,2,104,64,152,72,160,0,177,138,133,203,200,177,138,133,204,48,95,169,0
100 DATA 133,205,169,80,133,206,169,0,133,207,133,208,160,11,56,165,203,229,205,141,122,159,165,204,229
110 DATA 206,141,123,159,24,48,11,173,122,159,133,203,173,123,159,133,204,56,38,207,38,208,70,206,102
120 DATA 205,136,16,216,6,207,38,208,24,165,208,101,106,105,4,133,208,160,0,24,177,207,105,1,145
130 DATA 207,208,15,200,24,177,207,105,1,145,207,16,5,169,0,141,0,159,104,168,104,64,0,0
140 REM SET UP TIMER INTERRUPT
150 POKE 528,1:REM VTIMR1 VECTOR LOW
160 POKE 529,159:REM VTIMR1 HI
170 POKE 53761,0:REM VOLUME TO ZERO
180 POKE 53760,255:REM FREQ OF TMR1
190 POKE 53769,255:REM TURN ON TIMERS
200 POKE 16,193:REM SET TMINT1
210 ? "Done...":? :? "Next:":? :? "LOAD the program to be Profiled":?
220 ? :? "ENTER ";CHR$(34);"D:PROFILE2.LST";CHR$(34):?
230 ? :? "RUN"
PROFILE1.BAS lowers RAMTOP (location 106) by 32 pages to reserve 8K at the top of memory, and reissues GRAPHICS 0 so the screen moves below it. It then POKEs the sampler into 40704–40827 ($9F00–$9F7B) and starts POKEY's Timer 1: it points the timer vector VTIMR1 (528–529) at the routine, silences voice 1, sets its divider to 255, restarts the timers with STIMER (53769) and enables the timer interrupt through POKMSK (16). With the default 64 kHz clock, a divider of 255 means 63,920 / 256, about 250 interrupts a second.
1 REM BASIC PROFILER, PART II (c)1988, ANTIC PUBLISHING
2 I=(PEEK(106)+4)*256:FOR J=I TO I+6551:POKE J,0:NEXT J:REM CLEAR COUNTERS
3 POKE 40704,1:REM START PROFILING
31000 POKE 40704,0:REM STOP PROFILING
31010 ? "Done...":? :? "Next:":? :? "RUN ";CHR$(34);"D:PROFILE3.BAS";CHR$(34)
PROFILE2.LST is merged into the program you want to profile with ENTER. Line 2 clears the counters (this takes about a minute in BASIC), line 3 switches sampling on by POKEing 1 into the flag at 40704, and line 31000 switches it off. Your program must therefore leave lines 1–3 and 31000–31010 free, and it must reach line 31000 when it ends. If it stops with END or an error instead, type GOTO 31000 before going on.
10 REM BASIC PROFILER, PART 3
20 REM BY STAN LACKEY
30 REM (c)1988, ANTIC PUBLISHING
40 POKE 40704,0:REM STOP PROFILING
100 DIM L(1000),V(1000):REM LINE#, VALUE
110 I=0:TOTAL=0
115 ? "Reading Counters..."
116 X=(PEEK(106)+4)*256
120 FOR J=0 TO 6550 STEP 2
130 REM READ EACH COUNTER
131 K=PEEK(J+X)+256*PEEK(J+X+1)
140 IF K=0 THEN 170
150 REM SAVE ALL NON-ZERO VALUES
155 I=I+1
160 L(I)=J*5:V(I)=K:TOTAL=TOTAL+K
170 NEXT J
175 MAX=I
180 ? "TOTAL COUNTS = ";TOTAL
185 ? "IN ";MAX;" PROGRAM LINES"
190 ? "SORTING..."
200 REM ROUTINE SORTS ALL ENTRIES
205 FOR J=1 TO MAX
210 FOR I=J TO MAX
220 IF V(I)<V(J) THEN 260
230 REM EXCHANGE L&V VALUES TO SORT
240 X=V(I):V(I)=V(J):V(J)=X
250 X=L(I):L(I)=L(J):L(J)=X
260 NEXT I
265 ? ".";
270 NEXT J
380 TRAP 450
390 PF=1
395 ?
400 ? "LINE","COUNTS","PERCENT","CUM PCT"
410 LPRINT "TOTAL COUNTS = ";TOTAL
420 LPRINT
430 LPRINT "LINE","COUNTS","PERCENT","CUM PCT"
440 GOTO 480
450 PF=0
480 REM PRINT TOP 20 IF TO SCREEN, OR
485 REM TOP 40 IF PRINTER IS ON
490 J=20+20*PF:IF MAX<J THEN J=MAX
500 CUM=0
510 FOR I=1 TO J
520 PCT=V(I)/TOTAL:CUM=CUM+PCT
530 Y=(INT(0.5+10000*CUM))/100
540 X=(INT(0.5+10000*PCT))/100
550 ? L(I),V(I),X,Y
560 IF PF THEN LPRINT L(I),V(I),X,Y
570 NEXT I
PROFILE3.BAS reads the 3,276 counters, keeps the non-zero ones, sorts them with a simple exchange sort, and prints the top 20 (40 if a printer answers). The line number shown is the counter number times ten, because one counter covers ten line numbers: lines 120–129 all count as "120". If you number in tens, as most people do, each line gets its own counter.
3. How the sampler works
We disassembled the DATA statements. The routine at $9F01 does this on every interrupt:
- If the flag at $9F00 is 0, it returns at once (
PLA,RTI: the OS's IRQ dispatcher has already pushed A). - It saves Y, then reads the line number from (STMCUR),Y into $CB–$CC. A line number of 32768 or more means direct mode, which it ignores.
- It divides the line number by 10 with a 12-step shift-and-subtract loop, starting from a divisor of $5000 (20,480).
- It doubles the quotient and adds the page (RAMTOP + 4) × 256, giving the address of a 16-bit counter, and increments it.
- If a counter's high byte reaches $80, it clears the flag, so a very long run stops sampling rather than overflowing and losing accuracy.
It uses zero-page locations 203–208 ($CB–$D0), so a program that uses them (some machine-language helpers do) will disturb it. The routine sits at the fixed address $9F00 while the counters are placed relative to the original RAMTOP, so it expects the standard 40K BASIC memory layout (RAMTOP = 160).
4. Running it
The sequence, as the programs' own prompts give it:
RUN "D:PROFILE1.BAS"
LOAD "D:PROFTEST.BAS"
ENTER "D:PROFILE2.LST"
RUN
RUN "D:PROFILE3.BAS"
PROFTEST.BAS is the article's test subject, a loop doing a division, a cosine, an addition, a multiplication, a string copy, a LEN and a REM:
10 REM PROFTST TESTS PROFILER
40 DIM Q$(100),W$(100)
50 W$="This is dummy text for the string copy timing test"
100 FOR X=1 TO 100
110 Y=1/X
120 Z=COS(X)
130 W=X+1
140 Q=X*Y
150 Q$=W$
160 Y=LEN(W$)
170 REM TIME TO SKIP OVER A REM
300 NEXT X
Our report (in the screenshot) counted 1,806 samples in 15 counters:
| Line | Counts | Percent | What it does |
|---|---|---|---|
| 120 | 973 | 53.88 | Z=COS(X) |
| 110 | 317 | 17.55 | Y=1/X |
| 140 | 140 | 7.75 | Q=X*Y |
| 160 | 110 | 6.09 | Y=LEN(W$) |
| 130 | 90 | 4.98 | W=X+1 |
| 150 | 87 | 4.82 | Q$=W$ |
| 300 | 61 | 3.38 | NEXT X |
The rest were a handful of samples each, including line "0" (the profiler's own lines 1–3), 31000 and a stray "20510". The article warns about the last kind: if a sample lands while BASIC is moving from one line to the next, STMCUR can briefly point at something that isn't a line, and a garbage number appears. It is rare enough to ignore.
The article's own report, from 1989, had a total of 3,389 counts with line 120 at 71.47% and line 110 at 10.91%. Our run took about half as many samples, so it finished in about half the time, and the cosine's share was smaller. The most likely reason is that we ran with AltirraOS, whose floating-point routines are not Atari's, rather than an original Atari OS ROM: COS is computed by the OS math pack, and its speed changes the picture. The order of the top lines was the same, and that is what a profiler is for. Because it samples, no two runs give exactly the same counts.
5. Profiling your own program
- Renumber in steps of 10 if your lines are closer together, so each line gets its own counter (keep a backup: renumbering can break computed GOTOs).
- Make room: the program must fit with 8K less memory, and lines 1–3 and 31000–31010 must be free.
- Make it finish. Profile a part that runs and then stops, for example a level of a game with a fixed demo input, rather than a program that waits for keys forever. For a big program, LIST a group of subroutines to disk with a small driver and profile that.
- Read the cumulative column. In the test, four lines took 85% of the time. Speeding up anything else hardly matters.
- Fix, then measure again. Typical cures for the top lines: replace COS, SIN and division inside loops with tables computed once, take constant expressions out of loops, use variables instead of numeric constants, and move the hot loop to the start of the program (see Making Atari BASIC Faster).
Troubleshooting
- No report, or "TOTAL COUNTS = 0": sampling never started or the program never reached line 31000. Type
GOTO 31000, then run PROFILE3. - The program crashes when profiled: it uses locations 203–208, the top 8K of memory, or lines 1–3/31000–31010.
- Counts stop growing on a long run: a counter reached 32,768 and the sampler switched itself off. Profile a shorter section.
- Strange line numbers with a few counts: samples taken between lines. Ignore them.
- PROFILE2.LST won't ENTER cleanly from a downloaded copy: our copy of the file ended in a run of $1A padding bytes after the last line. Remove them if ENTER complains.
Further reading
- Stan Lackey, "Bottleneck Breaker", Antic, April 1989 (Vol. 7 No. 12), with the MAC/65 source of the sampler on the disk.
- Ian Chadwick, Mapping the Atari: STMCUR (138–139), RAMTOP (106), VTIMR1 (528–529), POKMSK (16) and STIMER (53769).
- On this site: Making Atari BASIC Faster (and Smaller), Debugging Atari BASIC Programs, POKEY Timer Interrupts in Depth, Inside a SAVEd Atari BASIC Program, and Making 6502 Code Faster: Altirra's Profiler for machine code.
Credits
- Picture: Bottleneck Breaker's report on its own test program: line 120, with a COS call, used over half the running time. — Program authors; screenshot by the site
- Antic, April 1989 (Vol. 7 No. 12) — Stan Lackey, Bottleneck Breaker — AtariMagazines.com (Kevin Savetz), 1989
- Antic disk 89-04a — PROFILE1.BAS, PROFILE2.LST, PROFILE3.BAS, PROFTEST.BAS (detokenized listings) — AtariMagazines.com (Kevin Savetz)
- Mapping the Atari — STMCUR, RAMTOP, VTIMR1, POKMSK, STIMER — AtariArchives.org
