1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-20 20:18:05 +11:00
collapseos/kernel/kbd.asm

138 lines
4.2 KiB
NASM
Raw Normal View History

; kbd - implement GetC for PS/2 keyboard
;
; It reads raw key codes from a FetchKC routine and returns, if appropriate,
; a proper ASCII char to type. See recipes rc2014/ps2 and sms/kbd.
;
; *** Defines ***
; Pointer to a routine that fetches the last typed keyword in A. Should return
; 0 when nothing was typed.
; KBD_FETCHKC
2019-07-22 06:58:16 +10:00
; *** Consts ***
.equ KBD_KC_BREAK 0xf0
.equ KBD_KC_EXT 0xe0
.equ KBD_KC_LSHIFT 0x12
.equ KBD_KC_RSHIFT 0x59
; *** Variables ***
2019-07-22 06:58:16 +10:00
; Set to previously received scan code
.equ KBD_PREV_KC KBD_RAMSTART
2019-12-07 14:45:39 +11:00
; Whether Shift key is pressed. When not pressed, holds 0. When pressed, holds
; 0x80. This allows for quick shifting in the glyph table.
.equ KBD_SHIFT_ON @+1
.equ KBD_RAMEND @+1
kbdInit:
xor a
2019-07-22 06:58:16 +10:00
ld (KBD_PREV_KC), a
ld (KBD_SHIFT_ON), a
ret
kbdGetC:
call KBD_FETCHKC
or a
jr z, .nothing
; scan code not zero, maybe we have something.
; Do we need to skip it?
2019-07-22 06:58:16 +10:00
ex af, af' ; save fetched KC
ld a, (KBD_PREV_KC)
2019-07-22 07:30:50 +10:00
; Whatever the KC, the new A becomes our prev. The easiest way to do
; this is to do it now.
ex af, af' ; restore KC
ld (KBD_PREV_KC), a
ex af, af' ; restore prev KC
2019-07-22 06:58:16 +10:00
; If F0 (break code) or E0 (extended code), we skip this code
cp KBD_KC_BREAK
2019-07-22 07:30:50 +10:00
jr z, .break
2019-07-22 06:58:16 +10:00
cp KBD_KC_EXT
2019-12-07 14:45:39 +11:00
jr z, .nothing
2019-07-22 06:58:16 +10:00
ex af, af' ; restore saved KC
2019-12-07 14:45:39 +11:00
; A scan code over 0x80 is out of bounds or prev KC tell us we should
; skip. Ignore.
cp 0x80
2019-12-07 14:45:39 +11:00
jr nc, .nothing
2019-07-22 07:30:50 +10:00
; No need to skip, code within bounds, we have something!
call .isShift
jr z, .shiftPressed
; Let's see if there's a ASCII code associated to it.
2019-07-22 06:58:16 +10:00
push hl ; --> lvl 1
2019-07-22 07:30:50 +10:00
ld hl, KBD_SHIFT_ON
2019-12-07 14:45:39 +11:00
or (hl) ; if shift is on, A now ranges in 0x80-0xff.
2019-07-22 07:30:50 +10:00
ld hl, kbdScanCodes ; no flag changed
2019-07-22 06:58:16 +10:00
call addHL
ld a, (hl)
pop hl ; <-- lvl 1
or a
2019-12-07 14:45:39 +11:00
jr z, kbdGetC ; no code.
; We have something!
cp a ; ensure Z
ret
2019-07-22 07:30:50 +10:00
.shiftPressed:
2019-12-07 14:45:39 +11:00
ld a, 0x80
2019-07-22 07:30:50 +10:00
ld (KBD_SHIFT_ON), a
2019-12-07 14:45:39 +11:00
jr .nothing ; no actual char to return
2019-07-22 07:30:50 +10:00
.break:
ex af, af' ; restore saved KC
call .isShift
2019-12-07 14:45:39 +11:00
jr nz, .nothing
2019-07-22 07:30:50 +10:00
; We had a shift break, update status
xor a
2019-07-22 07:30:50 +10:00
ld (KBD_SHIFT_ON), a
2019-12-07 14:45:39 +11:00
; continue to .nothing
.nothing:
; We have nothing. Before we go further, we'll wait a bit to give our
; device the time to "breathe". When we're in a "nothing" loop, the z80
; hammers the device really fast and continuously generates interrupts
; on it and it interferes with its other task of reading the keyboard.
2019-12-07 14:45:39 +11:00
xor a
.wait:
2019-12-07 14:45:39 +11:00
inc a
jr nz, .wait
jr kbdGetC
2019-07-22 07:30:50 +10:00
; Whether KC in A is L or R shift
.isShift:
cp KBD_KC_LSHIFT
ret z
cp KBD_KC_RSHIFT
ret
; A list of the values associated with the 0x80 possible scan codes of the set
2019-12-07 14:45:39 +11:00
; 2 of the PS/2 keyboard specs. 0 means no value. That value is a character that
; can be read in a GetC routine. No make code in the PS/2 set 2 reaches 0x80.
kbdScanCodes:
; 0x00 1 2 3 4 5 6 7 8 9 a b c d e f
.db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,'`', 0
; 0x10 9 = TAB
.db 0, 0, 0, 0, 0,'q','1', 0, 0, 0,'z','s','a','w','2', 0
; 0x20 32 = SPACE
.db 0,'c','x','d','e','4','3', 0, 0, 32,'v','f','t','r','5', 0
; 0x30
.db 0,'n','b','h','g','y','6', 0, 0, 0,'m','j','u','7','8', 0
; 0x40 59 = ;
.db 0,',','k','i','o','0','9', 0, 0,'.','/','l', 59,'p','-', 0
; 0x50 13 = RETURN 39 = '
.db 0, 0, 39, 0,'[','=', 0, 0, 0, 0, 13,']', 0,'\', 0, 0
; 0x60 8 = BKSP
2019-07-26 04:22:17 +10:00
.db 0, 0, 0, 0, 0, 0, 8, 0, 0,'1', 0,'4','7', 0, 0, 0
; 0x70 27 = ESC
2019-07-26 04:22:17 +10:00
.db '0','.','2','5','6','8', 27, 0, 0, 0,'3', 0, 0,'9', 0, 0
2019-07-22 07:30:50 +10:00
2019-12-07 14:45:39 +11:00
; Same values, but shifted, exactly 0x80 bytes after kbdScanCodes
2019-07-22 07:30:50 +10:00
; 0x00 1 2 3 4 5 6 7 8 9 a b c d e f
.db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,'~', 0
; 0x10 9 = TAB
.db 0, 0, 0, 0, 0,'Q','!', 0, 0, 0,'Z','S','A','W','@', 0
; 0x20 32 = SPACE
.db 0,'C','X','D','E','$','#', 0, 0, 32,'V','F','T','R','%', 0
; 0x30
.db 0,'N','B','H','G','Y','^', 0, 0, 0,'M','J','U','&','*', 0
; 0x40 59 = ;
.db 0,'<','K','I','O',')','(', 0, 0,'>','?','L',':','P','_', 0
2019-07-26 04:22:17 +10:00
; 0x50 13 = RETURN
.db 0, 0,'"', 0,'{','+', 0, 0, 0, 0, 13,'}', 0,'|', 0, 0
2019-07-22 07:30:50 +10:00
; 0x60 8 = BKSP
.db 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0
; 0x70 27 = ESC
.db 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0