rayzor

view src/keyb.c @ 0:2a5340a6eee4

rayzor first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 08:46:27 +0300
parents
children d94a69933a71
line source
1 /*
2 DOS interrupt-based keyboard driver.
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with the program. If not, see <http://www.gnu.org/licenses/>
17 */
18 #define KEYB_C_
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <conio.h>
24 #include <dos.h>
25 #include <i86.h>
26 #include "keyb.h"
27 #include "scancode.h"
29 #define KB_INTR 0x9
30 #define KB_PORT 0x60
32 #define PIC1_CMD_PORT 0x20
33 #define OCW2_EOI (1 << 5)
35 #define DONE_INIT (prev_handler)
37 static void __interrupt __far kbintr();
39 static void (__interrupt __far *prev_handler)();
41 static int *buffer;
42 static int buffer_size, buf_ridx, buf_widx;
43 static int last_key;
45 static unsigned int num_pressed;
46 static unsigned char keystate[256];
48 #define ADVANCE(x) ((x) = ((x) + 1) % buffer_size)
50 int kb_init(int bufsz)
51 {
52 if(DONE_INIT) {
53 fprintf(stderr, "keyboard driver already initialized!\n");
54 return 0;
55 }
57 buffer_size = bufsz;
58 if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
59 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
60 buffer_size = 0;
61 }
62 buf_ridx = buf_widx = 0;
63 last_key = -1;
65 memset(keystate, 0, sizeof keystate);
66 num_pressed = 0;
68 /* set our interrupt handler */
69 _disable();
70 prev_handler = _dos_getvect(KB_INTR);
71 _dos_setvect(KB_INTR, kbintr);
72 _enable();
74 return 0;
75 }
77 void kb_shutdown(void)
78 {
79 if(!DONE_INIT) {
80 return;
81 }
83 /* restore the original interrupt handler */
84 _disable();
85 _dos_setvect(KB_INTR, prev_handler);
86 _enable();
88 free(buffer);
89 }
91 int kb_isdown(int key)
92 {
93 if(key == KB_ANY) {
94 return num_pressed;
95 }
96 return (int)keystate[key];
97 }
99 void kb_wait(void)
100 {
101 int key;
102 while((key = kb_getkey()) == -1) {
103 /* put the processor to sleep while waiting for keypresses, but first
104 * make sure interrupts are enabled, or we'll sleep forever
105 */
106 __asm {
107 sti
108 hlt
109 }
110 }
111 kb_putback(key);
112 }
114 int kb_getkey(void)
115 {
116 int res;
118 if(buffer) {
119 if(buf_ridx == buf_widx) {
120 return -1;
121 }
122 res = buffer[buf_ridx];
123 ADVANCE(buf_ridx);
124 } else {
125 res = last_key;
126 last_key = -1;
127 }
128 return res;
129 }
131 void kb_putback(int key)
132 {
133 if(buffer) {
134 /* go back a place */
135 if(--buf_ridx < 0) {
136 buf_ridx += buffer_size;
137 }
139 /* if the write end hasn't caught up with us, go back one place
140 * and put it there, otherwise just overwrite the oldest key which
141 * is right where we were.
142 */
143 if(buf_ridx == buf_widx) {
144 ADVANCE(buf_ridx);
145 }
147 buffer[buf_ridx] = key;
148 } else {
149 last_key = key;
150 }
151 }
153 static void __interrupt __far kbintr()
154 {
155 unsigned char code;
156 int key, press;
158 code = inp(KB_PORT);
160 if(code >= 128) {
161 press = 0;
162 code -= 128;
164 if(num_pressed > 0) {
165 num_pressed--;
166 }
167 } else {
168 press = 1;
170 num_pressed++;
171 }
173 key = scantbl[code];
175 if(press) {
176 /* append to buffer */
177 last_key = key;
178 if(buffer_size > 0) {
179 buffer[buf_widx] = key;
180 ADVANCE(buf_widx);
181 /* if the write end overtook the read end, advance the read end
182 * too, to discard the oldest keypress from the buffer
183 */
184 if(buf_widx == buf_ridx) {
185 ADVANCE(buf_ridx);
186 }
187 }
188 }
190 /* and update keystate table */
191 keystate[key] = press;
193 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */
194 }