Character Set Animation: Conveyor Belts, Water, Soft Sprites and Font Flipping

Text screen titled Character Set Animation showing a diagonal-striped conveyor belt, a block of falling water texture and a small round ball sprite

In a character mode the screen holds only character numbers. The shapes live in the font, and every copy of a character on screen shares one 8-byte definition. Change those 8 bytes and every copy changes at once, with no screen redrawing at all. Atari's Space Invaders cartridge was built this way: its invaders are characters with four animation frames each. The same idea gives you rolling conveyor belts, rippling water, flickering fire and, with some bit shifting, smoothly moving objects in a text mode.

Redefined Character Sets shows how to copy the ROM font to RAM and redefine characters. This guide assumes you can do that and concentrates on animation: what to change, how to change it cheaply, and when.

What you'll learn: frame swapping, rotating and scrolling character bitmaps, how to move an object pixel by pixel by shifting it across two characters, flipping complete fonts with CHBAS from a vertical blank interrupt, and how to avoid tearing.

What you need: Atari BASIC for the first example, MADS for the others, and a basic grasp of Vertical Blank Interrupts.

1. Four ways to animate with a font

  1. Frame swapping. Store several versions of a shape somewhere in memory and copy the next one over the displayed character's 8 bytes. Every alien, flag or torch that uses that character moves in step.
  2. In-place transforms. Instead of storing frames, compute the next one from the current bitmap: rotate each row sideways for a conveyor or scrolling pattern, or rotate the rows downwards for falling water. A repeating pattern rotated inside its own cell joins seamlessly with its neighbours.
  3. Soft sprites. Draw an object into a small group of characters at a pixel offset, so it can move one pixel at a time while the rest of the screen stays a character display.
  4. Font flipping. Keep several complete fonts and change CHBAS (756, $2F4) to switch between them. One store re-animates the entire screen.

Font switching falls into three speeds. "Human-slow" switching (more than a second) changes scenery. "Human-fast" switching (1/60 to 1 second) animates. "Machine-fast" switching (within a frame) puts several fonts on one screen, which is covered in Character Set Switching with DLIs.

2. Rotating and scrolling from BASIC

This program copies the ROM font into RAM (slowly; the 34-byte USR routine in Redefined Character Sets does it instantly), turns internal character 64 into a striped belt and character 65 into a water texture, and then animates both forever:

10 REM CHARACTER ANIMATION IN BASIC
20 A=PEEK(106)-8:POKE 106,A:GRAPHICS 0
30 CH=A+4:CS=CH*256:POKE 752,1
40 ? "COPYING FONT..."
50 FOR I=0 TO 1023:POKE CS+I,PEEK(57344+I):NEXT I
60 C=CS+64*8:W=CS+65*8
70 FOR I=0 TO 7:READ D:POKE C+I,D:NEXT I
80 FOR I=0 TO 7:READ D:POKE W+I,D:NEXT I
90 POKE 756,CH:? CHR$(125)
100 POSITION 2,3:? "CONVEYOR:"
110 FOR I=1 TO 30:? CHR$(27);CHR$(0);:NEXT I
120 POSITION 2,8:? "WATERFALL:"
130 FOR Y=10 TO 16:POSITION 18,Y
140 ? CHR$(27);CHR$(1);CHR$(27);CHR$(1):NEXT Y
150 FOR I=0 TO 7:B=PEEK(C+I)
160 B=INT(B/2)+128*(B-2*INT(B/2))
170 POKE C+I,B:NEXT I
180 T=PEEK(W+7)
190 FOR I=7 TO 1 STEP -1:POKE W+I,PEEK(W+I-1):NEXT I
200 POKE W,T:GOTO 150
210 DATA 255,136,204,102,51,17,136,255
220 DATA 68,17,136,34,68,17,136,34
  • Lines 110 and 140 print the characters. Internal 64 and 65 are the control-graphics characters CTRL-, and CTRL-A, so the program prints CHR$(27) (ESC) in front of CHR$(0) and CHR$(1) to display them instead of executing them.
  • Lines 150–170 rotate each row one bit to the right. BASIC has no rotate instruction, so line 160 does it arithmetically: shift right by halving, then put the bit that fell off (B−2·INT(B/2)) back in as 128. The diagonal stripes appear to travel to the right along the whole 30-character belt.
  • Lines 180–200 move every row down one place and wrap the bottom row to the top. The column of water characters looks like a waterfall.

BASIC manages a few steps per second, which is fine for slow effects. For smooth effects that run every frame, use machine code in a VBI.

3. Everything every frame: a VBI version

The MADS program below does the same two effects from a deferred VBI, adding a "soft sprite": a ball that glides across the screen one pixel per frame using just two characters.

; character-set animation - mads charanim.asm -o:charanim.xex
SDMCTL = $022F
SDLSTL = $0230
COLOR1 = $02C5
COLOR2 = $02C6
CHBAS  = $02F4
SETVBV = $E45C
XITVBV = $E462
FONT   = $3000              ; RAM copy of the ROM font (1K aligned)
CONV   = FONT+$40*8         ; internal $40: conveyor belt
WATER  = FONT+$41*8         ; internal $41: falling water
BALL_L = FONT+$42*8         ; internal $42/$43: soft-sprite ball
BALL_R = FONT+$43*8
BALLROW = screen+18*40
src    = $80
dst    = $82
tick   = $84
ballx  = $85                ; 0..311, pixel position (16-bit)
cell   = $87

        org $2000
main    ldy #0                  ; copy 1K ROM font to RAM
        sty src
        sty dst
        lda #$E0
        sta src+1
        lda #>FONT
        sta dst+1
        ldx #4
copy    lda (src),y
        sta (dst),y
        iny
        bne copy
        inc src+1
        inc dst+1
        dex
        bne copy
        ldx #7                  ; define conveyor and water
def     lda conv_d,x
        sta CONV,x
        lda water_d,x
        sta WATER,x
        dex
        bpl def
        lda #0
        sta ballx
        sta ballx+1
        sta tick
        lda #>FONT
        sta CHBAS
        lda #$0E
        sta COLOR1              ; bright text
        lda #$90
        sta COLOR2              ; dark blue background
        lda #<dlist
        sta SDLSTL
        lda #>dlist
        sta SDLSTL+1
        lda #7                  ; deferred VBI
        ldy #<vbi
        ldx #>vbi
        jsr SETVBV
        jmp *

vbi     inc tick
        lda tick
        and #3                  ; every 4th frame: move the belt
        bne nobelt
        ldx #7
belt    lda CONV,x              ; rotate each row right one bit
        lsr @
        bcc @+
        ora #$80
@       sta CONV,x
        dex
        bpl belt
nobelt  lda tick
        and #1                  ; every 2nd frame: water falls one row
        bne nowater
        ldy WATER+7
        ldx #6
fall    lda WATER,x
        sta WATER+1,x
        dex
        bpl fall
        sty WATER
nowater jsr moveball
        jmp XITVBV

; soft sprite: draw the 8x8 ball at pixel ballx using chars $42/$43
moveball ldy cell              ; erase the two cells used last frame
        lda #0
        sta BALLROW,y
        sta BALLROW+1,y
        inc ballx               ; one pixel per frame
        bne @+
        inc ballx+1
@       lda ballx+1             ; wrap at 312 (39 cells)
        beq @+
        lda ballx
        cmp #<312
        bcc @+
        lda #0
        sta ballx
        sta ballx+1
@       lda ballx+1             ; cell = ballx / 8
        lsr @
        lda ballx
        ror @
        lsr @
        lsr @
        sta cell
        ldx #7                  ; build both characters, row by row
row     lda ballx
        and #7
        tay                     ; Y = shift count 0..7
        lda ball_d,x
        sta BALL_L,x
        lda #0
        sta BALL_R,x
        cpy #0
        beq done
shift   lsr BALL_L,x            ; 16-bit shift right through both chars
        ror BALL_R,x
        dey
        bne shift
done    dex
        bpl row
        ldy cell
        lda #$42
        sta BALLROW,y
        lda #$43
        sta BALLROW+1,y
        rts

conv_d  dta $FF,$88,$CC,$66,$33,$11,$88,$FF
water_d dta $44,$11,$88,$22,$44,$11,$88,$22
ball_d  dta $3C,$7E,$FF,$FF,$FF,$FF,$7E,$3C

dlist   dta $70,$70,$70,$42,a(screen)
        :23 dta $02
        dta $41,a(dlist)

screen  dta d'                                        '
        dta d'   CHARACTER SET ANIMATION              '
        dta d'                                        '
        dta d'   CONVEYOR: ROTATE EACH ROW SIDEWAYS   '
        dta d'                                        '
        dta d'   '
        :34 dta $40             ; conveyor characters
        dta d'   '
        dta d'                                        '
        dta d'   WATER: ROTATE THE ROWS DOWNWARD      '
        dta d'                                        '
        .rept 5
        dta d'                 '
        :6 dta $41              ; water characters
        dta d'                 '
        .endr
        dta d'                                        '
        dta d'   SOFT SPRITE: SHIFT BITS BETWEEN TWO  '
        dta d'   CHARACTERS                           '
        dta d'                                        '
        :8 dta d'                                        '
        run main

The conveyor uses LSR and puts the lost bit back with ORA #$80, a one-bit rotate right. It runs every fourth frame (tick AND 3) to set the speed.

The waterfall saves row 7, copies rows 6…0 down one place and stores the saved row as row 0. It runs every second frame.

The ball is the interesting one. Its position ballx is in pixels, 0–311. The character cell is ballx / 8 and the shift is ballx AND 7. For each of the 8 rows, the routine loads the ball's row into the left character, clears the right one and shifts the pair right through the carry (LSR BALL_L,X / ROR BALL_R,X) as many times as the shift count. The result is the ball straddling two characters at any pixel offset. The routine then erases the two cells it used last frame and writes characters $42 and $43 at the new cell.

Some practical refinements:

  • Pre-shift instead of shifting. Shifting at run time costs up to 7 shift pairs per row. Games often store all 8 shifted versions (4 in mode 4, where a pixel is two bits) and simply copy one, trading memory for speed.
  • Merge with the background. This ball moves over blank space. Over scenery, build each sprite character as background character AND mask, OR sprite, then put the sprite characters on screen and restore the original characters when it moves on. Every sprite then needs its own private set of characters: an 8 × 8 object needs 2 × 2 cells (4 characters) to move freely in both directions.
  • Mode 4 needs two-bit steps. In ANTIC 4 and 5 each pixel is a bit pair, so rotate or shift by two bits per pixel. A one-bit shift would move the colour bits into the wrong pairs and scramble the colours.

4. Fire and other in-place tricks

Once you think of a character as eight bytes you can change, effects get cheap:

  • Fire or static: every few frames, AND each row of a flame shape with a random byte from POKEY's RANDOM register (53770, $D20A) to make it flicker.
  • Blinking lights: swap one byte between two values.
  • Growing bars: keep the bar characters fixed and change which character is on screen. Atari's ENERGY CZAR used a bar-chart font with one character for each bar width from one to eight pixels, giving single-pixel resolution with character graphics.
  • Swap the screen byte instead of the font for objects that animate independently. A classic example hatches eggs this way: each egg steps through a string of character pairs printed in the same place, and different eggs can be at different stages.

5. Flipping whole fonts from a VBI

The cheapest animation of all is to point ANTIC at a different font. This program builds four copies of the ROM font, gives internal character 64 a different frame in each (|, /, –, \), fills the screen with that character and lets a deferred VBI change CHBAS every 8 frames:

; font flipping from a VBI - mads flip.asm -o:flip.xex
SDLSTL = $0230
CHBAS  = $02F4
SETVBV = $E45C
XITVBV = $E462
src    = $80
dst    = $82
timer  = $84
frame  = $85
ysave  = $86

        org $2000
main    ldx #0                  ; for each of the four RAM fonts...
nextf   lda #0
        sta src
        sta dst
        lda #$E0
        sta src+1
        lda pages,x
        sta dst+1
        ldy #0
        lda #4
        sta timer               ; (borrowed as a page counter)
copy    lda (src),y             ; ...copy the ROM font
        sta (dst),y
        iny
        bne copy
        inc src+1
        inc dst+1
        dec timer
        bne copy
        lda pages,x             ; ...then give internal char $40 frame X
        clc
        adc #2                  ; $40*8 = $200 bytes into the font
        sta dst+1
        txa
        asl @
        asl @
        asl @
        tay                     ; Y = X*8 = offset of this frame's data
        lda #8
        sta timer
def     lda frames,y
        sty ysave
        ldy #0
        sta (dst),y
        inc dst
        ldy ysave
        iny
        dec timer
        bne def
        inx
        cpx #4
        bne nextf
        lda #0
        sta frame
        lda #8
        sta timer
        lda #<dlist
        sta SDLSTL
        lda #>dlist
        sta SDLSTL+1
        lda #7
        ldy #<vbi
        ldx #>vbi
        jsr SETVBV
        jmp *

vbi     dec timer               ; every 8 frames...
        bne exit
        lda #8
        sta timer
        lda frame               ; ...select the next font
        clc
        adc #1
        and #3
        sta frame
        tax
        lda pages,x
        sta CHBAS               ; OS copies it to CHBASE ($D409)
exit    jmp XITVBV

pages   dta $30,$34,$38,$3C     ; fonts at $3000, $3400, $3800, $3C00
frames  dta $18,$18,$18,$18,$18,$18,$18,$18     ; |
        dta $03,$06,$0C,$18,$18,$30,$60,$C0     ; /
        dta $00,$00,$00,$FF,$FF,$00,$00,$00     ; -
        dta $C0,$60,$30,$18,$18,$0C,$06,$03     ; \

dlist   dta $70,$70,$70,$42,a(screen)
        :23 dta $02
        dta $41,a(dlist)

screen  dta d'   ONE POKE TO CHBAS ANIMATES THEM ALL   '
        :23 dta $00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40,$00,$40
        run main

Each font costs 1K, so a four-frame cycle uses 4K. That is worthwhile only when many characters animate at once, such as a whole screen of rippling water or spinning coins. The text line at the top stays readable because all four fonts are ordinary copies of the ROM set apart from character 64.

Fonts must start on a 1K boundary ($3000, $3400, …), and CHBAS holds the page number. Writing the shadow CHBAS lets the OS copy it into CHBASE ($D409, 54281) during the next vertical blank, so the switch always happens between frames.

6. Timing and tearing

ANTIC reads the font afresh for every scan line it draws. If your program rewrites a character while the beam is half way down a row of those characters, the top of the row shows the old frame and the bottom the new one. Two rules avoid this:

  • Change character data during vertical blank. A VBI routine is the natural place. A main-loop routine can wait for RTCLOK (20) to change, or for VCOUNT ($D40B, 54283) to pass the bottom of the screen.
  • Keep the per-frame work small. Rotating three characters takes a few hundred cycles. If you need to rebuild dozens of sprite characters, double-buffer them: build the next frame's shapes in a second set of character codes and switch the screen bytes or CHBAS in the VBI.

Troubleshooting

  • Animated characters show up in the wrong places: you redefined a character the program also uses for text. Pick unused codes, such as the control graphics (internal 64–95).
  • Stripes jump instead of flowing: the rotation amount isn't a whole pixel (mode 4 needs 2 bits), or the pattern doesn't repeat evenly within 8 pixels.
  • Ball or sprite leaves a trail: you aren't erasing the old cells, or you erase them after drawing the new ones when the cells overlap.
  • Font flipping shows garbage: a font isn't 1K-aligned or wasn't fully copied before the VBI started switching.
  • Top half of a row lags one frame behind: you are changing font data during the visible display; move the work into the VBI.

Further reading

  • De Re Atari (Atari, 1982), Chapter 3: character-set multiplexing in its three time regimes, with the Space Invaders and ENERGY CZAR examples.
  • Atari Graphics and Arcade Game Design by Jeffrey Stanton and Dan Pinal (Arrays, 1984), Chapter 3: "Rotating Character Sets" and "Animation Using Different Characters".
  • COMPUTE!'s Second Book of Atari Graphics (COMPUTE! Books, 1984), Chapter 3: character generation and custom characters.
  • Antic, October 1984: "ANTIC 4/5 Editor Animator" by Paul Chabot, a tool for building multi-frame four-colour character animations.
  • Rob McMullen's Player/Missile Podcast DLI tutorial: character sets and soft sprites with interleaved fonts.

Credits