Assembly Language - Joystick Read

From NerdConsole
Jump to navigationJump to search
; Purpose: This routine reads all 12 buttons of both main controllers 
;          and stores the results in the specified results locations 
;          in RAM.
; For: NerdConsole (65C02 Assembly, ca65 format)
; By: David Stephens (NerdOfEpic)
; License: Public Domain - Can be used or modified by anyone 
;                          for any purpose.


; Control Page Registers
JoyLatchClock  = $0200
JoyData        = $0201
; Location of results (Can be moved anywhere you need.)
Joy1Low        = $FC
Joy1High       = $FD
Joy2Low        = $FE
Joy2High       = $FF

ReadControllers:
	; Save state so we can put it back at the end
	php
	pha
	txa
	pha
	tya
	pha
	
	; Setup 
	ldx #$1f
	stz Joy1Low
	stx Joy1High
	stz Joy2Low
	stx Joy2High
	ldx #$01           ; Prep for Latch
	ldy #$02           ; Prep for Clock
	
	; Latch the controllers
	stx JoyLatchClock
	nop                ; waste a little time
	stz JoyLatchClock

ContinueControllerRead:
	; Read data from the controllers
	lda JoyData
	; Save button for controller 1
	lsr A
	rol Joy1High
	rol Joy1Low
	; Save button for controller 2
	lsr A
	rol Joy2High
	rol Joy2Low
			
	; Advance the clock for the controllers
	sty JoyLatchClock
	nop                ; waste a little time
	stz JoyLatchClock
	
	; Are we done?
	bcc ContinueControllerRead
		
ReadControllersDone:
	; Restore the state
	pla
	tay
	pla
	tax
	pla
	plp
	
	; All done
	rts

Back to the list of Assembly Language files.