aboutsummaryrefslogtreecommitdiffstats
path: root/src/joypad.asm
diff options
context:
space:
mode:
Diffstat (limited to 'src/joypad.asm')
-rw-r--r--src/joypad.asm61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/joypad.asm b/src/joypad.asm
new file mode 100644
index 0000000..73283ac
--- /dev/null
+++ b/src/joypad.asm
@@ -0,0 +1,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