gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/libc/targets/z80/sms/pad.s
1 .include "global.s"
2
3 .title "JOYPad utilities"
4 .module JOYPad
5 .area _HOME
6
7 ; Get Keypad Button Status
8 ; return result in l
9 ;
10 ;uint8_t joypad(void) OLDCALL PRESERVES_REGS(b, c, d, e, iyh, iyl);
11 _joypad::
12 .jpad::
13 in a, (.JOY_PORT1)
14 cpl
15 and #(.JOY_P1_UP | .JOY_P1_DOWN | .JOY_P1_LEFT | .JOY_P1_RIGHT | .JOY_P1_SW1 | .JOY_P1_SW2)
16 ld l, a
17
18 ld a, #.JOY_TH_LO
19 out (.JOY_CTL), a
20
21 in a, (.JOY_PORT1)
22 ld h, a
23 and #(.JOY_P1_LEFT | .JOY_P1_RIGHT)
24 jp nz, 1$
25
26 ld a, h ; three button controller detected
27 cpl
28 ld h, a
29 and #.JOY_P1_MD_START ; .START = .JOY_P1_MD_START << 1
30 rlca
31 or l
32 ld l, a
33 ld a, h
34 and #.JOY_P1_MD_A ; .SELECT = .JOY_P1_MD_A << 3
35 rlca
36 rlca
37 rlca
38 or l
39 ld l, a
40
41 .rept 3 ; skip 6-button controller keys
42 ld a, #.JOY_TH_HI
43 out (.JOY_CTL), a
44 ld a, #.JOY_TH_LO
45 out (.JOY_CTL), a
46 .endm
47
48 ld a, #.JOY_TH_HI
49 out (.JOY_CTL), a
50 ret
51 1$:
52 ld a, l ; copy buttons 1 and 2 into start and select
53 and #(.JOY_P1_SW1 | .JOY_P1_SW2)
54 rlca
55 rlca
56 or l ; .START = .JOY_P1_SW1, .SELECT = .JOY_P1_SW2
57 ld l, a
58 ret
59
60 ; wait for 1/4 frame for the MD 3 and 6-button controllers
61 do_sleep:
62 push bc
63 ld c, #10
64 0$:
65 ld b, #0xff
66 1$:
67 .rept 4
68 ex (sp), hl
69 .endm
70 djnz 1$
71
72 dec c
73 jr nz, 0$
74 pop bc
75 ret
76
77 ; Wait until all buttons have been released
78 ;
79 ; void waitpadup(void) PRESERVES_REGS(d, e, iyh, iyl);
80 .padup::
81 _waitpadup::
82 1$:
83 call .jpad
84 ld a, l
85 or a ; have all buttons been released?
86 ret z
87 call do_sleep ; wait 1/4 frame
88 jr 1$ ; not yet
89
90 ; Wait for the key to be pressed
91 ;
92 ; uint8_t waitpad(uint8_t mask) Z88DK_FASTCALL PRESERVES_REGS(d, e, iyh, iyl);
93 _waitpad::
94 .wait_pad::
95 ld c, l
96 1$:
97 call .jpad ; read pad
98 ld a, l
99 and c ; compare with mask?
100 ret nz ; return if intersection
101 call do_sleep ; wait 1/4 frame
102 jr 1$
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.