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 static WORD
BiosGetCharacter(VOID
)
96 WORD CharacterData
= 0;
98 /* Check if there is a key available, and if so, remove it from the queue */
99 if (BiosKbdBufferTop(&CharacterData
)) BiosKbdBufferPop();
100 else CharacterData
= 0xFFFF;
102 return CharacterData
;
105 static VOID WINAPI
BiosKeyboardService(LPWORD Stack
)
109 /* Wait for keystroke and read */
111 /* Wait for extended keystroke and read */
112 case 0x10: // FIXME: Temporarily do the same as INT 16h, 00h
114 /* Read the character (and wait if necessary) */
115 WORD Character
= BiosGetCharacter();
117 if (Character
== 0xFFFF)
119 /* No key available. Set the handler CF to repeat the BOP */
129 /* Get keystroke status */
131 /* Get extended keystroke status */
132 case 0x11: // FIXME: Temporarily do the same as INT 16h, 01h
134 WORD Character
= BiosPeekCharacter();
136 if (Character
!= 0xFFFF)
138 /* There is a character, clear ZF and return it */
139 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_ZF
;
144 /* No character, set ZF */
145 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_ZF
;
151 /* Get shift status */
154 /* Return the lower byte of the keyboard shift status word */
155 setAL(LOBYTE(Bda
->KeybdShiftFlags
));
162 DPRINT1("BIOS Function INT 16h, AH = 0x04 is RESERVED\n");
169 /* Return 0 if success, 1 if failure */
170 setAL(BiosKbdBufferPush(getCX()) == FALSE
);
174 /* Get extended shift status */
178 * Be careful! The returned word is similar to Bda->KeybdShiftFlags
179 * but the high byte is organized differently:
180 * the bytes 2 and 3 of the high byte are not the same...
182 WORD KeybdShiftFlags
= (Bda
->KeybdShiftFlags
& 0xF3FF);
184 /* Return the extended keyboard shift status word */
185 setAX(KeybdShiftFlags
);
191 DPRINT1("BIOS Function INT 16h, AH = 0x%02X NOT IMPLEMENTED\n",
198 static VOID WINAPI
BiosKeyboardIrq(LPWORD Stack
)
200 BYTE ScanCode
, VirtualKey
;
203 /* Get the scan code and virtual key code */
204 ScanCode
= IOReadB(PS2_DATA_PORT
);
205 VirtualKey
= MapVirtualKey(ScanCode
& 0x7F, MAPVK_VSC_TO_VK
);
207 /* Check if this is a key press or release */
208 if (!(ScanCode
& (1 << 7)))
211 if (VirtualKey
== VK_NUMLOCK
||
212 VirtualKey
== VK_CAPITAL
||
213 VirtualKey
== VK_SCROLL
||
214 VirtualKey
== VK_INSERT
)
216 /* For toggle keys, toggle the lowest bit in the keyboard map */
217 BiosKeyboardMap
[VirtualKey
] ^= ~(1 << 0);
220 /* Set the highest bit */
221 BiosKeyboardMap
[VirtualKey
] |= (1 << 7);
223 /* Find out which character this is */
225 if (ToAscii(VirtualKey
, ScanCode
, BiosKeyboardMap
, &Character
, 0) == 0)
231 /* Push it onto the BIOS keyboard queue */
232 BiosKbdBufferPush(MAKEWORD(Character
, ScanCode
));
236 /* Key release, unset the highest bit */
237 BiosKeyboardMap
[VirtualKey
] &= ~(1 << 7);
240 /* Clear the keyboard flags */
241 Bda
->KeybdShiftFlags
= 0;
243 /* Set the appropriate flags based on the state */
244 if (BiosKeyboardMap
[VK_RSHIFT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_RSHIFT
;
245 if (BiosKeyboardMap
[VK_LSHIFT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_LSHIFT
;
246 if (BiosKeyboardMap
[VK_CONTROL
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_CTRL
;
247 if (BiosKeyboardMap
[VK_MENU
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_ALT
;
248 if (BiosKeyboardMap
[VK_SCROLL
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_SCROLL_ON
;
249 if (BiosKeyboardMap
[VK_NUMLOCK
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_NUMLOCK_ON
;
250 if (BiosKeyboardMap
[VK_CAPITAL
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_CAPSLOCK_ON
;
251 if (BiosKeyboardMap
[VK_INSERT
] & (1 << 0)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_INSERT_ON
;
252 if (BiosKeyboardMap
[VK_RMENU
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_RALT
;
253 if (BiosKeyboardMap
[VK_LMENU
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_LALT
;
254 if (BiosKeyboardMap
[VK_SNAPSHOT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_SYSRQ
;
255 if (BiosKeyboardMap
[VK_PAUSE
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_PAUSE
;
256 if (BiosKeyboardMap
[VK_SCROLL
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_SCROLL
;
257 if (BiosKeyboardMap
[VK_NUMLOCK
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_NUMLOCK
;
258 if (BiosKeyboardMap
[VK_CAPITAL
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_CAPSLOCK
;
259 if (BiosKeyboardMap
[VK_INSERT
] & (1 << 7)) Bda
->KeybdShiftFlags
|= BDA_KBDFLAG_INSERT
;
261 PicIRQComplete(Stack
);
264 /* PUBLIC FUNCTIONS ***********************************************************/
266 BOOLEAN
KbdBios32Initialize(VOID
)
268 /* Initialize the common Keyboard BIOS Support Library */
269 if (!KbdBiosInitialize()) return FALSE
;
271 /* Initialize the BDA */
272 Bda
->KeybdBufferStart
= FIELD_OFFSET(BIOS_DATA_AREA
, KeybdBuffer
);
273 Bda
->KeybdBufferEnd
= Bda
->KeybdBufferStart
+ BIOS_KBD_BUFFER_SIZE
* sizeof(WORD
);
274 Bda
->KeybdBufferHead
= Bda
->KeybdBufferTail
= 0;
276 /* Register the BIOS 32-bit Interrupts */
278 /* Initialize software vector handlers */
279 RegisterBiosInt32(BIOS_KBD_INTERRUPT
, BiosKeyboardService
);
281 /* Set up the HW vector interrupts */
282 EnableHwIRQ(1, BiosKeyboardIrq
);
283 // EnableHwIRQ(12, BiosMouseIrq);
288 VOID
KbdBios32Cleanup(VOID
)
290 /* Cleanup the common Keyboard BIOS Support Library */