Character Set Switching with DLIs: More Than 128 Characters per Screen

Atari screen with two lines of blue text at the top, a black panel showing a thin white sine wave inside a frame, and more blue text below

A character set holds 128 shapes, and normally the whole screen shares one. But ANTIC looks up the font address in its CHBASE register as it draws, and a display list interrupt can change that register part way down the screen. That gives you a graphics font for the playfield and a readable font for the score line, several "alphabets" on one page, or a different 128 characters for every row of the screen. The last option is how many games show detailed, apparently bitmapped scenery while still drawing it with fast character graphics.

What you'll learn: which register to write and when, a BASIC program that shows two fonts on one GRAPHICS 0 screen, an assembly program that switches four fonts to build a 320 × 96 hi-res picture out of 480 unique characters, interleaved fonts for soft sprites, and the common bugs.

What you need: familiarity with Display List Interrupts and Redefined Character Sets, Atari BASIC, and MADS for the assembly example.

1. CHBAS versus CHBASE

Address Name What it is
756 ($2F4) CHBAS OS shadow. The OS copies it into CHBASE every vertical blank.
54281 ($D409) CHBASE ANTIC's hardware register: page number of the font in use right now.

Everything from Redefined Character Sets uses CHBAS, which changes the font for the whole screen from the next frame. To change fonts part way down, a DLI must write CHBASE directly. The OS then puts the CHBAS value back at the top of the next frame, so the top of your screen always uses the shadow's font. If you write CHBAS in the DLI by mistake, the last value written wins and the whole screen uses that font.

A few hardware details matter:

  • For ANTIC modes 2–5 the font is 1K and must be 1K-aligned, so only the top six bits of CHBASE count. For modes 6 and 7 (GRAPHICS 1 and 2) it is 512 bytes on a 512-byte boundary, so bit 1 counts too. ANTIC stores bit 1 even in the 1K modes, and it takes effect if a later mode line is mode 6 or 7.
  • A write to CHBASE takes effect about two cycles later. ANTIC fetches font data separately for every scan line of a character row, so a write in mid-line changes the rest of that scan line. Always do the store after STA WSYNC, during horizontal blank.
  • Because the font is read per scan line, you can change it inside a character row too. The new font applies to the remaining scan lines of that row.

2. Two fonts on a BASIC screen

This program builds an italic copy of the ROM font by shifting the top three rows of every character right and the bottom three left (about 20 seconds in BASIC), then installs an 11-byte DLI that switches CHBASE half way down a GRAPHICS 0 screen:

10 REM TWO FONTS WITH A DLI
20 A=PEEK(106)-8:POKE 106,A:GRAPHICS 0
30 CH=A+4:CS=CH*256
40 ? "BUILDING AN ITALIC FONT..."
50 FOR C=0 TO 127:FOR R=0 TO 7
60 B=PEEK(57344+C*8+R)
70 IF R<3 THEN B=INT(B/2)
80 IF R>4 THEN B=B*2-256*(B>127)
90 POKE CS+C*8+R,B:NEXT R:NEXT C
100 FOR I=0 TO 10:READ D:POKE 1536+I,D:NEXT I
110 POKE 1538,CH
120 DL=PEEK(560)+256*PEEK(561)
130 POKE 512,0:POKE 513,6
140 POKE DL+15,130:POKE 54286,192
150 ? CHR$(125)
160 FOR I=1 TO 22
170 ? "LINE ";I;": THE QUICK BROWN FOX"
180 NEXT I
190 GOTO 190
200 DATA 72,169,0,141,10,212,141,9,212
210 DATA 104,64

The DLI bytes in lines 200–210 are:

        PHA             ; 72
        LDA #0          ; 169,0   page number, poked in by line 110
        STA WSYNC       ; 141,10,212  ($D40A)
        STA CHBASE      ; 141,9,212   ($D409)
        PLA             ; 104
        RTI             ; 64

Line 140 gives mode line 11 the DLI bit (DL+15 holds 2 + 128 = 130 after the POKE), so from line 12 down every character comes from the italic font. Lines 1–11 still use the font in CHBAS. Both halves contain exactly the same character codes, so you can type on the bottom half of the screen and see it appear in italics. In a game the same 11 bytes would put a text font under a graphics font.

3. 480 characters, four fonts, one picture

Character graphics run out of shapes quickly: a detailed picture needs a different character in nearly every cell. The answer is to give each band of rows its own font. This MADS program divides 12 rows of GRAPHICS 0 characters into four bands of three rows. Every band uses character codes 0–119 in the same layout, but each band has its own font, so the screen effectively has 480 unique characters. The program then treats the four fonts as a 320 × 96 bitmap and plots a sine wave and a frame into them:

; five fonts on one screen - mads charsplit.asm -o:charsplit.xex
VDSLST = $0200
SDLSTL = $0230
COLOR2 = $02C6
COLPF2 = $D018
CHBASE = $D409
WSYNC  = $D40A
NMIEN  = $D40E
SETVBV = $E45C
SYSVBV = $E45F
FONTS  = $4000              ; four 1K fonts: $4000, $4400, $4800, $4C00
idx    = $80                ; which DLI comes next
px     = $81                ; plot x (16-bit)
py     = $83                ; plot y
ptr    = $84

        org $2000
main    lda #0                  ; clear the four fonts (4K)
        sta ptr
        lda #>FONTS
        sta ptr+1
        ldx #16
        ldy #0
        tya
clr     sta (ptr),y
        iny
        bne clr
        inc ptr+1
        dex
        bne clr
        jsr draw
        lda #$84
        sta COLOR2              ; blue text background at the top
        lda #<dlist
        sta SDLSTL
        lda #>dlist
        sta SDLSTL+1
        lda #6                  ; immediate VBI resets the DLI index
        ldy #<vbi
        ldx #>vbi
        jsr SETVBV
        lda #<dli
        sta VDSLST
        lda #>dli
        sta VDSLST+1
        lda #$C0
        sta NMIEN
        jmp *

vbi     lda #0
        sta idx
        jmp SYSVBV

; one DLI routine, five uses: font page and colour come from tables
dli     pha
        txa
        pha
        ldx idx
        lda chtab,x             ; preload the new font page
        sta WSYNC               ; wait for horizontal blank
        sta CHBASE              ; hardware register, not CHBAS
        lda coltab,x
        sta COLPF2
        inc idx
        pla
        tax
        pla
        rti

chtab   dta >FONTS, >FONTS+4, >FONTS+8, >FONTS+12, $E0
coltab  dta $02, $02, $02, $02, $84

; draw a sine wave and a frame into the 320x96 "character bitmap"
draw    lda #0
        sta px
        sta px+1
hloop   ldx px                  ; y = sine(x); sine table is 320 bytes
        lda px+1
        beq @+
        lda sine+256,x
        jmp gotit
@       lda sine,x
gotit   sta py
        jsr plot
        lda #0                  ; top edge
        sta py
        jsr plot
        lda #95                 ; bottom edge
        sta py
        jsr plot
        inc px
        bne @+
        inc px+1
@       lda px+1                ; until x = 320
        beq hloop
        lda px
        cmp #<320
        bne hloop
        lda #95                 ; left and right edges
        sta py
vloop   lda #0
        sta px
        sta px+1
        jsr plot
        lda #<319
        sta px
        lda #>319
        sta px+1
        jsr plot
        dec py
        bpl vloop
        rts

; plot pixel (px,py): font = row band, char = row-in-band*40 + x/8
plot    lda py
        lsr @
        lsr @
        lsr @
        tay                     ; Y = character row 0..11
        lda px
        and #$F8                ; (x/8)*8 = byte offset of the character
        clc
        adc rowlo,y
        sta ptr
        lda px+1
        adc rowhi,y             ; font page + row-in-band*320
        sta ptr+1
        lda py
        and #7                  ; + scan line within the character
        tay
        lda px
        and #7
        tax
        lda (ptr),y
        ora bits,x
        sta (ptr),y
        rts

bits    dta $80,$40,$20,$10,$08,$04,$02,$01
; for character rows 0..11: font page and 0/320/640 offset within it
rowlo   :4 dta <0, <320, <640
rowhi   dta >FONTS+0, >FONTS+1, >FONTS+2
        dta >FONTS+4, >FONTS+5, >FONTS+6
        dta >FONTS+8, >FONTS+9, >FONTS+10
        dta >FONTS+12, >FONTS+13, >FONTS+14
sine    dta sin(48,40,320,0,319)

dlist   dta $70,$70,$70
        dta $42,a(top)
        dta $82                 ; DLI -> font 0 from the next line
        dta $02,$02,$82         ; band 0 (font 0), DLI -> font 1
        dta $02,$02,$82         ; band 1, DLI -> font 2
        dta $02,$02,$82         ; band 2, DLI -> font 3
        dta $02,$02,$82         ; band 3, DLI -> ROM font again
        :10 dta $02
        dta $41,a(dlist)

top     dta d'  480 UNIQUE CHARACTERS FROM 4 FONTS    '
        dta d'  CHBASE CHANGED BY DLI EVERY 3 LINES   '
        .rept 4
        :40 dta #               ; codes 0-39
        :40 dta #+40            ; codes 40-79
        :40 dta #+80            ; codes 80-119
        .endr
        dta d'                                        '
        dta d'  BACK TO THE ROM FONT FOR THE TEXT     '
        dta d'  BELOW THE PICTURE.                    '
        :7 dta d'                                        '
        run main

How it fits together:

  • The display list has five DLIs: one on the second text line to select font 0, one on the last line of each band to select the next font, and one at the end of the picture to return to the ROM font ($E0) for the text below.
  • One DLI routine serves all five. It reads the font page and a background colour from chtab and coltab using an index, then increments the index. This index-table method is the simplest way to run many DLIs that differ only in their values; see Advanced DLI Techniques for other ways.
  • An immediate VBI resets the index to 0 every frame and ends with JMP SYSVBV. Without it, the first DLI of a frame could use the wrong table entry, and the fonts would appear in random bands depending on where the beam was when DLIs were enabled.
  • plot turns (x, y) into a font address. The character row is y/8, and two small tables give that row's font page and its 0/320/640-byte offset within the font. Then x AND $1F8 is the character's byte offset, y AND 7 the scan line and x AND 7 the bit. This is the same arithmetic a bitmap plot uses, just routed through character codes.

The cost is memory: each font takes 1K, and screen RAM for the picture is only 480 bytes. A full-screen version with a new font every character row needs 24 fonts (24K) for 3,072 unique cells, which is why many games mix the approaches: unique characters where detail is needed, and repeated tiles elsewhere.

4. Interleaved fonts for soft sprites

The same trick helps character-based "soft sprites" (see Character Set Animation). A soft sprite needs its own private characters wherever it overlaps the background, and with one font you quickly run out. If a DLI switches fonts every character row, each row gets its own 128 characters. Here is the arithmetic for ANTIC 4. Because mode 4 characters are 8 scan lines tall and 4 pixels wide, a sprite that can sit at any offset needs room for the worst case: up to 9 scan lines tall fits in two character rows, up to 17 in three; up to 5 pixels wide fits in two columns, up to 9 in three. So a sprite 9 pixels wide and 16 lines tall needs a 3 × 3 block of characters, and if 64 characters per font go to the background, the other 64 hold only a handful of such sprites. A new font every row multiplies that budget.

The usual design is a table of font pages, one entry per character row, and a single DLI routine (or a kernel) that walks through it every frame. Font pages can also be swapped per frame in the VBI to double-buffer the sprite characters.

5. Other uses

  • Game font above, text font below. Mode 4 graphics for the playfield and a mode 2 status line with the normal ROM font, or the reverse.
  • "Rosetta Stone" screens. Several typefaces or alphabets can share one screen.
  • Upside-down halves. CHACTL ($D401, 54273, shadow 755) bit 2 flips characters vertically. A DLI that sets it part way down can mirror the lower half, as on a playing card or a reflection in water.
  • GRAPHICS 1 and 2 mixes. Big-letter titles from one 512-byte font and graphic symbols from another.

6. Timing and pitfalls

  • Character lines are DMA-heavy. On the first scan line of a mode 2 or mode 4 row ANTIC fetches both character names and font data, leaving the CPU very few cycles. Preload A, X and Y before STA WSYNC, then do only stores. If you also change several colours, put the DLI on a blank line or split the work over two lines.
  • Something else may be writing CHBASE. An OS replacement or a C runtime IRQ handler that refreshes CHBASE will undo your DLI; cc65 users have run into this with a runtime configuration that runs code under the OS ROM.
  • Keyboard clicks shift DLIs by a line. The OS key-click routine uses WSYNC, which can push a font change down a scan line while a key is pressed.
  • Vertical scrolling moves DLIs. In a vertically scrolled region the shortened first or last row still triggers its DLI, but at a different scan line, so font bands can jump.

Troubleshooting

  • Whole screen uses the second font: the DLI writes CHBAS (756) instead of CHBASE (54281).
  • Fonts appear in the wrong bands, different each run: the DLI index isn't reset in a VBI.
  • One scan line of the band uses the old font: the store happens too late; move it right after STA WSYNC and preload the value.
  • Garbage characters: the font isn't 1K-aligned (512 bytes for GRAPHICS 1/2), or the font area wasn't cleared before plotting.
  • Machine locks up: NMIEN written before VDSLST was set, or a register not restored before RTI.

Further reading

  • De Re Atari (Atari, 1982), Chapters 3 and 5: character set multiplexing and applications of DLIs.
  • Mapping the Atari by Ian Chadwick (COMPUTE! Books), entries for 755, 756, 54273 and 54281.
  • Altirra Hardware Reference Manual, section 4.4: character set storage and CHBASE timing.
  • Rob McMullen's "Atari 8-bit Display List Interrupts: A Complete(ish) Tutorial" (Player/Missile Podcast), Topic 3: changing and interleaving character sets.
  • COMPUTE!, April 1984: "Atari Display List Interrupts", which includes a program combining DLIs with custom character sets.

Credits