ANTIC DMA and Cycle Timing: Where the CPU's Time Goes
The Atari's 6502 runs at 1.79 MHz, but it never gets all of those cycles. ANTIC shares the memory bus with it and halts the CPU whenever it needs to read the display list, screen memory, character shapes or player graphics, and whenever the RAM needs refreshing. How much time is left depends on the graphics mode, the playfield width and whether players are on. That decides how much a game can do per frame, how long a DLI can run, and why BASIC programs speed up when the screen is turned off. This guide explains where the cycles go, line by line, and then measures it with a program you can run yourself.
What you'll learn: the frame and scan-line budget on NTSC and PAL, each kind of ANTIC DMA and its cost, why character modes cost more than bitmap modes, a measured comparison of seven display settings, and how WSYNC and VCOUNT fit into the scan-line timing.
What you need: MADS and an emulator for the measuring program, and the basics from Display Lists Explained: How ANTIC Builds the Atari Screen. The program was assembled with MADS and run in atari800 (NTSC, genuine XL/XE OS Rev. 2 ROM).
1. The budget
Everything is counted in machine cycles: one cycle is one tick of the 6502's clock.
| NTSC | PAL | |
|---|---|---|
| Machine clock | 1.789773 MHz | 1.773447 MHz |
| Cycles per scan line | 114 | 114 |
| Scan lines per frame | 262 | 312 |
| Cycles per frame | 29,868 | 35,568 |
| Frame rate | 59.92 Hz | 49.86 Hz |
A scan line is always 114 cycles. The machine clock and the line length are the same on both systems, so PAL machines get 50 more lines, 5,700 extra cycles per frame, at a lower frame rate. The visible picture occupies scan lines 8 to 247 at most; the rest of each frame is vertical blank.
2. Who takes the cycles
ANTIC uses the bus for five kinds of work:
- Memory refresh: 9 cycles on every scan line, visible or not, starting at cycle 25 and then every 4 cycles. That is 2,358 cycles per NTSC frame even with the screen off.
- Display list fetch: 1 cycle per instruction byte, on the first scan line of each mode line, plus 2 more for the address of an LMS or jump instruction.
- Playfield, bitmap modes: 1 cycle per byte of screen data, only on the first scan line of each mode line. ANTIC keeps the line in a 48-byte internal buffer and replays it for the remaining scan lines. A normal-width line fetches 10 bytes (GRAPHICS 3 and 4), 20 bytes (GRAPHICS 5 and 6) or 40 bytes (GRAPHICS 7, 8 and 15).
- Playfield, character modes: the character names (screen bytes) are fetched on the first scan line and buffered, but the character shapes are fetched on every scan line. In 40-column modes that is 40 cycles on each of the 8 lines of a text row, plus 40 more on the first line for the names. Even the double-height modes (ANTIC 5 and 7) fetch shape data on every line.
- Player/missile graphics: 1 cycle for the missiles and 4 for the players, on every visible scan line when enabled: 5 cycles per line, or 1,200 per frame. Single-line and double-line resolution cost the same; the difference is only in addressing.
Playfield width multiplies the playfield costs: narrow fetches 32 bytes per 40-column line, normal 40 and wide 48. Horizontal scrolling silently switches ANTIC to the next wider fetch window.
Per scan line, that leaves roughly:
| Scan line | Cycles left for the CPU |
|---|---|
| Vertical blank or blank line | 105 |
| GRAPHICS 8 line (normal width) | about 64 |
| Second to eighth line of a GRAPHICS 0 row | about 65 |
| First line of a GRAPHICS 0 row | about 30 (names and shapes compete; some refresh cycles are dropped) |
| Any visible line with player/missile DMA | 5 fewer |
3. Measuring it
Rather than trust the arithmetic, the program below measures it. With all interrupts off, it switches ANTIC between seven settings. For each one, it counts how many passes of a 17-cycle loop the CPU completes between two visits to the same scan line. More DMA means fewer passes. At the end, it restores the OS screen and prints the counts.
; Measure how much CPU time ANTIC leaves per frame under different
; DMA settings. Interrupts off; one frame is timed with VCOUNT.
SAVMSC = $58
SDMCTL = $022F
SDLSTL = $0230
DMACTL = $D400
DLISTL = $D402
PMBASE = $D407
NMIEN = $D40E
VCOUNT = $D40B
CNT = $CB ; 16-bit loop counter
PTR = $CD
SCR = $4000 ; dummy screen data (contents don't matter)
org $3000
start sei
lda #0
sta NMIEN
ldx #0
test lda VCOUNT ; switch only during vertical blank
cmp #$7C
bcc test
lda dmatab,x ; configure: DMACTL value
sta DMACTL
lda dltab,x ; display list for this test (low byte)
sta DLISTL
lda dlhi,x
sta DLISTL+1
lda #>[SCR+$1000] ; P/M data base (only used with PM DMA)
sta PMBASE
jsr frame ; let one frame settle
jsr frame ; then time one
lda CNT
sta reslo,x
lda CNT+1
sta reshi,x
inx
cpx #NTEST
bne test
lda SDLSTL ; restore the OS screen and interrupts
sta DLISTL
lda SDLSTL+1
sta DLISTL+1
lda SDMCTL
sta DMACTL
lda #$40
sta NMIEN
cli
jsr report
jmp *
; count 17-cycle loop passes from one VCOUNT=$10 to the next
frame lda #0
sta CNT
sta CNT+1
f1 lda VCOUNT ; wait for line 32...
cmp #$10
bne f1
f2 lda VCOUNT ; ...and for it to pass
cmp #$10
beq f2
count inc CNT ; 5 cycles
bne nohi ; 3 (taken)
inc CNT+1
nohi lda VCOUNT ; 4
cmp #$10 ; 2
bne count ; 3 -> 17 cycles per pass
rts
report ldx #0
r1 lda SAVMSC ; row = x+1
clc
adc rowlo,x
sta PTR
lda SAVMSC+1
adc rowhi,x
sta PTR+1
txa ; label
asl
asl
asl
asl
tay
sty lbl
ldy #0
r2 sty tmpy
ldy lbl
lda labels,y
inc lbl
ldy tmpy
sta (PTR),y
iny
cpy #16
bne r2
lda reslo,x ; count in decimal at column 18
sta num
lda reshi,x
sta num+1
ldy #18
jsr dec5
inx
cpx #NTEST
bne r1
rts
dec5 stx savx ; print 16-bit num as 5 digits at (PTR),y
ldx #0
d1 lda #$10 ; internal code for '0'
sta digit
d2 lda num
sec
sbc p10lo,x
sta tmp
lda num+1
sbc p10hi,x
bcc d3
sta num+1
lda tmp
sta num
inc digit
bne d2
d3 lda digit
sta (PTR),y
iny
inx
cpx #5
bne d1
ldx savx
rts
p10lo dta <10000,<1000,<100,<10,<1
p10hi dta >10000,>1000,>100,>10,>1
NTEST = 7
dmatab dta $00,$21,$22,$23,$22,$22,$3E
dltab dta <dl2,<dl2,<dl2,<dl2,<dlf,<dld,<dl2
dlhi dta >dl2,>dl2,>dl2,>dl2,>dlf,>dld,>dl2
rowlo dta <40,<80,<120,<160,<200,<240,<280
rowhi dta >40,>80,>120,>160,>200,>240,>280
labels dta d'DMA OFF '
dta d'GR.0 NARROW '
dta d'GR.0 NORMAL '
dta d'GR.0 WIDE '
dta d'GR.8 (MODE F) '
dta d'GR.7 (MODE D) '
dta d'GR.0 + PM DMA '
reslo .ds NTEST
reshi .ds NTEST
num dta a(0)
digit dta 0
tmp dta 0
tmpy dta 0
lbl dta 0
savx dta 0
.align $400
dl2 dta $70,$70,$70,$42,a(SCR)
:23 dta $02
dta $41,a(dl2)
dlf dta $70,$70,$70,$4F,a(SCR)
:101 dta $0F
dta $4F,a(SCR+$1000)
:89 dta $0F
dta $41,a(dlf)
dld dta $70,$70,$70,$4D,a(SCR)
:95 dta $0D
dta $41,a(dld)
run start
Points about the method:
- Interrupts are off (SEI and NMIEN = 0), so neither the VBI nor the keyboard steals time, and the OS can't copy its shadow registers over our DMACTL.
- The display lists are the standard shapes of GRAPHICS 0, 8 and 7, written directly into ANTIC's DLISTL. They are aligned to a 1K boundary, because a display list must not cross one. Screen memory is never filled, since DMA costs the same whatever the bytes contain.
- Switch during vertical blank. Our first version changed DLISTL in the middle of the screen. Several settings then reported no playfield cost at all, as if ANTIC had stopped fetching. Waiting for VCOUNT ≥ $7C (vertical blank) before each switch fixed it. Changing the display list pointer outside vertical blank is a classic source of trouble anyway, which is why the OS copies its shadow SDLSTL only during the VBI.
- One loop pass is 17 cycles, so multiplying a count by 17 gives the CPU cycles available in that frame, to within a pass or two.
4. The results
Converted to cycles and compared with the 29,868 cycles of an NTSC frame:
| Setting | Loop passes | CPU cycles per frame | Share of the frame | Cost compared with DMA off |
|---|---|---|---|---|
| DMA off (DMACTL = 0) | 1,605 | 27,285 | 91.4% | — |
| GRAPHICS 0, narrow | 1,212 | 20,604 | 69.0% | 6,681 |
| GRAPHICS 0, normal | 1,113 | 18,921 | 63.3% | 8,364 |
| GRAPHICS 0, wide | 1,024 | 17,408 | 58.3% | 9,877 |
| GRAPHICS 8 (ANTIC F) | 1,147 | 19,499 | 65.3% | 7,786 |
| GRAPHICS 7 (ANTIC D) | 1,375 | 23,375 | 78.3% | 3,910 |
| GRAPHICS 0 + players and missiles | 1,043 | 17,731 | 59.4% | 9,554 |
What the numbers say:
- With DMA off, about 91% of the frame is free. The missing 9% is almost exactly the refresh cost (2,358 cycles), plus the loop's own overhead.
- The bitmap modes match the arithmetic closely. GRAPHICS 8 should cost 192 × 40 = 7,680 cycles for screen data plus about 200 for the display list. We measured 7,786. GRAPHICS 7 fetches its 40 bytes only on every other line: about 3,940 expected, 3,910 measured.
- Text is the most expensive standard mode. GRAPHICS 0 costs more than GRAPHICS 8 even though it shows far fewer pixels, because the character shapes are fetched again on every scan line. The measured cost is a little below the simple sum of 8,640 for normal width. The reason: on the first line of each text row, the bus is so busy that most of the refresh cycles are dropped.
- Players and missiles added 70 passes × 17 = 1,190 cycles, against the predicted 1,200.
- Width matters. Going from narrow to wide costs about 3,200 cycles a frame in text mode, roughly 15% of what remains.
This is where the classic advice "POKE 559,0 to speed up a long calculation" comes from. The usual estimate of the gain is 30%. In our measurement, a normal GRAPHICS 0 screen used 31% of the time that would otherwise be free. Games make the same trade on a smaller scale: a narrow playfield, a bitmap mode instead of text, or a few blank lines at the top of the screen all hand cycles back to the CPU.
5. Timing within the scan line
Numbering the 114 cycles of a line from 0 to 113, the landmarks a programmer meets most often:
- Cycle 0: the missile fetch. Players follow at cycles 2–5, and the display list byte is read at cycle 1.
- Cycle 8: a pending DLI or VBI is flagged in NMIST. The CPU starts its interrupt sequence once the current instruction finishes. The OS NMI handler then needs a few more instructions before your DLI code runs.
- Playfield fetches start between cycles 10 and 28, depending on width and mode, and stop by cycle 105 at the latest.
- WSYNC: a write to $D40A at cycle 103 or earlier halts the CPU until cycle 105, the start of horizontal blank. The instruction after the
STA WSYNCtherefore runs at the very start of the next line's horizontal blank, which is the safe moment to change colours. A write at cycle 104 or later waits for the following line. Wide playfields, horizontal scrolling and delayed refresh can push the restart one or two cycles later. - VCOUNT ($D40B) holds the scan line number divided by 2, so it changes every two lines. It counts up to $82 on NTSC and $9B on PAL, and it changes at cycle 111 of the line before.
These costs add up quickly. On the first line of a text row, a minimal DLI that saves two registers, loads two colours, waits on WSYNC and stores them uses a whole scan line of time, mostly waiting for DMA. That is why per-line effects are best done by one DLI that runs a kernel loop, as Advanced DLI Techniques: Kernels, Dispatch Tables and Cycle Budgets shows.
6. Practical tips
- Need speed for a calculation? Turn the playfield off (SDMCTL = 0, or POKE 559,0 from BASIC) and restore the old value afterwards.
- Need speed in a game? Prefer a narrow playfield, a bitmap mode instead of a character mode when you don't need text, fewer visible lines, and player/missile DMA only while sprites are in use.
- Timing-critical code (sample playback, serial bit-banging) should run in vertical blank or with the screen off, where every line gives the CPU 105 cycles.
- Use VCOUNT with care: compare with ≥ rather than =, because a long interrupt can make you miss the exact value. The manual notes that this is a common cause of programs written for PAL locking up on NTSC.
- PAL and NTSC: a loop that just fits in one NTSC frame has about 19% more cycles available on PAL, though PAL's frame rate is lower.
Troubleshooting
- A DLI or kernel runs out of time on text lines: the first line of each text row leaves only about 30 cycles. Put the DLI one line earlier, or prepare values before the WSYNC.
- Program runs slower than expected: check SDMCTL. A wide playfield or enabled player/missile DMA costs cycles even when nothing is drawn.
- Timing results that make no sense: interrupts are enabled (the VBI and the keyboard click take time), or the display list was switched in the middle of the screen.
- A WSYNC loop drifts by a line: a write after cycle 103 waits for the next line. Keep the work between WSYNCs short.
Further reading
- Avery Lee, Altirra Hardware Reference Manual, chapter 4: display timing, WSYNC, VCOUNT, playfield and P/M DMA, the scan-line timing charts and the cycle-counting example.
- Ian Chadwick, Mapping the Atari: SDMCTL (559) and DMACTL (54272), WSYNC (54282), VCOUNT (54283).
- Chris Crawford and others, De Re Atari, chapter 2 (ANTIC and the display list).
- On this site: Display Lists Explained: How ANTIC Builds the Atari Screen, Display List Interrupts (DLIs): More Colours, Mid-Screen Changes, Advanced DLI Techniques: Kernels, Dispatch Tables and Cycle Budgets, POKEY Timer Interrupts in Depth.
Credits
- Picture: The measuring program's results on NTSC: each number is how many 17-cycle loop passes the CPU completed in one frame under that display setting. — Program authors; screenshot by the site
- Altirra Hardware Reference Manual — 4.2 Display timing, 4.3 Playfield widths, 4.9 WSYNC, 4.10 VCOUNT, 4.11 Playfield DMA (fetch rates, line buffer), 4.13 P/M DMA, 4.14 Scan line timing (refresh, DL DMA, fetch windows), 4.15 Cycle counting example — Avery Lee (Altirra)
- Mapping the Atari — 559 SDMCTL (POKE 0 speeds processing by 30%), DMACTL bits — AtariArchives.org
