1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <pc.h> |
---|
6 | #include <go32.h> |
---|
7 | #include <bsp.h> |
---|
8 | |
---|
9 | /* |
---|
10 | * Ports for PC keyboard |
---|
11 | */ |
---|
12 | #define KBD_CTL 0x61 |
---|
13 | #define KBD_DATA 0x60 |
---|
14 | #define KBD_STATUS 0x64 |
---|
15 | |
---|
16 | static char key_map[] = { |
---|
17 | 0,033,'1','2','3','4','5','6','7','8','9','0','-','=','\b','\t', |
---|
18 | 'q','w','e','r','t','y','u','i','o','p','[',']',015,0x80, |
---|
19 | 'a','s','d','f','g','h','j','k','l',';',047,0140,0x80, |
---|
20 | 0134,'z','x','c','v','b','n','m',',','.','/',0x80, |
---|
21 | '*',0x80,' ',0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, |
---|
22 | 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, |
---|
23 | 0x80,0x80,0x80,'0',0177 |
---|
24 | }; |
---|
25 | |
---|
26 | static char shift_map[] = { |
---|
27 | 0,033,'!','@','#','$','%','^','&','*','(',')','_','+','\b','\t', |
---|
28 | 'Q','W','E','R','T','Y','U','I','O','P','{','}',015,0x80, |
---|
29 | 'A','S','D','F','G','H','J','K','L',':',042,'~',0x80, |
---|
30 | '|','Z','X','C','V','B','N','M','<','>','?',0x80, |
---|
31 | '*',0x80,' ',0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, |
---|
32 | 0x80,0x80,0x80,0x80,'7','8','9',0x80,'4','5','6',0x80, |
---|
33 | '1','2','3','0',177 |
---|
34 | }; |
---|
35 | |
---|
36 | extern int _IBMPC_Use_Go32_IO; |
---|
37 | |
---|
38 | #define KBD_BUF_SIZE 256 |
---|
39 | static char kbd_buffer[ KBD_BUF_SIZE ]; |
---|
40 | static unsigned int kbd_first = 0; |
---|
41 | static unsigned int kbd_last = 0; |
---|
42 | |
---|
43 | /* This function can be called during a poll for input, or by an ISR. */ |
---|
44 | /* Basically any time you want to process a keypress. */ |
---|
45 | int _IBMPC_scankey( char * ch ) |
---|
46 | { |
---|
47 | unsigned char c; |
---|
48 | unsigned char outch; |
---|
49 | static int shift_pressed = 0; |
---|
50 | static int ctrl_pressed = 0; |
---|
51 | static int caps_pressed = 0; |
---|
52 | |
---|
53 | /* Read keyboard controller, toggle enable */ |
---|
54 | inport_byte( KBD_CTL, c ); |
---|
55 | outport_byte( KBD_CTL, c & ~0x80 ); |
---|
56 | outport_byte( KBD_CTL, c | 0x80 ); |
---|
57 | outport_byte( KBD_CTL, c & ~0x80 ); |
---|
58 | |
---|
59 | /* See if it has data */ |
---|
60 | inport_byte( KBD_STATUS, c ); |
---|
61 | if ( ( c & 0x01 ) == 0 ) |
---|
62 | return 0; |
---|
63 | |
---|
64 | /* Read the data. Handle nonsense with shift, control, etc. */ |
---|
65 | inport_byte( KBD_DATA, c ); |
---|
66 | switch ( c ) { |
---|
67 | case 0x36: |
---|
68 | case 0x2a: |
---|
69 | shift_pressed = 1; |
---|
70 | return 0; |
---|
71 | case 0x3a: |
---|
72 | caps_pressed = 1; |
---|
73 | return 0; |
---|
74 | case 0x1d: |
---|
75 | ctrl_pressed = 1; |
---|
76 | return 0; |
---|
77 | case 0xb6: |
---|
78 | case 0xaa: |
---|
79 | shift_pressed = 0; |
---|
80 | return 0; |
---|
81 | case 0xba: |
---|
82 | caps_pressed = 0; |
---|
83 | return 0; |
---|
84 | case 0x9d: |
---|
85 | ctrl_pressed = 0; |
---|
86 | return 0; |
---|
87 | /* |
---|
88 | * Ignore unrecognized keys--usually arrow and such |
---|
89 | */ |
---|
90 | default: |
---|
91 | if ( c & 0x80 ) |
---|
92 | /* High-bit on means key is being released, not pressed */ |
---|
93 | return 0; |
---|
94 | if ( c == 88 ) |
---|
95 | /* F12 - abort */ |
---|
96 | exit( 1 ); |
---|
97 | if ( c > 0x39 ) { |
---|
98 | return 0; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /* Strip high bit, look up in our map */ |
---|
103 | c &= 127; |
---|
104 | if ( ctrl_pressed ) { |
---|
105 | outch = key_map[c]; |
---|
106 | outch &= 037; |
---|
107 | } else { |
---|
108 | outch = shift_pressed ? shift_map[c] : key_map[c]; |
---|
109 | if ( caps_pressed ) { |
---|
110 | if ( outch >= 'A' && outch <= 'Z' ) outch += 'a' - 'A'; |
---|
111 | else if ( outch >= 'a' && outch <= 'z' ) outch -= 'a' - 'A'; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | *ch = outch; |
---|
116 | return 1; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void _IBMPC_keyboard_isr( rtems_unsigned32 interrupt ) |
---|
121 | { |
---|
122 | if ( _IBMPC_scankey( & kbd_buffer[ kbd_last ] ) ) { |
---|
123 | /* Got one; save it if there is enough room in buffer. */ |
---|
124 | unsigned int next = (kbd_last + 1) % KBD_BUF_SIZE; |
---|
125 | if ( next != kbd_first ) |
---|
126 | kbd_last = next; |
---|
127 | } |
---|
128 | |
---|
129 | /* Mark interrupt as handled */ |
---|
130 | outport_byte( 0x20, 0x20 ); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | int _IBMPC_chrdy( char * ch ) |
---|
135 | { |
---|
136 | if ( _IBMPC_Use_Go32_IO ) { |
---|
137 | /* Read keyboard via BIOS: raw mode. */ |
---|
138 | if ( kbhit() ) { |
---|
139 | *ch = getkey(); |
---|
140 | return 1; |
---|
141 | } else { |
---|
142 | return 0; |
---|
143 | } |
---|
144 | } else { |
---|
145 | /* Check buffer our ISR builds */ |
---|
146 | if ( kbd_first != kbd_last ) { |
---|
147 | *ch = kbd_buffer[ kbd_first ]; |
---|
148 | kbd_first = (kbd_first + 1) % KBD_BUF_SIZE; |
---|
149 | return 1; |
---|
150 | } else { |
---|
151 | return 0; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | int _IBMPC_inch( void ) |
---|
157 | { |
---|
158 | char Ch; |
---|
159 | while ( ! _IBMPC_chrdy( & Ch ) ) |
---|
160 | continue; |
---|
161 | return Ch; |
---|
162 | } |
---|