forked from august0815/forthos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_kbd.fth
132 lines (114 loc) · 2.49 KB
/
kernel_kbd.fth
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
; program: kernel_kbd
; Words related to the keyboard driver
; License: GPL
; José Dinuncio <[email protected]>, 12/2009.
%include "forth.h"
%include "kernel_words.h"
%include "kernel_video.h"
extern name_hexprint
%undef OLDLINK
%xdefine LINK name_hexprint
extern keymap
%define keymap keymap
[BITS 32]
%define _key_stat_caps 0x01
%define _key_stat_shift 0x02
; variable: key_status
; Store the status of caps, shift and CONTROL keys.
defvar "key_status", key_status, 0, 0
; function: kbd_flags
; Returns the keyboard status code.
;
; Stack:
; -- kbd_status
: kbd_flags
0x64 inb
;
; function: kbd_buffer_full
; true if there is a scancode waiting to be readed
;
; Stack:
; -- bool
: kbd_buffer_full
kbd_flags 1 and
;
; function: kbd_scancode_now
; Returns the scancode readed on the keyboard at this moment.
;
; Stack:
; -- scancode
: kbd_scancode_now
0x60 inb
;
; function: kbd_scancode
; Waits for a key pressed and returns its sacancode.
;
; Stack:
; -- scancode
: kbd_scancode
begin kbd_buffer_full until
kbd_scancode_now 0xFF and
;
; function _tx_key_status
; Test and xor the key_status variable.
;
; If the scancode is equal to the given test, makes an xor
; between key_status and flags.
;
; stack:
; scancode test flag --
: _tx_key_status
-rot =
if
key_status @ xor key_status !
else
drop
then
;
; function: _update_key_status
; Updates the kbd_flags variable according with the scancode given.
;
; Stack:
; scancode --
: _update_key_status
; TODO - xor could fail in some cases. Set o clear the bit.
dup 58 _key_stat_caps _tx_key_status ; caps down
dup 42 _key_stat_shift _tx_key_status ; lshift down
dup 170 _key_stat_shift _tx_key_status ; lshift up
dup 54 _key_stat_shift _tx_key_status ; rshift down
dup 182 _key_stat_shift _tx_key_status ; rshift up
drop
;
; stack:
; scancode -- bool
: _key_down?
0x80 and 0=
;
; function: sc_to_c (SCANCODE2CHAR)
; Converts a scancode to an ASCII character.
;
; If the scancode correspond to keyup or to a non-character
; it returns 0
;
; stack:
; scancode -- char
: sc_to_c
dup _key_down? if
4 * key_status @ + keymap + c@
else drop 0 then
;
; function: getchar
; Waits for a key to be pressed and then returns its ASCII code.
;
; Stack:
; -- c
: getchar
0
begin
drop
kbd_scancode
dup _update_key_status
sc_to_c dup
until
;
global name_getchar