aboutsummaryrefslogtreecommitdiffstats
path: root/src/joypad.asm
diff options
context:
space:
mode:
authoruser <user@clank>2026-07-16 16:07:01 +0200
committeruser <user@clank>2026-07-16 16:07:01 +0200
commita5557c75767f7201d5df8c6f95c162044072ef10 (patch)
treeed4ddd29ba3e6411b1b20f41fcbf8f67c023c93e /src/joypad.asm
parentsyscall: mirror console output to the LCD terminal (diff)
downloadgbos-a5557c75767f7201d5df8c6f95c162044072ef10.tar.gz
gbos-a5557c75767f7201d5df8c6f95c162044072ef10.tar.xz
gbos-a5557c75767f7201d5df8c6f95c162044072ef10.zip
osk: toggle-able on-screen keyboard on the window layer
A joypad-driven keyboard that costs zero permanent screen space: it lives on the GB window layer, so SELECT just flips LCDC bit 5 (window enable) and the terminal's background layer underneath is never disturbed. - src/joypad.asm: read $FF00 with edge detection (wPadCur/Prev/New). - src/osk.asm: 3x20 key grid (letters, digits, space, punctuation, plus Enter/Backspace) built once into spare bank-1 VRAM tiles (104+) and laid out on the $9C00 window map, docked to the bottom 3 rows. The highlighted key is a palette swap on its window attribute byte (CGB BG palette 1 = inverted), so moving the cursor is 1-2 attribute writes with no tile rebuilding. SELECT toggles, d-pad moves, A types. - KGetc's console poll loop now polls the joypad and osk_handle each iteration; a key press returns its byte to the reader exactly like a serial byte, so the shell is oblivious to the input source. Verified in the emulator (via --keys): SELECT shows the keyboard, the highlight tracks the d-pad, and typing "ls"+Enter runs the command and lists the files; a second SELECT hides it and reclaims the full 18 rows. Known limitation: the OSK overlays the bottom 3 terminal rows, so if the prompt has scrolled to the very bottom the current input line can be hidden. A follow-up can add a scroll region (terminal uses rows 0-14 while the OSK is up). Input over serial still works unchanged.
Diffstat (limited to '')
-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