2 DOS interrupt-based keyboard driver.
3 Copyright (C) 2013-2023 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/>
44 #define PIC1_CMD_PORT 0x20
45 #define OCW2_EOI (1 << 5)
48 #define INTERRUPT __interrupt __far
50 #define DONE_INIT (prev_handler)
51 static void (INTERRUPT *prev_handler)();
57 #define DONE_INIT prev_intr.pm_offset
58 static _go32_dpmi_seginfo intr, prev_intr;
61 static void INTERRUPT kbintr();
64 static int buffer[BUFSIZE];
65 static int buf_ridx, buf_widx;
68 static unsigned int num_pressed;
69 static unsigned char keystate[512];
71 #define ADVANCE(x) ((x) = ((x) + 1) & (BUFSIZE - 1))
76 fprintf(stderr, "keyboard driver already initialized!\n");
80 buf_ridx = buf_widx = 0;
83 memset(keystate, 0, sizeof keystate);
86 /* set our interrupt handler */
89 prev_handler = _dos_getvect(KEY_INTR);
90 _dos_setvect(KEY_INTR, kbintr);
93 _go32_dpmi_get_protected_mode_interrupt_vector(KEY_INTR, &prev_intr);
94 intr.pm_offset = (intptr_t)kbintr;
95 intr.pm_selector = _go32_my_cs();
96 _go32_dpmi_allocate_iret_wrapper(&intr);
97 _go32_dpmi_set_protected_mode_interrupt_vector(KEY_INTR, &intr);
102 void kb_shutdown(void)
108 /* restore the original interrupt handler */
111 _dos_setvect(KEY_INTR, prev_handler);
114 _go32_dpmi_set_protected_mode_interrupt_vector(KEY_INTR, &prev_intr);
115 _go32_dpmi_free_iret_wrapper(&intr);
120 int kb_isdown(int key)
127 return keystate[KEY_LALT] + keystate[KEY_RALT];
130 return keystate[KEY_LCTRL] + keystate[KEY_RCTRL];
133 return keystate[KEY_LSHIFT] + keystate[KEY_RSHIFT];
139 return keystate[key];
150 #define halt() asm volatile("sti\n\thlt\n\t")
156 while((key = kb_getkey()) == -1) {
158 /* put the processor to sleep while waiting for keypresses, but first
159 * 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 */
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()
212 code = inp(KEY_PORT);
223 if(num_pressed > 0) {
233 key = scantbl_ext[code];
238 c = (keystate[KEY_LSHIFT] | keystate[KEY_RSHIFT]) ? scantbl_shift[code] : key;
242 /* append to buffer */
244 buffer[buf_widx] = c;
246 /* if the write end overtook the read end, advance the read end
247 * too, to discard the oldest keypress from the buffer
249 if(buf_widx == buf_ridx) {
254 /* and update keystate table */
255 keystate[key] = press;
258 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */