]> git.proxmox.com Git - qemu.git/blame - hw/pckbd.c
win32 port (initial patch by kazu)
[qemu.git] / hw / pckbd.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU PC keyboard emulation
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
80cabfad
FB
24#include "vl.h"
25
26/* debug PC keyboard */
27//#define DEBUG_KBD
28
29/* debug PC keyboard : only mouse */
30//#define DEBUG_MOUSE
31
32/* Keyboard Controller Commands */
33#define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
34#define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
35#define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
36#define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
37#define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
38#define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
39#define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
40#define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
41#define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
42#define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
43#define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
44#define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
45#define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
46#define KBD_CCMD_WRITE_OBUF 0xD2
47#define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
48 initiated by the auxiliary device */
49#define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
50#define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
51#define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
52#define KBD_CCMD_RESET 0xFE
53
54/* Keyboard Commands */
55#define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
56#define KBD_CMD_ECHO 0xEE
57#define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
58#define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
59#define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
60#define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
61#define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
62#define KBD_CMD_RESET 0xFF /* Reset */
63
64/* Keyboard Replies */
65#define KBD_REPLY_POR 0xAA /* Power on reset */
66#define KBD_REPLY_ACK 0xFA /* Command ACK */
67#define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
68
69/* Status Register Bits */
70#define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
71#define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
72#define KBD_STAT_SELFTEST 0x04 /* Self test successful */
73#define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
74#define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
75#define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
76#define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
77#define KBD_STAT_PERR 0x80 /* Parity error */
78
79/* Controller Mode Register Bits */
80#define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
81#define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
82#define KBD_MODE_SYS 0x04 /* The system flag (?) */
83#define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
84#define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
85#define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
86#define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
87#define KBD_MODE_RFU 0x80
88
89/* Mouse Commands */
90#define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
91#define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
92#define AUX_SET_RES 0xE8 /* Set resolution */
93#define AUX_GET_SCALE 0xE9 /* Get scaling factor */
94#define AUX_SET_STREAM 0xEA /* Set stream mode */
95#define AUX_POLL 0xEB /* Poll */
96#define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
97#define AUX_SET_WRAP 0xEE /* Set wrap mode */
98#define AUX_SET_REMOTE 0xF0 /* Set remote mode */
99#define AUX_GET_TYPE 0xF2 /* Get type */
100#define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
101#define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
102#define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
103#define AUX_SET_DEFAULT 0xF6
104#define AUX_RESET 0xFF /* Reset aux device */
105#define AUX_ACK 0xFA /* Command byte ACK. */
106
107#define MOUSE_STATUS_REMOTE 0x40
108#define MOUSE_STATUS_ENABLED 0x20
109#define MOUSE_STATUS_SCALE21 0x10
110
111#define KBD_QUEUE_SIZE 256
112
113typedef struct {
114 uint8_t data[KBD_QUEUE_SIZE];
115 int rptr, wptr, count;
116} KBDQueue;
117
118typedef struct KBDState {
119 KBDQueue queues[2];
120 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
121 uint8_t status;
122 uint8_t mode;
123 /* keyboard state */
124 int kbd_write_cmd;
125 int scan_enabled;
126 /* mouse state */
127 int mouse_write_cmd;
128 uint8_t mouse_status;
129 uint8_t mouse_resolution;
130 uint8_t mouse_sample_rate;
131 uint8_t mouse_wrap;
132 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
133 uint8_t mouse_detect_state;
134 int mouse_dx; /* current values, needed for 'poll' mode */
135 int mouse_dy;
136 int mouse_dz;
137 uint8_t mouse_buttons;
138} KBDState;
139
140KBDState kbd_state;
141int reset_requested;
142
143/* update irq and KBD_STAT_[MOUSE_]OBF */
144/* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
145 incorrect, but it avoids having to simulate exact delays */
146static void kbd_update_irq(KBDState *s)
147{
148 int irq12_level, irq1_level;
149
150 irq1_level = 0;
151 irq12_level = 0;
152 s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
153 if (s->queues[0].count != 0 ||
154 s->queues[1].count != 0) {
155 s->status |= KBD_STAT_OBF;
156 if (s->queues[1].count != 0) {
157 s->status |= KBD_STAT_MOUSE_OBF;
158 if (s->mode & KBD_MODE_MOUSE_INT)
159 irq12_level = 1;
160 } else {
161 if ((s->mode & KBD_MODE_KBD_INT) &&
162 !(s->mode & KBD_MODE_DISABLE_KBD))
163 irq1_level = 1;
164 }
165 }
166 pic_set_irq(1, irq1_level);
167 pic_set_irq(12, irq12_level);
168}
169
170static void kbd_queue(KBDState *s, int b, int aux)
171{
b41a2cd1 172 KBDQueue *q = &s->queues[aux];
80cabfad
FB
173
174#if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
175 if (aux)
176 printf("mouse event: 0x%02x\n", b);
177#ifdef DEBUG_KBD
178 else
179 printf("kbd event: 0x%02x\n", b);
180#endif
181#endif
182 if (q->count >= KBD_QUEUE_SIZE)
183 return;
184 q->data[q->wptr] = b;
185 if (++q->wptr == KBD_QUEUE_SIZE)
186 q->wptr = 0;
187 q->count++;
188 kbd_update_irq(s);
189}
190
191void kbd_put_keycode(int keycode)
192{
193 KBDState *s = &kbd_state;
194 kbd_queue(s, keycode, 0);
195}
196
b41a2cd1 197static uint32_t kbd_read_status(void *opaque, uint32_t addr)
80cabfad 198{
b41a2cd1 199 KBDState *s = opaque;
80cabfad
FB
200 int val;
201 val = s->status;
202#if defined(DEBUG_KBD)
203 printf("kbd: read status=0x%02x\n", val);
204#endif
205 return val;
206}
207
b41a2cd1 208static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val)
80cabfad 209{
b41a2cd1 210 KBDState *s = opaque;
80cabfad
FB
211
212#ifdef DEBUG_KBD
213 printf("kbd: write cmd=0x%02x\n", val);
214#endif
215 switch(val) {
216 case KBD_CCMD_READ_MODE:
217 kbd_queue(s, s->mode, 0);
218 break;
219 case KBD_CCMD_WRITE_MODE:
220 case KBD_CCMD_WRITE_OBUF:
221 case KBD_CCMD_WRITE_AUX_OBUF:
222 case KBD_CCMD_WRITE_MOUSE:
223 case KBD_CCMD_WRITE_OUTPORT:
224 s->write_cmd = val;
225 break;
226 case KBD_CCMD_MOUSE_DISABLE:
227 s->mode |= KBD_MODE_DISABLE_MOUSE;
228 break;
229 case KBD_CCMD_MOUSE_ENABLE:
230 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
231 break;
232 case KBD_CCMD_TEST_MOUSE:
233 kbd_queue(s, 0x00, 0);
234 break;
235 case KBD_CCMD_SELF_TEST:
236 s->status |= KBD_STAT_SELFTEST;
237 kbd_queue(s, 0x55, 0);
238 break;
239 case KBD_CCMD_KBD_TEST:
240 kbd_queue(s, 0x00, 0);
241 break;
242 case KBD_CCMD_KBD_DISABLE:
243 s->mode |= KBD_MODE_DISABLE_KBD;
244 kbd_update_irq(s);
245 break;
246 case KBD_CCMD_KBD_ENABLE:
247 s->mode &= ~KBD_MODE_DISABLE_KBD;
248 kbd_update_irq(s);
249 break;
250 case KBD_CCMD_READ_INPORT:
251 kbd_queue(s, 0x00, 0);
252 break;
253 case KBD_CCMD_READ_OUTPORT:
254 /* XXX: check that */
255#ifdef TARGET_I386
256 val = 0x01 | (((cpu_single_env->a20_mask >> 20) & 1) << 1);
257#else
258 val = 0x01;
259#endif
260 if (s->status & KBD_STAT_OBF)
261 val |= 0x10;
262 if (s->status & KBD_STAT_MOUSE_OBF)
263 val |= 0x20;
264 kbd_queue(s, val, 0);
265 break;
266#ifdef TARGET_I386
267 case KBD_CCMD_ENABLE_A20:
b41a2cd1 268 cpu_x86_set_a20(cpu_single_env, 1);
80cabfad
FB
269 break;
270 case KBD_CCMD_DISABLE_A20:
b41a2cd1 271 cpu_x86_set_a20(cpu_single_env, 0);
80cabfad
FB
272 break;
273#endif
274 case KBD_CCMD_RESET:
275 reset_requested = 1;
276 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
277 break;
278 case 0xff:
279 /* ignore that - I don't know what is its use */
280 break;
281 default:
282 fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
283 break;
284 }
285}
286
b41a2cd1 287static uint32_t kbd_read_data(void *opaque, uint32_t addr)
80cabfad 288{
b41a2cd1 289 KBDState *s = opaque;
80cabfad
FB
290 KBDQueue *q;
291 int val, index;
292
293 q = &s->queues[0]; /* first check KBD data */
294 if (q->count == 0)
295 q = &s->queues[1]; /* then check AUX data */
296 if (q->count == 0) {
297 /* NOTE: if no data left, we return the last keyboard one
298 (needed for EMM386) */
299 /* XXX: need a timer to do things correctly */
300 q = &s->queues[0];
301 index = q->rptr - 1;
302 if (index < 0)
303 index = KBD_QUEUE_SIZE - 1;
304 val = q->data[index];
305 } else {
306 val = q->data[q->rptr];
307 if (++q->rptr == KBD_QUEUE_SIZE)
308 q->rptr = 0;
309 q->count--;
310 /* reading deasserts IRQ */
311 if (q == &s->queues[0])
312 pic_set_irq(1, 0);
313 else
314 pic_set_irq(12, 0);
315 }
316 /* reassert IRQs if data left */
317 kbd_update_irq(s);
318#ifdef DEBUG_KBD
319 printf("kbd: read data=0x%02x\n", val);
320#endif
321 return val;
322}
323
324static void kbd_reset_keyboard(KBDState *s)
325{
326 s->scan_enabled = 1;
327}
328
329static void kbd_write_keyboard(KBDState *s, int val)
330{
331 switch(s->kbd_write_cmd) {
332 default:
333 case -1:
334 switch(val) {
335 case 0x00:
336 kbd_queue(s, KBD_REPLY_ACK, 0);
337 break;
338 case 0x05:
339 kbd_queue(s, KBD_REPLY_RESEND, 0);
340 break;
341 case KBD_CMD_GET_ID:
342 kbd_queue(s, KBD_REPLY_ACK, 0);
343 kbd_queue(s, 0xab, 0);
344 kbd_queue(s, 0x83, 0);
345 break;
346 case KBD_CMD_ECHO:
347 kbd_queue(s, KBD_CMD_ECHO, 0);
348 break;
349 case KBD_CMD_ENABLE:
350 s->scan_enabled = 1;
351 kbd_queue(s, KBD_REPLY_ACK, 0);
352 break;
353 case KBD_CMD_SET_LEDS:
354 case KBD_CMD_SET_RATE:
355 s->kbd_write_cmd = val;
356 kbd_queue(s, KBD_REPLY_ACK, 0);
357 break;
358 case KBD_CMD_RESET_DISABLE:
359 kbd_reset_keyboard(s);
360 s->scan_enabled = 0;
361 kbd_queue(s, KBD_REPLY_ACK, 0);
362 break;
363 case KBD_CMD_RESET_ENABLE:
364 kbd_reset_keyboard(s);
365 s->scan_enabled = 1;
366 kbd_queue(s, KBD_REPLY_ACK, 0);
367 break;
368 case KBD_CMD_RESET:
369 kbd_reset_keyboard(s);
370 kbd_queue(s, KBD_REPLY_ACK, 0);
371 kbd_queue(s, KBD_REPLY_POR, 0);
372 break;
373 default:
374 kbd_queue(s, KBD_REPLY_ACK, 0);
375 break;
376 }
377 break;
378 case KBD_CMD_SET_LEDS:
379 kbd_queue(s, KBD_REPLY_ACK, 0);
380 s->kbd_write_cmd = -1;
381 break;
382 case KBD_CMD_SET_RATE:
383 kbd_queue(s, KBD_REPLY_ACK, 0);
384 s->kbd_write_cmd = -1;
385 break;
386 }
387}
388
389static void kbd_mouse_send_packet(KBDState *s)
390{
391 unsigned int b;
392 int dx1, dy1, dz1;
393
394 dx1 = s->mouse_dx;
395 dy1 = s->mouse_dy;
396 dz1 = s->mouse_dz;
397 /* XXX: increase range to 8 bits ? */
398 if (dx1 > 127)
399 dx1 = 127;
400 else if (dx1 < -127)
401 dx1 = -127;
402 if (dy1 > 127)
403 dy1 = 127;
404 else if (dy1 < -127)
405 dy1 = -127;
406 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
407 kbd_queue(s, b, 1);
408 kbd_queue(s, dx1 & 0xff, 1);
409 kbd_queue(s, dy1 & 0xff, 1);
410 /* extra byte for IMPS/2 or IMEX */
411 switch(s->mouse_type) {
412 default:
413 break;
414 case 3:
415 if (dz1 > 127)
416 dz1 = 127;
417 else if (dz1 < -127)
418 dz1 = -127;
419 kbd_queue(s, dz1 & 0xff, 1);
420 break;
421 case 4:
422 if (dz1 > 7)
423 dz1 = 7;
424 else if (dz1 < -7)
425 dz1 = -7;
426 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
427 kbd_queue(s, b, 1);
428 break;
429 }
430
431 /* update deltas */
432 s->mouse_dx -= dx1;
433 s->mouse_dy -= dy1;
434 s->mouse_dz -= dz1;
435}
436
437void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
438{
439 KBDState *s = &kbd_state;
440
441 /* check if deltas are recorded when disabled */
442 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
443 return;
444
445 s->mouse_dx += dx;
446 s->mouse_dy -= dy;
447 s->mouse_dz += dz;
448 s->mouse_buttons = buttons_state;
449
450 if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
451 (s->queues[1].count < (KBD_QUEUE_SIZE - 16))) {
452 for(;;) {
453 /* if not remote, send event. Multiple events are sent if
454 too big deltas */
455 kbd_mouse_send_packet(s);
456 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
457 break;
458 }
459 }
460}
461
462static void kbd_write_mouse(KBDState *s, int val)
463{
464#ifdef DEBUG_MOUSE
465 printf("kbd: write mouse 0x%02x\n", val);
466#endif
467 switch(s->mouse_write_cmd) {
468 default:
469 case -1:
470 /* mouse command */
471 if (s->mouse_wrap) {
472 if (val == AUX_RESET_WRAP) {
473 s->mouse_wrap = 0;
474 kbd_queue(s, AUX_ACK, 1);
475 return;
476 } else if (val != AUX_RESET) {
477 kbd_queue(s, val, 1);
478 return;
479 }
480 }
481 switch(val) {
482 case AUX_SET_SCALE11:
483 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
484 kbd_queue(s, AUX_ACK, 1);
485 break;
486 case AUX_SET_SCALE21:
487 s->mouse_status |= MOUSE_STATUS_SCALE21;
488 kbd_queue(s, AUX_ACK, 1);
489 break;
490 case AUX_SET_STREAM:
491 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
492 kbd_queue(s, AUX_ACK, 1);
493 break;
494 case AUX_SET_WRAP:
495 s->mouse_wrap = 1;
496 kbd_queue(s, AUX_ACK, 1);
497 break;
498 case AUX_SET_REMOTE:
499 s->mouse_status |= MOUSE_STATUS_REMOTE;
500 kbd_queue(s, AUX_ACK, 1);
501 break;
502 case AUX_GET_TYPE:
503 kbd_queue(s, AUX_ACK, 1);
504 kbd_queue(s, s->mouse_type, 1);
505 break;
506 case AUX_SET_RES:
507 case AUX_SET_SAMPLE:
508 s->mouse_write_cmd = val;
509 kbd_queue(s, AUX_ACK, 1);
510 break;
511 case AUX_GET_SCALE:
512 kbd_queue(s, AUX_ACK, 1);
513 kbd_queue(s, s->mouse_status, 1);
514 kbd_queue(s, s->mouse_resolution, 1);
515 kbd_queue(s, s->mouse_sample_rate, 1);
516 break;
517 case AUX_POLL:
518 kbd_queue(s, AUX_ACK, 1);
519 kbd_mouse_send_packet(s);
520 break;
521 case AUX_ENABLE_DEV:
522 s->mouse_status |= MOUSE_STATUS_ENABLED;
523 kbd_queue(s, AUX_ACK, 1);
524 break;
525 case AUX_DISABLE_DEV:
526 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
527 kbd_queue(s, AUX_ACK, 1);
528 break;
529 case AUX_SET_DEFAULT:
530 s->mouse_sample_rate = 100;
531 s->mouse_resolution = 2;
532 s->mouse_status = 0;
533 kbd_queue(s, AUX_ACK, 1);
534 break;
535 case AUX_RESET:
536 s->mouse_sample_rate = 100;
537 s->mouse_resolution = 2;
538 s->mouse_status = 0;
539 kbd_queue(s, AUX_ACK, 1);
540 kbd_queue(s, 0xaa, 1);
541 kbd_queue(s, s->mouse_type, 1);
542 break;
543 default:
544 break;
545 }
546 break;
547 case AUX_SET_SAMPLE:
548 s->mouse_sample_rate = val;
549#if 0
550 /* detect IMPS/2 or IMEX */
551 switch(s->mouse_detect_state) {
552 default:
553 case 0:
554 if (val == 200)
555 s->mouse_detect_state = 1;
556 break;
557 case 1:
558 if (val == 100)
559 s->mouse_detect_state = 2;
560 else if (val == 200)
561 s->mouse_detect_state = 3;
562 else
563 s->mouse_detect_state = 0;
564 break;
565 case 2:
566 if (val == 80)
567 s->mouse_type = 3; /* IMPS/2 */
568 s->mouse_detect_state = 0;
569 break;
570 case 3:
571 if (val == 80)
572 s->mouse_type = 4; /* IMEX */
573 s->mouse_detect_state = 0;
574 break;
575 }
576#endif
577 kbd_queue(s, AUX_ACK, 1);
578 s->mouse_write_cmd = -1;
579 break;
580 case AUX_SET_RES:
581 s->mouse_resolution = val;
582 kbd_queue(s, AUX_ACK, 1);
583 s->mouse_write_cmd = -1;
584 break;
585 }
586}
587
b41a2cd1 588void kbd_write_data(void *opaque, uint32_t addr, uint32_t val)
80cabfad 589{
b41a2cd1 590 KBDState *s = opaque;
80cabfad
FB
591
592#ifdef DEBUG_KBD
593 printf("kbd: write data=0x%02x\n", val);
594#endif
595
596 switch(s->write_cmd) {
597 case 0:
598 kbd_write_keyboard(s, val);
599 break;
600 case KBD_CCMD_WRITE_MODE:
601 s->mode = val;
602 kbd_update_irq(s);
603 break;
604 case KBD_CCMD_WRITE_OBUF:
605 kbd_queue(s, val, 0);
606 break;
607 case KBD_CCMD_WRITE_AUX_OBUF:
608 kbd_queue(s, val, 1);
609 break;
610 case KBD_CCMD_WRITE_OUTPORT:
611#ifdef TARGET_I386
b41a2cd1 612 cpu_x86_set_a20(cpu_single_env, (val >> 1) & 1);
80cabfad
FB
613#endif
614 if (!(val & 1)) {
615 reset_requested = 1;
616 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
617 }
618 break;
619 case KBD_CCMD_WRITE_MOUSE:
620 kbd_write_mouse(s, val);
621 break;
622 default:
623 break;
624 }
625 s->write_cmd = 0;
626}
627
628void kbd_reset(KBDState *s)
629{
630 KBDQueue *q;
631 int i;
632
633 s->kbd_write_cmd = -1;
634 s->mouse_write_cmd = -1;
635 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
636 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
637 for(i = 0; i < 2; i++) {
638 q = &s->queues[i];
639 q->rptr = 0;
640 q->wptr = 0;
641 q->count = 0;
642 }
643}
644
645void kbd_init(void)
646{
b41a2cd1
FB
647 KBDState *s = &kbd_state;
648
649 kbd_reset(s);
650 register_ioport_read(0x60, 1, 1, kbd_read_data, s);
651 register_ioport_write(0x60, 1, 1, kbd_write_data, s);
652 register_ioport_read(0x64, 1, 1, kbd_read_status, s);
653 register_ioport_write(0x64, 1, 1, kbd_write_command, s);
80cabfad 654}