Making 6502 Code Faster: Altirra's Profiler, Cycle Counting and Optimisation

When an Atari program is too slow, the hard part is usually finding where the time goes, not making that code faster. Altirra, Avery Lee's emulator, has a cycle-accurate profiler built into its debugger that answers the question directly. This guide shows how to drive it, how to read what it reports, how to count cycles by hand and with debugger commands, and which classic 6502 optimisations are worth doing. A small benchmark run on an emulated NTSC and PAL machine gives the real numbers.

What you'll learn: the profiler's five collection modes and what each is for; what the Clocks, CPU clocks and DMA% columns mean; the branch, page-crossing and redundant-instruction counters; multi-frame profiling with VBI and PC triggers; timing routines with @clk, tracepoints and gcr; and the optimisations that pay off on the Atari, including the cost of ANTIC's DMA.

What you need: Altirra, a program assembled with a symbol file (MADS: -l and -t), and comfort with the debugger basics in Mastering the Altirra Debugger. The benchmark uses MADS.

1. Running a profiling session

Open Debug > Profile > Profile View. It's an ordinary debugger pane that you can dock anywhere. The first three toolbar buttons are stop, pause and start. Start the profiler, let the program run through the slow part, then stop it. The results appear when the session ends.

Choose the mode before you start:

Mode Groups samples by Use it to
Instruction Each instruction See exactly which instructions eat the time
Function JSR/RTS and interrupt entry/RTI scopes Get a high-level "which routine" view
Call graph The full call stack on entry Find which caller makes a routine expensive
Basic block Runs of code ending in a branch, jump or return Find hot loops, the best starting point
BASIC line The BASIC line being executed Profile Atari BASIC, Altirra BASIC, Turbo-BASIC XL, and BASIC XL/XE outside EXTEND mode

In function mode, JMP and branches don't open a new scope, so a routine that JMPs into a shared tail is counted as part of its caller. That's worth remembering when a "function" looks bigger than it should.

Load symbols first. The Address column then shows your labels instead of bare hex addresses, and the results become much easier to read.

2. Reading the columns

Sort any list by clicking a column header (click again to reverse). Select several lines to see their totals summed at the bottom.

  • Thread. Main line, IRQ, VBI or DLI. In instruction mode this is decided by the CPU's I flag, so code running with interrupts masked counts as interrupt code. In function and call-graph modes, samples are grouped by the interrupt type in the call stack. This is the quickest way to see whether your DLIs or VBI, rather than the main loop, are using up the frame.
  • Calls and Insns. How often a function was entered, and how many instructions ran.
  • Clocks. Machine cycles, including those when ANTIC halted the CPU for DMA.
  • CPU clocks. Only the cycles the CPU actually executed ("unhalted cycles").
  • Clocks%, Insns%, CPUClocks%. Each sample's share of the total. Use these to judge whether an optimisation is worth doing at all. A routine that takes 10% of the time can only ever make the program about 11% faster, however good your rewrite. One taking 75% is where the wins are.
  • DMA%. Halted cycles as a fraction of total cycles for that sample. High values mean the code runs while ANTIC is busy, for example in a dense graphics mode.

Double-click a sample for the detailed view: disassembly around it with per-instruction figures. The figures combine every call in the frame. The disassembly is of what's in memory now, so it can be wrong for self-modifying code. A useful trick: a branch's average CPU cycles per instruction (CCPI) tells you how often it's taken, because a taken branch costs one extra cycle. A CCPI of 2.6 means it's taken about 60% of the time.

3. Performance counters

Up to two counters can be collected alongside the samples:

  • Branch taken / not taken. Shows branches you could reverse so that the common case falls through, saving a cycle or two each time.
  • Page crossing. Counts conditional branches and indexed reads that paid the extra cycle for crossing a page. Indexed stores (STA abs,X) and read-modify-write instructions (INC abs,X) aren't counted, because they always take the extra cycle anyway.
  • Redundant instruction. Flags flag-setting instructions that didn't change anything (a CLC when carry was already clear) and immediate loads that reloaded the value already in the register. A 100% figure suggests the instruction can go. Remember that it only covers the paths your session actually ran.

4. Multi-frame profiling

A one-off stutter is hard to catch in a session averaged over many frames. Set a frame trigger in the Options drop-down and the profiler records each frame separately, drawing a frame graph at the top of the view:

  • Vertical blank splits at the start of vertical blank (scan line 248), so profiler frames match TV frames.
  • PC trigger splits when execution reaches an address, for example the top of your main loop. Give an end address, or use function mode to end when the stack rises above its level at the start. The addresses can be expressions such as dw vvblki, which captures every run of the immediate VBI as its own frame. They're evaluated once when the session starts.

Click a spike in the graph to see just that frame, or drag across a range. For conditions too complex for a trigger, the debugger commands .profile_beginframe and .profile_endframe can be fired from a breakpoint's command string.

5. Counting cycles in the debugger

The expression evaluator has two clock counters: @clk (machine cycles) and @cclk (CPU cycles, without DMA halts). They match the profiler's two Clocks columns. Break at the start of a routine, type ? @clk, step over it with F10, and ask again. The difference is the cost of the call.

Tracepoints automate this. The bt command can print on entry and again at the return address:

bt clear_ind "clear_ind: start %u, end %%u" @clk -- @clk

The first @clk fills in the start value when the routine is entered. The %%u survives into a second, one-shot tracepoint at the return address, which fills it from the argument after --. Other useful commands for timing work:

  • gcr <n> runs exactly n machine cycles. gs <line> runs to a scan line, gf to the end of the frame, gv to the next VBI.
  • ? hpos and ? vpos give the beam position. .beam shows it too.
  • .dma shows ANTIC's DMA pattern for the current scan line. .dmamap shows the actual DMA pattern for every line of the frame, with DMA and free cycle counts per line. Enable ANTIC DMA analysis in the View menu first.
  • h shows the instruction history once history recording is enabled in the CPU options. h -c compresses loops.

6. A benchmark: three ways to clear 4 KB

This program times three versions of the same job, filling 4 KB with zeros 30 times, and prints each result in frames (hex) at the top of the screen. Build it with MADS and boot it with BASIC enabled so the OS screen exists.

; bench.asm - time three ways of clearing 4 KB
; build: mads bench.asm -o:bench.xex -l:bench.lst -t:bench.lab
RTCLOK  = $14
SAVMSC  = $58           ; screen memory address (OS)
SDMCTL  = $022F
ptr     = $CB           ; two free zero-page bytes
scr     = $CD
BUF     = $5000         ; 4 KB work buffer ($5000-$5FFF)
PASSES  = 30

        org $2000

start   lda SAVMSC
        sta scr
        lda SAVMSC+1
        sta scr+1

        jsr sync                ; test 1: indirect loop, screen on
        ldx #PASSES
t1      jsr clear_ind
        dex
        bne t1
        jsr elapsed
        ldy #0
        jsr show

        jsr sync                ; test 2: unrolled abs,Y, screen on
        ldx #PASSES
t2      jsr clear_abs
        dex
        bne t2
        jsr elapsed
        ldy #6
        jsr show

        lda SDMCTL              ; test 3: indirect loop, screen off
        pha
        lda #0
        sta SDMCTL
        jsr sync                ; DMA really stops at the next VBI
        jsr sync
        ldx #PASSES
t3      jsr clear_ind
        dex
        bne t3
        jsr elapsed
        tax
        pla
        sta SDMCTL
        txa
        ldy #12
        jsr show
done    jmp done

; ---- version 1: (zp),Y loop, 16 pages ----
clear_ind lda #<BUF
        sta ptr
        lda #>BUF
        sta ptr+1
        lda #0
        ldy #0
ci1     sta (ptr),y             ; 6 cycles
        iny                     ; 2
        bne ci1                 ; 3 when taken
        inc ptr+1
        ldy ptr+1
        cpy #>(BUF+$1000)
        ldy #0
        bcc ci1
        rts

; ---- version 2: unrolled abs,Y, one store per page ----
clear_abs lda #0
        tay
ca1
        .rept 16
        sta BUF+#*$100,y        ; 5 cycles each
        .endr
        iny                     ; 2
        bne ca1                 ; 3 when taken
        rts

; ---- timing helpers ----
sync    lda RTCLOK              ; wait for a new frame
s1      cmp RTCLOK
        beq s1
        lda RTCLOK
        sta start_t
        rts

elapsed lda RTCLOK              ; A = frames since sync
        sec
        sbc start_t
        rts

show    pha                     ; print A as 2 hex digits at column Y
        lsr
        lsr
        lsr
        lsr
        jsr hex
        pla
        and #$0F
hex     tax
        lda digits,x
        sta (scr),y
        iny
        rts

digits  dta d'0123456789ABCDEF'
start_t dta 0

        run start

Results from atari800 with the GRAPHICS 0 screen, 122,880 bytes cleared per test:

Test NTSC frames PAL frames
1. (zp),Y loop, screen on 74 ($4A) 57 ($39)
2. Unrolled abs,Y, screen on 35 ($23) 27 ($1B)
3. (zp),Y loop, DMA off 50 ($32) 42 ($2A)

The arithmetic explains them. The inner loop of version 1 costs 6 + 2 + 3 = 11 cycles per byte: STA (zp),Y always takes 6, and a taken branch 3. Version 2 does 16 stores of 5 cycles (an indexed store always pays the extra cycle) plus 5 for the loop, so 85 cycles per 16 bytes, about 5.3 per byte. That's twice as fast, exactly as measured. In the profiler's basic-block mode, ci1 would come out on top in test 1.

Test 3 shows the cost of the display. Turning off ANTIC's DMA made the same loop about a third faster, in line with the classic advice that POKE 559,0 saves up to 30%. Running a profile with and without the screen shows the same thing as the gap between Clocks and CPU clocks, and in the DMA% column.

7. Optimisations that pay on the Atari

Use zero page. A zero-page access is a byte shorter than an absolute one and a cycle faster (3 cycles for LDA zp against 4 for LDA abs). Keep hot variables and pointers there. Only a few locations are free when the OS and BASIC are in use, so plan them.

Unroll hot loops. Every pass of a loop pays for the index update and the branch. Version 2 above spends 5 of every 85 cycles on loop control instead of 5 of every 11. MADS's .rept with the # counter writes the copies for you.

Replace arithmetic with tables. Multiplying a row number by 40 takes several shifts and adds each time. A table built at assembly time costs two indexed loads. Star Raiders keeps exactly such row-address tables for its playfield.

ptr     = $CB
SCREEN  = $4000

; in: X = row (0..23), Y = column; out: A stored at that cell
plot    lda rowlo,x             ; 4 cycles (5 if the table crosses a page)
        sta ptr
        lda rowhi,x
        sta ptr+1
        lda #$0A                ; screen code for '*'
        sta (ptr),y
        rts

rowlo   :24 dta l(SCREEN+#*40)
rowhi   :24 dta h(SCREEN+#*40)

Avoid page crossings in hot code. An indexed read (abs,X, abs,Y, (zp),Y) that crosses into the next page costs one extra cycle, and so does a taken branch whose target is on a different page from the next instruction. Align tables and tight loops (MADS .align) and check with the page-crossing counter. Note that old instruction tables, including some in 1980s books, give 4 cycles for STA abs,X. The real figure is 5, because indexed stores always take the extra cycle.

Reverse branches the profiler shows are usually taken, so that the common path falls through for 2 cycles instead of 3.

Mind ANTIC. On a stock machine the CPU gets roughly 60–90% of the bus, depending on the display. Each scan line is 114 machine cycles, and ANTIC takes cycles for memory refresh (up to nine per line), display-list fetches, character and bitmap data, and player/missile graphics. To reduce the cost: use a narrow playfield (SDMCTL bits 0–1 = 1) or fewer and simpler mode lines, blank the screen during long calculations or loading, and put heavy work in vertical blank, where there is no playfield DMA.

Troubleshooting

  • The profile is empty. Profiling must be started and stopped. Results appear only when the session ends.
  • Labels are missing. Load the symbol file with the same base name as the program, or with .loadsym.
  • Interrupt time is lumped together. Instruction mode only separates code by the I flag. Use function or call-graph mode to see IRQ, VBI and DLI threads separately.
  • Frame triggers don't follow your code after a rebuild. Trigger expressions are evaluated when the session starts. Stop and start again.
  • Benchmarks differ between runs. Interrupts (VBI, key click, serial I/O) and DMA vary with what's on screen. Measure with the same display, and compare CPU clocks rather than Clocks when you want the code's own cost.

Further reading

  • Altirra's help file, "Profiling" and "Using the debugger", plus .help inside the debugger.
  • Altirra Hardware Reference Manual by Avery Lee: CPU timing, page-crossing rules, ANTIC DMA and refresh.
  • Assembly Language Programming for the Atari Computers by Mark Chasin (McGraw-Hill, 1984): addressing modes and cycle counts.
  • De Re Atari (Atari, Inc., 1982): ANTIC DMA and the program-speed tips.
  • Mapping the Atari by Ian Chadwick (COMPUTE! Books): SDMCTL (559) and playfield widths.

Credits