Interlace Display Kernels: Building an APAC 256-Colour Screen

Atari screen of fine horizontal lines forming a grid of colours: hues change from left to right and brightness rises from top to bottom

No standard Atari graphics mode shows all 256 colours at once. GRAPHICS 9 gives 16 shades of one hue, and GRAPHICS 11 gives 16 hues at one brightness. But a picture can borrow from both. Show a GRAPHICS 9 line, then a GRAPHICS 11 line, then 9 again, and on a TV the eye merges each pair of lines into a single colour with its own hue and its own brightness. That is APAC ("Any Point, Any Color"), the best known of the Atari's software display modes. Modes like this aren't hardware modes but techniques: a small program, the kernel, has to keep switching GTIA while the picture is drawn. This guide builds a working APAC kernel and explains the timing lessons that come with it.

What you'll learn: how GTIA modes 9 and 11 get their colours, how APAC combines them, how to switch modes on exact scan lines with a single DLI that runs a kernel loop, the 4K screen-memory rule, and how HIP, RIP and the other interlace formats differ.

What you need: MADS, an emulator, and the background from GTIA Graphics Modes 9, 10 and 11 and Display List Interrupts. A GTIA chip is required: on an early CTIA machine the program shows only dots. The example was assembled with MADS and run in atari800.

1. Where the colours come from

Both GTIA modes read the high-resolution data of ANTIC mode F (GRAPHICS 8) four bits at a time, giving 80 wide pixels per line. They differ in where the colour comes from:

  • Mode 9 (PRIOR bits 7–6 = 01, $40): each pixel is a luminance 0–15. The hue comes from the background register COLBK. Its luminance should be 0, because it is ORed with the pixels.
  • Mode 11 (PRIOR bits 7–6 = 11, $C0): each pixel is a hue 0–15. The luminance comes from COLBK, whose hue should be 0. Pixel value 0 is always forced to black.

So an APAC "pixel" is two scan lines tall: a mode 9 line carrying its brightness and a mode 11 line carrying its colour. The result is 80 × 96 pixels with 16 × 16 = 256 combinations.

2. Why one DLI per line doesn't work

The obvious approach is a DLI on every mode F line that flips PRIOR for the next line. We tried it, and it crashes. A one-scan-line mode line triggers its DLI at the start of that same line. The handler waits on WSYNC until the end of the line and then writes PRIOR. By the time it has restored its registers and returned, the next line's DLI has already fired. The NMI interrupts the handler that is still running, that one is in turn interrupted by the next, and so on down the screen. In our first version this nesting lost track of which mode came next and produced unstable pictures. A version that kept even more work after the WSYNC overflowed the stack and crashed the emulator.

The robust solution is the classic kernel: one DLI on the blank line just above the picture, which stays in a tight loop for the whole picture. Each pass waits for the start of a line with WSYNC and writes the registers for it. The CPU is fully occupied during the display, which is fine for a picture viewer or a slideshow. The vertical blank and the area below the picture are still free for other work.

3. Screen memory and the 4K rule

The picture uses ordinary mode F display-list lines, 40 bytes each, 192 of them: 7,680 bytes. ANTIC's memory counter can't cross a 4K boundary. A line that runs over one wraps back to the start of the same 4K block. So the data is split: lines 0–101 start at $4000 (102 × 40 = 4,080 bytes, just under 4K), and a second LMS (load memory scan) instruction points lines 102–191 at $5000. The OS's GRAPHICS 8 display list needs the same kind of split.

4. The program

; APAC-style display: GTIA mode 9 (luminance) and mode 11 (hue) on alternate
; scan lines, switched by a one-DLI kernel. 80x96 "pixels", 256 colours.
SDMCTL  = $022F
SDLSTL  = $0230
GPRIOR  = $026F
COLOR4  = $02C8
VDSLST  = $0200
NMIEN   = $D40E
WSYNC   = $D40A
PRIOR   = $D01B
COLBK   = $D01A
PTR     = $CB
SCR1    = $4000             ; lines 0-101
SCR2    = $5000             ; lines 102-191 (ANTIC can't cross a 4K boundary)
HUELUM  = $08               ; brightness used on the hue (mode 11) lines

        org $3000
start   jsr fill
        lda #0
        sta SDMCTL          ; screen off while we switch
        lda #<dlist
        sta SDLSTL
        lda #>dlist
        sta SDLSTL+1
        lda #$40            ; frame starts with a mode 9 (luminance) line
        sta GPRIOR
        lda #0
        sta COLOR4          ; mode 9 takes its hue from COLBK: 0 = grey
        lda #<dli
        sta VDSLST
        lda #>dli
        sta VDSLST+1
        lda #$C0
        sta NMIEN           ; DLI + VBI
        lda #$22
        sta SDMCTL          ; normal playfield, DMA on
loop    jmp loop

; One DLI, on the blank line just above the picture, runs a "kernel":
; it waits for the start of each of the 192 lines and switches GTIA
; between mode 9 (luminance) and mode 11 (hue). The CPU is busy for the
; whole picture, but the timing is exact. (A DLI on every line can't work
; here: each would still be running when the next one fired.)
dli     pha
        tya
        pha
        ldy #96             ; 96 pairs of lines
kern    lda #$40            ; even line: mode 9, grey hue from COLBK
        sta WSYNC
        sta PRIOR
        lda #0
        sta COLBK
        lda #$C0            ; odd line: mode 11, luminance from COLBK
        sta WSYNC
        sta PRIOR
        lda #HUELUM
        sta COLBK
        dey
        bne kern
        lda #0              ; black border below the picture
        sta WSYNC
        sta COLBK
        pla
        tay
        pla
        rti

; fill: even lines = luminance rising down the screen, odd lines = 16 hues
fill    lda #<SCR1
        sta PTR
        lda #>SCR1
        sta PTR+1
        ldx #0              ; X = scan line 0..191
fl1     cpx #102
        bne fl2
        lda #<SCR2
        sta PTR
        lda #>SCR2
        sta PTR+1
fl2     txa
        lsr                 ; carry = odd line
        bcs hue
        lsr                 ; lum = line/16 -> 0..11
        lsr
        lsr
        ldy #0
        sta tmp
        asl
        asl
        asl
        asl
        ora tmp             ; two pixels of the same luminance
fl3     sta (PTR),y
        iny
        cpy #40
        bne fl3
        beq next
hue     ldy #0
fl4     lda div5,y          ; byte y holds pixels 2y and 2y+1: hue = y*2/5
        asl
        asl
        asl
        asl
        ora div5,y
        sta (PTR),y
        iny
        cpy #40
        bne fl4
next    clc
        lda PTR
        adc #40
        sta PTR
        bcc nx1
        inc PTR+1
nx1     inx
        cpx #192
        bne fl1
        rts

div5    :40 dta [#*2]/5     ; hue 0..15 across 40 bytes
tmp     dta 0

dlist   dta $70,$70,$F0       ; DLI on the last blank line
        dta $4F,a(SCR1)
        :101 dta $0F
        dta $4F,a(SCR2)
        :89 dta $0F
        dta $41,a(dlist)

        run start

How it works:

  • The display list is three blank-line instructions, the last of which has its DLI bit set ($F0), then 192 mode F lines ($0F) with LMS instructions ($4F) at the start and at line 102, then JVB.
  • The kernel (label dli) handles one pair of lines per pass: write $40 to PRIOR and 0 to COLBK for a luminance line, then $C0 and the chosen luminance for a hue line. Each STA WSYNC stops the CPU until the end of the current scan line, so the following stores land in the horizontal blank, before the next line is drawn.
  • The shadows GPRIOR and COLOR4 are set to the values for the first line, since the OS copies them to the hardware every vertical blank. Without that, the top of the picture would start in the wrong mode.
  • The test data (fill) puts a luminance that rises down the screen on even lines, and 16 hues across the screen on odd lines, so the result is a hue-by-brightness chart.

HUELUM sets the brightness of the hue lines. It makes a big difference to how the blended colours look: at $0C the result was washed out and pastel, and at $08 it was richer, as in the screenshot. Emulators and modern monitors show the individual lines sharply. On a CRT television, or with an emulator's interlace or frame-blending options, they merge into solid colours.

5. Other interlace modes

APAC is one of a family. The Atari demoscene developed many such techniques, each with its own picture format and viewer:

  • HIP (Hard Interlace Picture) and RIP combine GRAPHICS 9 and GRAPHICS 10 data. They exploit a half-pixel shift that GTIA produces in mode 10, reaching 160 pixels across instead of 80. In HIP both modes carry luminance. In RIP, mode 10 supplies colour and mode 9 the luminance.
  • TIP, INP, CIN, MCP and others are further variations, each trading resolution, colours and CPU time differently.

Atari Graphics File Formats lists these formats and their typical resolutions. Tools such as Graph2Font can build mixed-mode pictures, and RastaConverter produces near-photographic images that use the CPU throughout the display.

6. Making it practical

  • Doing other work: everything except the kernel must fit into the vertical blank and the lines below the picture. Music players are normally driven from the VBI.
  • PAL and NTSC: the kernel counts lines, not time, so it works on both.
  • Players over the picture need care. In mode 9 the playfield acts as background for priority, and the kernel's PRIOR writes also change the player priority bits.

Troubleshooting

  • Unstable or flickering picture, or a crash: you are using one DLI per line. Switch to a single kernel DLI.
  • The top lines are in the wrong mode: set GPRIOR and COLOR4 for the first line.
  • A horizontal glitch about halfway down: the data crosses a 4K boundary. Add an LMS at the boundary.
  • Only dots, no colours: CTIA machine, or PRIOR is not being written. Check that NMIEN has the DLI bit set and that the DLI bit is on the right display-list instruction.
  • Colours look grey: the hue-line luminance is too high or too low. Try values around $06–$0A.

Further reading

  • Avery Lee, Altirra Hardware Reference Manual, chapter 6: GTIA modes 9, 10 and 11, and ANTIC's 4K boundary behaviour.
  • On this site: GTIA Graphics Modes 9, 10 and 11, Advanced DLI Techniques: Kernels, Dispatch Tables and Cycle Budgets, Atari Graphics File Formats, Graph2Font (G2F).

Credits