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/>
43 #define PIC1_CMD_PORT 0x20
44 #define OCW2_EOI (1 << 5)
47 #define INTERRUPT __interrupt __far
49 #define DONE_INIT (prev_handler)
50 static void (INTERRUPT *prev_handler)();
56 #define DONE_INIT prev_intr.pm_offset
57 static _go32_dpmi_seginfo intr, prev_intr;
59 #define outp(p, v) outportb(p, v)
60 #define inp(p) inportb(p)
63 static void INTERRUPT kbintr();
66 static int buffer_size, buf_ridx, buf_widx;
69 static unsigned int num_pressed;
70 static unsigned char keystate[256];
72 #define ADVANCE(x) ((x) = ((x) + 1) % buffer_size)
74 int kb_init(int bufsz)
77 fprintf(stderr, "keyboard driver already initialized!\n");
82 if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
83 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
86 buf_ridx = buf_widx = 0;
89 memset(keystate, 0, sizeof keystate);
92 /* set our interrupt handler */
95 prev_handler = _dos_getvect(KB_INTR);
96 _dos_setvect(KB_INTR, kbintr);
99 _go32_dpmi_get_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
100 intr.pm_offset = (intptr_t)kbintr;
101 intr.pm_selector = _go32_my_cs();
102 _go32_dpmi_allocate_iret_wrapper(&intr);
103 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &intr);
110 void kb_shutdown(void)
116 /* restore the original interrupt handler */
119 _dos_setvect(KB_INTR, prev_handler);
122 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
123 _go32_dpmi_free_iret_wrapper(&intr);
130 int kb_isdown(int key)
137 return keystate[KB_LALT] + keystate[KB_RALT];
140 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
146 return keystate[key];
157 #define halt() asm volatile("sti\n\thlt\n\t")
163 while((key = kb_getkey()) == -1) {
165 /* put the processor to sleep while waiting for keypresses, but first
166 * make sure interrupts are enabled, or we'll sleep forever
179 if(buf_ridx == buf_widx) {
182 res = buffer[buf_ridx];
191 void kb_putback(int key)
194 /* go back a place */
196 buf_ridx += buffer_size;
199 /* if the write end hasn't caught up with us, go back one place
200 * and put it there, otherwise just overwrite the oldest key which
201 * is right where we were.
203 if(buf_ridx == buf_widx) {
207 buffer[buf_ridx] = key;
213 static void INTERRUPT kbintr()
224 if(num_pressed > 0) {
234 c = (keystate[KB_LSHIFT] | keystate[KB_RSHIFT]) ? scantbl_shift[code] : key;
237 /* append to buffer */
239 if(buffer_size > 0) {
240 buffer[buf_widx] = c;
242 /* if the write end overtook the read end, advance the read end
243 * too, to discard the oldest keypress from the buffer
245 if(buf_widx == buf_ridx) {
251 /* and update keystate table */
252 keystate[key] = press;
254 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */