Player Multiplexing: More Sprites from Four Players

Atari screen with a two-line title and six rows of small multicoloured invader-shaped sprites, four per row, at different horizontal positions

The Atari has four players and four missiles, but games such as Atari's Galaxian cartridge show many more objects than that. The trick is that a player is really a vertical strip the full height of the screen. ANTIC draws whatever bytes are in its memory on each scan line at whatever horizontal position GTIA holds at that moment. Change the position, colour or width part way down the screen and the lower part of the strip becomes a different, independent-looking object. A DLI used this way works like a pair of scissors, cutting the strip into pieces.

What you'll learn: fixed-band multiplexing with a DLI and data tables (a complete 24-sprite program), the timing limits at band edges, how general multiplexers sort objects by Y, flicker multiplexing for when objects share lines, horizontal reuse, and keeping collision detection working.

What you need: MADS, an emulator, and the basics from Player/Missile Graphics in Assembly and Display List Interrupts.

1. The rule that limits everything

A player can be reused vertically, never on the same scan line (with one exotic exception, below). Two objects can share a player only if there is a gap between the bottom of one and the top of the next, large enough for the CPU to change the registers in between. Everything in this guide is a way of meeting that rule:

  • Fixed bands: divide the screen into horizontal zones, give each zone its own set of four players, and keep each object inside its zone. Simple and flicker-free; ideal for games whose enemies move in rows (Galaxian, Space Invaders-style games, lane-based games such as Frogger clones).
  • Sorted multiplexing: objects move freely; each frame they are sorted by Y and players are handed out top to bottom.
  • Flicker: when more objects than players share some lines, show different subsets on alternate frames.

2. Fixed bands with one DLI routine

This program divides a text screen into six bands of 32 scan lines. Each band contains a copy of the same 8-line shape in all four players, and a DLI at the top of every band loads that band's four horizontal positions and colours from tables. A VBI moves all 24 objects and resets the band index:

; 24 sprites from 4 players: band multiplexing - mads mplex.asm -o:mplex.xex
VDSLST = $0200
SDMCTL = $022F
SDLSTL = $0230
GPRIOR = $026F
HPOSP0 = $D000
COLPM0 = $D012
GRACTL = $D01D
NMIEN  = $D40E
PMBASE = $D407
SETVBV = $E45C
SYSVBV = $E45F
PM     = $3000              ; 2K-aligned P/M area, single-line resolution
BANDS  = 6
idx    = $80                ; table offset of the next band (band*4)
ptr    = $81
band   = $83

        org $2000
main    lda #0                  ; clear the P/M area
        sta ptr
        lda #>PM
        sta ptr+1
        ldx #8
        ldy #0
        tya
clr     sta (ptr),y
        iny
        bne clr
        inc ptr+1
        dex
        bne clr
        lda #0                  ; draw a shape into every player in every band
        sta band
dband   lda band                ; Y = 32 + band*32 + 12 (clear of the band edges)
        asl @
        asl @
        asl @
        asl @
        asl @
        clc
        adc #32+12
        tay
        ldx #0
dshape  lda shape,x
        sta PM+$400,y           ; player 0
        sta PM+$500,y           ; player 1
        sta PM+$600,y           ; player 2
        sta PM+$700,y           ; player 3
        iny
        inx
        cpx #8
        bne dshape
        inc band
        lda band
        cmp #BANDS
        bne dband
        lda #>PM
        sta PMBASE
        lda #3
        sta GRACTL              ; players and missiles on
        lda #1
        sta GPRIOR              ; players in front of the playfield
        lda #<dlist
        sta SDLSTL
        lda #>dlist
        sta SDLSTL+1
        lda #$3E                ; normal playfield, P/M DMA, single-line
        sta SDMCTL
        lda #6
        ldy #<vbi
        ldx #>vbi
        jsr SETVBV
        lda #<dli
        sta VDSLST
        lda #>dli
        sta VDSLST+1
        lda #$C0
        sta NMIEN
        jmp *

; DLI: reposition and recolour all four players for the next band
dli     pha
        txa
        pha
        ldx idx
        lda xpos,x
        sta HPOSP0
        lda xpos+1,x
        sta HPOSP0+1
        lda xpos+2,x
        sta HPOSP0+2
        lda xpos+3,x
        sta HPOSP0+3
        lda colour,x
        sta COLPM0
        lda colour+1,x
        sta COLPM0+1
        lda colour+2,x
        sta COLPM0+2
        lda colour+3,x
        sta COLPM0+3
        txa
        clc
        adc #4
        sta idx
        pla
        tax
        pla
        rti

; VBI: move all 24 objects, then reset the band index
vbi     ldx #BANDS*4-1
move    lda xpos,x
        clc
        adc dx,x
        sta xpos,x
        cmp #48                 ; bounce off the playfield edges
        bcc flip
        cmp #200
        bcc next
flip    lda #0
        sec
        sbc dx,x
        sta dx,x
next    dex
        bpl move
        lda #0
        sta idx
        jmp SYSVBV

shape   dta $18,$3C,$7E,$DB,$FF,$24,$5A,$A5
xpos    dta  60, 90,120,150,  70,100,130,160,  55, 85,115,145
        dta  65, 95,125,155,  75,105,135,165,  58, 88,118,148
dx      dta   1,$FF,  2,$FE, $FF,  1,$FE,  2,   2,$FE,  1,$FF
        dta $FE,  2,$FF,  1,   1,  2,$FF,$FE, $FF,$FE,  2,  1
colour  dta $36,$C6,$1A,$86,  $46,$D6,$2A,$96,  $56,$E6,$3A,$A6
        dta $66,$F6,$4A,$B6,  $76,$16,$5A,$C6,  $86,$26,$6A,$D6

dlist   dta $70,$70,$F0         ; DLI on the last blank line: band 0
        dta $42,a(screen)
        dta $02,$02,$82         ; text rows 0-3 = band 0, DLI -> band 1
        dta $02,$02,$02,$82     ; band 1
        dta $02,$02,$02,$82     ; band 2
        dta $02,$02,$02,$82     ; band 3
        dta $02,$02,$02,$82     ; band 4
        dta $02,$02,$02,$02     ; band 5
        dta $41,a(dlist)

screen  dta d'  PLAYER MULTIPLEXING: 4 PLAYERS,       '
        dta d'  6 BANDS, 24 MOVING OBJECTS            '
        :22 dta d'                                        '
        run main

Key details:

  • Player memory holds all the images. The shapes are written once into each band's area of the player strips (single-line resolution: player n at PMBASE + $400 + $100·n, with the standard playfield starting at byte 32). An object that moves vertically within its band would be redrawn there by the VBI.
  • The first DLI sits on the last blank line before the display ($F0), so even band 0 gets its positions from the table.
  • No WSYNC. The shapes start 12 lines into each band, so it doesn't matter exactly when on the DLI's line the stores land; they will all be done long before the next shape is drawn. This "buffer zone" is the most important design rule for multiplexers: it turns a cycle-exact problem into an easy one.
  • The DLI only copies. All movement, bouncing and state lives in the VBI; the DLI just indexes tables with idx, which advances by four per band.

Adding sizes (SIZEP0–3) or different shapes per band is simply more tables. Missiles can be multiplexed the same way through HPOSM0–3.

3. What goes wrong at band edges

Without a buffer zone, timing gets tight. GTIA uses a new horizontal position only if the write lands about five colour clocks before the beam reaches the new position, and the old position stops triggering immediately. A player moved late can be clipped, can disappear for a line, or can appear in both places on the same line. ANTIC mode 4 is the worst case, because character lines steal so many CPU cycles. In a mode 4 test with a WSYNC and a colour change first, three player positions were set in time but the fourth was a line late, and size changes all slipped to the second line. The conclusion is to leave several scan lines between objects that share a player.

Other limits to plan for:

  • Width, priority and colour are as reusable as position: a DLI can change SIZEPn, COLPMn and even PRIOR per band.
  • Vertical movement across bands isn't possible with fixed bands. An object leaving its band would be cut in two by the next DLI. Clamp each object to its band, or hand it over to the next band's table when it is fully inside it.
  • Erase only within the band when redrawing an object vertically, or you'll damage the neighbouring band's object in the same player.

4. Free movement: sorting by Y

For objects that move anywhere, the usual design is:

  1. Every frame (in the VBI), sort the objects by Y. Last frame's order is a good starting point, because objects move only a little each frame, so an insertion sort finishes in almost one pass.
  2. Hand out players from the top down. Track where each player's current object ends. Give the next object the first player whose object ends at least a few lines above this one's top (the buffer zone again). If no player is free, the object can't be shown this frame.
  3. Build a change list: for each assignment, the scan line (or mode line) where that player must be repositioned, with its new X, colour and size.
  4. Let DLIs (or a kernel) play the list. With a DLI on every mode line, each one applies the changes due in the next row. Finer placement needs a kernel or POKEY timer IRQs, which some modern engines use to split at any scan line.

Here is the sort step in 6502 code. It reorders an order array of object numbers by their ypos, keeping objects with equal Y in their previous order. It was tested with eight sample objects:

; --- snippet: insertion sort of object numbers by Y (top first)
; order[] keeps last frame's result, so it is nearly sorted already
sortobj lda #1
        sta i
sloop   ldx i                   ; X = the "hole" being filled
        ldy order,x
        sty key                 ; object being inserted
        lda ypos,y
        sta keyy
sshift  ldy order-1,x           ; object in the slot above the hole
        lda ypos,y
        cmp keyy
        bcc place               ; above it is higher up: stop
        beq place               ; same Y: keep the old order
        tya
        sta order,x             ; move that object down one slot
        dex
        bne sshift              ; X = 0: reached the top
place   lda key
        sta order,x
        inc i
        lda i
        cmp #NOBJ
        bne sloop
        rts

How many objects you can handle depends mostly on CPU time: sorting, drawing each object into player memory and building the list all happen in vertical blank and the main loop. Fast engines go to great lengths here. One well-known engine, shanti77's, draws sprites with long unrolled runs of immediate loads and indexed stores rather than copy loops; another programmer found the speed came at a cost of six to eight times the memory per sprite. A flicker-free multiplexer is usually designed around a particular game's constraints rather than as a general library, as experienced coders have pointed out on AtariAge.

5. Flicker multiplexing

When more than four objects share the same lines, there is no way around time-sharing. Show half of the objects on even frames and the other half on odd frames:

; --- snippet: 8 objects on 4 players, alternate halves each frame
flick   lda RTCLOK+2            ; frame counter: even or odd?
        and #1
        asl @
        asl @
        tay                     ; Y = 0 or 4: first object this frame
        ldx #0                  ; X = player 0..3
fl      lda objx,y
        sta HPOSP0,x
        lda objcol,y
        sta PCOLR0,x            ; shadow: copied to COLPM0-3 by the OS
        jsr drawobj             ; copy object Y's image into player X
        iny
        inx
        cpx #4
        bne fl
        rts

Each object is then visible 30 times a second on NTSC (25 on PAL). It flickers, but stays readable. Better variations rotate priorities, so the object that was dropped last frame is drawn first this frame, and flicker only the bands that are actually overcrowded.

6. Reusing a player on the same line

A player triggers whenever the beam reaches its horizontal position, so moving it right again after it has started drawing makes it appear a second time on the same scan line. Timed HPOSP3 writes can draw player 3 up to four times per line. It works only with cycle-counted code, it costs the CPU every line it is used on, and text modes leave too few cycles on their first scan line. Treat it as a demo effect rather than a game technique.

7. Collisions with reused players

Collision registers latch for the whole frame, so with multiplexing a bit in P0PF only says that some incarnation of player 0 hit something. To know which, read the collision registers in each band's DLI, store them in a per-band table, and write HITCLR so the next band starts clean. The VBI then checks the table. This keeps hardware collision detection usable even when players are reused. See Collision Detection in Depth for the registers.

Troubleshooting

  • An object is cut in half or shows a stray line at a band boundary: it is too close to the edge; keep a buffer of several scan lines.
  • All bands show the same positions: the DLI writes OS shadows (HPOS has none; colours have PCOLR0–3 at 704–707) or the index isn't advancing.
  • Bands drift down one per frame: the index isn't reset in the VBI.
  • Objects jitter or vanish occasionally: the VBI's work is too long and spills into the display, so tables change while DLIs are reading them. Move work to the main loop and double-buffer the tables.
  • Glitches during disk I/O: NMIs can be missed while SIO is running.

Further reading

  • De Re Atari (Atari, 1982), Chapter 5: DLIs used to reposition players and the kernel alternative.
  • Rob McMullen's "Atari 8-bit Display List Interrupts: A Complete(ish) Tutorial" (Player/Missile Podcast), Topic 4: multiplexing vertically, with horizontal motion and horizontally.
  • Atari Graphics and Arcade Game Design by Jeffrey Stanton and Dan Pinal (Arrays, 1984), Chapter 6.
  • Altirra Hardware Reference Manual, sections 6.5 and 6.10: player triggering and mid-line timing.
  • shanti77's Sprite Splitter Demo Puts Eight Three-Colour Sprites on Screen (on this site): a compact modern DLI sprite multiplexer.

Credits