Player/Missile Graphics in Assembly: Sprites, Missiles and Collisions

In BASIC, player/missile graphics are held back by the speed of the interpreter: copying eight bytes to move a sprite one line takes visible time. In assembly language the same work takes microseconds, and by doing it inside the vertical blank interrupt (VBI) you get perfectly smooth, flicker-free motion at 60 frames per second (50 on PAL) while your main program does something else entirely.

What you'll learn: how to set up single-line P/M graphics in machine code, run sprite movement from a deferred VBI, fire a missile, and use the hardware collision registers — in one complete, assemblable program.

What you need: the MADS cross-assembler (or adapt the syntax for ATasm/MAC/65), Altirra or atari800 to run the resulting XEX, and a joystick. The BASIC companion guide explains the memory layout and registers in more detail.

The plan

  • P/M block at $3000 (a 2K boundary, required for single-line resolution). Missiles at $3300, player 0 at $3400, player 1 at $3500 — the standard $300/$400 single-line offsets.
  • Player 0 is the ship, moved by the joystick. Player 1 is a stationary target.
  • Pressing the trigger fires missile 0 upward. When the missile touches player 1, the target changes colour.
  • All movement happens in a deferred VBI installed with the OS routine SETVBV.

Register summary

Label Address Notes
HPOSP0/HPOSP1 $D000/$D001 write: player horizontal positions (no shadows)
HPOSM0 $D004 write: missile 0 horizontal position
M0PL $D008 read: missile 0 to player collisions (bit n = player n)
SIZEP0/SIZEP1/SIZEM $D008/$D009/$D00C write: widths
GRACTL $D01D 3 = players + missiles on
HITCLR $D01E write anything: clear all collisions
RANDOM $D20A read: POKEY random number
PMBASE $D407 high byte of the P/M block
SDMCTL $022F shadow of DMACTL; $3E = single-line PMG + normal screen
GPRIOR $026F shadow of PRIOR; 1 = players over playfield
PCOLR0/PCOLR1 $02C0/$02C1 player colour shadows
STICK0 $0278 joystick 0 (OS shadow, updated in VBI)
STRIG0 $0284 trigger 0, 0 = pressed

Note how $D008 is SIZEP0 when written and M0PL when read — GTIA reuses addresses heavily.

The program

; pmdemo.asm - assemble with: mads pmdemo.asm -o:pmdemo.xex
HPOSP0  = $D000
HPOSP1  = $D001
HPOSM0  = $D004
M0PL    = $D008         ; read
SIZEP0  = $D008         ; write
SIZEP1  = $D009
SIZEM   = $D00C
GRACTL  = $D01D
HITCLR  = $D01E
RANDOM  = $D20A
PMBASE  = $D407
SDMCTL  = $022F
GPRIOR  = $026F
PCOLR0  = $02C0
PCOLR1  = $02C1
STICK0  = $0278
STRIG0  = $0284
SETVBV  = $E45C
XITVBV  = $E462

PMG      = $3000        ; 2K aligned
MISSILES = PMG+$300
PLAYER0  = PMG+$400
PLAYER1  = PMG+$500

        org $2000

main    lda #>PMG
        sta PMBASE
        lda #0                  ; clear missiles, P0 and P1
        tax
clr     sta MISSILES,x
        sta PLAYER0,x
        sta PLAYER1,x
        inx
        bne clr

        ldx #7                  ; draw the target into player 1
tgt     lda target,x
        sta PLAYER1+60,x
        dex
        bpl tgt

        lda #120
        sta HPOSP1
        lda #$46                ; red target
        sta PCOLR1
        lda #$1C                ; light gold ship
        sta PCOLR0
        lda #0
        sta SIZEP0
        sta SIZEP1
        sta SIZEM
        lda #1
        sta GPRIOR
        lda #$3E                ; 2+4+8+16+32
        sta SDMCTL
        lda #3
        sta GRACTL

        lda #7                  ; 7 = deferred VBI
        ldy #<vbi
        ldx #>vbi
        jsr SETVBV
forever jmp forever             ; main program is free for other work

; ---------------- vertical blank routine ----------------
vbi     lda M0PL                ; did missile 0 hit player 1 last frame?
        and #$02
        beq nohit
        lda RANDOM              ; new random hue, luminance 6
        and #$F0
        ora #$06
        sta PCOLR1
        jsr merase
        lda #0
        sta mact
nohit   sta HITCLR              ; clear for the coming frame

        lda STICK0              ; bits 0-3 = up, down, left, right (0 = pushed)
        lsr
        bcs notup
        dec ypos
notup   lsr
        bcs notdn
        inc ypos
notdn   lsr
        bcs notlf
        dec xpos
notlf   lsr
        bcs notrt
        inc xpos
notrt
        lda ypos                ; keep ypos in 32..206
        cmp #32
        bcs y1
        lda #32
y1      cmp #207
        bcc y2
        lda #206
y2      sta ypos
        lda xpos                ; keep xpos in 48..200
        cmp #48
        bcs x1
        lda #48
x1      cmp #201
        bcc x2
        lda #200
x2      sta xpos
        sta HPOSP0

        ldx ypos                ; draw ship (10 bytes incl. blank top/bottom)
        ldy #0
draw    lda ship,y
        sta PLAYER0,x
        inx
        iny
        cpy #10
        bne draw

        lda mact                ; missile logic
        bne mmove
        lda STRIG0
        bne mdone               ; not pressed
        lda xpos
        clc
        adc #3                  ; centre of the 8-pixel ship
        sta mx
        lda ypos
        sta my
        lda #1
        sta mact
        bne mdraw               ; always taken
mmove   jsr merase
        lda my
        sec
        sbc #4                  ; 4 scan lines per frame
        sta my
        cmp #32
        bcs mdraw
        lda #0                  ; off the top: deactivate
        sta mact
        beq mdone               ; always taken
mdraw   ldx my
        ldy #4
md1     lda MISSILES,x
        ora #%00000011          ; missile 0 = bits 0-1
        sta MISSILES,x
        inx
        dey
        bne md1
        lda mx
        sta HPOSM0
mdone   jmp XITVBV              ; deferred VBIs exit here

merase  ldx my                  ; clear missile 0 bits for 4 rows
        ldy #4
me1     lda MISSILES,x
        and #%11111100
        sta MISSILES,x
        inx
        dey
        bne me1
        rts

; ---------------- data ----------------
xpos    dta 120
ypos    dta 180
mx      dta 0
my      dta 0
mact    dta 0
ship    dta $00,$18,$3C,$7E,$DB,$FF,$24,$5A,$A5,$00
target  dta $3C,$7E,$FF,$DB,$FF,$7E,$24,$42

        run main

Walking through it

Setup. PMBASE takes only the high byte ($30). Clearing 256 bytes each at $3300, $3400 and $3500 removes any garbage left in RAM. SDMCTL is the shadow of ANTIC's DMACTL: $3E = 62 = normal playfield (2) + missile DMA (4) + player DMA (8) + single-line resolution (16) + display-list DMA (32). GRACTL = 3 tells GTIA to accept player and missile data. Both must be set — ANTIC fetches the data and GTIA displays it, so each side has its own switch.

Installing the VBI. SETVBV ($E45C) takes A = 7 for the deferred vector (6 would be immediate), Y = low byte and X = high byte, and changes the vector safely even though a VBI could occur at any moment. A deferred VBI must end with JMP XITVBV ($E462).

Collisions first. A collision bit is set only when the beam actually draws the overlap, so collisions should be checked at the start of the VBI (they describe the frame just shown), then cleared with HITCLR, and only then should objects be moved. That is exactly the order used here. M0PL bit 1 means "missile 0 touched player 1".

Joystick. The OS copies the hardware joystick bits into STICK0 ($278) every VBI. Each direction is a bit that reads 0 when pushed, so four LSRs shift them into the carry one at a time: up, down, left, right. Diagonals naturally set two bits.

Clamping. In single-line resolution the visible rows run from about 32 to 223, and the visible horizontal range of a normal-width screen is roughly 48–208. The ship is 10 bytes tall, so ypos is kept between 32 and 206.

Drawing without erasing. The ship shape has a blank byte at the top and bottom. Since the ship moves at most one row per frame, redrawing it at the new position automatically overwrites the row it just left. This is a standard trick that saves a separate erase pass.

Missiles. All four missiles share one table, two bits each, so missile code must use AND/ORA masks to avoid disturbing the others — the main inconvenience of missiles. Here missile 0 is four rows tall, moves up four rows per frame and is erased before each move.

Colour change. RANDOM ($D20A) is POKEY's free-running random number generator; AND #$F0 keeps a random hue, ORA #$06 sets a medium luminance.

Extending the demo

  • Wider sprites: store 1 (double) or 3 (quadruple) into SIZEP0. Objects grow to the right from their position.
  • Multi-colour sprites: overlay players 0 and 1 at the same position and set GPRIOR bit 5 (add 32); overlapping pixels take the OR of the two colours.
  • A fifth player: set GPRIOR bit 4 (add 16) and the four missiles take colour from COLOR3 (711) instead of their players' colours, while still moving independently.
  • Reusing players down the screen: a display list interrupt can change HPOSP0 and PCOLR's hardware register (COLPM0, $D012) partway down the screen, so one player can appear several times at different heights — in effect, the DLI acts as a pair of scissors that cuts the player column into pieces.
  • Animation: keep several 10-byte frames and pick one per VBI.

Troubleshooting

  • Nothing on screen: check that SDMCTL and GRACTL are both set, that PMBASE is 2K aligned for single-line mode (use 1K alignment and $3E-16 = $2E for double-line), and that positions are inside the visible range.
  • The ship leaves a trail when moving fast: increase the blank padding if you move more than one row per frame, or erase explicitly.
  • Program crashes on start: the VBI must end with JMP XITVBV, never RTS or RTI. Make sure SETVBV is called with the right A value.
  • Collision never registers: M0PL bits are numbered by player (bit 0 = player 0, bit 1 = player 1 ...). Missiles fired from inside the ship will register a hit on player 0 — that's why only bit 1 is tested.
  • Stripes remain after exit: set GRACTL to 0 and clear the graphics latches GRAFP0–GRAFP3/GRAFM ($D00D–$D011), or move the objects to horizontal position 0.

Further reading

  • De Re Atari (Atari, 1982), Chapters 4 and 8.
  • Mapping the Atari by Ian Chadwick (COMPUTE! Books), GTIA section starting at 53248.
  • Atari Roots: A Guide to Atari Assembly Language by Mark Andrews (Datamost, 1984) — includes player-missile and character animation chapters.
  • Atari Graphics and Arcade Game Design by Jeffrey Stanton and Dan Pinal (Arrays, 1984).
  • Altirra Hardware Reference Manual — P/M DMA timing, VDELAY, size-change quirks.

Credits