2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VDM Keyboard 32-bit BIOS
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
16 #include "kbdbios32.h"
17 #include "../kbdbios.h"
21 #include "hardware/ps2.h"
23 /* PRIVATE VARIABLES **********************************************************/
25 static BYTE BiosKeyboardMap
[256];
27 /* PRIVATE FUNCTIONS **********************************************************/
29 static BOOLEAN
BiosKbdBufferPush(WORD Data
)
31 /* Get the location of the element after the tail */
32 WORD NextElement
= Bda
->KeybdBufferTail
+ sizeof(WORD
);
34 /* Wrap it around if it's at or beyond the end */
35 if (NextElement
>= Bda
->KeybdBufferEnd
) NextElement
= Bda
->KeybdBufferStart
;
37 /* If it's full, fail */
38 if (NextElement
== Bda
->KeybdBufferHead
) return FALSE
;
40 /* Put the value in the queue */
41 *((LPWORD
)((ULONG_PTR
)Bda
+ Bda
->KeybdBufferTail
)) = Data
;
42 Bda
->KeybdBufferTail
+= sizeof(WORD
);
44 /* Check if we are at, or have passed, the end of the buffer */
45 if (Bda
->KeybdBufferTail
>= Bda
->KeybdBufferEnd
)
47 /* Return it to the beginning */
48 Bda
->KeybdBufferTail
= Bda
->KeybdBufferStart
;
55 static BOOLEAN
BiosKbdBufferTop(LPWORD Data
)
57 /* If it's empty, fail */
58 if (Bda
->KeybdBufferHead
== Bda
->KeybdBufferTail
) return FALSE
;
60 /* Otherwise, get the value and return success */
61 *Data
= *((LPWORD
)((ULONG_PTR
)Bda
+ Bda
->KeybdBufferHead
));
66 static BOOLEAN
BiosKbdBufferPop(VOID
)
68 /* If it's empty, fail */
69 if (Bda
->KeybdBufferHead
== Bda
->KeybdBufferTail
) return FALSE
;
71 /* Remove the value from the queue */
72 Bda
->KeybdBufferHead
+= sizeof(WORD
);
74 /* Check if we are at, or have passed, the end of the buffer */
75 if (Bda
->KeybdBufferHead
>= Bda
->KeybdBufferEnd
)
77 /* Return it to the beginning */
78 Bda
->KeybdBufferHead
= Bda
->KeybdBufferStart
;
85 static WORD
BiosPeekCharacter(VOID
)
87 WORD CharacterData
= 0;
89 /* Get the key from the queue, but don't remove it */
90 if (BiosKbdBufferTop(&CharacterData
)) return CharacterData
;
94 WORD
BiosGetCharacter(VOID
)
96 WORD CharacterData
= 0;
98 /* Check if there is a key available */
99 if (BiosKbdBufferTop(&CharacterData
))
101 /* A key was available, remove it from the queue */
106 /* No key available. Set the handler CF to repeat the BOP */
108 // CharacterData = 0xFFFF;
111 return CharacterData
;
114 static VOID WINAPI
BiosKeyboardService(LPWORD Stack
)
118 /* Wait for keystroke and read */
120 /* Wait for extended keystroke and read */
121 case 0x10: // FIXME: Temporarily do the same as INT 16h, 00h
123 /* Read the character (and wait if necessary) */
124 setAX(BiosGetCharacter());
128 /* Get keystroke status */
130 /* Get extended keystroke status */
131 case 0x11: // FIXME: Temporarily do the same as INT 16h, 01h
133 WORD Data
= BiosPeekCharacter();
137 /* There is a character, clear ZF and return it */
138 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_ZF
;
143 /* No character, set ZF */
144 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_ZF
;
150 /* Get shift status */
153 /* Return the lower byte of the keyboard shift status word */
154 setAL(LOBYTE(Bda
->KeybdShiftFlags
));
161 DPRINT1("BIOS Function INT 16h, AH = 0x04 is RESERVED\n");
168 /* Return 0 if success, 1 if failure */
169 setAL(BiosKbdBufferPush(getCX()) == FALSE
);
173 /* Get extended shift status */
177 * Be careful! The returned word is similar to Bda->KeybdShiftFlags
178 * but the high byte is organized differently:
179 * the bytes 2 and 3 of the high byte are not the same...
181 WORD KeybdShiftFlags
= (Bda
->KeybdShiftFlags
& 0xF3FF);
183 /* Return the extended keyboard shift status word */
184 setAX(KeybdShiftFlags
);
190 DPRINT1("BIOS Function INT 16h, AH = 0x%02X NOT IMPLEMENTED\n",
197 static VOID WINAPI
BiosKeyboardIrq(LPWORD Stack
)
199 BYTE ScanCode
, VirtualKey
;
202 /* Get the scan code and virtual key code */
203 ScanCode
= IOReadB(PS2_DATA_PORT
);
204 VirtualKey
= MapVirtualKey(ScanCode
& 0x7F, MAPVK_VSC_TO_VK
);
206 /* Check if this is a key press or release */
207 if (!(ScanCode
& (1 << 7)))
210 if (VirtualKey
== VK_NUMLOCK
||
211 VirtualKey
== VK_CAPITAL
||
212 VirtualKey
== VK_SCROLL
||
213 VirtualKey
== VK_INSERT
)
215 /* For toggle keys, toggle the lowest bit in the keyboard map */
216 BiosKeyboardMap
[VirtualKey
] ^= ~(1 << 0);
219 /* Set the highest bit */
220 BiosKeyboardMap
[VirtualKey
] |= (1 << 7);
222 /* Find out which character this is */
224 if (ToAscii(VirtualKey
, ScanCode
, BiosKeyboardMap
, &Character
, 0) == 0)
230 /* Push it onto the BIOS keyboard queue */
231 BiosKbdBufferPush(MAKEWORD(Character
, ScanCode
));
235 /* Key release, unset the highest bit */
236 BiosKeyboardMap
[VirtualKey
] &= ~(1 << 7);
239 /* Clear the keyboard flags */
240 Bda
->KeybdShiftFlags
= 0;
242 /* Set the appropriate flags based on the state */
243 if (BiosKeyboardMap
[VK_RSHIFT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_RSHIFT
;
244 if (BiosKeyboardMap
[VK_LSHIFT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_LSHIFT
;
245 if (BiosKeyboardMap
[VK_CONTROL
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_CTRL
;
246 if (BiosKeyboardMap
[VK_MENU
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_ALT
;
247 if (BiosKeyboardMap
[VK_SCROLL
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_SCROLL_ON
;
248 if (BiosKeyboardMap
[VK_NUMLOCK
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_NUMLOCK_ON
;
249 if (BiosKeyboardMap
[VK_CAPITAL
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_CAPSLOCK_ON
;
250 if (BiosKeyboardMap
[VK_INSERT
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_INSERT_ON
;
251 if (BiosKeyboardMap
[VK_RMENU
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_RALT
;
252 if (BiosKeyboardMap
[VK_LMENU
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_LALT
;
253 if (BiosKeyboardMap
[VK_SNAPSHOT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_SYSRQ
;
254 if (BiosKeyboardMap
[VK_PAUSE
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_PAUSE
;
255 if (BiosKeyboardMap
[VK_SCROLL
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_SCROLL
;
256 if (BiosKeyboardMap
[VK_NUMLOCK
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_NUMLOCK
;
257 if (BiosKeyboardMap
[VK_CAPITAL
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_CAPSLOCK
;
258 if (BiosKeyboardMap
[VK_INSERT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_INSERT
;
260 PicIRQComplete(Stack
);
263 /* PUBLIC FUNCTIONS ***********************************************************/
265 BOOLEAN
KbdBios32Initialize(VOID
)
267 /* Initialize the common Keyboard BIOS Support Library */
268 if (!KbdBiosInitialize()) return FALSE
;
270 /* Initialize the BDA */
271 Bda
->KeybdBufferStart
= FIELD_OFFSET(BIOS_DATA_AREA
, KeybdBuffer
);
272 Bda
->KeybdBufferEnd
= Bda
->KeybdBufferStart
+ BIOS_KBD_BUFFER_SIZE
* sizeof(WORD
);
273 Bda
->KeybdBufferHead
= Bda
->KeybdBufferTail
= 0;
275 /* Register the BIOS 32-bit Interrupts */
277 /* Initialize software vector handlers */
278 RegisterBiosInt32(BIOS_KBD_INTERRUPT
, BiosKeyboardService
);
280 /* Set up the HW vector interrupts */
281 EnableHwIRQ(1, BiosKeyboardIrq
);
282 // EnableHwIRQ(12, BiosMouseIrq);
287 VOID
KbdBios32Cleanup(VOID
)
289 /* Cleanup the common Keyboard BIOS Support Library */