ASM Joypad
From GbdevWiki
Revision as of 09:09, 6 July 2019 by PinoBatch (Talk | contribs) (It's more propagation delay than bouncing)
Reads the status of the joypad, saving it in a (byte) variable "_PAD", uses the definitions of gbhw.inc:
read_pad: ; init ld a, 0 ld [_PAD], a ; read the d-pad: ld a, %00100000 ; bit 4 = 0, bit 5 = 1 (d-pad on, buttons off) ld [rP1], a ; read the status of the d-pad ; the first first few reads waste a few cycles to let ; the signals settle after bits 5 and 4 have changed ld a, [rP1] ld a, [rP1] ld a, [rP1] ld a, [rP1] ; keep this read and $0F ; just keep low nibble (d-pad status) swap a ; swap low and high nibbles ld b, a ; save d-pad status in b register ; let's read the buttons ld a, %00010000 ; bit 4 = 1, bit 5 = 0 (buttons on, d-pad off) ld [rP1], a ; signal propagation... ld a, [rP1] ld a, [rP1] ld a, [rP1] ld a, [rP1] ld a, [rP1] ld a, [rP1] ; we have in "a" the status of the buttons and $0F ; just keep low nibble. or b ; OR with "b" to put in the high byte of "a" ; the d-pad status. ; now we have in "a" the status of all the controls ; make the complement of it (so activated buttons read as 1) ; and save it cpl ld [_PAD], a ; reset the joypad ld a,$30 ld [rP1], a ; return ret