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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
; =============================================================================
; osk.asm - on-screen keyboard on the WINDOW layer (CGB).
;
; The terminal owns the BG layer ($9800 map, tiles 0-359). The OSK lives on the
; window ($9C00 map) so toggling it is just LCDC bit 5 (window enable) - the
; terminal underneath is never touched. Key glyphs are built once into spare
; bank-1 VRAM tiles (104+). The highlighted key is a palette swap on its window
; attribute byte (palette 1 = inverted), so moving the cursor is 1-2 byte writes
; with no tile rebuilding. SELECT toggles; d-pad moves; A types into KGetc.
; =============================================================================
INCLUDE "include/gbos.inc"
DEF rVBK EQU $FF4F
DEF rBCPS EQU $FF68
DEF rBCPD EQU $FF69
DEF rWY EQU $FF4A
DEF rWX EQU $FF4B
DEF OSK_ROWS EQU 3
DEF OSK_COLS EQU 20
DEF OSK_KEYS EQU OSK_ROWS * OSK_COLS
DEF OSK_TILE0 EQU 104 ; first OSK tile index (bank 1; terminal uses 0-103)
DEF OSK_MAP EQU $9C00 ; window tilemap
SECTION "osk_state", WRAM0
wOskVisible:: DS 1
wOskRow:: DS 1
wOskCol:: DS 1
wOskBuild:: DS 1 ; scratch for the init build loop
SECTION "osk_keys", ROM0
; type values, row-major (OSK_ROWS x OSK_COLS)
osk_keys:
db "abcdefghijklmnopqrst"
db "uvwxyz0123456789 .-/"
db 10, 8, ":;,=+*_!?()[]<>@#~"
SECTION "osk", ROM0
; -----------------------------------------------------------------------------
; osk_disp - A = key type value -> B = left glyph, C = right glyph (2-char label)
; -----------------------------------------------------------------------------
osk_disp:
cp 10
jr nz, .n1
ld b, "C"
ld c, "R"
ret ; enter
.n1
cp 8
jr nz, .n2
ld b, "<"
ld c, "-"
ret ; backspace
.n2
cp " "
jr nz, .n3
ld b, "S"
ld c, "P"
ret ; space
.n3
ld b, a ; regular: char in left cell
ld c, " "
ret
; -----------------------------------------------------------------------------
; osk_init - build key tiles, lay out the window map, set up palette 1. Window
; starts disabled.
; -----------------------------------------------------------------------------
osk_init::
; CGB BG palette 1 = inverted: color0 black, color1 white
ld a, $88 ; BCPS index 8 (palette 1, color 0), auto-inc
ld [rBCPS], a
xor a
ld [rBCPD], a
ld [rBCPD], a ; c0 = $0000 black
ld a, $FF
ld [rBCPD], a
ld a, $7F
ld [rBCPD], a ; c1 = $7FFF white
; build the key tiles (no cursor underline)
xor a
ld [wCurMask], a
ld [wOskBuild], a
.build
ld a, [wOskBuild]
ld e, a
ld d, 0
ld hl, osk_keys
add hl, de
ld a, [hl]
call osk_disp ; B=left, C=right
ld a, [wOskBuild]
add OSK_TILE0
ld e, a
ld d, 1 ; VRAM bank 1
call build_tile
ld a, [wOskBuild]
inc a
ld [wOskBuild], a
cp OSK_KEYS
jr c, .build
xor a
ld [rVBK], a
; window map indices (bank 0): [r,c] = OSK_TILE0 + r*20 + c
ld c, 0
.i_row
ld a, c
call .rowbase ; A = OSK_TILE0 + row*20 (clobbers HL/DE)
ld e, a
call .maprow ; HL = OSK_MAP + row*32 (preserves E)
ld b, OSK_COLS
.i_col
ld a, e
ld [hl+], a
inc e
dec b
jr nz, .i_col
inc c
ld a, c
cp OSK_ROWS
jr c, .i_row
; window map attributes (bank 1): $08 = bank-1 tile, palette 0
ld a, 1
ld [rVBK], a
ld c, 0
.a_row
call .maprow
ld b, OSK_COLS
.a_col
ld a, $08
ld [hl+], a
dec b
jr nz, .a_col
inc c
ld a, c
cp OSK_ROWS
jr c, .a_row
xor a
ld [rVBK], a
; window position: 3 rows at the bottom (screen rows 15-17)
ld a, 7
ld [rWX], a
ld a, 120
ld [rWY], a
; select window map $9C00 (LCDC bit 6); leave window disabled (bit 5 = 0)
ld a, [rLCDC]
or %01000000
ld [rLCDC], a
xor a
ld [wOskVisible], a
ld [wOskRow], a
ld [wOskCol], a
call osk_highlight
ret
; HL = OSK_MAP + C*32
.maprow
ld a, c
ld h, 0
ld l, a
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
ld a, h
add HIGH(OSK_MAP)
ld h, a
ret
; A(row) -> A = OSK_TILE0 + row*20
.rowbase
ld h, 0
ld l, a
add hl, hl
add hl, hl ; *4
ld d, h
ld e, l
add hl, hl
add hl, hl ; *16
add hl, de ; *20
ld a, l
add OSK_TILE0
ret
; -----------------------------------------------------------------------------
; osk_set_pal - write palette A (0 or 1) into the current (wOskRow,wOskCol)
; window attribute.
; -----------------------------------------------------------------------------
osk_set_pal:
or $08 ; bank-1 bit
ld e, a ; save attr byte
ld a, 1
ld [rVBK], a
ld a, [wOskRow]
ld h, 0
ld l, a
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl ; row*32
ld a, h
add HIGH(OSK_MAP)
ld h, a
ld a, [wOskCol]
add l
ld l, a
ld a, h
adc 0
ld h, a
ld a, e
ld [hl], a
xor a
ld [rVBK], a
ret
osk_highlight:
ld a, 1
jr osk_set_pal
osk_unhighlight:
xor a
jr osk_set_pal
; -----------------------------------------------------------------------------
; osk_toggle - show/hide the keyboard (window enable, LCDC bit 5).
; -----------------------------------------------------------------------------
osk_toggle::
ld a, [wOskVisible]
xor 1
ld [wOskVisible], a
or a
jr z, .hide
ld a, [rLCDC]
or %00100000
ld [rLCDC], a
ret
.hide
ld a, [rLCDC]
and %11011111
ld [rLCDC], a
ret
; movement (with clamping); each moves the highlight
osk_up:
ld a, [wOskRow]
or a
ret z
call osk_unhighlight
ld hl, wOskRow
dec [hl]
jr osk_highlight
osk_down:
ld a, [wOskRow]
cp OSK_ROWS-1
ret z
call osk_unhighlight
ld hl, wOskRow
inc [hl]
jr osk_highlight
osk_left:
ld a, [wOskCol]
or a
ret z
call osk_unhighlight
ld hl, wOskCol
dec [hl]
jr osk_highlight
osk_right:
ld a, [wOskCol]
cp OSK_COLS-1
ret z
call osk_unhighlight
ld hl, wOskCol
inc [hl]
jr osk_highlight
; -> A = type value of the selected key
osk_selected:
ld a, [wOskRow]
ld h, 0
ld l, a
add hl, hl
add hl, hl
ld d, h
ld e, l
add hl, hl
add hl, hl
add hl, de ; row*20
ld a, [wOskCol]
add l
ld l, a
ld a, h
adc 0
ld h, a
ld de, osk_keys
add hl, de
ld a, [hl]
ret
; -----------------------------------------------------------------------------
; osk_handle - process the latest pad edges. Returns CF set + A = typed byte if
; the user pressed A over a key, else CF clear. (SELECT toggles; d-pad moves.)
; -----------------------------------------------------------------------------
osk_handle::
ld a, [wPadNew]
bit 2, a ; SELECT
jr z, .nosel
call osk_toggle
.nosel
ld a, [wOskVisible]
or a
jr z, .none
ld a, [wPadNew]
bit 6, a
call nz, osk_up
ld a, [wPadNew]
bit 7, a
call nz, osk_down
ld a, [wPadNew]
bit 5, a
call nz, osk_left
ld a, [wPadNew]
bit 4, a
call nz, osk_right
ld a, [wPadNew]
bit 0, a ; A: type the selected key
jr z, .none
call osk_selected
scf
ret
.none
or a ; CF = 0
ret
|