-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathps2.spin2
483 lines (414 loc) · 19.9 KB
/
ps2.spin2
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
{
PS/2 Keyboard Driver
Copyright (c) 2021-22 by Marco Maccaferri <[email protected]>
TERMS OF USE: MIT License
}
CON
_CLKFREQ = 160_000_000
PS2_DATA_PIN = 47
PS2_CLOCK_PIN = 46
DAT ' Startup
org $000
asmclk
drvl #56
drvl #57
coginit #0, ##@ps2_driver
DAT ' PS/2 keyboard driver
org $000
ps2_driver
fltl #PS2_DATA_PIN
fltl #PS2_CLOCK_PIN
mov ps2_data, #$F3 ' auto-repeat
call #ps2_transmit
and ps2_data, #%011_11111 ' slow 1s / 2cps
call #ps2_transmit
mov ps2_state, #%0_000 ' turn off all leds
jmp #ps2_locks
ps2_reset
ps2_loop and ps2_state, #$07 ' keep locks state
.l1 testp #PS2_DATA_PIN wz ' wait initial clock low
if_x1 jmp #$-1
call #ps2_rx
getbyte ps2_code, ps2_data, #0
cmp ps2_data, #$F0 wz ' release
if_z bith ps2_state, #7
if_z jmp #.l1
cmp ps2_data, #$E0 wz
if_z bith ps2_state, #28
if_z jmp #ps2_ext0
cmp ps2_data, #$E1 wz
if_z bith ps2_state, #29
if_z jmp #ps2_ext1
ps2_ext_ret testbn ps2_state, #29 wz
if_nz jmp #ps2_loop ' E1 codes are ignored
testb ps2_state, #7 wc ' release
cmp ps2_code, #$14 wz ' left control
if_z testbn ps2_state, #28 andz
if_z muxnc ps2_shift, #LEFT_CTRLF
cmp ps2_code, #$14 wz ' right control
if_z testb ps2_state, #28 andz
if_z muxnc ps2_shift, #RIGHT_CTRLF
cmp ps2_code, #$12 wz ' left shift
if_z testbn ps2_state, #28 andz
if_z muxnc ps2_shift, #LEFT_SHIFTF
cmp ps2_data, #$59 wz ' right shift
if_z testbn ps2_state, #28 andz
if_z muxnc ps2_shift, #RIGHT_SHIFTF
cmp ps2_code, #$11 wz ' left alt
if_z testbn ps2_state, #28 andz
if_z muxnc ps2_shift, #LEFT_ALTF
cmp ps2_code, #$11 wz ' right alt
if_z testb ps2_state, #28 andz
if_z muxnc ps2_shift, #RIGHT_ALTF
cmp ps2_code, #$1F wz ' left gui
if_z testb ps2_state, #28 andz
if_z muxnc ps2_shift, #LEFT_GUIF
cmp ps2_code, #$27 wz ' right gui
if_z testb ps2_state, #28 andz
if_z muxnc ps2_shift, #RIGHT_GUIF
cmp ps2_code, #$77 wz ' num. lock
if_z_and_c jmp #ps2_loop ' | ignore if released
if_z bitnot ps2_state, #0 ' | toggle state
if_z jmp #ps2_locks ' | update leds
cmp ps2_code, #$58 wz ' caps lock
if_z_and_c jmp #ps2_loop ' | ignore if released
if_z bitnot ps2_state, #1 ' | toggle state
if_z jmp #ps2_locks ' | update leds
cmp ps2_code, #$7E wz ' scroll lock
if_z_and_c jmp #ps2_loop ' | ignore if released
if_z bitnot ps2_state, #2 ' | toggle state
if_z jmp #ps2_locks ' | update leds
testb ps2_state, #28 wc ' extended code
rcl ps2_code, #1 ' to bit 0
altgb ps2_code, #ps2_table ' get usb scancode
getbyte ps2_key ' |
cmp ps2_key, #$E0 wcz
if_ae mov ps2_key, #0
if_b testb ps2_state, #7 wc ' release
call #ps2_translate
jmp #ps2_loop
ps2_locks mov ps2_data, #$ED
call #ps2_transmit
mov ps2_data, #0
testb ps2_state, #0 wz ' num. lock
bitz ps2_data, #1
testb ps2_state, #1 wz ' caps lock
bitz ps2_data, #2
testb ps2_state, #2 wz ' scroll lock
bitz ps2_data, #0
call #ps2_transmit
jmp #ps2_loop
ps2_ext1 call #ps2_receive
setbyte ps2_code, ps2_data, #1
cmp ps2_data, #$F0 wz ' release
if_z bith ps2_state, #7
if_z jmp #ps2_ext1
' fall through
ps2_ext0 call #ps2_receive
setbyte ps2_code, ps2_data, #0
cmp ps2_data, #$F0 wz ' release
if_z bith ps2_state, #7
if_z jmp #ps2_ext0
jmp #ps2_ext_ret
ps2_transmit drvl #PS2_CLOCK_PIN ' pull clock low
getct ps2_tout ' hold clock for 128us (must be > 100us)
addct1 ps2_tout, ps2_us128 ' |
jnct1 #$
drvl #PS2_DATA_PIN ' pull data low
getct ps2_tout ' hold data for 4us
addct1 ps2_tout, ps2_us4 ' |
jnct1 #$
fltl #PS2_CLOCK_PIN ' release clock
getct ps2_tout ' allow pin to float
addct1 ps2_tout, ps2_us1 ' |
jnct1 #$
test ps2_data, #$FF wc ' append parity
muxnc ps2_data, #$100 ' |
bith ps2_data, #9 ' append stop bit
getct ps2_tout ' safety timeout
addct1 ps2_tout, ps2_us6000
mov ps2_bits, #10
.l1 testp #PS2_CLOCK_PIN wz ' wait until clock low
if_x1 jct1 #ps2_reset ' | check timeout
if_x1 jmp #.l1 ' |
shr ps2_data, #1 wc ' output data bit
drvc #PS2_DATA_PIN ' |
.l2 testp #PS2_CLOCK_PIN wz ' wait until clock high
if_x0 jct1 #ps2_reset ' | check timeout
if_x0 jmp #.l2 ' |
djnz ps2_bits, #.l1 ' another bit ?
fltl #PS2_DATA_PIN
.l3 testp #PS2_CLOCK_PIN wc ' wait until clock and data low
testp #PS2_DATA_PIN wz ' |
if_not_00 jct1 #ps2_reset ' | check timeout
if_not_00 jmp #.l3 ' |
.l4 testp #PS2_CLOCK_PIN wc ' wait until clock and data high
testp #PS2_DATA_PIN wz ' |
if_not_11 jct1 #ps2_reset ' | check timeout
if_not_11 jmp #.l4 ' |
' Fall through to receive ack
ps2_receive getct ps2_tout ' safety timeout
addct1 ps2_tout, ps2_us6000
.l0 testp #PS2_DATA_PIN wz ' wait until data low
if_x1 jct1 #ps2_reset ' | check timeout
if_x1 jmp #.l0 ' |
ps2_rx
getct ps2_tout ' safety timeout
addct1 ps2_tout, ps2_us3000
mov ps2_bits, #11
.l1 testp #PS2_CLOCK_PIN wz ' wait until clock low
if_x1 jct1 #ps2_reset ' | check timeout
if_x1 jmp #.l1 ' |
testp #PS2_DATA_PIN wc ' sample data
rcr ps2_data, #1 ' |
.l2 testp #PS2_CLOCK_PIN wz ' wait until clock high
if_x0 jct1 #ps2_reset ' | check timeout
if_x0 jmp #.l2 ' |
djnz ps2_bits, #.l1 ' another bit?
.l3 testp #PS2_CLOCK_PIN wc ' wait until clock and data high
testp #PS2_DATA_PIN wz ' |
if_not_11 jct1 #.l4 ' | check timeout
if_not_11 jmp #.l3 ' |
.l4
shr ps2_data, #22 ' align byte
test ps2_data, #$1FF wc ' test parity
if_nc jmp #ps2_reset ' |
_ret_ and ps2_data, #$FF ' ok
'------------------------------------------------------------------------------
' Translate keyboard scancodes
'------------------------------------------------------------------------------
' On entry:
' ps2_key - scancode
' ps2_shift - key modifiers state
' c - pressed (0) or released (1) flag
' On exit:
' ps2_key - bit 31 = 0 pressed, 1 released
' 23..16 = key modifiers state
' 15..8 = scancode
' 7..0 = ASCII key code (or 0 if none)
'------------------------------------------------------------------------------
ps2_translate
mov ps2_temp, ps2_key
shl ps2_temp, #1
test ps2_shift, #LEFT_SHIFTF|RIGHT_SHIFTF wz
if_nz add ps2_temp, #1
add ps2_temp, ##@kbd_table
rdbyte ps2_temp, ps2_temp
shl ps2_key, #8
setbyte ps2_key, ps2_temp, #0
setbyte ps2_key, ps2_shift, #2
setbyte ps2_key, ps2_state, #3
bitc ps2_key, #31
debug(uhex_long(ps2_key))
ret
ps2_us1 long _CLKFREQ / 1_000_000 * 1 ' 1 usec.
ps2_us4 long _CLKFREQ / 1_000_000 * 4 ' 4 usec.
ps2_us128 long _CLKFREQ / 1_000_000 * 128 ' 128 usec.
ps2_us3000 long _CLKFREQ / 1_000_000 * 3000 ' 3000 usec.
ps2_us6000 long _CLKFREQ / 1_000_000 * 6000 ' 6000 usec.
ps2_bits long 0
ps2_code long 0
ps2_state long 0
ps2_shift long 0
ps2_key long 0
ps2_data long 0
ps2_tout long 0
ps2_temp long 0
ps2_table byte $00, $00 '00
byte $42, $00 '01 F9
byte $00, $00 '02
byte $3E, $00 '03 F5
byte $3C, $00 '04 F3
byte $3A, $00 '05 F1
byte $3B, $00 '06 F2
byte $45, $00 '07 F12
byte $00, $00 '08
byte $43, $00 '09 F10
byte $41, $00 '0A F8
byte $3F, $00 '0B F6
byte $3D, $00 '0C F4
byte $2B, $00 '0D Tab
byte $35, $00 '0E `
byte $00, $00 '0F
byte $00, $00 '10
byte $E2, $E6 '11 Alt-L Alt-R
byte $E1, $00 '12 Shift-L
byte $00, $00 '13
byte $E0, $E4 '14 Ctrl-L Ctrl-R
byte $14, $00 '15 q
byte $1E, $00 '16 1
byte $00, $00 '17
byte $00, $00 '18
byte $00, $00 '19
byte $1D, $00 '1A z
byte $16, $00 '1B s
byte $04, $00 '1C a
byte $1A, $00 '1D w
byte $1F, $00 '1E 2
byte $00, $E3 '1F Win-L
byte $00, $00 '20
byte $06, $00 '21 c
byte $1B, $00 '22 x
byte $07, $00 '23 d
byte $08, $00 '24 e
byte $21, $00 '25 4
byte $20, $00 '26 3
byte $00, $E7 '27 Win-R
byte $00, $00 '28
byte $2C, $00 '29 Space
byte $19, $00 '2A v
byte $09, $00 '2B f
byte $17, $00 '2C t
byte $15, $00 '2D r
byte $22, $00 '2E 5
byte $00, $65 '2F Apps
byte $00, $00 '30
byte $11, $00 '31 n
byte $05, $00 '32 b
byte $0B, $00 '33 h
byte $0A, $00 '34 g
byte $1C, $00 '35 y
byte $23, $00 '36 6
byte $00, $00 '37 Power
byte $00, $00 '38
byte $00, $00 '39
byte $10, $00 '3A m
byte $0D, $00 '3B j
byte $18, $00 '3C u
byte $24, $00 '3D 7
byte $25, $00 '3E 8
byte $00, $00 '3F Sleep
byte $00, $00 '40
byte $36, $00 '41 ,
byte $0E, $00 '42 k
byte $0C, $00 '43 i
byte $12, $00 '44 o
byte $27, $00 '45 0
byte $26, $00 '46 9
byte $00, $00 '47
byte $00, $00 '48
byte $37, $00 '49 .
byte $38, $54 '4A / (/)
byte $0F, $00 '4B l
byte $33, $00 '4C ;
byte $13, $00 '4D p
byte $2D, $00 '4E -
byte $00, $00 '4F
byte $00, $00 '50
byte $00, $00 '51
byte $34, $00 '52 '
byte $00, $00 '53
byte $2F, $00 '54 [
byte $2E, $00 '55 =
byte $00, $00 '56
byte $00, $00 '57
byte $39, $00 '58 CapsLock
byte $E5, $00 '59 Shift-R
byte $28, $58 '5A Enter (Enter)
byte $30, $00 '5B ]
byte $00, $00 '5C
byte $31, $00 '5D \
byte $00, $00 '5E WakeUp
byte $00, $00 '5F
byte $00, $00 '60
byte $64, $00 '61
byte $00, $00 '62
byte $00, $00 '63
byte $00, $00 '64
byte $00, $00 '65
byte $2A, $00 '66 BackSpace
byte $00, $00 '67
byte $00, $00 '68
byte $59, $4D '69 (1) End
byte $00, $00 '6A
byte $5C, $50 '6B (4) Left
byte $5F, $4A '6C (7) Home
byte $00, $00 '6D
byte $00, $00 '6E
byte $00, $00 '6F
byte $62, $49 '70 (0) Insert
byte $63, $4C '71 (.) Delete
byte $5A, $51 '72 (2) Down
byte $5D, $00 '73 (5)
byte $5E, $4F '74 (6) Right
byte $60, $52 '75 (8) Up
byte $29, $00 '76 Esc
byte $53, $00 '77 NumLock
byte $44, $00 '78 F11
byte $57, $00 '79 (+)
byte $5B, $4E '7A (3) PageDn
byte $56, $00 '7B (-)
byte $55, $46 '7C (*) PrScr
byte $61, $4B '7D (9) PageUp
byte $47, $00 '7E ScrLock
byte $00, $00 '7F
byte $00, $00 '80
byte $00, $00 '81
byte $00, $00 '82
byte $40, $00 '83 F7
fit $1F0
DAT ' Scancode translation table
orgh
kbd_table ' Index is the key scan code
{$00} word $0000, $0000, $0000, $0000 ' $03
{$04} word $4161, $4262, $4363, $4464 ' $07: Aa Bb Cc Dd
{$08} word $4565, $4666, $4767, $4868, $4969, $4A6A, $4B6B, $4C6C ' $0f: Ee Ff Gg Hh Ii Jj Kk Ll
{$10} word $4D6D, $4E6E, $4F6F, $5070, $5171, $5272, $5373, $5474 ' $17: Mm Nn Oo Pp Qq Rr Ss Tt
{$18} word $5575, $5676, $5777, $5878, $5979, $5A7A, $2131, $4032 ' $1f: Uu Vv Ww Xx Yy Zz !1 @2
{$20} word $2333, $2434, $2535, $5E36, $2637, $2A38, $2839, $2930 ' $27: #3 $4 %5 ^6 &7 *8 (9 )0
{$28} word $0D0D, $1B1B, $0808, $0909, $2020, $5F2D, $2B3D, $7B5B ' $2f: Enter Esc BkSpc Tab Spc _- += {[
{$30} word $7D5D, $7C5C, $7E23, $3A3B, $2227, $7E60, $3C2C, $3E2E ' $37: }] |\ ~# :; "' ~` <, >.
{$38} word $3F2F, $0000, $0000, $0000, $0000, $0000, $0000, $0000 ' $3f: ?/ CapsLock F1 F2 F3 F4 F5 F6
{$40} word $0000, $0000, $0000, $0000, $0000, $0000, $0000, $0000 ' $47: F7 F8 F9 F10 F11 F12 PrtSc, ScrLk
{$48} word $0000, $0000, $0000, $0000, $7F7F, $0000, $0000, $0000 ' $4f: Pause, Ins, Home PgUp BkSpc_Del End PgDn Right
{$50} word $0000, $0000, $0000, $0000, $2F2F, $2A2A, $2D2D, $2B2B ' $57: Left Down Up KpdNumLck Kp/ Kp* Kp- Kp+
{$58} word $0D0D, $3131, $3232, $3333, $5034, $3535, $3636, $3737 ' $5f: KpEnter Kp1_End Kp2_Down Kp3_PgDn Kp4_Left Kp5 Kp6_Right Kp7_Home
{$60} word $3838, $3939, $3030, $7F2E, $5C7C, $0000 ' $65: Kp8_Up Kp9_PgUp Kp0_Ins Kp._Del Kp\_| App
CON ' PS/2 keyboard constants
' Keyboard LED output report toggle key bit postions:
#0, LED_NUMLKB, LED_CAPSLKB, LED_SCRLKB
' Keyboard LED output report toggle key bitflags:
LED_NUMLKF = decod(LED_NUMLKB)
LED_CAPSLKF = decod(LED_CAPSLKB)
LED_SCRLKF = decod(LED_SCRLKB)
' Keyboard modifier key bit positions:
#0, LEFT_CTRLB, LEFT_SHIFTB, LEFT_ALTB, LEFT_GUIB
RIGHT_CTRLB, RIGHT_SHIFTB, RIGHT_ALTB, RIGHT_GUIB
' Keyboard modifier bitflags
LEFT_CTRLF = decod(LEFT_CTRLB)
LEFT_SHIFTF = decod(LEFT_SHIFTB)
LEFT_ALTF = decod(LEFT_ALTB)
LEFT_GUIF = decod(LEFT_GUIB)
RIGHT_CTRLF = decod(RIGHT_CTRLB)
RIGHT_SHIFTF = decod(RIGHT_SHIFTB)
RIGHT_ALTF = decod(RIGHT_ALTB)
RIGHT_GUIF = decod(RIGHT_GUIB)
' Consolidated left/right modkeys. Add the modifier keys you want to trap
' together, then add the key scan code e.g. CTRL+ALT+"X" would be: $600 + KEY_X_x.
' The key() Spin2 method does this and the rawKey() method returns the key data
' as packed by the USB keyboard driver.
' SHIFT = $100
' CTRL = $200
' ALT = $400
' APP = $800
' L|R key modifier flag combinations:
KEYS_APP = LEFT_GUIF + RIGHT_GUIF
KEYS_ALT = LEFT_ALTF + RIGHT_ALTF
KEYS_CTRL = LEFT_CTRLF + RIGHT_CTRLF
KEYS_SHIFT = LEFT_SHIFTF + RIGHT_SHIFTF
CON ' License
{{
TERMS OF USE: MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}