Skip to content

Joystick Reading

Read Amiga joystick using the JOY1DAT register with XOR decoding for up/down directions.

Taught in Game 1, Unit 2 joystickinputcustom-chips

Overview

Read joystick input from the custom chip register JOY1DAT. The Amiga uses quadrature encoding, requiring an XOR operation to decode vertical movement. Horizontal directions are read directly from specific bits. This pattern provides smooth, continuous movement.

Code

; =============================================================================
; JOYSTICK READING - AMIGA
; Read joystick port 2 via JOY1DAT
; Taught: Game 1 (Signal), Unit 2
; CPU: ~20 cycles | Memory: ~30 bytes
; =============================================================================

CUSTOM      equ $dff000
JOY1DAT     equ $00c            ; Joystick port 2 data (offset from CUSTOM)

; Read joystick directions
; Input:  A5 = CUSTOM chip base
; Output: D0 = decoded directions
;         Bit 8 = up, Bit 0 = down
;         Bit 9 = left, Bit 1 = right
read_joystick:
            move.w  JOY1DAT(a5),d0      ; Read joystick data
            move.w  d0,d1
            lsr.w   #1,d1               ; Shift for XOR decode
            eor.w   d1,d0               ; Decode vertical movement
            rts

Usage:

            lea     CUSTOM,a5           ; Set up custom chip base

mainloop:
            bsr     wait_vblank
            bsr     read_joystick       ; D0 = directions

            ; Check directions using BTST
            btst    #8,d0               ; Up?
            beq.s   .no_up
            sub.w   #MOVE_SPEED,player_y
.no_up:
            btst    #0,d0               ; Down?
            beq.s   .no_down
            add.w   #MOVE_SPEED,player_y
.no_down:
            btst    #9,d0               ; Left?
            beq.s   .no_left
            sub.w   #MOVE_SPEED,player_x
.no_left:
            btst    #1,d0               ; Right?
            beq.s   .no_right
            add.w   #MOVE_SPEED,player_x
.no_right:
            bra     mainloop

Trade-offs

AspectCost
CPU~20 cycles
Memory~30 bytes
LimitationNo edge detection (continuous input)

When to use: Games with smooth, continuous movement (shooters, racing).

When to avoid: Grid-based games where you want single-step movement per press.

How It Works

The Amiga joystick uses quadrature encoding (like a mouse). The JOY1DAT register returns:

  • Bits 1 and 9: Directly indicate right and left
  • Bits 0 and 8: Require XOR decoding for down and up

The XOR decode formula:

decoded = raw XOR (raw >> 1)

This converts the quadrature signals into simple direction flags.

Direction Bit Reference

BitDirectionCheck
8Upbtst #8,d0
0Downbtst #0,d0
9Leftbtst #9,d0
1Rightbtst #1,d0

Fire Button

The fire button is read from the CIA, not the custom chips:

            btst    #7,$bfe001          ; Fire button (active low)
            beq.s   fire_pressed        ; Branch if pressed

Patterns: VBlank Game Loop

Vault: Commodore Amiga