1 | /*
|
---|
2 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
3 | *
|
---|
4 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
5 | * available from http://www.virtualbox.org. This file is free software;
|
---|
6 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
7 | * General Public License (GPL) as published by the Free Software
|
---|
8 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
9 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
10 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
11 | * --------------------------------------------------------------------
|
---|
12 | *
|
---|
13 | * This code is based on:
|
---|
14 | *
|
---|
15 | * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
|
---|
16 | *
|
---|
17 | * Copyright (C) 2002 MandrakeSoft S.A.
|
---|
18 | *
|
---|
19 | * MandrakeSoft S.A.
|
---|
20 | * 43, rue d'Aboukir
|
---|
21 | * 75002 Paris - France
|
---|
22 | * http://www.linux-mandrake.com/
|
---|
23 | * http://www.mandrakesoft.com/
|
---|
24 | *
|
---|
25 | * This library is free software; you can redistribute it and/or
|
---|
26 | * modify it under the terms of the GNU Lesser General Public
|
---|
27 | * License as published by the Free Software Foundation; either
|
---|
28 | * version 2 of the License, or (at your option) any later version.
|
---|
29 | *
|
---|
30 | * This library is distributed in the hope that it will be useful,
|
---|
31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
33 | * Lesser General Public License for more details.
|
---|
34 | *
|
---|
35 | * You should have received a copy of the GNU Lesser General Public
|
---|
36 | * License along with this library; if not, write to the Free Software
|
---|
37 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
38 | *
|
---|
39 | */
|
---|
40 |
|
---|
41 |
|
---|
42 | #include <stdint.h>
|
---|
43 | #include "inlines.h"
|
---|
44 | #include "biosint.h"
|
---|
45 |
|
---|
46 | #if DEBUG_INT16
|
---|
47 | # define BX_DEBUG_INT16(...) BX_DEBUG(__VA_ARGS__)
|
---|
48 | #else
|
---|
49 | # define BX_DEBUG_INT16(...)
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | extern void post(void);
|
---|
53 | #pragma aux post "*";
|
---|
54 |
|
---|
55 | void jmp_post(void);
|
---|
56 | #pragma aux jmp_post = "jmp far ptr post" aborts;
|
---|
57 |
|
---|
58 | #define none 0
|
---|
59 | #define MAX_SCAN_CODE 0x58
|
---|
60 |
|
---|
61 | struct {
|
---|
62 | uint16_t normal;
|
---|
63 | uint16_t shift;
|
---|
64 | uint16_t control;
|
---|
65 | uint16_t alt;
|
---|
66 | uint8_t lock_flags;
|
---|
67 | } static const scan_to_scanascii[MAX_SCAN_CODE + 1] = {
|
---|
68 | { none, none, none, none, none },
|
---|
69 | { 0x011b, 0x011b, 0x011b, 0x0100, none }, /* escape */
|
---|
70 | { 0x0231, 0x0221, none, 0x7800, none }, /* 1! */
|
---|
71 | { 0x0332, 0x0340, 0x0300, 0x7900, none }, /* 2@ */
|
---|
72 | { 0x0433, 0x0423, none, 0x7a00, none }, /* 3# */
|
---|
73 | { 0x0534, 0x0524, none, 0x7b00, none }, /* 4$ */
|
---|
74 | { 0x0635, 0x0625, none, 0x7c00, none }, /* 5% */
|
---|
75 | { 0x0736, 0x075e, 0x071e, 0x7d00, none }, /* 6^ */
|
---|
76 | { 0x0837, 0x0826, none, 0x7e00, none }, /* 7& */
|
---|
77 | { 0x0938, 0x092a, none, 0x7f00, none }, /* 8* */
|
---|
78 | { 0x0a39, 0x0a28, none, 0x8000, none }, /* 9( */
|
---|
79 | { 0x0b30, 0x0b29, none, 0x8100, none }, /* 0) */
|
---|
80 | { 0x0c2d, 0x0c5f, 0x0c1f, 0x8200, none }, /* -_ */
|
---|
81 | { 0x0d3d, 0x0d2b, none, 0x8300, none }, /* =+ */
|
---|
82 | { 0x0e08, 0x0e08, 0x0e7f, none, none }, /* backspace */
|
---|
83 | { 0x0f09, 0x0f00, none, none, none }, /* tab */
|
---|
84 | { 0x1071, 0x1051, 0x1011, 0x1000, 0x40 }, /* Q */
|
---|
85 | { 0x1177, 0x1157, 0x1117, 0x1100, 0x40 }, /* W */
|
---|
86 | { 0x1265, 0x1245, 0x1205, 0x1200, 0x40 }, /* E */
|
---|
87 | { 0x1372, 0x1352, 0x1312, 0x1300, 0x40 }, /* R */
|
---|
88 | { 0x1474, 0x1454, 0x1414, 0x1400, 0x40 }, /* T */
|
---|
89 | { 0x1579, 0x1559, 0x1519, 0x1500, 0x40 }, /* Y */
|
---|
90 | { 0x1675, 0x1655, 0x1615, 0x1600, 0x40 }, /* U */
|
---|
91 | { 0x1769, 0x1749, 0x1709, 0x1700, 0x40 }, /* I */
|
---|
92 | { 0x186f, 0x184f, 0x180f, 0x1800, 0x40 }, /* O */
|
---|
93 | { 0x1970, 0x1950, 0x1910, 0x1900, 0x40 }, /* P */
|
---|
94 | { 0x1a5b, 0x1a7b, 0x1a1b, none, none }, /* [{ */
|
---|
95 | { 0x1b5d, 0x1b7d, 0x1b1d, none, none }, /* ]} */
|
---|
96 | { 0x1c0d, 0x1c0d, 0x1c0a, none, none }, /* Enter */
|
---|
97 | { none, none, none, none, none }, /* L Ctrl */
|
---|
98 | { 0x1e61, 0x1e41, 0x1e01, 0x1e00, 0x40 }, /* A */
|
---|
99 | { 0x1f73, 0x1f53, 0x1f13, 0x1f00, 0x40 }, /* S */
|
---|
100 | { 0x2064, 0x2044, 0x2004, 0x2000, 0x40 }, /* D */
|
---|
101 | { 0x2166, 0x2146, 0x2106, 0x2100, 0x40 }, /* F */
|
---|
102 | { 0x2267, 0x2247, 0x2207, 0x2200, 0x40 }, /* G */
|
---|
103 | { 0x2368, 0x2348, 0x2308, 0x2300, 0x40 }, /* H */
|
---|
104 | { 0x246a, 0x244a, 0x240a, 0x2400, 0x40 }, /* J */
|
---|
105 | { 0x256b, 0x254b, 0x250b, 0x2500, 0x40 }, /* K */
|
---|
106 | { 0x266c, 0x264c, 0x260c, 0x2600, 0x40 }, /* L */
|
---|
107 | { 0x273b, 0x273a, none, none, none }, /* ;: */
|
---|
108 | { 0x2827, 0x2822, none, none, none }, /* '" */
|
---|
109 | { 0x2960, 0x297e, none, none, none }, /* `~ */
|
---|
110 | { none, none, none, none, none }, /* L shift */
|
---|
111 | { 0x2b5c, 0x2b7c, 0x2b1c, none, none }, /* |\ */
|
---|
112 | { 0x2c7a, 0x2c5a, 0x2c1a, 0x2c00, 0x40 }, /* Z */
|
---|
113 | { 0x2d78, 0x2d58, 0x2d18, 0x2d00, 0x40 }, /* X */
|
---|
114 | { 0x2e63, 0x2e43, 0x2e03, 0x2e00, 0x40 }, /* C */
|
---|
115 | { 0x2f76, 0x2f56, 0x2f16, 0x2f00, 0x40 }, /* V */
|
---|
116 | { 0x3062, 0x3042, 0x3002, 0x3000, 0x40 }, /* B */
|
---|
117 | { 0x316e, 0x314e, 0x310e, 0x3100, 0x40 }, /* N */
|
---|
118 | { 0x326d, 0x324d, 0x320d, 0x3200, 0x40 }, /* M */
|
---|
119 | { 0x332c, 0x333c, none, none, none }, /* ,< */
|
---|
120 | { 0x342e, 0x343e, none, none, none }, /* .> */
|
---|
121 | { 0x352f, 0x353f, none, none, none }, /* /? */
|
---|
122 | { none, none, none, none, none }, /* R Shift */
|
---|
123 | { 0x372a, 0x372a, none, none, none }, /* * */
|
---|
124 | { none, none, none, none, none }, /* L Alt */
|
---|
125 | { 0x3920, 0x3920, 0x3920, 0x3920, none }, /* space */
|
---|
126 | { none, none, none, none, none }, /* caps lock */
|
---|
127 | { 0x3b00, 0x5400, 0x5e00, 0x6800, none }, /* F1 */
|
---|
128 | { 0x3c00, 0x5500, 0x5f00, 0x6900, none }, /* F2 */
|
---|
129 | { 0x3d00, 0x5600, 0x6000, 0x6a00, none }, /* F3 */
|
---|
130 | { 0x3e00, 0x5700, 0x6100, 0x6b00, none }, /* F4 */
|
---|
131 | { 0x3f00, 0x5800, 0x6200, 0x6c00, none }, /* F5 */
|
---|
132 | { 0x4000, 0x5900, 0x6300, 0x6d00, none }, /* F6 */
|
---|
133 | { 0x4100, 0x5a00, 0x6400, 0x6e00, none }, /* F7 */
|
---|
134 | { 0x4200, 0x5b00, 0x6500, 0x6f00, none }, /* F8 */
|
---|
135 | { 0x4300, 0x5c00, 0x6600, 0x7000, none }, /* F9 */
|
---|
136 | { 0x4400, 0x5d00, 0x6700, 0x7100, none }, /* F10 */
|
---|
137 | { none, none, none, none, none }, /* Num Lock */
|
---|
138 | { none, none, none, none, none }, /* Scroll Lock */
|
---|
139 | { 0x4700, 0x4737, 0x7700, none, 0x20 }, /* 7 Home */
|
---|
140 | { 0x4800, 0x4838, none, none, 0x20 }, /* 8 UP */
|
---|
141 | { 0x4900, 0x4939, 0x8400, none, 0x20 }, /* 9 PgUp */
|
---|
142 | { 0x4a2d, 0x4a2d, none, none, none }, /* - */
|
---|
143 | { 0x4b00, 0x4b34, 0x7300, none, 0x20 }, /* 4 Left */
|
---|
144 | { 0x4c00, 0x4c35, none, none, 0x20 }, /* 5 */
|
---|
145 | { 0x4d00, 0x4d36, 0x7400, none, 0x20 }, /* 6 Right */
|
---|
146 | { 0x4e2b, 0x4e2b, none, none, none }, /* + */
|
---|
147 | { 0x4f00, 0x4f31, 0x7500, none, 0x20 }, /* 1 End */
|
---|
148 | { 0x5000, 0x5032, none, none, 0x20 }, /* 2 Down */
|
---|
149 | { 0x5100, 0x5133, 0x7600, none, 0x20 }, /* 3 PgDn */
|
---|
150 | { 0x5200, 0x5230, none, none, 0x20 }, /* 0 Ins */
|
---|
151 | { 0x5300, 0x532e, none, none, 0x20 }, /* Del */
|
---|
152 | { none, none, none, none, none },
|
---|
153 | { none, none, none, none, none },
|
---|
154 | { 0x565c, 0x567c, none, none, none }, /* \| */
|
---|
155 | { 0x8500, 0x8700, 0x8900, 0x8b00, none }, /* F11 */
|
---|
156 | { 0x8600, 0x8800, 0x8a00, 0x8c00, none } /* F12 */
|
---|
157 | };
|
---|
158 |
|
---|
159 |
|
---|
160 | /* Keyboard initialization. */
|
---|
161 |
|
---|
162 | //--------------------------------------------------------------------------
|
---|
163 | // keyboard_panic
|
---|
164 | //--------------------------------------------------------------------------
|
---|
165 | void keyboard_panic(uint16_t status)
|
---|
166 | {
|
---|
167 | // If you're getting a 993 keyboard panic here,
|
---|
168 | // please see the comment in keyboard_init
|
---|
169 |
|
---|
170 | BX_PANIC("Keyboard error:%u\n",status);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | //--------------------------------------------------------------------------
|
---|
175 | // keyboard_init
|
---|
176 | //--------------------------------------------------------------------------
|
---|
177 | // this file is based on LinuxBIOS implementation of keyboard.c
|
---|
178 | // could convert to #asm to gain space
|
---|
179 | void BIOSCALL keyboard_init(void)
|
---|
180 | {
|
---|
181 | uint16_t max;
|
---|
182 |
|
---|
183 | /* ------------------- controller side ----------------------*/
|
---|
184 | /* send cmd = 0xAA, self test 8042 */
|
---|
185 | outb(0x64, 0xaa);
|
---|
186 |
|
---|
187 | /* Wait until buffer is empty */
|
---|
188 | max=0xffff;
|
---|
189 | while ( (inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x00);
|
---|
190 | if (max==0x0) keyboard_panic(00);
|
---|
191 |
|
---|
192 | /* Wait for data */
|
---|
193 | max=0xffff;
|
---|
194 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x01);
|
---|
195 | if (max==0x0) keyboard_panic(01);
|
---|
196 |
|
---|
197 | /* read self-test result, 0x55 should be returned from 0x60 */
|
---|
198 | if ((inb(0x60) != 0x55)){
|
---|
199 | keyboard_panic(991);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* send cmd = 0xAB, keyboard interface test */
|
---|
203 | outb(0x64,0xab);
|
---|
204 |
|
---|
205 | /* Wait until buffer is empty */
|
---|
206 | max=0xffff;
|
---|
207 | while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x10);
|
---|
208 | if (max==0x0) keyboard_panic(10);
|
---|
209 |
|
---|
210 | /* Wait for data */
|
---|
211 | max=0xffff;
|
---|
212 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x11);
|
---|
213 | if (max==0x0) keyboard_panic(11);
|
---|
214 |
|
---|
215 | /* read keyboard interface test result, */
|
---|
216 | /* 0x00 should be returned form 0x60 */
|
---|
217 | if ((inb(0x60) != 0x00)) {
|
---|
218 | keyboard_panic(992);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* ------------------- keyboard side ------------------------*/
|
---|
222 | /* reset keyboard and self test (keyboard side) */
|
---|
223 | /* also enables the keyboard interface */
|
---|
224 | outb(0x60, 0xff);
|
---|
225 |
|
---|
226 | /* Wait until buffer is empty */
|
---|
227 | max=0xffff;
|
---|
228 | while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x20);
|
---|
229 | if (max==0x0) keyboard_panic(20);
|
---|
230 |
|
---|
231 | /* Wait for data */
|
---|
232 | max=0xffff;
|
---|
233 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x21);
|
---|
234 | if (max==0x0) keyboard_panic(21);
|
---|
235 |
|
---|
236 | /* keyboard should return ACK */
|
---|
237 | if ((inb(0x60) != 0xfa)) {
|
---|
238 | keyboard_panic(993);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Wait for data */
|
---|
242 | max=0xffff;
|
---|
243 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x31);
|
---|
244 | if (max==0x0) keyboard_panic(31);
|
---|
245 |
|
---|
246 | if ((inb(0x60) != 0xaa && inb(0x60) != 0xaa)) {
|
---|
247 | keyboard_panic(994);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* Disable keyboard */
|
---|
251 | outb(0x60, 0xf5);
|
---|
252 |
|
---|
253 | /* Wait until buffer is empty */
|
---|
254 | max=0xffff;
|
---|
255 | while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x40);
|
---|
256 | if (max==0x0) keyboard_panic(40);
|
---|
257 |
|
---|
258 | /* Wait for data */
|
---|
259 | max=0xffff;
|
---|
260 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x41);
|
---|
261 | if (max==0x0) keyboard_panic(41);
|
---|
262 |
|
---|
263 | /* keyboard should return ACK */
|
---|
264 | if ((inb(0x60) != 0xfa)) {
|
---|
265 | keyboard_panic(995);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* Write Keyboard Mode */
|
---|
269 | outb(0x64, 0x60);
|
---|
270 |
|
---|
271 | /* Wait until buffer is empty */
|
---|
272 | max=0xffff;
|
---|
273 | while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x50);
|
---|
274 | if (max==0x0) keyboard_panic(50);
|
---|
275 |
|
---|
276 | /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
|
---|
277 | outb(0x60, 0x65);
|
---|
278 |
|
---|
279 | /* Wait until buffer is empty */
|
---|
280 | max=0xffff;
|
---|
281 | while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x60);
|
---|
282 | if (max==0x0) keyboard_panic(60);
|
---|
283 |
|
---|
284 | /* Enable keyboard */
|
---|
285 | outb(0x60, 0xf4);
|
---|
286 |
|
---|
287 | /* Wait until buffer is empty */
|
---|
288 | max=0xffff;
|
---|
289 | while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x70);
|
---|
290 | if (max==0x0) keyboard_panic(70);
|
---|
291 |
|
---|
292 | /* Wait for data */
|
---|
293 | max=0xffff;
|
---|
294 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x71);
|
---|
295 | if (max==0x0) keyboard_panic(70);
|
---|
296 |
|
---|
297 | /* keyboard should return ACK */
|
---|
298 | if ((inb(0x60) != 0xfa)) {
|
---|
299 | keyboard_panic(996);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* Enable aux interface */
|
---|
303 | outb(0x64,0xa8);
|
---|
304 |
|
---|
305 | /* While we're here, disable the A20 gate. Required for
|
---|
306 | * compatibility with the IBM PC and DOS.
|
---|
307 | */
|
---|
308 | set_enable_a20(0);
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | unsigned int enqueue_key(uint8_t scan_code, uint8_t ascii_code)
|
---|
313 | {
|
---|
314 | uint16_t buffer_start, buffer_end, buffer_head, buffer_tail, temp_tail;
|
---|
315 |
|
---|
316 | #if BX_CPU < 2
|
---|
317 | buffer_start = 0x001E;
|
---|
318 | buffer_end = 0x003E;
|
---|
319 | #else
|
---|
320 | buffer_start = read_word(0x0040, 0x0080);
|
---|
321 | buffer_end = read_word(0x0040, 0x0082);
|
---|
322 | #endif
|
---|
323 |
|
---|
324 | buffer_head = read_word(0x0040, 0x001A);
|
---|
325 | buffer_tail = read_word(0x0040, 0x001C);
|
---|
326 |
|
---|
327 | temp_tail = buffer_tail;
|
---|
328 | buffer_tail += 2;
|
---|
329 | if (buffer_tail >= buffer_end)
|
---|
330 | buffer_tail = buffer_start;
|
---|
331 |
|
---|
332 | if (buffer_tail == buffer_head)
|
---|
333 | return(0);
|
---|
334 |
|
---|
335 | write_byte(0x0040, temp_tail, ascii_code);
|
---|
336 | write_byte(0x0040, temp_tail+1, scan_code);
|
---|
337 | write_word(0x0040, 0x001C, buffer_tail);
|
---|
338 | return(1);
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /* Keyboard hardware interrupt handler. */
|
---|
343 | //@todo: should this be declared as taking arguments at all?
|
---|
344 | void BIOSCALL int09_function(uint16_t ES, uint16_t DI, uint16_t SI, uint16_t BP, uint16_t SP,
|
---|
345 | uint16_t BX, uint16_t DX, uint16_t CX, uint16_t AX)
|
---|
346 | {
|
---|
347 | uint8_t scancode, asciicode, shift_flags;
|
---|
348 | uint8_t mf2_flags, mf2_state, flag;
|
---|
349 |
|
---|
350 | //
|
---|
351 | // DS has been set to F000 before call
|
---|
352 | //
|
---|
353 |
|
---|
354 |
|
---|
355 | scancode = GET_AL();
|
---|
356 |
|
---|
357 | if (scancode == 0) {
|
---|
358 | BX_INFO("KBD: int09 handler: AL=0\n");
|
---|
359 | return;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | shift_flags = read_byte(0x0040, 0x17);
|
---|
364 | mf2_flags = read_byte(0x0040, 0x18);
|
---|
365 | mf2_state = read_byte(0x0040, 0x96);
|
---|
366 | asciicode = 0;
|
---|
367 |
|
---|
368 | switch (scancode) {
|
---|
369 | case 0x3a: /* Caps Lock press */
|
---|
370 | shift_flags ^= 0x40;
|
---|
371 | write_byte(0x0040, 0x17, shift_flags);
|
---|
372 | mf2_flags |= 0x40;
|
---|
373 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
374 | break;
|
---|
375 | case 0xba: /* Caps Lock release */
|
---|
376 | mf2_flags &= ~0x40;
|
---|
377 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
378 | break;
|
---|
379 |
|
---|
380 | case 0x2a: /* L Shift press */
|
---|
381 | case 0xaa: /* L Shift release */
|
---|
382 | case 0x36: /* R Shift press */
|
---|
383 | case 0xb6: /* R Shift release */
|
---|
384 | /* If this was an extended (i.e. faked) key, leave flags alone. */
|
---|
385 | if (!(mf2_state & 0x02)) {
|
---|
386 | flag = (scancode & 0x7f) == 0x2a ? 0x02 : 0x01;
|
---|
387 | if (scancode & 0x80)
|
---|
388 | shift_flags &= ~flag;
|
---|
389 | else
|
---|
390 | shift_flags |= flag;
|
---|
391 | write_byte(0x0040, 0x17, shift_flags);
|
---|
392 | }
|
---|
393 | break;
|
---|
394 |
|
---|
395 | case 0x1d: /* Ctrl press */
|
---|
396 | if ((mf2_state & 0x01) == 0) {
|
---|
397 | shift_flags |= 0x04;
|
---|
398 | write_byte(0x0040, 0x17, shift_flags);
|
---|
399 | if (mf2_state & 0x02) {
|
---|
400 | mf2_state |= 0x04;
|
---|
401 | write_byte(0x0040, 0x96, mf2_state);
|
---|
402 | } else {
|
---|
403 | mf2_flags |= 0x01;
|
---|
404 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
405 | }
|
---|
406 | }
|
---|
407 | break;
|
---|
408 | case 0x9d: /* Ctrl release */
|
---|
409 | if ((mf2_state & 0x01) == 0) {
|
---|
410 | shift_flags &= ~0x04;
|
---|
411 | write_byte(0x0040, 0x17, shift_flags);
|
---|
412 | if (mf2_state & 0x02) {
|
---|
413 | mf2_state &= ~0x04;
|
---|
414 | write_byte(0x0040, 0x96, mf2_state);
|
---|
415 | } else {
|
---|
416 | mf2_flags &= ~0x01;
|
---|
417 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
418 | }
|
---|
419 | }
|
---|
420 | break;
|
---|
421 |
|
---|
422 | case 0x38: /* Alt press */
|
---|
423 | shift_flags |= 0x08;
|
---|
424 | write_byte(0x0040, 0x17, shift_flags);
|
---|
425 | if (mf2_state & 0x02) {
|
---|
426 | mf2_state |= 0x08;
|
---|
427 | write_byte(0x0040, 0x96, mf2_state);
|
---|
428 | } else {
|
---|
429 | mf2_flags |= 0x02;
|
---|
430 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
431 | }
|
---|
432 | break;
|
---|
433 | case 0xb8: /* Alt release */
|
---|
434 | shift_flags &= ~0x08;
|
---|
435 | write_byte(0x0040, 0x17, shift_flags);
|
---|
436 | if (mf2_state & 0x02) {
|
---|
437 | mf2_state &= ~0x08;
|
---|
438 | write_byte(0x0040, 0x96, mf2_state);
|
---|
439 | } else {
|
---|
440 | mf2_flags &= ~0x02;
|
---|
441 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
442 | }
|
---|
443 | break;
|
---|
444 |
|
---|
445 | case 0x45: /* Num Lock press */
|
---|
446 | if ((mf2_state & 0x03) == 0) {
|
---|
447 | mf2_flags |= 0x20;
|
---|
448 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
449 | shift_flags ^= 0x20;
|
---|
450 | write_byte(0x0040, 0x17, shift_flags);
|
---|
451 | }
|
---|
452 | break;
|
---|
453 | case 0xc5: /* Num Lock release */
|
---|
454 | if ((mf2_state & 0x03) == 0) {
|
---|
455 | mf2_flags &= ~0x20;
|
---|
456 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
457 | }
|
---|
458 | break;
|
---|
459 |
|
---|
460 | case 0x46: /* Scroll Lock press */
|
---|
461 | mf2_flags |= 0x10;
|
---|
462 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
463 | shift_flags ^= 0x10;
|
---|
464 | write_byte(0x0040, 0x17, shift_flags);
|
---|
465 | break;
|
---|
466 |
|
---|
467 | case 0xc6: /* Scroll Lock release */
|
---|
468 | mf2_flags &= ~0x10;
|
---|
469 | write_byte(0x0040, 0x18, mf2_flags);
|
---|
470 | break;
|
---|
471 |
|
---|
472 | case 0x53: /* Del press */
|
---|
473 | if ((shift_flags & 0x0c) == 0x0c)
|
---|
474 | jmp_post();
|
---|
475 | /* fall through */
|
---|
476 |
|
---|
477 | default:
|
---|
478 | if (scancode & 0x80) {
|
---|
479 | break; /* toss key releases ... */
|
---|
480 | }
|
---|
481 | if (scancode > MAX_SCAN_CODE) {
|
---|
482 | BX_INFO("KBD: int09h_handler(): unknown scancode read: 0x%02x!\n", scancode);
|
---|
483 | return;
|
---|
484 | }
|
---|
485 | if (shift_flags & 0x08) { /* ALT */
|
---|
486 | asciicode = scan_to_scanascii[scancode].alt;
|
---|
487 | scancode = scan_to_scanascii[scancode].alt >> 8;
|
---|
488 | } else if (shift_flags & 0x04) { /* CONTROL */
|
---|
489 | asciicode = scan_to_scanascii[scancode].control;
|
---|
490 | scancode = scan_to_scanascii[scancode].control >> 8;
|
---|
491 | } else if (((mf2_state & 0x02) > 0) && ((scancode >= 0x47) && (scancode <= 0x53))) {
|
---|
492 | /* extended keys handling */
|
---|
493 | asciicode = 0xe0;
|
---|
494 | scancode = scan_to_scanascii[scancode].normal >> 8;
|
---|
495 | } else if (shift_flags & 0x03) { /* LSHIFT + RSHIFT */
|
---|
496 | /* check if lock state should be ignored
|
---|
497 | * because a SHIFT key are pressed */
|
---|
498 |
|
---|
499 | if (shift_flags & scan_to_scanascii[scancode].lock_flags) {
|
---|
500 | asciicode = scan_to_scanascii[scancode].normal;
|
---|
501 | scancode = scan_to_scanascii[scancode].normal >> 8;
|
---|
502 | } else {
|
---|
503 | asciicode = scan_to_scanascii[scancode].shift;
|
---|
504 | scancode = scan_to_scanascii[scancode].shift >> 8;
|
---|
505 | }
|
---|
506 | } else {
|
---|
507 | /* check if lock is on */
|
---|
508 | if (shift_flags & scan_to_scanascii[scancode].lock_flags) {
|
---|
509 | asciicode = scan_to_scanascii[scancode].shift;
|
---|
510 | scancode = scan_to_scanascii[scancode].shift >> 8;
|
---|
511 | } else {
|
---|
512 | asciicode = scan_to_scanascii[scancode].normal;
|
---|
513 | scancode = scan_to_scanascii[scancode].normal >> 8;
|
---|
514 | }
|
---|
515 | }
|
---|
516 | if (scancode==0 && asciicode==0) {
|
---|
517 | BX_INFO("KBD: int09h_handler(): scancode & asciicode are zero?\n");
|
---|
518 | }
|
---|
519 | enqueue_key(scancode, asciicode);
|
---|
520 | break;
|
---|
521 | }
|
---|
522 | if ((scancode & 0x7f) != 0x1d) {
|
---|
523 | mf2_state &= ~0x01;
|
---|
524 | }
|
---|
525 | mf2_state &= ~0x02;
|
---|
526 | write_byte(0x0040, 0x96, mf2_state);
|
---|
527 | }
|
---|
528 |
|
---|
529 | unsigned int dequeue_key(uint8_t __far *scan_code, uint8_t __far *ascii_code, unsigned incr)
|
---|
530 | {
|
---|
531 | uint16_t buffer_start, buffer_end, buffer_head, buffer_tail;
|
---|
532 | uint8_t acode, scode;
|
---|
533 |
|
---|
534 | #if BX_CPU < 2
|
---|
535 | buffer_start = 0x001E;
|
---|
536 | buffer_end = 0x003E;
|
---|
537 | #else
|
---|
538 | buffer_start = read_word(0x0040, 0x0080);
|
---|
539 | buffer_end = read_word(0x0040, 0x0082);
|
---|
540 | #endif
|
---|
541 |
|
---|
542 | buffer_head = read_word(0x0040, 0x001a);
|
---|
543 | buffer_tail = read_word(0x0040, 0x001c);
|
---|
544 |
|
---|
545 | if (buffer_head != buffer_tail) {
|
---|
546 | acode = read_byte(0x0040, buffer_head);
|
---|
547 | scode = read_byte(0x0040, buffer_head+1);
|
---|
548 | *ascii_code = acode;
|
---|
549 | *scan_code = scode;
|
---|
550 | BX_DEBUG_INT16("dequeue_key: ascii=%02x scan=%02x \n", acode, scode);
|
---|
551 |
|
---|
552 | if (incr) {
|
---|
553 | buffer_head += 2;
|
---|
554 | if (buffer_head >= buffer_end)
|
---|
555 | buffer_head = buffer_start;
|
---|
556 | write_word(0x0040, 0x001a, buffer_head);
|
---|
557 | }
|
---|
558 | return(1);
|
---|
559 | }
|
---|
560 | else {
|
---|
561 | return(0);
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 |
|
---|
566 | //@todo: move somewhere else?
|
---|
567 | #define AX r.gr.u.r16.ax
|
---|
568 | #define BX r.gr.u.r16.bx
|
---|
569 | #define CX r.gr.u.r16.cx
|
---|
570 | #define DX r.gr.u.r16.dx
|
---|
571 | #define SI r.gr.u.r16.si
|
---|
572 | #define DI r.gr.u.r16.di
|
---|
573 | #define BP r.gr.u.r16.bp
|
---|
574 | #define SP r.gr.u.r16.sp
|
---|
575 | #define FLAGS r.ra.flags.u.r16.flags
|
---|
576 |
|
---|
577 | /* Interrupt 16h service implementation. */
|
---|
578 |
|
---|
579 | void BIOSCALL int16_function(volatile kbd_regs_t r)
|
---|
580 | {
|
---|
581 | uint8_t scan_code, ascii_code, shift_flags, led_flags, count;
|
---|
582 | uint16_t kbd_code, max;
|
---|
583 |
|
---|
584 | BX_DEBUG_INT16("int16: AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
|
---|
585 |
|
---|
586 | shift_flags = read_byte(0x0040, 0x17);
|
---|
587 | led_flags = read_byte(0x0040, 0x97);
|
---|
588 | if ((((shift_flags >> 4) & 0x07) ^ (led_flags & 0x07)) != 0) {
|
---|
589 | int_disable(); //@todo: interrupts should be disabled already??
|
---|
590 | outb(0x60, 0xed);
|
---|
591 | while ((inb(0x64) & 0x01) == 0) outb(0x80, 0x21);
|
---|
592 | if ((inb(0x60) == 0xfa)) {
|
---|
593 | led_flags &= 0xf8;
|
---|
594 | led_flags |= ((shift_flags >> 4) & 0x07);
|
---|
595 | outb(0x60, led_flags & 0x07);
|
---|
596 | while ((inb(0x64) & 0x01) == 0)
|
---|
597 | outb(0x80, 0x21);
|
---|
598 | inb(0x60);
|
---|
599 | write_byte(0x0040, 0x97, led_flags);
|
---|
600 | }
|
---|
601 | int_enable();
|
---|
602 | }
|
---|
603 |
|
---|
604 | switch (GET_AH()) {
|
---|
605 | case 0x00: /* read keyboard input */
|
---|
606 | if ( !dequeue_key(&scan_code, &ascii_code, 1) ) {
|
---|
607 | BX_PANIC("KBD: int16h: out of keyboard input\n");
|
---|
608 | }
|
---|
609 | if (scan_code !=0 && ascii_code == 0xF0)
|
---|
610 | ascii_code = 0;
|
---|
611 | else if (ascii_code == 0xE0)
|
---|
612 | ascii_code = 0;
|
---|
613 | AX = (scan_code << 8) | ascii_code;
|
---|
614 | break;
|
---|
615 |
|
---|
616 | case 0x01: /* check keyboard status */
|
---|
617 | SET_IF(); /* Enable interrupts. Some callers depend on that! */
|
---|
618 | if ( !dequeue_key(&scan_code, &ascii_code, 0) ) {
|
---|
619 | SET_ZF();
|
---|
620 | return;
|
---|
621 | }
|
---|
622 | if (scan_code !=0 && ascii_code == 0xF0)
|
---|
623 | ascii_code = 0;
|
---|
624 | else if (ascii_code == 0xE0)
|
---|
625 | ascii_code = 0;
|
---|
626 | AX = (scan_code << 8) | ascii_code;
|
---|
627 | CLEAR_ZF();
|
---|
628 | break;
|
---|
629 |
|
---|
630 | case 0x02: /* get shift flag status */
|
---|
631 | shift_flags = read_byte(0x0040, 0x17);
|
---|
632 | SET_AL(shift_flags);
|
---|
633 | break;
|
---|
634 |
|
---|
635 | case 0x05: /* store key-stroke into buffer */
|
---|
636 | if ( !enqueue_key(GET_CH(), GET_CL()) ) {
|
---|
637 | SET_AL(1);
|
---|
638 | }
|
---|
639 | else {
|
---|
640 | SET_AL(0);
|
---|
641 | }
|
---|
642 | break;
|
---|
643 |
|
---|
644 | case 0x09: /* GET KEYBOARD FUNCTIONALITY */
|
---|
645 | // bit Bochs Description
|
---|
646 | // 7 0 reserved
|
---|
647 | // 6 0 INT 16/AH=20h-22h supported (122-key keyboard support)
|
---|
648 | // 5 1 INT 16/AH=10h-12h supported (enhanced keyboard support)
|
---|
649 | // 4 1 INT 16/AH=0Ah supported
|
---|
650 | // 3 0 INT 16/AX=0306h supported
|
---|
651 | // 2 0 INT 16/AX=0305h supported
|
---|
652 | // 1 0 INT 16/AX=0304h supported
|
---|
653 | // 0 0 INT 16/AX=0300h supported
|
---|
654 | //
|
---|
655 | SET_AL(0x30);
|
---|
656 | break;
|
---|
657 |
|
---|
658 | case 0x0A: /* GET KEYBOARD ID */
|
---|
659 | count = 2;
|
---|
660 | kbd_code = 0x0;
|
---|
661 | //@todo: Might be better to just mask the KB interrupt
|
---|
662 | int_disable();
|
---|
663 | outb(0x60, 0xf2);
|
---|
664 | /* Wait for data */
|
---|
665 | max=0xffff;
|
---|
666 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) )
|
---|
667 | inb(0x80);
|
---|
668 | if (max>0x0) {
|
---|
669 | if ((inb(0x60) == 0xfa)) {
|
---|
670 | do {
|
---|
671 | max=0xffff;
|
---|
672 | while ( ((inb(0x64) & 0x01) == 0) && (--max>0) )
|
---|
673 | inb(0x80);
|
---|
674 | if (max>0x0) {
|
---|
675 | kbd_code >>= 8;
|
---|
676 | kbd_code |= (inb(0x60) << 8);
|
---|
677 | }
|
---|
678 | } while (--count>0);
|
---|
679 | }
|
---|
680 | }
|
---|
681 | BX=kbd_code;
|
---|
682 | break;
|
---|
683 |
|
---|
684 | case 0x10: /* read MF-II keyboard input */
|
---|
685 | if ( !dequeue_key(&scan_code, &ascii_code, 1) ) {
|
---|
686 | BX_PANIC("KBD: int16h: out of keyboard input\n");
|
---|
687 | }
|
---|
688 | if (scan_code !=0 && ascii_code == 0xF0)
|
---|
689 | ascii_code = 0;
|
---|
690 | AX = (scan_code << 8) | ascii_code;
|
---|
691 | break;
|
---|
692 |
|
---|
693 | case 0x11: /* check MF-II keyboard status */
|
---|
694 | SET_IF();
|
---|
695 | if ( !dequeue_key(&scan_code, &ascii_code, 0) ) {
|
---|
696 | SET_ZF();
|
---|
697 | return;
|
---|
698 | }
|
---|
699 | if (scan_code !=0 && ascii_code == 0xF0)
|
---|
700 | ascii_code = 0;
|
---|
701 | AX = (scan_code << 8) | ascii_code;
|
---|
702 | CLEAR_ZF();
|
---|
703 | break;
|
---|
704 |
|
---|
705 | case 0x12: /* get extended keyboard status */
|
---|
706 | shift_flags = read_byte(0x0040, 0x17);
|
---|
707 | SET_AL(shift_flags);
|
---|
708 | shift_flags = read_byte(0x0040, 0x18) & 0x73;
|
---|
709 | shift_flags |= read_byte(0x0040, 0x96) & 0x0c;
|
---|
710 | SET_AH(shift_flags);
|
---|
711 | BX_DEBUG_INT16("int16: func 12 sending %04x\n",AX);
|
---|
712 | break;
|
---|
713 |
|
---|
714 | case 0x92: /* keyboard capability check called by DOS 5.0+ keyb */
|
---|
715 | SET_AH(0x80); // function int16 ah=0x10-0x12 supported
|
---|
716 | break;
|
---|
717 |
|
---|
718 | case 0xA2: /* 122 keys capability check called by DOS 5.0+ keyb */
|
---|
719 | // don't change AH : function int16 ah=0x20-0x22 NOT supported
|
---|
720 | break;
|
---|
721 |
|
---|
722 | //@todo: what's the point of handling this??
|
---|
723 | #if 0
|
---|
724 | case 0x6F:
|
---|
725 | if (GET_AL() == 0x08)
|
---|
726 | SET_AH(0x02); // unsupported, aka normal keyboard
|
---|
727 | #endif
|
---|
728 |
|
---|
729 | default:
|
---|
730 | BX_INFO("KBD: unsupported int 16h function %02x\n", GET_AH());
|
---|
731 | BX_INFO("AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
|
---|
732 | }
|
---|
733 | BX_DEBUG_INT16("int16ex: AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
|
---|
734 | }
|
---|