-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyboard.cpp
1778 lines (1584 loc) · 49.6 KB
/
Keyboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ES40 emulator.
* Copyright (C) 2007-2008 by the ES40 Emulator Project
*
* WWW : http://sourceforge.net/projects/es40
* E-mail : [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Although this is not required, the author would appreciate being notified of,
* and receiving any modifications you may make to the source code that might serve
* the general public.
*/
/**
* \file
* Contains the code for the emulated Keyboard and mouse devices and controller.
*
* $Id: Keyboard.cpp,v 1.4 2008/02/29 10:23:09 iamcamiel Exp $
*
* X-1.4 Brian Wheeler 29-FEB-2008
* ACK recognized, but unhandled, keyboard commands.
*
* X-1.3 Brian Wheeler 27-FEB-2008
* Avoid compiler warnings.
*
* X-1.2 David Leonard 20-FEB-2008
* Avoid 'Xlib: unexpected async reply' errors on Linux/Unix/BSD's by
* adding some thread interlocking.
*
* X-1.1 Camiel Vanderhoeven 12-FEB-2008
* Created. Contains code previously found in AliM1543C.cpp
*
* \author Camiel Vanderhoeven ([email protected] / http://www.camicom.com)
**/
#include "StdAfx.h"
#include "System.h"
#include "Keyboard.h"
#include "AliM1543C.h"
#include <math.h>
#include "gui/scancodes.h"
#include "gui/keymap.h"
/**
* Constructor.
**/
CKeyboard::CKeyboard(CConfigurator * cfg, CSystem * c) : CSystemComponent(cfg,c)
{
if (theKeyboard != 0)
FAILURE("More than one Keyboard controller!!");
theKeyboard = this;
int i;
c->RegisterMemory (this, 0, X64(00000801fc000060), 1);
c->RegisterMemory (this, 1, X64(00000801fc000064), 1);
c->RegisterClock(this, true);
resetinternals(1);
state.kbd_internal_buffer.led_status = 0;
state.kbd_internal_buffer.scanning_enabled = 1;
state.mouse_internal_buffer.num_elements = 0;
for (i=0; i<BX_MOUSE_BUFF_SIZE; i++)
state.mouse_internal_buffer.buffer[i] = 0;
state.mouse_internal_buffer.head = 0;
state.status.pare = 0;
state.status.tim = 0;
state.status.auxb = 0;
state.status.keyl = 1;
state.status.c_d = 1;
state.status.sysf = 0;
state.status.inpb = 0;
state.status.outb = 0;
state.kbd_clock_enabled = 1;
state.aux_clock_enabled = 0;
state.allow_irq1 = 1;
state.allow_irq12 = 1;
state.kbd_output_buffer = 0;
state.aux_output_buffer = 0;
state.last_comm = 0;
state.expecting_port60h = 0;
state.irq1_requested = 0;
state.irq12_requested = 0;
state.expecting_mouse_parameter = 0;
state.bat_in_progress = 0;
state.scancodes_translate = 1;
state.timer_pending = 0;
// Mouse initialization stuff
state.mouse.captured = myCfg->get_bool_value("mouse.enabled",true);
state.mouse.sample_rate = 100; // reports per second
state.mouse.resolution_cpmm = 4; // 4 counts per millimeter
state.mouse.scaling = 1; /* 1:1 (default) */
state.mouse.mode = MOUSE_MODE_RESET;
state.mouse.enable = 0;
state.mouse.delayed_dx = 0;
state.mouse.delayed_dy = 0;
state.mouse.delayed_dz = 0;
state.mouse.im_request = 0; // wheel mouse mode request
state.mouse.im_mode = 0; // wheel mouse mode
for (i=0; i<BX_KBD_CONTROLLER_QSIZE; i++)
state.kbd_controller_Q[i] = 0;
state.kbd_controller_Qsize = 0;
state.kbd_controller_Qsource = 0;
printf("kbc: $Id: Keyboard.cpp,v 1.4 2008/02/29 10:23:09 iamcamiel Exp $\n");
}
/**
* Destructor.
**/
CKeyboard::~CKeyboard()
{
}
u64 CKeyboard::ReadMem(int index, u64 address, int dsize)
{
switch (index)
{
case 0:
return read_60();
break;
case 1:
return read_64();
break;
default:
FAILURE("kbc: ReadMem index out of range");
}
}
void CKeyboard::WriteMem(int index, u64 address, int dsize, u64 data)
{
switch (index)
{
case 0:
write_60((u8)data);
break;
case 1:
write_64((u8)data);
break;
default:
FAILURE("kbc: ReadMem index out of range");
}
}
/**
* Enqueue scancode for a keypress or key-release. Used by the GUI implementation
* to send keypresses to the keyboard controller.
**/
void CKeyboard::gen_scancode(u32 key)
{
unsigned char *scancode;
u8 i;
#if defined(DEBUG_KBD)
printf("gen_scancode(): %s %s \n", bx_keymap->getBXKeyName(key), (key >> 31)?"released":"pressed");
if (!state.scancodes_translate)
BX_DEBUG(("keyboard: gen_scancode with scancode_translate cleared"));
#endif
// Ignore scancode if keyboard clock is driven low
if (state.kbd_clock_enabled==0)
return;
// Ignore scancode if scanning is disabled
if (state.kbd_internal_buffer.scanning_enabled==0)
return;
// Source: http://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html
//
// Three scancode sets
//
// The usual PC keyboards are capable of producing three sets
// of scancodes. Writing 0xf0 followed by 1, 2 or 3 to port
// 0x60 will put the keyboard in scancode mode 1, 2 or 3.
// Writing 0xf0 followed by 0 queries the mode, resulting in
// a scancode byte 43, 41 or 3f from the keyboard.
//
// Set 1 contains the values that the XT keyboard (with only
// one set of scancodes) produced, with extensions for new
// keys. Someone decided that another numbering was more
// logical and invented scancode Set 2. However, it was
// realized that new scancodes would break old programs, so
// the keyboard output was fed to a 8042 microprocessor on
// the motherboard that could translate Set 2 back into Set
// 1. Indeed a smart construction. This is the default today.
// Finally there is the PS/2 version, Set 3, more regular,
// but used by almost nobody.
//
// Sets 2 and 3 are designed to be translated by the 8042.
// Set 1 should not be translated.
//
// Make and Break Codes
//
// The key press / key release is coded as follows:
//
// For Set 1, if the make code of a key is c, the break
// code will be c+0x80. If the make code is e0 c, the
// break code will be e0 c+0x80. The Pause key has make
// code e1 1d 45 e1 9d c5 and does not generate a break code.
//
// For Set 2, if the make code of a key is c, the break code
// will be f0 c. If the make code is e0 c, the break code
// will be e0 f0 c. The Pause key has the 8-byte make code
// e1 14 77 e1 f0 14 f0 77.
//
// For Set 3, by default most keys do not generate a break
// code - only CapsLock, LShift, RShift, LCtrl and LAlt do.
// However, by default all non-traditional keys do generate
// a break code - thus, LWin, RWin, Menu do, and for example
// on the Microsoft Internet keyboard, so do Back, Forward,
// Stop, Mail, Search, Favorites, Web/Home, MyComputer,
// Calculator, Sleep. On my BTC keyboard, also the Macro key
// does.
//
// In Scancode Mode 3 it is possible to enable or disable
// key repeat and the production of break codes either on a
// key-by-key basis or for all keys at once. And just like
// for Set 2, key release is indicated by a f0 prefix in
// those cases where it is indicated. There is nothing
// special with the Pause key in scancode mode 3.
if (key & BX_KEY_RELEASED)
scancode=(unsigned char *)scancodes[(key&0xFF)][state.current_scancodes_set].brek;
else
scancode=(unsigned char *)scancodes[(key&0xFF)][state.current_scancodes_set].make;
// Translation
//
// The 8042 microprocessor translates the incoming byte stream
// produced by the keyboard, and turns an f0 prefix into an OR
// with 80 for the next byte.
//
// Unless told not to translate, the keyboard controller translates
// keyboard scancodes into the scancodes it returns to the CPU using
// the following table (in hex):
//
// +----+-------------------------------------------------+
// | | 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |
// +----+-------------------------------------------------+
// | 00 | ff 43 41 3f 3d 3b 3c 58 64 44 42 40 3e 0f 29 59 |
// | 10 | 65 38 2a 70 1d 10 02 5a 66 71 2c 1f 1e 11 03 5b |
// | 20 | 67 2e 2d 20 12 05 04 5c 68 39 2f 21 14 13 06 5d |
// | 30 | 69 31 30 23 22 15 07 5e 6a 72 32 24 16 08 09 5f |
// | 40 | 6b 33 25 17 18 0b 0a 60 6c 34 35 26 27 19 0c 61 |
// | 50 | 6d 73 28 74 1a 0d 62 6e 3a 36 1c 1b 75 2b 63 76 |
// | 60 | 55 56 77 78 79 7a 0e 7b 7c 4f 7d 4b 47 7e 7f 6f |
// | 70 | 52 53 50 4c 4d 48 01 45 57 4e 51 4a 37 49 46 54 |
// | 80 | 80 81 82 41 54 85 86 87 88 89 8a 8b 8c 8d 8e 8f |
// | 90 | 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f |
// | a0 | a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af |
// | b0 | b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf |
// | c0 | c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf |
// | d0 | d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df |
// | e0 | e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef |
// | f0 | - f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff |
// +----+-------------------------------------------------+
if (state.scancodes_translate)
{
// Translate before send
u8 escaped=0x00;
for (i=0; i<strlen((const char *)scancode); i++)
{
if (scancode[i] == 0xF0)
{
escaped=0x80;
}
else
{
#if defined(DEBUG_KBD)
printf("gen_scancode(): writing translated %02x \n",translation8042[scancode[i] ] | escaped);
#endif
enQ(translation8042[scancode[i]] | escaped);
escaped=0x00;
}
}
}
else
{
// Send raw data
for (i=0; i<strlen((const char *)scancode); i++)
{
#if defined(DEBUG_KBD)
printf("gen_scancode(): writing raw %02x \n",scancode[i]);
#endif
enQ(scancode[i]);
}
}
}
/**
* Reset keyboard internals.
**/
void CKeyboard::resetinternals(bool powerup)
{
state.kbd_internal_buffer.num_elements = 0;
for (int i=0; i<BX_KBD_ELEMENTS; i++)
state.kbd_internal_buffer.buffer[i] = 0;
state.kbd_internal_buffer.head = 0;
state.kbd_internal_buffer.expecting_typematic = 0;
state.kbd_internal_buffer.expecting_make_break = 0;
// Default scancode set is mf2 (translation is controlled by the 8042)
state.expecting_scancodes_set = 0;
//state.current_scancodes_set = 1;
state.current_scancodes_set = 2;
//state.scancodes_translate = 1;
if (powerup) {
state.kbd_internal_buffer.expecting_led_write = 0;
state.kbd_internal_buffer.delay = 1; // 500 mS
state.kbd_internal_buffer.repeat_rate = 0x0b; // 10.9 chars/sec
}
}
/**
* Enqueue a byte (scancode) into the keyboard buffer.
**/
void CKeyboard::enQ(u8 scancode)
{
int tail;
#if defined(DEBUG_KBD)
printf("enQ(0x%02x)", (unsigned) scancode);
#endif
if (state.kbd_internal_buffer.num_elements >= BX_KBD_ELEMENTS) {
printf("internal keyboard buffer full, ignoring scancode.(%02x) \n",
(unsigned) scancode);
return;
}
/* enqueue scancode in multibyte internal keyboard buffer */
#if defined(DEBUG_KBD)
BX_DEBUG(("enQ: putting scancode 0x%02x in internal buffer",
(unsigned) scancode));
#endif
tail = (state.kbd_internal_buffer.head + state.kbd_internal_buffer.num_elements) %
BX_KBD_ELEMENTS;
state.kbd_internal_buffer.buffer[tail] = scancode;
state.kbd_internal_buffer.num_elements++;
if (!state.status.outb && state.kbd_clock_enabled) {
state.timer_pending = 1;
return;
}
}
/**
* Read a byte from keyboard port 60.
**/
u8 CKeyboard::read_60()
{
u8 val;
/* output buffer */
if (state.status.auxb) { /* mouse byte available */
val = state.aux_output_buffer;
state.aux_output_buffer = 0;
state.status.outb = 0;
state.status.auxb = 0;
state.irq12_requested = 0;
if (state.kbd_controller_Qsize) {
unsigned i;
state.aux_output_buffer = state.kbd_controller_Q[0];
state.status.outb = 1;
state.status.auxb = 1;
if (state.allow_irq12)
state.irq12_requested = 1;
for (i=0; i<state.kbd_controller_Qsize-1; i++) {
// move Q elements towards head of queue by one
state.kbd_controller_Q[i] = state.kbd_controller_Q[i+1];
}
state.kbd_controller_Qsize--;
}
//DEV_pic_lower_irq(12);
state.timer_pending = 1;
DoClock();
#if defined(DEBUG_KBD)
BX_DEBUG(("[mouse] read from 0x60 returns 0x%02x", val));
#endif
return val;
}
else if (state.status.outb) { /* kbd byte available */
val = state.kbd_output_buffer;
state.status.outb = 0;
state.status.auxb = 0;
state.irq1_requested = 0;
state.bat_in_progress = 0;
if (state.kbd_controller_Qsize) {
unsigned i;
state.aux_output_buffer = state.kbd_controller_Q[0];
state.status.outb = 1;
state.status.auxb = 1;
if (state.allow_irq1)
state.irq1_requested = 1;
for (i=0; i<state.kbd_controller_Qsize-1; i++) {
// move Q elements towards head of queue by one
state.kbd_controller_Q[i] = state.kbd_controller_Q[i+1];
}
#if defined(DEBUG_KBD)
BX_DEBUG(("s.controller_Qsize: %02X",state.kbd_controller_Qsize));
#endif
state.kbd_controller_Qsize--;
}
// DEV_pic_lower_irq(1);
state.timer_pending = 1;
DoClock();
#if defined(DEBUG_KBD)
BX_DEBUG(("READ(60) = %02x", (unsigned) val));
#endif
return val;
}
else {
#if defined(DEBUG_KBD)
BX_DEBUG(("num_elements = %d", state.kbd_internal_buffer.num_elements));
BX_DEBUG(("read from port 60h with outb empty"));
BX_DEBUG(("READ(60) = %02x", state.kbd_output_buffer));
#endif
return state.kbd_output_buffer;
}
}
/**
* Read a byte from keyboard port 64
*
* The keyboard controller status register
*
* The keyboard controller has an 8-bit status register. It can be inspected by the CPU by reading port 0x64.
* (Typically, it has the value 0x14: keyboard not locked, self-test completed.)
*
* \code
* +------+-----+------+------+-----+------+------+------+
* | PARE | TIM | AUXB | KEYL | C/D | SYSF | INPB | OUTB |
* +------+-----+------+------+-----+------+------+------+
* \endcode
*
* Bit 7: Parity error
* 0: OK.
* 1: Parity error with last byte.
*
* Bit 6: Timeout
* 0: OK.
* 1: General timeout.
*
* Bit 5: Auxiliary output buffer full
* Bit 0 tells whether a read from port 0x60 will be valid. If it is valid, this bit 5 tells what data will be read from port 0x60.
* 0: Keyboard data.
* 1: Mouse data.
*
* Bit 4: Keyboard lock
* 0: Locked.
* 1: Not locked.
*
* Bit 3: Command/Data
* 0: Last write to input buffer was data (written via port 0x60).
* 1: Last write to input buffer was a command (written via port 0x64). (This bit is also referred to as Address Line A2.)
*
* Bit 2: System flag
* Set to 0 after power on reset. Set to 1 after successful completion of the keyboard controller self-test (Basic Assurance Test, BAT). Can also be set by command (see below).
*
* Bit 1: Input buffer status
* 0: Input buffer empty, can be written.
* 1: Input buffer full, don't write yet.
*
* Bit 0: Output buffer status
* 0: Output buffer empty, don't read yet.
* 1: Output buffer full, can be read. (Bit 5 tells whether the available data is from keyboard or mouse.) This bit is cleared when port 0x60 is read.
**/
u8 CKeyboard::read_64()
{
u8 val;
/* status register */
val = (state.status.pare << 7) |
(state.status.tim << 6) |
(state.status.auxb << 5) |
(state.status.keyl << 4) |
(state.status.c_d << 3) |
(state.status.sysf << 2) |
(state.status.inpb << 1) |
(state.status.outb << 0);
state.status.tim = 0;
#if defined(DEBUG_KBD)
BX_DEBUG(("read from 0x64 returns 0x%02x", val));
#endif
return val;
}
/**
* Write a byte to keyboard port 60.
**/
void CKeyboard::write_60(u8 value)
{
#if defined(DEBUG_KBD)
printf("kbd: port 60 write: %02x. \n",value);
#endif
// data byte written last to 0x60
state.status.c_d = 0;
// if expecting data byte from command last sent to port 64h
if (state.expecting_port60h)
{
state.expecting_port60h = 0;
#if defined(DEBUG_KBD)
if (state.status.inpb)
printf("write to port 60h, not ready for write \n");
#endif
switch (state.last_comm)
{
case 0x60: // write command byte
{
//The keyboard controller is provided with some RAM, for example
// 32 bytes, that can be accessed by the CPU. The most important
// part of this RAM is byte 0, the Controller Command Byte (CCB).
// It can be read/written by writing 0x20/0x60 to port 0x64 and
// then reading/writing a data byte from/to port 0x60.
//
// This byte has the following layout.
//
// +---+-------+----+----+---+------+-----+-----+
// | 0 | XLATE | ME | KE | 0 | SYSF | MIE | KIE |
// +---+-------+----+----+---+------+-----+-----+
//
// Bit 6: Translate
// 0: No translation.
// 1: Translate keyboard scancodes, using the translation table
// given above. MCA type 2 controllers cannot set this bit
// to 1. In this case scan code conversion is set using
// keyboard command 0xf0 to port 0x60.
//
// Bit 5: Mouse enable
// 0: Enable mouse.
// 1: Disable mouse by driving the clock line low.
//
// Bit 4: Keyboard enable
// 0: Enable keyboard.
// 1: Disable keyboard by driving the clock line low.
//
// Bit 2: System flag
// This bit is shown in bit 2 of the status register. A
// "cold reboot" is one with this bit set to zero. A
// "warm reboot" is one with this bit set to one (BAT
// already completed). This will influence the tests and
// initializations done by the POST.
//
// Bit 1: Mouse interrupt enable
// 0: Do not use mouse interrupts.
// 1: Send interrupt request IRQ12 when the mouse output
// buffer is full.
//
// Bit 0: Keyboard interrupt enable
// 0: Do not use keyboard interrupts.
// 1: Send interrupt request IRQ1 when the keyboard output
// buffer is full.
//
// When no interrupts are used, the CPU has to poll bits 0
// (and 5) of the status register.
bool scan_convert, disable_keyboard,
disable_aux;
scan_convert = (value >> 6) & 0x01;
disable_aux = (value >> 5) & 0x01;
disable_keyboard = (value >> 4) & 0x01;
state.status.sysf = (value >> 2) & 0x01;
state.allow_irq1 = (value >> 0) & 0x01;
state.allow_irq12 = (value >> 1) & 0x01;
set_kbd_clock_enable(!disable_keyboard);
set_aux_clock_enable(!disable_aux);
if (state.allow_irq12 && state.status.auxb)
state.irq12_requested = 1;
else if (state.allow_irq1 && state.status.outb)
state.irq1_requested = 1;
#if defined(DEBUG_KBD)
BX_DEBUG(( " allow_irq12 set to %u", (unsigned) state.allow_irq12));
if (!scan_convert)
BX_INFO(("keyboard: scan convert turned off"));
#endif
// (mch) NT needs this
state.scancodes_translate = scan_convert;
}
break;
case 0xd1: // write output port
#if defined(DEBUG_KBD)
BX_DEBUG(("write output port with value %02xh", (unsigned) value));
#endif
break;
case 0xd4: // Write to mouse
// I don't think this enables the AUX clock
//set_aux_clock_enable(1); // enable aux clock line
ctrl_to_mouse(value);
// ??? should I reset to previous value of aux enable?
break;
case 0xd3: // write mouse output buffer
// Queue in mouse output buffer
controller_enQ(value, 1);
break;
case 0xd2:
// Queue in keyboard output buffer
controller_enQ(value, 0);
break;
default:
printf("=== unsupported write to port 60h(lastcomm=%02x): %02x \n",
(unsigned) state.last_comm, (unsigned) value);
}
}
else
{
// data byte written last to 0x60
state.status.c_d = 0;
state.expecting_port60h = 0;
/* pass byte to keyboard */
/* ??? should conditionally pass to mouse device here ??? */
if (state.kbd_clock_enabled==0)
set_kbd_clock_enable(1);
ctrl_to_kbd(value);
}
DoClock();
}
/**
* Write a byte to keyboard port 64.
**/
void CKeyboard::write_64(u8 value)
{
#if defined(DEBUG_KBD)
printf("kbd: port 64 write: %02x. \n",value);
#endif
static int kbd_initialized=0;
u8 command_byte;
// command byte written last to 0x64
state.status.c_d = 1;
state.last_comm = value;
// most commands NOT expecting port60 write next
state.expecting_port60h = 0;
switch (value)
{
case 0x20: // get keyboard command byte
#if defined(DEBUG_KBD)
BX_DEBUG(("get keyboard command byte"));
#endif
// controller output buffer must be empty
if (state.status.outb)
{
#if defined(DEBUG_KBD)
BX_ERROR(("kbd: OUTB set and command 0x%02x encountered", value));
#endif
break;
}
command_byte =
(state.scancodes_translate << 6) |
((!state.aux_clock_enabled) << 5) |
((!state.kbd_clock_enabled) << 4) |
(0 << 3) |
(state.status.sysf << 2) |
(state.allow_irq12 << 1) |
(state.allow_irq1 << 0);
controller_enQ(command_byte, 0);
break;
case 0x60: // write command byte
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command 60: write command byte. \n");
#endif
// following byte written to port 60h is command byte
state.expecting_port60h = 1;
break;
case 0xa0:
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command a0: BIOS name (not supported). \n");
#endif
break;
case 0xa1:
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command a0: BIOS version (not supported). \n");
#endif
break;
case 0xa7: // disable the aux device
set_aux_clock_enable(0);
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command a7: aux i/f disable. \n");
#endif
break;
case 0xa8: // enable the aux device
set_aux_clock_enable(1);
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command a7: aux i/f enable. \n");
#endif
break;
case 0xa9: // Test Mouse Port
// controller output buffer must be empty
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command a9: aux i/f test. \n");
#endif
if (state.status.outb) {
printf("kbd: OUTB set and command 0x%02x encountered", value);
break;
}
controller_enQ(0x00, 0); // no errors detected
break;
case 0xaa: // motherboard controller self test
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command aa: self test. \n");
#endif
if (kbd_initialized == 0)
{
state.kbd_controller_Qsize = 0;
state.status.outb = 0;
kbd_initialized = 1;
}
// controller output buffer must be empty
if (state.status.outb)
{
printf("kbd: OUTB set and command 0x%02x encountered", value);
//break;
// drain the queue?
state.kbd_internal_buffer.head = 0;
state.kbd_internal_buffer.num_elements = 0;
state.status.outb = 0;
}
state.status.sysf = 1; // self test complete
controller_enQ(0x55, 0); // controller OK
break;
case 0xab: // Interface Test
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command ab: kbd i/f test. \n");
#endif
// controller output buffer must be empty
if (state.status.outb)
{
printf("kbd: OUTB set and command 0x%02x encountered", value);
break;
}
controller_enQ(0x00, 0);
break;
case 0xad: // disable keyboard
set_kbd_clock_enable(0);
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command ad: kbd i/f disable. \n");
#endif
break;
case 0xae: // enable keyboard
set_kbd_clock_enable(1);
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command ae: kbd i/f enable. \n");
#endif
break;
case 0xaf: // get controller version
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command af: controller version (not supported). \n");
#endif
break;
case 0xc0: // read input port
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command c0: read input port. \n");
#endif
// controller output buffer must be empty
if (state.status.outb)
{
BX_PANIC(("kbd: OUTB set and command 0x%02x encountered", value));
break;
}
// keyboard not inhibited
controller_enQ(0x80, 0);
break;
case 0xd0: // read output port: next byte read from port 60h
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command d0: read output port. (partial) \n");
#endif
// controller output buffer must be empty
if (state.status.outb)
{
BX_PANIC(("kbd: OUTB set and command 0x%02x encountered", value));
break;
}
controller_enQ(
(state.irq12_requested << 5) |
(state.irq1_requested << 4) |
// (BX_GET_ENABLE_A20() << 1) |
0x01, 0);
break;
case 0xd1: // write output port: next byte written to port 60h
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command d1: write output port. \n");
#endif
// following byte to port 60h written to output port
state.expecting_port60h = 1;
break;
case 0xd3: // write mouse output buffer
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command d3: write aux output buffer. \n");
#endif
// following byte to port 60h written to output port as mouse write.
state.expecting_port60h = 1;
break;
case 0xd4: // write to mouse
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command d4: write to aux. \n");
#endif
// following byte written to port 60h
state.expecting_port60h = 1;
break;
case 0xd2: // write keyboard output buffer
#if defined(DEBUG_KBD)
printf("kbd_ctrl: command d2: write kbd output buffer. \n");
#endif
state.expecting_port60h = 1;
break;
case 0xc1: // Continuous Input Port Poll, Low
case 0xc2: // Continuous Input Port Poll, High
case 0xe0: // Read Test Inputs
BX_PANIC(("io write 0x64: command = %02xh", (unsigned) value));
break;
default:
if (value==0xff || (value>=0xf0 && value<=0xfd))
{
/* useless pulse output bit commands ??? */
#if defined(DEBUG_KBD)
BX_DEBUG(("io write to port 64h, useless command %02x", (unsigned) value));
#endif
return;
}
BX_ERROR(("unsupported io write to keyboard port 64, value = %x", (unsigned) value));
break;
}
DoClock();
}
/**
* Enqueue a byte into one of the keyboard controller's output buffers.
**/
void CKeyboard::controller_enQ(u8 data, unsigned source)
{
// source is 0 for keyboard, 1 for mouse
#if defined(DEBUG_KBD)
BX_DEBUG(("controller_enQ(%02x) source=%02x", (unsigned) data,source));
#endif
// see if we need to Q this byte from the controller
// remember this includes mouse bytes.
if (state.status.outb)
{
if (state.kbd_controller_Qsize >= BX_KBD_CONTROLLER_QSIZE)
FAILURE("controller_enq(): controller_Q full!");
state.kbd_controller_Q[state.kbd_controller_Qsize++] = data;
state.kbd_controller_Qsource = source;
return;
}
// the Q is empty
if (source == 0)
{ // keyboard
state.kbd_output_buffer = data;
state.status.outb = 1;
state.status.auxb = 0;
state.status.inpb = 0;
if (state.allow_irq1)
state.irq1_requested = 1;
}
else
{ // mouse
state.aux_output_buffer = data;
state.status.outb = 1;
state.status.auxb = 1;
state.status.inpb = 0;
if (state.allow_irq12)
state.irq12_requested = 1;
}
}
/**
* Enable or disable the keyboard clock.
**/
void CKeyboard::set_kbd_clock_enable(u8 value)
{
bool prev_kbd_clock_enabled;
if (value==0)
{
state.kbd_clock_enabled = 0;
}
else
{
/* is another byte waiting to be sent from the keyboard ? */
prev_kbd_clock_enabled = state.kbd_clock_enabled;
state.kbd_clock_enabled = 1;
if (prev_kbd_clock_enabled==0 && state.status.outb==0)
state.timer_pending = 1;
}
}
/**
* Enable or disable the mouse clock.
**/
void CKeyboard::set_aux_clock_enable(u8 value)
{
bool prev_aux_clock_enabled;
#if defined(DEBUG_KBD)
BX_DEBUG(("set_aux_clock_enable(%u)", (unsigned) value));
#endif
if (value==0)
{
state.aux_clock_enabled = 0;
}
else
{
/* is another byte waiting to be sent from the keyboard ? */
prev_aux_clock_enabled = state.aux_clock_enabled;
state.aux_clock_enabled = 1;
if (prev_aux_clock_enabled==0 && state.status.outb==0)
state.timer_pending = 1;
}
}
/**
* Send a byte from controller to keyboard
**/
void CKeyboard::ctrl_to_kbd(u8 value)
{
#if defined(DEBUG_KBD)
BX_DEBUG(("controller passed byte %02xh to keyboard", value));
#endif
if (state.kbd_internal_buffer.expecting_make_break) {
state.kbd_internal_buffer.expecting_make_break = 0;
#if defined(DEBUG_KBD)
printf("setting key %x to make/break mode (unused) \n", value);
#endif
enQ(0xFA); // send ACK
return;
}