Page Flipping: Instant Screens and Full-Screen Animation

A pink square ball above a green floor line on a dark blue screen

Drawing a detailed picture with PLOT and DRAWTO takes seconds in BASIC, and the viewer watches every line appear. Page flipping hides that. You draw one or more complete screens in memory the viewer can't see, then make ANTIC display one of them — instantly — by changing two bytes in the display list. Flip through a series of slightly different pictures quickly enough and you have full-screen animation, like frames of film. David Plotkin, who wrote the classic Antic tutorial on the subject, demonstrated it with his game "Road Race", and pointed to the earlier Antic game "Maze Maniac", which used it to make a whole new maze appear the moment the player stepped off the edge of the screen.

What you'll learn: the two pointers that make page flipping work, how to reserve and prepare memory, a complete BASIC animation, how to get steady timing with an interrupt, and how double buffering works in assembly.

What you need: Atari BASIC on any 8-bit Atari or emulator; the assembly section uses MADS syntax.

Two pointers: the one you see and the one you draw on

Plotkin's explanation rests on two separate addresses:

  • Display memory — the LMS address in the display list, at DLIST+4 (low byte) and DLIST+5 (high byte), where DLIST = PEEK(560)+256*PEEK(561). This is what ANTIC shows.
  • Write memory — SAVMSC at 88/89, the address where the OS thinks the screen starts. PRINT, PLOT, DRAWTO and friends draw relative to it.

Normally they're equal, so what you draw is what you see. Page flipping simply separates them:

  1. POKE 88/89 with the address of a hidden, protected area.
  2. Draw as usual — nothing visible happens.
  3. When ready, POKE that address into DLIST+4/DLIST+5 and the picture appears.

Seen from ANTIC's side, the idea is the same: changing only the LMS operand during vertical blank swaps between pre-drawn displays, so the program touches just two address bytes to show thousands of bytes of RAM.

Planning memory

Each frame needs a full screen's worth of bytes. Plotkin's COMPUTE! article lists them:

Mode Bytes per screen Mode Bytes per screen
GRAPHICS 0 960 GRAPHICS 5 960
GRAPHICS 1 480 GRAPHICS 6 1,920
GRAPHICS 2 240 GRAPHICS 7 3,840
GRAPHICS 3 240 GRAPHICS 8 7,680
GRAPHICS 4 480

Rules of thumb from the sources:

  • Start every frame on a page boundary (low byte 0). Then flipping means changing only DLIST+5, and the arithmetic stays simple. Round each frame up to whole pages: 1 page for GRAPHICS 2 or 3, 4 pages for GRAPHICS 0.
  • Don't cross a 4K boundary inside a frame. ANTIC's memory scan counter can't, without an extra LMS (see Display Lists Explained). Page-aligned frames of up to 1K never cross one if they start at a multiple of 4 pages.
  • Reserve the memory by lowering RAMTOP (location 106) before a GRAPHICS statement, so the OS builds its own screen below your frames.
  • Clear the frames yourself. The OS only clears the memory of the screen it opened. Spare pages contain leftover garbage, which shows up when you flip to them.
  • Memory-hungry modes don't suit it. Plotkin calls GRAPHICS 7 (3.5K per frame) and GRAPHICS 8 (8K) too expensive to be practical, and recommends character modes with redefined characters for detailed frames at low cost.

A complete BASIC example: a bouncing ball

Eight GRAPHICS 3 frames of a bouncing ball, one page each:

10 REM PAGE FLIPPING - BOUNCING BALL
20 RT=INT((PEEK(106)-8)/8)*8:POKE 106,RT:GRAPHICS 3+16
30 SETCOLOR 0,4,10:SETCOLOR 1,12,8:SETCOLOR 4,9,2
40 DL=PEEK(560)+256*PEEK(561)
50 POKE 559,0
60 FOR I=RT*256 TO RT*256+2047:POKE I,0:NEXT I
70 FOR F=0 TO 7
80 POKE 88,0:POKE 89,RT+F
90 COLOR 2:PLOT 0,23:DRAWTO 39,23
100 Y=20-INT(18*SIN(F*3.14159/8))
110 COLOR 1:FOR J=0 TO 1:PLOT 18,Y+J:DRAWTO 21,Y+J:NEXT J
120 NEXT F
130 POKE 88,PEEK(DL+4):POKE 89,PEEK(DL+5)
140 POKE 559,34:POKE DL+4,0
150 FOR F=0 TO 7:POKE DL+5,RT+F
160 FOR W=1 TO 15:NEXT W
170 NEXT F:GOTO 150

How it works

  • Line 20 reserves 8 pages (2K) above a new, lower RAMTOP, rounded to a multiple of 8 pages, then opens a full-screen GRAPHICS 3 (40 × 24, four colours, 240 bytes per screen).
  • Line 50 turns off ANTIC DMA so the preparation runs faster (Kirtley's "Screen Flipping" article suggests the same trick to hide drawing and speed it up); line 60 clears all eight pages.
  • Lines 70–120 point SAVMSC at each page in turn and draw a frame: the ground in COLOR 2 and a 4 × 2 ball whose height follows a sine curve. Frame 0 has the ball on the ground and frame 4 at the top, so the sequence loops smoothly back to frame 0.
  • Line 130 points the OS back at the real screen, so any later PRINT or PLOT doesn't scribble on a frame. Plotkin warns about this: leave SAVMSC on a frame and later output appears only when that frame is shown.
  • Lines 140–170 turn the display back on, set the LMS low byte to 0 and cycle the high byte through the eight frames.

Steady timing with an interrupt

From BASIC, Plotkin notes, the flipping rate wobbles whenever the program does other work, because the time between POKEs varies. His fix in Antic was to move the flipping into an interrupt: System Timer 2, whose countdown value lives at 538/539 (CDTMV2, $21A) and whose vector is at 552/553 (CDTMA2, $228). The OS counts it down once per frame and calls the routine when it reaches zero; the routine flips the page and reloads the countdown, so speed is controlled by a single POKE from BASIC, independent of what BASIC is doing.

The same thing as a deferred vertical blank routine in assembly, assuming eight page-aligned frames starting at frames and a display list whose LMS high byte is labelled lmshi:

XITVBV  = $E462

vbi     dec delay               ; wait 'speed' frames between flips
        bne done
        lda speed
        sta delay
        ldx frame
        inx
        cpx #8                  ; eight frames, then wrap
        bcc ok
        ldx #0
ok      stx frame
        txa
        clc
        adc #>frames            ; frame n lives at page (frames/256)+n
        sta lmshi               ; high byte of the display list's LMS operand
done    jmp XITVBV

delay   dta 1
speed   dta 4
frame   dta 0

Install it with SETVBV ($E45C, A = 7, Y/X = low/high address of vbi). Because the change is made during vertical blank, the new frame always begins at the top of the screen — no tearing.

Double buffering

Games with lots of moving software sprites often use just two pages: the display shows buffer A while the program draws the next frame into buffer B; at the next vertical blank they swap roles. The mechanics are identical — point your drawing routines at the hidden buffer, and have the VBI (or a "frame ready" flag the VBI checks) rewrite the LMS address. A cheaper variant: if only part of the screen changes, give the changing rows their own LMS in a custom display list and flip just those, keeping the static part in one shared copy.

More tricks with flipping

  • Player/missile graphics on top. P/M objects ignore the LMS address, so Plotkin recommends page flipping for scrolling or animated backgrounds with players for anything the user controls.
  • Flipping faster than the eye. Cycling two to four images on successive frames makes them appear superimposed, or lets different luminance versions of a picture blend into more shades. The cost: memory, flicker, and washed-out images — best kept to two or three frames on a dark background.
  • Character-set flipping. Switching CHBAS (756) between several fonts animates a whole character-based screen for the price of one POKE per frame (see Redefined Character Sets).
  • 130XE banks. The 130XE can let ANTIC (PORTB bit 5) see an extended memory bank separately from the CPU (bit 4) in the $4000–$7FFF window. Many third-party RAM upgrades don't support separate ANTIC access, so test on the hardware you target.

Troubleshooting

  • Garbage in some frames: the spare pages weren't cleared, or a frame overlaps the OS screen. Lower RAMTOP before GRAPHICS and clear every page you use.
  • Picture splits or wraps partway down: a frame crosses a 4K boundary. Align frames so that can't happen.
  • PRINT output appears only on one frame: SAVMSC (88/89) is still pointing at a hidden frame.
  • Animation speed varies: do the flipping in a VBI or system timer routine instead of a BASIC loop.
  • Everything resets on RESET or a new GRAPHICS statement: the display list is rebuilt; re-apply your LMS changes.

Further reading

  • David Plotkin, "Page Flipping: A racy tutorial", Antic Vol. 2 No. 10 (January 1984), with the "Road Race" game and a System Timer 2 routine.
  • David N. Plotkin, "Animation by Page Flipping", and Michael Kirtley, "Screen Flipping", in COMPUTE!'s Second Book of Atari Graphics (COMPUTE! Books, 1984).
  • De Re Atari (Atari, 1982), Chapter 2, "Applications of display lists".
  • Advanced Programming Techniques for the Atari by Linda M. Schreiber (TAB Books, 1983), which covers screen flipping among other graphics techniques.

Credits