[NTVDM][KERNEL32][BASESRV]
[reactos.git] / reactos / subsystems / ntvdm / bios / bios32 / kbdbios32.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: kbdbios32.c
5 * PURPOSE: VDM Keyboard 32-bit BIOS
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "emulator.h"
14 #include "callback.h"
15
16 #include "kbdbios32.h"
17 #include "../kbdbios.h"
18 #include "bios32p.h"
19
20 #include "io.h"
21 #include "hardware/ps2.h"
22
23 /* PRIVATE VARIABLES **********************************************************/
24
25 static BYTE BiosKeyboardMap[256];
26
27 /* PRIVATE FUNCTIONS **********************************************************/
28
29 static BOOLEAN BiosKbdBufferPush(WORD Data)
30 {
31 /* Get the location of the element after the tail */
32 WORD NextElement = Bda->KeybdBufferTail + sizeof(WORD);
33
34 /* Wrap it around if it's at or beyond the end */
35 if (NextElement >= Bda->KeybdBufferEnd) NextElement = Bda->KeybdBufferStart;
36
37 /* If it's full, fail */
38 if (NextElement == Bda->KeybdBufferHead)
39 {
40 DPRINT1("BIOS keyboard buffer full.\n");
41 return FALSE;
42 }
43
44 /* Put the value in the queue */
45 *((LPWORD)((ULONG_PTR)Bda + Bda->KeybdBufferTail)) = Data;
46 Bda->KeybdBufferTail = NextElement;
47
48 /* Return success */
49 return TRUE;
50 }
51
52 static BOOLEAN BiosKbdBufferTop(LPWORD Data)
53 {
54 /* If it's empty, fail */
55 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return FALSE;
56
57 /* Otherwise, get the value and return success */
58 *Data = *((LPWORD)((ULONG_PTR)Bda + Bda->KeybdBufferHead));
59
60 return TRUE;
61 }
62
63 static BOOLEAN BiosKbdBufferPop(VOID)
64 {
65 /* If it's empty, fail */
66 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return FALSE;
67
68 /* Remove the value from the queue */
69 Bda->KeybdBufferHead += sizeof(WORD);
70
71 /* Check if we are at, or have passed, the end of the buffer */
72 if (Bda->KeybdBufferHead >= Bda->KeybdBufferEnd)
73 {
74 /* Return it to the beginning */
75 Bda->KeybdBufferHead = Bda->KeybdBufferStart;
76 }
77
78 /* Return success */
79 return TRUE;
80 }
81
82 static WORD BiosPeekCharacter(VOID)
83 {
84 WORD CharacterData = 0;
85
86 /* Get the key from the queue, but don't remove it */
87 if (BiosKbdBufferTop(&CharacterData)) return CharacterData;
88 else return 0xFFFF;
89 }
90
91 static WORD BiosGetCharacter(VOID)
92 {
93 WORD CharacterData = 0;
94
95 /* Check if there is a key available, and if so, remove it from the queue */
96 if (BiosKbdBufferTop(&CharacterData)) BiosKbdBufferPop();
97 else CharacterData = 0xFFFF;
98
99 return CharacterData;
100 }
101
102 static VOID WINAPI BiosKeyboardService(LPWORD Stack)
103 {
104 switch (getAH())
105 {
106 /* Wait for keystroke and read */
107 case 0x00:
108 /* Wait for extended keystroke and read */
109 case 0x10: // FIXME: Temporarily do the same as INT 16h, 00h
110 {
111 /* Read the character (and wait if necessary) */
112 WORD Character = BiosGetCharacter();
113
114 if (Character == 0xFFFF)
115 {
116 /* No key available. Set the handler CF to repeat the BOP */
117 setCF(1);
118 break;
119 }
120
121 setAX(Character);
122
123 break;
124 }
125
126 /* Get keystroke status */
127 case 0x01:
128 /* Get extended keystroke status */
129 case 0x11: // FIXME: Temporarily do the same as INT 16h, 01h
130 {
131 WORD Character = BiosPeekCharacter();
132
133 if (Character != 0xFFFF)
134 {
135 /* There is a character, clear ZF and return it */
136 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_ZF;
137 setAX(Character);
138 }
139 else
140 {
141 /* No character, set ZF */
142 Stack[STACK_FLAGS] |= EMULATOR_FLAG_ZF;
143 }
144
145 break;
146 }
147
148 /* Get shift status */
149 case 0x02:
150 {
151 /* Return the lower byte of the keyboard shift status word */
152 setAL(LOBYTE(Bda->KeybdShiftFlags));
153 break;
154 }
155
156 /* Reserved */
157 case 0x04:
158 {
159 DPRINT1("BIOS Function INT 16h, AH = 0x04 is RESERVED\n");
160 break;
161 }
162
163 /* Push keystroke */
164 case 0x05:
165 {
166 /* Return 0 if success, 1 if failure */
167 setAL(BiosKbdBufferPush(getCX()) == FALSE);
168 break;
169 }
170
171 /* Get extended shift status */
172 case 0x12:
173 {
174 /*
175 * Be careful! The returned word is similar to Bda->KeybdShiftFlags
176 * but the high byte is organized differently:
177 * the bytes 2 and 3 of the high byte are not the same...
178 */
179 WORD KeybdShiftFlags = (Bda->KeybdShiftFlags & 0xF3FF);
180
181 /* Return the extended keyboard shift status word */
182 setAX(KeybdShiftFlags);
183 break;
184 }
185
186 default:
187 {
188 DPRINT1("BIOS Function INT 16h, AH = 0x%02X NOT IMPLEMENTED\n",
189 getAH());
190 }
191 }
192 }
193
194 // Keyboard IRQ 1
195 static VOID WINAPI BiosKeyboardIrq(LPWORD Stack)
196 {
197 BYTE ScanCode, VirtualKey;
198 WORD Character;
199
200 /* Get the scan code and virtual key code */
201 ScanCode = IOReadB(PS2_DATA_PORT);
202 VirtualKey = MapVirtualKey(ScanCode & 0x7F, MAPVK_VSC_TO_VK);
203
204 /* Check if this is a key press or release */
205 if (!(ScanCode & (1 << 7)))
206 {
207 /* Key press */
208 if (VirtualKey == VK_NUMLOCK ||
209 VirtualKey == VK_CAPITAL ||
210 VirtualKey == VK_SCROLL ||
211 VirtualKey == VK_INSERT)
212 {
213 /* For toggle keys, toggle the lowest bit in the keyboard map */
214 BiosKeyboardMap[VirtualKey] ^= ~(1 << 0);
215 }
216
217 /* Set the highest bit */
218 BiosKeyboardMap[VirtualKey] |= (1 << 7);
219
220 /* Find out which character this is */
221 Character = 0;
222 if (ToAscii(VirtualKey, ScanCode, BiosKeyboardMap, &Character, 0) == 0)
223 {
224 /* Not ASCII */
225 Character = 0;
226 }
227
228 /* Push it onto the BIOS keyboard queue */
229 BiosKbdBufferPush(MAKEWORD(Character, ScanCode));
230 }
231 else
232 {
233 /* Key release, unset the highest bit */
234 BiosKeyboardMap[VirtualKey] &= ~(1 << 7);
235 }
236
237 /* Clear the keyboard flags */
238 Bda->KeybdShiftFlags = 0;
239
240 /* Set the appropriate flags based on the state */
241 if (BiosKeyboardMap[VK_RSHIFT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_RSHIFT;
242 if (BiosKeyboardMap[VK_LSHIFT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_LSHIFT;
243 if (BiosKeyboardMap[VK_CONTROL] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_CTRL;
244 if (BiosKeyboardMap[VK_MENU] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_ALT;
245 if (BiosKeyboardMap[VK_SCROLL] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_SCROLL_ON;
246 if (BiosKeyboardMap[VK_NUMLOCK] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_NUMLOCK_ON;
247 if (BiosKeyboardMap[VK_CAPITAL] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_CAPSLOCK_ON;
248 if (BiosKeyboardMap[VK_INSERT] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_INSERT_ON;
249 if (BiosKeyboardMap[VK_RMENU] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_RALT;
250 if (BiosKeyboardMap[VK_LMENU] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_LALT;
251 if (BiosKeyboardMap[VK_SNAPSHOT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_SYSRQ;
252 if (BiosKeyboardMap[VK_PAUSE] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_PAUSE;
253 if (BiosKeyboardMap[VK_SCROLL] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_SCROLL;
254 if (BiosKeyboardMap[VK_NUMLOCK] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_NUMLOCK;
255 if (BiosKeyboardMap[VK_CAPITAL] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_CAPSLOCK;
256 if (BiosKeyboardMap[VK_INSERT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_INSERT;
257
258 PicIRQComplete(Stack);
259 }
260
261 /* PUBLIC FUNCTIONS ***********************************************************/
262
263 BOOLEAN KbdBios32Initialize(VOID)
264 {
265 /* Initialize the common Keyboard BIOS Support Library */
266 if (!KbdBiosInitialize()) return FALSE;
267
268 /* Initialize the BDA */
269 Bda->KeybdBufferStart = FIELD_OFFSET(BIOS_DATA_AREA, KeybdBuffer);
270 Bda->KeybdBufferEnd = Bda->KeybdBufferStart + BIOS_KBD_BUFFER_SIZE * sizeof(WORD);
271 Bda->KeybdBufferHead = Bda->KeybdBufferTail = Bda->KeybdBufferStart;
272
273 // FIXME: Fill the keyboard buffer with invalid values, for diagnostic purposes...
274 RtlFillMemory(((LPVOID)((ULONG_PTR)Bda + Bda->KeybdBufferStart)), BIOS_KBD_BUFFER_SIZE * sizeof(WORD), 'A');
275
276 /* Register the BIOS 32-bit Interrupts */
277
278 /* Initialize software vector handlers */
279 RegisterBiosInt32(BIOS_KBD_INTERRUPT, BiosKeyboardService);
280
281 /* Set up the HW vector interrupts */
282 EnableHwIRQ(1, BiosKeyboardIrq);
283 // EnableHwIRQ(12, BiosMouseIrq);
284
285 return TRUE;
286 }
287
288 VOID KbdBios32Cleanup(VOID)
289 {
290 /* Cleanup the common Keyboard BIOS Support Library */
291 KbdBiosCleanup();
292 }
293
294 /* EOF */