VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS-new/keyboard.c@ 38833

Last change on this file since 38833 was 38833, checked in by vboxsync, 13 years ago

Disable the A20 gate at POST.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.0 KB
Line 
1/*
2 * Copyright (C) 2006-2011 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
52extern void post(void);
53#pragma aux post "*";
54
55void 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
61struct {
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//--------------------------------------------------------------------------
165void 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
179void 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
312unsigned 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?
344void 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;
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 shift_flags |= 0x02;
382 write_byte(0x0040, 0x17, shift_flags);
383 break;
384 case 0xaa: /* L Shift release */
385 shift_flags &= ~0x02;
386 write_byte(0x0040, 0x17, shift_flags);
387 break;
388
389 case 0x36: /* R Shift press */
390 shift_flags |= 0x01;
391 write_byte(0x0040, 0x17, shift_flags);
392 break;
393 case 0xb6: /* R Shift release */
394 shift_flags &= ~0x01;
395 write_byte(0x0040, 0x17, shift_flags);
396 break;
397
398 case 0x1d: /* Ctrl press */
399 if ((mf2_state & 0x01) == 0) {
400 shift_flags |= 0x04;
401 write_byte(0x0040, 0x17, shift_flags);
402 if (mf2_state & 0x02) {
403 mf2_state |= 0x04;
404 write_byte(0x0040, 0x96, mf2_state);
405 } else {
406 mf2_flags |= 0x01;
407 write_byte(0x0040, 0x18, mf2_flags);
408 }
409 }
410 break;
411 case 0x9d: /* Ctrl release */
412 if ((mf2_state & 0x01) == 0) {
413 shift_flags &= ~0x04;
414 write_byte(0x0040, 0x17, shift_flags);
415 if (mf2_state & 0x02) {
416 mf2_state &= ~0x04;
417 write_byte(0x0040, 0x96, mf2_state);
418 } else {
419 mf2_flags &= ~0x01;
420 write_byte(0x0040, 0x18, mf2_flags);
421 }
422 }
423 break;
424
425 case 0x38: /* Alt press */
426 shift_flags |= 0x08;
427 write_byte(0x0040, 0x17, shift_flags);
428 if (mf2_state & 0x02) {
429 mf2_state |= 0x08;
430 write_byte(0x0040, 0x96, mf2_state);
431 } else {
432 mf2_flags |= 0x02;
433 write_byte(0x0040, 0x18, mf2_flags);
434 }
435 break;
436 case 0xb8: /* Alt release */
437 shift_flags &= ~0x08;
438 write_byte(0x0040, 0x17, shift_flags);
439 if (mf2_state & 0x02) {
440 mf2_state &= ~0x08;
441 write_byte(0x0040, 0x96, mf2_state);
442 } else {
443 mf2_flags &= ~0x02;
444 write_byte(0x0040, 0x18, mf2_flags);
445 }
446 break;
447
448 case 0x45: /* Num Lock press */
449 if ((mf2_state & 0x03) == 0) {
450 mf2_flags |= 0x20;
451 write_byte(0x0040, 0x18, mf2_flags);
452 shift_flags ^= 0x20;
453 write_byte(0x0040, 0x17, shift_flags);
454 }
455 break;
456 case 0xc5: /* Num Lock release */
457 if ((mf2_state & 0x03) == 0) {
458 mf2_flags &= ~0x20;
459 write_byte(0x0040, 0x18, mf2_flags);
460 }
461 break;
462
463 case 0x46: /* Scroll Lock press */
464 mf2_flags |= 0x10;
465 write_byte(0x0040, 0x18, mf2_flags);
466 shift_flags ^= 0x10;
467 write_byte(0x0040, 0x17, shift_flags);
468 break;
469
470 case 0xc6: /* Scroll Lock release */
471 mf2_flags &= ~0x10;
472 write_byte(0x0040, 0x18, mf2_flags);
473 break;
474
475 case 0x53: /* Del press */
476 if ((shift_flags & 0x0f) == 0x0c)
477 jmp_post();
478 /* fall through */
479
480 default:
481 if (scancode & 0x80) {
482 break; /* toss key releases ... */
483 }
484 if (scancode > MAX_SCAN_CODE) {
485 BX_INFO("KBD: int09h_handler(): unknown scancode read: 0x%02x!\n", scancode);
486 return;
487 }
488 if (shift_flags & 0x08) { /* ALT */
489 asciicode = scan_to_scanascii[scancode].alt;
490 scancode = scan_to_scanascii[scancode].alt >> 8;
491 } else if (shift_flags & 0x04) { /* CONTROL */
492 asciicode = scan_to_scanascii[scancode].control;
493 scancode = scan_to_scanascii[scancode].control >> 8;
494 } else if (((mf2_state & 0x02) > 0) && ((scancode >= 0x47) && (scancode <= 0x53))) {
495 /* extended keys handling */
496 asciicode = 0xe0;
497 scancode = scan_to_scanascii[scancode].normal >> 8;
498 } else if (shift_flags & 0x03) { /* LSHIFT + RSHIFT */
499 /* check if lock state should be ignored
500 * because a SHIFT key are pressed */
501
502 if (shift_flags & scan_to_scanascii[scancode].lock_flags) {
503 asciicode = scan_to_scanascii[scancode].normal;
504 scancode = scan_to_scanascii[scancode].normal >> 8;
505 } else {
506 asciicode = scan_to_scanascii[scancode].shift;
507 scancode = scan_to_scanascii[scancode].shift >> 8;
508 }
509 } else {
510 /* check if lock is on */
511 if (shift_flags & scan_to_scanascii[scancode].lock_flags) {
512 asciicode = scan_to_scanascii[scancode].shift;
513 scancode = scan_to_scanascii[scancode].shift >> 8;
514 } else {
515 asciicode = scan_to_scanascii[scancode].normal;
516 scancode = scan_to_scanascii[scancode].normal >> 8;
517 }
518 }
519 if (scancode==0 && asciicode==0) {
520 BX_INFO("KBD: int09h_handler(): scancode & asciicode are zero?\n");
521 }
522 enqueue_key(scancode, asciicode);
523 break;
524 }
525 if ((scancode & 0x7f) != 0x1d) {
526 mf2_state &= ~0x01;
527 }
528 mf2_state &= ~0x02;
529 write_byte(0x0040, 0x96, mf2_state);
530}
531
532unsigned int dequeue_key(uint8_t __far *scan_code, uint8_t __far *ascii_code, unsigned incr)
533{
534 uint16_t buffer_start, buffer_end, buffer_head, buffer_tail;
535 uint8_t acode, scode;
536
537#if BX_CPU < 2
538 buffer_start = 0x001E;
539 buffer_end = 0x003E;
540#else
541 buffer_start = read_word(0x0040, 0x0080);
542 buffer_end = read_word(0x0040, 0x0082);
543#endif
544
545 buffer_head = read_word(0x0040, 0x001a);
546 buffer_tail = read_word(0x0040, 0x001c);
547
548 if (buffer_head != buffer_tail) {
549 acode = read_byte(0x0040, buffer_head);
550 scode = read_byte(0x0040, buffer_head+1);
551 *ascii_code = acode;
552 *scan_code = scode;
553
554 if (incr) {
555 buffer_head += 2;
556 if (buffer_head >= buffer_end)
557 buffer_head = buffer_start;
558 write_word(0x0040, 0x001a, buffer_head);
559 }
560 return(1);
561 }
562 else {
563 return(0);
564 }
565}
566
567
568//@todo: move somewhere else?
569#define AX r.gr.u.r16.ax
570#define BX r.gr.u.r16.bx
571#define CX r.gr.u.r16.cx
572#define DX r.gr.u.r16.dx
573#define SI r.gr.u.r16.si
574#define DI r.gr.u.r16.di
575#define BP r.gr.u.r16.bp
576#define SP r.gr.u.r16.sp
577#define FLAGS r.fl.u.r16.flags
578
579/* Interrupt 16h service implementation. */
580
581void BIOSCALL int16_function(volatile kbd_regs_t r)
582{
583 uint8_t scan_code, ascii_code, shift_flags, led_flags, count;
584 uint16_t kbd_code, max;
585
586 BX_DEBUG_INT16("int16: AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
587
588 shift_flags = read_byte(0x0040, 0x17);
589 led_flags = read_byte(0x0040, 0x97);
590 if ((((shift_flags >> 4) & 0x07) ^ (led_flags & 0x07)) != 0) {
591 int_disable(); //@todo: interrupts should be disabled already??
592 outb(0x60, 0xed);
593 while ((inb(0x64) & 0x01) == 0) outb(0x80, 0x21);
594 if ((inb(0x60) == 0xfa)) {
595 led_flags &= 0xf8;
596 led_flags |= ((shift_flags >> 4) & 0x07);
597 outb(0x60, led_flags & 0x07);
598 while ((inb(0x64) & 0x01) == 0)
599 outb(0x80, 0x21);
600 inb(0x60);
601 write_byte(0x0040, 0x97, led_flags);
602 }
603 int_enable();
604 }
605
606 switch (GET_AH()) {
607 case 0x00: /* read keyboard input */
608 if ( !dequeue_key(&scan_code, &ascii_code, 1) ) {
609 BX_PANIC("KBD: int16h: out of keyboard input\n");
610 }
611 if (scan_code !=0 && ascii_code == 0xF0)
612 ascii_code = 0;
613 else if (ascii_code == 0xE0)
614 ascii_code = 0;
615 AX = (scan_code << 8) | ascii_code;
616 break;
617
618 case 0x01: /* check keyboard status */
619 if ( !dequeue_key(&scan_code, &ascii_code, 0) ) {
620 SET_ZF();
621 return;
622 }
623 if (scan_code !=0 && ascii_code == 0xF0)
624 ascii_code = 0;
625 else if (ascii_code == 0xE0)
626 ascii_code = 0;
627 AX = (scan_code << 8) | ascii_code;
628 CLEAR_ZF();
629 break;
630
631 case 0x02: /* get shift flag status */
632 shift_flags = read_byte(0x0040, 0x17);
633 SET_AL(shift_flags);
634 break;
635
636 case 0x05: /* store key-stroke into buffer */
637 if ( !enqueue_key(GET_CH(), GET_CL()) ) {
638 SET_AL(1);
639 }
640 else {
641 SET_AL(0);
642 }
643 break;
644
645 case 0x09: /* GET KEYBOARD FUNCTIONALITY */
646 // bit Bochs Description
647 // 7 0 reserved
648 // 6 0 INT 16/AH=20h-22h supported (122-key keyboard support)
649 // 5 1 INT 16/AH=10h-12h supported (enhanced keyboard support)
650 // 4 1 INT 16/AH=0Ah supported
651 // 3 0 INT 16/AX=0306h supported
652 // 2 0 INT 16/AX=0305h supported
653 // 1 0 INT 16/AX=0304h supported
654 // 0 0 INT 16/AX=0300h supported
655 //
656 SET_AL(0x30);
657 break;
658
659 case 0x0A: /* GET KEYBOARD ID */
660 count = 2;
661 kbd_code = 0x0;
662 outb(0x60, 0xf2);
663 /* Wait for data */
664 max=0xffff;
665 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x00);
666 if (max>0x0) {
667 if ((inb(0x60) == 0xfa)) {
668 do {
669 max=0xffff;
670 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x00);
671 if (max>0x0) {
672 kbd_code >>= 8;
673 kbd_code |= (inb(0x60) << 8);
674 }
675 } while (--count>0);
676 }
677 }
678 BX=kbd_code;
679 break;
680
681 case 0x10: /* read MF-II keyboard input */
682 if ( !dequeue_key(&scan_code, &ascii_code, 1) ) {
683 BX_PANIC("KBD: int16h: out of keyboard input\n");
684 }
685 if (scan_code !=0 && ascii_code == 0xF0)
686 ascii_code = 0;
687 AX = (scan_code << 8) | ascii_code;
688 break;
689
690 case 0x11: /* check MF-II keyboard status */
691 if ( !dequeue_key(&scan_code, &ascii_code, 0) ) {
692 SET_ZF();
693 return;
694 }
695 if (scan_code !=0 && ascii_code == 0xF0)
696 ascii_code = 0;
697 AX = (scan_code << 8) | ascii_code;
698 CLEAR_ZF();
699 break;
700
701 case 0x12: /* get extended keyboard status */
702 shift_flags = read_byte(0x0040, 0x17);
703 SET_AL(shift_flags);
704 shift_flags = read_byte(0x0040, 0x18) & 0x73;
705 shift_flags |= read_byte(0x0040, 0x96) & 0x0c;
706 SET_AH(shift_flags);
707 BX_DEBUG_INT16("int16: func 12 sending %04x\n",AX);
708 break;
709
710 case 0x92: /* keyboard capability check called by DOS 5.0+ keyb */
711 SET_AH(0x80); // function int16 ah=0x10-0x12 supported
712 break;
713
714 case 0xA2: /* 122 keys capability check called by DOS 5.0+ keyb */
715 // don't change AH : function int16 ah=0x20-0x22 NOT supported
716 break;
717
718 //@todo: what's the point of handling this??
719#if 0
720 case 0x6F:
721 if (GET_AL() == 0x08)
722 SET_AH(0x02); // unsupported, aka normal keyboard
723#endif
724
725 default:
726 BX_INFO("KBD: unsupported int 16h function %02x\n", GET_AH());
727 BX_INFO("AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
728 }
729 BX_DEBUG_INT16("int16ex: AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
730}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette