rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 DOS interrupt-based keyboard driver.
|
nuclear@0
|
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@0
|
4
|
nuclear@0
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@0
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@0
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@0
|
8 (at your option) any later version.
|
nuclear@0
|
9
|
nuclear@0
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@0
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@0
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@0
|
13 GNU General Public License for more details.
|
nuclear@0
|
14
|
nuclear@0
|
15 You should have received a copy of the GNU General Public License
|
nuclear@0
|
16 along with the program. If not, see <http://www.gnu.org/licenses/>
|
nuclear@0
|
17 */
|
nuclear@0
|
18 #define KEYB_C_
|
nuclear@0
|
19
|
nuclear@0
|
20 #include <stdio.h>
|
nuclear@0
|
21 #include <stdlib.h>
|
nuclear@0
|
22 #include <string.h>
|
nuclear@0
|
23 #include <conio.h>
|
nuclear@0
|
24 #include <dos.h>
|
nuclear@0
|
25 #include <i86.h>
|
nuclear@0
|
26 #include "keyb.h"
|
nuclear@0
|
27 #include "scancode.h"
|
nuclear@0
|
28
|
nuclear@0
|
29 #define KB_INTR 0x9
|
nuclear@0
|
30 #define KB_PORT 0x60
|
nuclear@0
|
31
|
nuclear@0
|
32 #define PIC1_CMD_PORT 0x20
|
nuclear@0
|
33 #define OCW2_EOI (1 << 5)
|
nuclear@0
|
34
|
nuclear@0
|
35 #define DONE_INIT (prev_handler)
|
nuclear@0
|
36
|
nuclear@0
|
37 static void __interrupt __far kbintr();
|
nuclear@0
|
38
|
nuclear@0
|
39 static void (__interrupt __far *prev_handler)();
|
nuclear@0
|
40
|
nuclear@0
|
41 static int *buffer;
|
nuclear@0
|
42 static int buffer_size, buf_ridx, buf_widx;
|
nuclear@0
|
43 static int last_key;
|
nuclear@0
|
44
|
nuclear@0
|
45 static unsigned int num_pressed;
|
nuclear@0
|
46 static unsigned char keystate[256];
|
nuclear@0
|
47
|
nuclear@0
|
48 #define ADVANCE(x) ((x) = ((x) + 1) % buffer_size)
|
nuclear@0
|
49
|
nuclear@0
|
50 int kb_init(int bufsz)
|
nuclear@0
|
51 {
|
nuclear@0
|
52 if(DONE_INIT) {
|
nuclear@0
|
53 fprintf(stderr, "keyboard driver already initialized!\n");
|
nuclear@0
|
54 return 0;
|
nuclear@0
|
55 }
|
nuclear@0
|
56
|
nuclear@0
|
57 buffer_size = bufsz;
|
nuclear@0
|
58 if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
|
nuclear@0
|
59 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
|
nuclear@0
|
60 buffer_size = 0;
|
nuclear@0
|
61 }
|
nuclear@0
|
62 buf_ridx = buf_widx = 0;
|
nuclear@0
|
63 last_key = -1;
|
nuclear@0
|
64
|
nuclear@0
|
65 memset(keystate, 0, sizeof keystate);
|
nuclear@0
|
66 num_pressed = 0;
|
nuclear@0
|
67
|
nuclear@0
|
68 /* set our interrupt handler */
|
nuclear@0
|
69 _disable();
|
nuclear@0
|
70 prev_handler = _dos_getvect(KB_INTR);
|
nuclear@0
|
71 _dos_setvect(KB_INTR, kbintr);
|
nuclear@0
|
72 _enable();
|
nuclear@0
|
73
|
nuclear@0
|
74 return 0;
|
nuclear@0
|
75 }
|
nuclear@0
|
76
|
nuclear@0
|
77 void kb_shutdown(void)
|
nuclear@0
|
78 {
|
nuclear@0
|
79 if(!DONE_INIT) {
|
nuclear@0
|
80 return;
|
nuclear@0
|
81 }
|
nuclear@0
|
82
|
nuclear@0
|
83 /* restore the original interrupt handler */
|
nuclear@0
|
84 _disable();
|
nuclear@0
|
85 _dos_setvect(KB_INTR, prev_handler);
|
nuclear@0
|
86 _enable();
|
nuclear@0
|
87
|
nuclear@0
|
88 free(buffer);
|
nuclear@0
|
89 }
|
nuclear@0
|
90
|
nuclear@0
|
91 int kb_isdown(int key)
|
nuclear@0
|
92 {
|
nuclear@0
|
93 if(key == KB_ANY) {
|
nuclear@0
|
94 return num_pressed;
|
nuclear@0
|
95 }
|
nuclear@0
|
96 return (int)keystate[key];
|
nuclear@0
|
97 }
|
nuclear@0
|
98
|
nuclear@0
|
99 void kb_wait(void)
|
nuclear@0
|
100 {
|
nuclear@0
|
101 int key;
|
nuclear@0
|
102 while((key = kb_getkey()) == -1) {
|
nuclear@0
|
103 /* put the processor to sleep while waiting for keypresses, but first
|
nuclear@0
|
104 * make sure interrupts are enabled, or we'll sleep forever
|
nuclear@0
|
105 */
|
nuclear@0
|
106 __asm {
|
nuclear@0
|
107 sti
|
nuclear@0
|
108 hlt
|
nuclear@0
|
109 }
|
nuclear@0
|
110 }
|
nuclear@0
|
111 kb_putback(key);
|
nuclear@0
|
112 }
|
nuclear@0
|
113
|
nuclear@0
|
114 int kb_getkey(void)
|
nuclear@0
|
115 {
|
nuclear@0
|
116 int res;
|
nuclear@0
|
117
|
nuclear@0
|
118 if(buffer) {
|
nuclear@0
|
119 if(buf_ridx == buf_widx) {
|
nuclear@0
|
120 return -1;
|
nuclear@0
|
121 }
|
nuclear@0
|
122 res = buffer[buf_ridx];
|
nuclear@0
|
123 ADVANCE(buf_ridx);
|
nuclear@0
|
124 } else {
|
nuclear@0
|
125 res = last_key;
|
nuclear@0
|
126 last_key = -1;
|
nuclear@0
|
127 }
|
nuclear@0
|
128 return res;
|
nuclear@0
|
129 }
|
nuclear@0
|
130
|
nuclear@0
|
131 void kb_putback(int key)
|
nuclear@0
|
132 {
|
nuclear@0
|
133 if(buffer) {
|
nuclear@0
|
134 /* go back a place */
|
nuclear@0
|
135 if(--buf_ridx < 0) {
|
nuclear@0
|
136 buf_ridx += buffer_size;
|
nuclear@0
|
137 }
|
nuclear@0
|
138
|
nuclear@0
|
139 /* if the write end hasn't caught up with us, go back one place
|
nuclear@0
|
140 * and put it there, otherwise just overwrite the oldest key which
|
nuclear@0
|
141 * is right where we were.
|
nuclear@0
|
142 */
|
nuclear@0
|
143 if(buf_ridx == buf_widx) {
|
nuclear@0
|
144 ADVANCE(buf_ridx);
|
nuclear@0
|
145 }
|
nuclear@0
|
146
|
nuclear@0
|
147 buffer[buf_ridx] = key;
|
nuclear@0
|
148 } else {
|
nuclear@0
|
149 last_key = key;
|
nuclear@0
|
150 }
|
nuclear@0
|
151 }
|
nuclear@0
|
152
|
nuclear@0
|
153 static void __interrupt __far kbintr()
|
nuclear@0
|
154 {
|
nuclear@0
|
155 unsigned char code;
|
nuclear@0
|
156 int key, press;
|
nuclear@0
|
157
|
nuclear@0
|
158 code = inp(KB_PORT);
|
nuclear@0
|
159
|
nuclear@0
|
160 if(code >= 128) {
|
nuclear@0
|
161 press = 0;
|
nuclear@0
|
162 code -= 128;
|
nuclear@0
|
163
|
nuclear@0
|
164 if(num_pressed > 0) {
|
nuclear@0
|
165 num_pressed--;
|
nuclear@0
|
166 }
|
nuclear@0
|
167 } else {
|
nuclear@0
|
168 press = 1;
|
nuclear@0
|
169
|
nuclear@0
|
170 num_pressed++;
|
nuclear@0
|
171 }
|
nuclear@0
|
172
|
nuclear@0
|
173 key = scantbl[code];
|
nuclear@0
|
174
|
nuclear@0
|
175 if(press) {
|
nuclear@0
|
176 /* append to buffer */
|
nuclear@0
|
177 last_key = key;
|
nuclear@0
|
178 if(buffer_size > 0) {
|
nuclear@0
|
179 buffer[buf_widx] = key;
|
nuclear@0
|
180 ADVANCE(buf_widx);
|
nuclear@0
|
181 /* if the write end overtook the read end, advance the read end
|
nuclear@0
|
182 * too, to discard the oldest keypress from the buffer
|
nuclear@0
|
183 */
|
nuclear@0
|
184 if(buf_widx == buf_ridx) {
|
nuclear@0
|
185 ADVANCE(buf_ridx);
|
nuclear@0
|
186 }
|
nuclear@0
|
187 }
|
nuclear@0
|
188 }
|
nuclear@0
|
189
|
nuclear@0
|
190 /* and update keystate table */
|
nuclear@0
|
191 keystate[key] = press;
|
nuclear@0
|
192
|
nuclear@0
|
193 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */
|
nuclear@0
|
194 }
|