1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
; =============================================================================
; joypad.asm - read the Game Boy joypad ($FF00) with edge detection.
; =============================================================================
INCLUDE "include/gbos.inc"
DEF rP1 EQU $FF00
DEF PAD_A EQU $01
DEF PAD_B EQU $02
DEF PAD_SELECT EQU $04
DEF PAD_START EQU $08
DEF PAD_RIGHT EQU $10
DEF PAD_LEFT EQU $20
DEF PAD_UP EQU $40
DEF PAD_DOWN EQU $80
SECTION "pad_state", WRAM0
wPadCur:: DS 1 ; currently held (1 = pressed)
wPadPrev:: DS 1 ; held on the previous poll
wPadNew:: DS 1 ; newly pressed this poll (cur & ~prev)
SECTION "pad", ROM0
; read_joypad -> A = state, 1=pressed:
; bit0 A, 1 B, 2 Select, 3 Start, 4 Right, 5 Left, 6 Up, 7 Down
read_joypad::
ld a, $20 ; P14=0: select d-pad
ld [rP1], a
ld a, [rP1]
ld a, [rP1] ; let the lines settle
cpl
and $0F ; right/left/up/down (bits 0-3)
swap a ; -> bits 4-7
ld b, a
ld a, $10 ; P15=0: select buttons
ld [rP1], a
ld a, [rP1]
ld a, [rP1]
ld a, [rP1]
ld a, [rP1] ; buttons bounce more; settle longer
cpl
and $0F ; A/B/Select/Start (bits 0-3)
or b
ld c, a
ld a, $30 ; deselect both
ld [rP1], a
ld a, c
ret
; pad_poll - refresh wPadCur/wPadPrev/wPadNew.
pad_poll::
ld a, [wPadCur]
ld [wPadPrev], a
call read_joypad
ld [wPadCur], a
ld b, a
ld a, [wPadPrev]
cpl
and b ; newly pressed edges
ld [wPadNew], a
ret
|