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/>
42 #define PIC1_CMD_PORT 0x20
43 #define OCW2_EOI (1 << 5)
46 #define INTERRUPT __interrupt __far
48 #define DONE_INIT (prev_handler)
49 static void (INTERRUPT *prev_handler)();
55 #define DONE_INIT prev_intr.pm_offset
56 static _go32_dpmi_seginfo intr, prev_intr;
58 #define outp(p, v) outportb(p, v)
59 #define inp(p) inportb(p)
62 static void INTERRUPT kbintr();
65 static int buffer_size, buf_ridx, buf_widx;
68 static unsigned int num_pressed;
69 static unsigned char keystate[256];
71 #define ADVANCE(x) ((x) = ((x) + 1) % buffer_size)
73 int kb_init(int bufsz)
76 fprintf(stderr, "keyboard driver already initialized!\n");
81 if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
82 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
85 buf_ridx = buf_widx = 0;
88 memset(keystate, 0, sizeof keystate);
91 /* set our interrupt handler */
94 prev_handler = _dos_getvect(KB_INTR);
95 _dos_setvect(KB_INTR, kbintr);
98 _go32_dpmi_get_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
99 intr.pm_offset = (intptr_t)kbintr;
100 intr.pm_selector = _go32_my_cs();
101 _go32_dpmi_allocate_iret_wrapper(&intr);
102 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &intr);
109 void kb_shutdown(void)
115 /* restore the original interrupt handler */
118 _dos_setvect(KB_INTR, prev_handler);
121 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
122 _go32_dpmi_free_iret_wrapper(&intr);
129 int kb_isdown(int key)
136 return keystate[KB_LALT] + keystate[KB_RALT];
139 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
141 return keystate[key];
152 #define halt() asm volatile("sti\n\thlt\n\t")
158 while((key = kb_getkey()) == -1) {
159 /* put the processor to sleep while waiting for keypresses, but first
160 * make sure interrupts are enabled, or we'll sleep forever
172 if(buf_ridx == buf_widx) {
175 res = buffer[buf_ridx];
184 void kb_putback(int key)
187 /* go back a place */
189 buf_ridx += buffer_size;
192 /* if the write end hasn't caught up with us, go back one place
193 * and put it there, otherwise just overwrite the oldest key which
194 * is right where we were.
196 if(buf_ridx == buf_widx) {
200 buffer[buf_ridx] = key;
206 static void INTERRUPT kbintr()
217 if(num_pressed > 0) {
229 /* append to buffer */
231 if(buffer_size > 0) {
232 buffer[buf_widx] = key;
234 /* if the write end overtook the read end, advance the read end
235 * too, to discard the oldest keypress from the buffer
237 if(buf_widx == buf_ridx) {
243 /* and update keystate table */
244 keystate[key] = press;
246 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */