GTIA 9++: Square Pixels and a Quarter of the DMA with a Vertical-Scroll Trick

Full-screen pattern of square blue pixels in sixteen shades, forming a diagonal checkerboard of gradients crossed by thin light diagonal lines

GTIA's modes 9, 10 and 11 give 16 shades, 9 colours or 16 hues, but their pixels are wide and flat: 80 across and 192 down, each one four colour clocks wide and a single scan line high. A picture of that shape needs 7,680 bytes, and ANTIC steals CPU time on every scan line to fetch it. Demos and games often want the opposite: chunky, square pixels that are quick to draw and cheap to display. The trick that delivers them is known as GTIA 9++. It stretches each line of screen data to four scan lines by abusing ANTIC's vertical scroll counter, so 80 × 48 square pixels cost 1,920 bytes and a quarter of the screen DMA. The demo Numen is a well-known user of it, and Mad Pascal's libraries include a version of the same trick for GTIA mode 10.

What you'll learn: why vertical scrolling can stretch a mode line, how a display list and a short DLI produce the mode, the timing that makes the DLI work, and what it actually saves, measured.

What you need: MADS and an emulator, and the background from GTIA Graphics Modes 9, 10 and 11, Display List Interrupts and Fine Scrolling (for VSCROL). The example was assembled with MADS and tested in two emulators: atari800 (NTSC, XL/XE mode with its built-in AltirraOS) and Altirra 4.40 (default settings, also with AltirraOS), which is known for cycle-exact ANTIC emulation. Both gave the same picture and the same benchmark figures. We have not tried it on real hardware.

1. Why the ordinary ways are expensive

Two obvious ways to make tall pixels both have costs:

  • Repeat each line with LMS. Four mode F lines that all point at the same 40 bytes give one tall row. Memory drops to 1,920 bytes, but ANTIC still fetches all 40 bytes on every scan line, so the DMA cost is that of a full GRAPHICS 9 screen, and the display list grows to three bytes per scan line.
  • Use a taller ANTIC mode. Mode D or E with GTIA turned on gives different pixel arrangements, not 16 shades in 80 columns.

ANTIC has a property that helps: in the bitmap ("mapped") modes it fetches a line's data only on the first scan line of the mode line and keeps it in an internal buffer, so if a mode line were taller than one scan line, the extra scan lines would cost no DMA at all. Mode F lines are one scan line tall, but vertical scrolling can change that.

2. How vertical scrolling stretches a line

Bit 5 of a display list instruction enables vertical scrolling. The Altirra Hardware Reference Manual describes the rule precisely. When the bit changes from 0 to 1, the first line of the scrolled region starts at the row given by VSCROL ($D405). When it changes from 1 to 0, the first line without the bit ends at the row given by VSCROL. Normally that shortens lines, which is how smooth scrolling works.

The important detail is that VSCROL and ANTIC's row counter are 4-bit counters in every mode. A mode F line normally runs from row 0 to row 0. But:

  • A line that starts a region with VSCROL = 13 counts 13, 14, 15, then wraps to 0, where a mode F line ends. That is four scan lines.
  • A line that ends a region with VSCROL = 3 counts 0, 1, 2, 3. Also four scan lines.

So a display list that alternates "scroll bit on" and "scroll bit off" mode F lines gets four scan lines from every line, as long as VSCROL is 13 when each "on" line starts and 3 when each "off" line ends. Each line still has its own 40 bytes of data (ANTIC's memory counter advances normally), so 48 lines give 48 rows of 80 pixels, 192 scan lines in all.

3. Switching VSCROL in time

VSCROL can't hold both values at once, so a DLI changes it twice for every pair of lines. The deadlines, again from the Altirra manual: the starting row of a scrolled line is taken from VSCROL by cycle 0 of its first scan line, and the ending row is checked against VSCROL up to cycle 108 of each scan line.

The DLI goes on every "off" line, and on the blank line just before the picture. It fires at the start of the line's last scan line, and does:

dli     pha
        sta WSYNC           ; wait for the end of this scan line
        lda #13             ; next line starts at row 13 (13,14,15,0)
        sta VSCROL
        lda #3              ; ...and the line after it ends at row 3
        sta VSCROL
        pla
        rti

According to the manual, the instruction after a write to WSYNC effectively starts on cycle 104. Counting from there, the store of 13 lands at about cycle 109: just after the cycle-108 check, so it doesn't disturb the end of the current line, and before cycle 0 of the next scan line, so the next line starts at row 13. The store of 3 lands a few cycles into that next line, too late to change its start, and stays in place to end the following "off" line at row 3. It is a two-cycle margin, so don't add instructions between the two stores. We tested what happens if the two values are written the other way round (SWAP = 1 in the listing): in both emulators the picture fell apart into stretched bands.

The DLI on the blank line before the picture matters too. Without it, VSCROL would still hold 3 from the bottom of the previous frame, and the first line would start at row 3 and run for 14 scan lines.

4. The display list

dlist   dta $70,$70,$F0     ; 24 blank lines, DLI on the last one
        dta $6F,a(SCREEN)   ; mode F + LMS + vertical scroll: starts a region
        dta $8F             ; mode F + DLI, no scroll bit: ends it
        :23 dta $2F,$8F     ; 23 more pairs
        dta $41,a(dlist)    ; jump and wait for vertical blank

That is 52 bytes for the whole screen. $6F and $2F are mode F with the scroll bit ($20), the first also with LMS ($40). $8F is mode F with the DLI bit ($80). GTIA mode 9 is switched on as usual through GPRIOR ($26F, value $40), with the hue in COLOR4 (712). The same display list works for the other GTIA modes: with GPRIOR = $C0 the picture appeared in mode 11's 16 hues.

5. The program

The program draws a pattern, shades (x XOR y) plus (x + y) / 4, two 4-bit pixels per byte, then shows it for 256 frames. It then runs the same counting loop for 120 frames in three situations: with the 9++ screen, with an ordinary GRAPHICS 9 screen opened through the OS, and with the display switched off, and prints the counts.

; GTIA 9++: 80 x 48 pixels in 16 shades, each pixel four scan lines
; tall, built from mode F lines stretched with VSCROL. Then a benchmark:
; free CPU time with 9++, with an ordinary GRAPHICS 9 screen, and with
; the display off. MADS.
RTCLOK  = $12
VDSLST  = $0200
SDMCTL  = $022F
SDLSTL  = $0230
GPRIOR  = $026F
COLOR4  = $02C8
VSCROL  = $D405
WSYNC   = $D40A
NMIEN   = $D40E
ICCOM   = $0342
ICBAL   = $0344
ICBLL   = $0348
ICAX1   = $034A
ICAX2   = $034B
CIOV    = $E456
PTR     = $CB
SCREEN  = $4000             ; 48 rows x 40 bytes = 1920 bytes
FRAMES  = 120               ; length of each benchmark run
SWAP    = 0                 ; 1 = write 3 before 13 in the DLI (broken)

        org $3000
start   jsr picture         ; draw the image into SCREEN
        lda #0              ; display off while we switch
        sta SDMCTL
        jsr vwait
        lda #<dli
        sta VDSLST
        lda #>dli
        sta VDSLST+1
        lda #<dlist
        sta SDLSTL
        lda #>dlist
        sta SDLSTL+1
        lda #$40            ; GTIA mode 9: 16 shades of one hue
        sta GPRIOR
        lda #$90            ; hue: blue
        sta COLOR4
        lda #$C0            ; enable DLIs (and VBI)
        sta NMIEN
        lda #$22            ; normal playfield, DL DMA on
        sta SDMCTL

        ldx #0              ; show the picture for 256 frames first
h1      jsr vwait
        dex
        bne h1
        jsr count           ; 1. free time with 9++ on screen
        lda cnt
        sta res1
        lda cnt+1
        sta res1+1
        lda cnt+2
        sta res1+2

        lda #$40            ; 2. ordinary GRAPHICS 9 from the OS
        sta NMIEN           ; (no DLIs)
        ldx #$60
        lda #12
        sta ICCOM,x
        jsr CIOV
        ldx #$60
        lda #3
        sta ICCOM,x
        lda #<sname
        sta ICBAL,x
        lda #>sname
        sta ICBAL+1,x
        lda #12
        sta ICAX1,x
        lda #9
        sta ICAX2,x
        jsr CIOV
        jsr count
        lda cnt
        sta res2
        lda cnt+1
        sta res2+1
        lda cnt+2
        sta res2+2

        lda #0              ; 3. no display at all
        sta SDMCTL
        jsr count
        lda cnt
        sta res3
        lda cnt+1
        sta res3+1
        lda cnt+2
        sta res3+2

        lda #0              ; back to GRAPHICS 0 and report
        sta GPRIOR
        ldx #$60
        lda #12
        sta ICCOM,x
        jsr CIOV
        ldx #$60
        lda #3
        sta ICCOM,x
        lda #12
        sta ICAX1,x
        lda #0
        sta ICAX2,x
        jsr CIOV
        jsr say
        dta $7D,c'LOOPS IN 120 FRAMES (MORE = MORE CPU)',$9B,$9B
        dta c'GTIA 9++ 80X48:     ',0
        ldx #0
        jsr prres
        jsr say
        dta $9B,c'GRAPHICS 9 80X192:  ',0
        ldx #3
        jsr prres
        jsr say
        dta $9B,c'DISPLAY OFF:        ',0
        ldx #6
        jsr prres
        jmp *

; ---- the DLI, on every second mode line ----
dli     pha
        sta WSYNC           ; wait for the end of this scan line
        .if SWAP
        lda #3
        sta VSCROL
        lda #13
        sta VSCROL
        .else
        lda #13             ; next line starts at row 13 (13,14,15,0)
        sta VSCROL
        lda #3              ; ...and the line after it ends at row 3
        sta VSCROL
        .endif
        pla
        rti

; ---- count loop passes for FRAMES frames ----
count   lda #0
        sta cnt
        sta cnt+1
        sta cnt+2
        jsr vwait
        clc
        lda RTCLOK+2
        adc #FRAMES
        sta tend
c1      inc cnt
        bne c2
        inc cnt+1
        bne c2
        inc cnt+2
c2      lda RTCLOK+2
        cmp tend
        bne c1
        rts
vwait   lda RTCLOK+2
vw1     cmp RTCLOK+2
        beq vw1
        rts

; ---- picture: shade = (x XOR y) + (x+y)/4, 2 pixels per byte ----
picture lda #<SCREEN
        sta PTR
        lda #>SCREEN
        sta PTR+1
        lda #0
        sta py
p1      ldy #0
p2      tya                 ; left pixel x = 2*byte
        asl
        jsr shade
        asl
        asl
        asl
        asl
        sta tmp
        tya                 ; right pixel x = 2*byte+1
        asl
        ora #1
        jsr shade
        ora tmp
        sta (PTR),y
        iny
        cpy #40
        bne p2
        clc
        lda PTR
        adc #40
        sta PTR
        bcc p3
        inc PTR+1
p3      inc py
        lda py
        cmp #48
        bne p1
        rts
shade   sta px              ; A = x, py = y -> A = shade 0-15
        eor py
        sta tmp2
        lda px
        clc
        adc py
        lsr
        lsr
        clc
        adc tmp2
        and #15
        rts

; ---- text output ----
prres   lda res1,x          ; print a 24-bit result in decimal
        sta n24
        lda res1+1,x
        sta n24+1
        lda res1+2,x
        sta n24+2
        ldx #0
pr1     lda #'0'
        sta digit
pr2     sec
        lda n24
        sbc p10a,x
        sta t24
        lda n24+1
        sbc p10b,x
        sta t24+1
        lda n24+2
        sbc p10c,x
        bcc pr3
        sta n24+2
        lda t24+1
        sta n24+1
        lda t24
        sta n24
        inc digit
        bne pr2
pr3     stx savx
        lda digit
        jsr putc
        ldx savx
        inx
        cpx #7
        bne pr1
        rts
p10a    dta <1000000,<100000,<10000,<1000,<100,<10,<1
p10b    dta >1000000,>100000,>10000,>1000,>100,>10,>1
p10c    dta ^1000000,^100000,^10000,^1000,^100,^10,^1
say     pla
        sta PTR
        pla
        sta PTR+1
s1      inc PTR
        bne s2
        inc PTR+1
s2      ldy #0
        lda (PTR),y
        beq s3
        jsr putc
        jmp s1
s3      lda PTR+1
        pha
        lda PTR
        pha
        rts
putc    ldx #0
        stx ICBLL
        stx ICBLL+1
        pha
        lda #11
        sta ICCOM
        pla
        jmp CIOV

sname   dta c'S:',$9B
cnt     dta 0,0,0
tend    dta 0
res1    dta 0,0,0
res2    dta 0,0,0
res3    dta 0,0,0
n24     dta 0,0,0
t24     dta 0,0
digit   dta 0
savx    dta 0
px      dta 0
py      dta 0
tmp     dta 0
tmp2    dta 0

; ---- the display list: 48 mode F lines of four scan lines each ----
        .align $400
dlist   dta $70,$70,$F0     ; 24 blank lines, DLI on the last one
        dta $6F,a(SCREEN)   ; mode F + LMS + vertical scroll: starts a region
        dta $8F             ; mode F + DLI, no scroll bit: ends it
        :23 dta $2F,$8F     ; 23 more pairs
        dta $41,a(dlist)    ; jump and wait for vertical blank
        run start

6. What it saves

Both emulators reported exactly the same counts (NTSC):

Display Loop passes in 120 frames Share of the display-off figure
Display off 168,518 100%
GTIA 9++, 80 × 48 139,442 82.7%
GRAPHICS 9, 80 × 192 118,818 70.5%

An ordinary GRAPHICS 9 screen took 29.5% of the CPU time the loop had with the display off. The 9++ screen took 17.3%. That is a real saving, about 40% of the display's cost, but not the three-quarters you might expect from the DMA alone. The 25 DLIs a frame are the reason: each one waits on WSYNC for the rest of a scan line. The manual mentions a refinement that halves the number of DLIs by writing VSCROL twice at the boundary between pairs; we haven't measured it.

The bigger win is often in the drawing code rather than the display. A full-screen effect that updates 1,920 bytes instead of 7,680 runs four times faster, and that is why plasma, tunnel and 3D demos like the mode.

Troubleshooting

  • The picture is 14 lines too tall at the top, or rolls: VSCROL is wrong when the first line starts. Put a DLI on the blank line before the picture, or set VSCROL to 13 in the vertical blank.
  • Rows are one scan line tall, or the display falls apart in bands: the two VSCROL writes are in the wrong order, too slow, or delayed by other code in the DLI. Keep the DLI exactly as shown, and keep other DLIs and long SEI sections away from this screen.
  • Garbage on part of the screen: the screen data or the display list crosses a 4K (data) or 1K (display list) boundary. The listing aligns the display list with .align $400.
  • Colours instead of shades: GPRIOR isn't $40. Remember the OS copies GPRIOR to the hardware every vertical blank, so set the shadow, not PRIOR.
  • Looks right in one emulator but not another: this is a timing trick. Test in Altirra, whose ANTIC timing is the reference, and on real hardware if you can.

Further reading

  • Avery Lee, Altirra Hardware Reference Manual, chapter 4 (ANTIC): vertical scrolling, "mode lines with unusual height", the GTIA 9++ figure, mapped-mode DMA, and WSYNC timing.
  • On this site: GTIA Graphics Modes 9, 10 and 11, Fine Scrolling on the Atari, Vertical Scrolling with DLIs, Advanced DLI Techniques, ANTIC DMA and Cycle Timing, and the Numen entry in the software section.

Credits