ASM Joypad
From GbdevWiki
					Revision as of 14:40, 8 December 2012 by Xzakox (Talk | contribs) (Created page with 'Reads the status of the joypad, saving it in a (byte) variable "_PAD", uses the definitions of gbhw.inc:  <pre> read_pad:     ; init     ld      a, 0     ld      [_PAD], a      ;…')
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
    ; we do several reads because of bouncing
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]
    ld      a, [rP1]
    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
    ; bouncing...
    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
