dmgtris/src/input.asm

221 lines
3.6 KiB
NASM
Raw Normal View History

2023-10-13 09:20:28 +00:00
IF !DEF(INPUT_ASM)
DEF INPUT_ASM EQU 1
2023-10-16 05:47:11 +00:00
INCLUDE "globals.asm"
2023-10-13 09:20:28 +00:00
SECTION "Input Variables", HRAM
hUpState:: ds 1
hDownState:: ds 1
hLeftState:: ds 1
hRightState:: ds 1
hAState:: ds 1
hBState:: ds 1
hStartState:: ds 1
hSelectState:: ds 1
2023-10-16 03:45:30 +00:00
hDASCharge:: ds 1
2023-10-13 09:20:28 +00:00
SECTION "Input Functions", ROM0
InputInit::
xor a, a
ldh [hUpState], a
ldh [hDownState], a
ldh [hLeftState], a
ldh [hRightState], a
ldh [hAState], a
ldh [hBState], a
ldh [hStartState], a
ldh [hSelectState], a
2023-10-16 03:45:30 +00:00
ldh [hDASCharge], a
2023-10-13 09:20:28 +00:00
ret
GetInput::
2023-10-16 03:45:30 +00:00
; Check if the left state > DAS charge.
ldh a, [hLeftState]
ld b, a
ldh a, [hDASCharge]
cp a, b
; If so, save the new DAS charge.
jr nc, :+
ld a, b
ldh [hDASCharge], a
; Check if the right state > DAS charge.
: ldh a, [hRightState]
ld b, a
ldh a, [hDASCharge]
cp a, b
; If so, save the new DAS charge.
jr nc, :+
ld a, b
ldh [hDASCharge], a
; There's an overflow risk here if the DAS charge is 255.
: cp a, $FF
jr nz, .btns
dec a
ldh [hDASCharge], a
2023-10-13 09:20:28 +00:00
; Get the button state.
.btns
ld a, P1F_GET_BTN
ldh [rP1], a
ldh a, [rP1]
ldh a, [rP1]
ldh a, [rP1]
ldh a, [rP1]
ld b, a
2023-10-16 03:45:30 +00:00
; Read A button.
2023-10-13 09:20:28 +00:00
.readA
2023-10-16 03:45:30 +00:00
bit 0, b
2023-10-13 09:20:28 +00:00
jr nz, .clearA
.setA
ldh a, [hAState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .readB
inc a
2023-10-13 09:20:28 +00:00
ldh [hAState], a
jr .readB
.clearA
xor a, a
ldh [hAState], a
2023-10-16 03:45:30 +00:00
; Read B button.
2023-10-13 09:20:28 +00:00
.readB
2023-10-16 03:45:30 +00:00
bit 1, b
2023-10-13 09:20:28 +00:00
jr nz, .clearB
.setB
ldh a, [hBState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .readSelect
inc a
2023-10-13 09:20:28 +00:00
ldh [hBState], a
jr .readSelect
.clearB
xor a, a
ldh [hBState], a
2023-10-16 03:45:30 +00:00
; Read select button.
2023-10-13 09:20:28 +00:00
.readSelect
2023-10-16 03:45:30 +00:00
bit 2, b
2023-10-13 09:20:28 +00:00
jr nz, .clearSelect
.setSelect
ldh a, [hSelectState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .readStart
inc a
2023-10-13 09:20:28 +00:00
ldh [hSelectState], a
jr .readStart
.clearSelect
xor a, a
ldh [hSelectState], a
2023-10-16 03:45:30 +00:00
; Read start button.
2023-10-13 09:20:28 +00:00
.readStart
2023-10-16 03:45:30 +00:00
bit 3, b
2023-10-13 09:20:28 +00:00
jr nz, .clearStart
.setStart
ldh a, [hStartState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .dpad
inc a
2023-10-13 09:20:28 +00:00
ldh [hStartState], a
jr .dpad
.clearStart
xor a, a
ldh [hStartState], a
; Get the dpad state.
.dpad
ld a, P1F_GET_DPAD
ldh [rP1], a
ldh a, [rP1]
ldh a, [rP1]
ldh a, [rP1]
ldh a, [rP1]
ld b, a
2023-10-16 03:45:30 +00:00
; Read up button.
2023-10-13 09:20:28 +00:00
.readUp
2023-10-16 03:45:30 +00:00
bit 2, b
2023-10-13 09:20:28 +00:00
jr nz, .clearUp
.setUp
ldh a, [hUpState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .readDown
inc a
2023-10-13 09:20:28 +00:00
ldh [hUpState], a
jr .readDown
.clearUp
xor a, a
ldh [hUpState], a
2023-10-16 03:45:30 +00:00
; Read down button.
2023-10-13 09:20:28 +00:00
.readDown
2023-10-16 03:45:30 +00:00
bit 3, b
2023-10-13 09:20:28 +00:00
jr nz, .clearDown
.setDown
ldh a, [hDownState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .readLeft
inc a
2023-10-13 09:20:28 +00:00
ldh [hDownState], a
jr .readLeft
.clearDown
xor a, a
ldh [hDownState], a
2023-10-16 03:45:30 +00:00
; Read left button. If it's just been pressed, restore the held DAS charge.
2023-10-13 09:20:28 +00:00
.readLeft
2023-10-16 03:45:30 +00:00
bit 1, b
2023-10-13 09:20:28 +00:00
jr nz, .clearLeft
.setLeft
ldh a, [hLeftState]
2023-10-16 03:45:30 +00:00
cp a, $FF
2023-10-13 09:20:28 +00:00
jr z, .readRight
2023-10-16 03:45:30 +00:00
cp a, 0
jr nz, :+
ldh a, [hDASCharge]
: inc a
2023-10-13 09:20:28 +00:00
ldh [hLeftState], a
jr .readRight
.clearLeft
xor a, a
ldh [hLeftState], a
2023-10-16 03:45:30 +00:00
; Read right button. If it's just been pressed, restore the held DAS charge.
2023-10-13 09:20:28 +00:00
.readRight
2023-10-16 03:45:30 +00:00
bit 0, b
2023-10-13 09:20:28 +00:00
jr nz, .clearRight
.setRight
ldh a, [hRightState]
2023-10-16 03:45:30 +00:00
cp a, $FF
jr z, .checkDAS
cp a, 0
jr nz, :+
ldh a, [hDASCharge]
: inc a
2023-10-13 09:20:28 +00:00
ldh [hRightState], a
2023-10-16 03:45:30 +00:00
jr .checkDAS
2023-10-13 09:20:28 +00:00
.clearRight
xor a, a
ldh [hRightState], a
2023-10-16 03:45:30 +00:00
; If none of the four directions are pressed, reset the DAS charge.
.checkDAS
ld a, b
or a, $F0
cp a, $FF
ret nz
xor a, a
ldh [hDASCharge], a
2023-10-13 09:20:28 +00:00
ret
ENDC