gameboy/game/controller.asm

67 lines
1.6 KiB
NASM

SECTION "ram_controller", WRAM0
controller_curkeys: ds 1
controller_newkeys: ds 1
SECTION "Controller", ROM0
InitController:
ld a, 0
ld [controller_curkeys], a
ld [controller_newkeys], a
ret
; Controller reading ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This controller reading routine is optimized for size.
; It stores currently pressed keys in controller_curkeys (1=pressed) and
; keys newly pressed since last read in controller_newkeys, with the same
; nibble ordering as the Game Boy Advance.
; 76543210
; |||||||+- A
; ||||||+-- B
; |||||+--- Select
; ||||+---- Start
; |||+----- Right
; ||+------ Left
; |+------- Up
; +-------- Down
; R
; L (just kidding)
UpdateController:
; Poll half the controller
ld a,P1F_4
call .onenibble
ld b,a ; B7-4 = 1; B3-0 = unpressed buttons
; Poll the other half
ld a,P1F_5
call .onenibble
swap a ; A3-0 = unpressed directions; A7-4 = 1
xor b ; A = pressed buttons + directions
ld b,a ; B = pressed buttons + directions
; And release the controller
ld a,P1F_N
ld [rP1],a
; Combine with previous controller_curkeys to make controller_newkeys
ld a, [controller_curkeys]
xor b ; A = keys that changed state
and b ; A = keys that changed to pressed
ld [controller_newkeys], a
ld a,b
ld [controller_curkeys], a
ret
.onenibble:
ldh [rP1],a ; switch the key matrix
ldh a,[rP1] ; ignore value while waiting for the key matrix to settle
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1]
ldh a,[rP1] ; the actual read
or $F0 ; A7-4 = 1; A3-0 = unpressed keys
ret