added PS/2 mouse code
[bootcensus] / src / keyb.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include "keyb.h"
21 #include "intr.h"
22 #include "asmops.h"
23 #include "kbregs.h"
24
25
26 /* table with rough translations from set 1 scancodes to ASCII-ish */
27 static int scantbl[] = {
28         0, KB_ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',            /* 0 - e */
29         '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',                 /* f - 1c */
30         KB_LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',                          /* 1d - 29 */
31         KB_LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', KB_RSHIFT,                   /* 2a - 36 */
32         KB_NUM_MUL, KB_LALT, ' ', KB_CAPSLK, KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8, KB_F9, KB_F10,                     /* 37 - 44 */
33         KB_NUMLK, KB_SCRLK, KB_NUM_7, KB_NUM_8, KB_NUM_9, KB_NUM_MINUS, KB_NUM_4, KB_NUM_5, KB_NUM_6, KB_NUM_PLUS,      /* 45 - 4e */
34         KB_NUM_1, KB_NUM_2, KB_NUM_3, KB_NUM_0, KB_NUM_DOT, KB_SYSRQ, 0, 0, KB_F11, KB_F12,                                             /* 4d - 58 */
35         0, 0, 0, 0, 0, 0, 0,                                                                                                                    /* 59 - 5f */
36         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                                                                 /* 60 - 6f */
37         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0                                                                  /* 70 - 7f */
38 };
39
40 static void kbintr();
41
42 #define BUFSZ   64
43 #define ADVANCE(x)      ((x) = ((x) + 1) & (BUFSZ - 1))
44
45 static int buffer[BUFSZ];
46 static int buf_ridx, buf_widx;
47
48 static unsigned int num_pressed;
49 static unsigned char keystate[256];
50
51 void kb_init(void)
52 {
53         buf_ridx = buf_widx = 0;
54         num_pressed = 0;
55         memset(keystate, 0, sizeof keystate);
56
57         interrupt(IRQ_TO_INTR(KB_IRQ), kbintr);
58 }
59
60 int kb_isdown(int key)
61 {
62         switch(key) {
63         case KB_ANY:
64                 return num_pressed;
65
66         case KB_ALT:
67                 return keystate[KB_LALT] + keystate[KB_RALT];
68
69         case KB_CTRL:
70                 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
71         }
72         return keystate[key];
73 }
74
75 void kb_wait(void)
76 {
77         int key;
78         while((key = kb_getkey()) == -1) {
79                 /* put the processor to sleep while waiting for keypresses, but first
80                  * make sure interrupts are enabled, or we'll sleep forever
81                  */
82                 enable_intr();
83                 halt_cpu();
84         }
85         kb_putback(key);
86 }
87
88 int kb_getkey(void)
89 {
90         int res;
91
92         if(buf_ridx == buf_widx) {
93                 return -1;
94         }
95         res = buffer[buf_ridx];
96         ADVANCE(buf_ridx);
97         return res;
98 }
99
100 void kb_putback(int key)
101 {
102         /* go back a place */
103         if(--buf_ridx < 0) {
104                 buf_ridx += BUFSZ;
105         }
106
107         /* if the write end hasn't caught up with us, go back one place
108          * and put it there, otherwise just overwrite the oldest key which
109          * is right where we were.
110          */
111         if(buf_ridx == buf_widx) {
112                 ADVANCE(buf_ridx);
113         }
114
115         buffer[buf_ridx] = key;
116 }
117
118 int kb_wait_write(void)
119 {
120         int i;
121         for(i=0; i<32768; i++) {
122                 if(!(inb(KB_STATUS_PORT) & KB_STAT_INBUF_FULL)) {
123                         return 1;
124                 }
125                 iodelay();
126         }
127         /*printf("kb_wait_write timeout\n");*/
128         return 0;
129 }
130
131 int kb_wait_read(void)
132 {
133         int i;
134         for(i=0; i<32768; i++) {
135                 if((inb(KB_STATUS_PORT) & KB_STAT_OUTBUF_FULL)) {
136                         return 1;
137                 }
138                 iodelay();
139         }
140         /*printf("kb_wait_read timeout\n");*/
141         return 0;
142 }
143
144 void kb_send_cmd(unsigned char cmd)
145 {
146         kb_wait_write();
147         outb(cmd, KB_CMD_PORT);
148 }
149
150 void kb_send_data(unsigned char data)
151 {
152         kb_wait_write();
153         outb(data, KB_DATA_PORT);
154 }
155
156 unsigned char kb_read_data(void)
157 {
158         kb_wait_read();
159         return inb(KB_DATA_PORT);
160 }
161
162 static void kbintr()
163 {
164         unsigned char code;
165         int key, press;
166
167         code = inb(KB_DATA_PORT);
168
169         if(code >= 128) {
170                 press = 0;
171                 code -= 128;
172
173                 if(num_pressed > 0) {
174                         num_pressed--;
175                 }
176         } else {
177                 press = 1;
178
179                 num_pressed++;
180         }
181
182         key = scantbl[code];
183
184         if(press) {
185                 /* append to buffer */
186                 buffer[buf_widx] = key;
187                 ADVANCE(buf_widx);
188                 /* if the write end overtook the read end, advance the read end
189                  * too, to discard the oldest keypress from the buffer
190                  */
191                 if(buf_widx == buf_ridx) {
192                         ADVANCE(buf_ridx);
193                 }
194         }
195
196         /* and update keystate table */
197         keystate[key] = press;
198 }