6a74fe9ebca59ac7808f55b23857182f388ca1fb
[reactos.git] / subsystems / ntvdm / bios.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: bios.c
5 * PURPOSE: VDM BIOS
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "bios.h"
14 #include "emulator.h"
15 #include "vga.h"
16 #include "pic.h"
17 #include "ps2.h"
18 #include "timer.h"
19
20 #include "registers.h"
21
22 /* PRIVATE VARIABLES **********************************************************/
23
24 PBIOS_DATA_AREA Bda;
25 static BYTE BiosKeyboardMap[256];
26 static HANDLE BiosConsoleInput = INVALID_HANDLE_VALUE;
27 static HANDLE BiosConsoleOutput = INVALID_HANDLE_VALUE;
28 static CONSOLE_SCREEN_BUFFER_INFO BiosSavedBufferInfo;
29 static HANDLE InputThread = NULL;
30
31 /*
32 * VGA Register Configurations for BIOS Video Modes
33 * The configurations come from DosBox.
34 */
35 static BYTE VideoMode_40x25_text[] =
36 {
37 /* Miscellaneous Register */
38 0x67,
39
40 /* Sequencer Registers */
41 0x00, 0x08, 0x03, 0x00, 0x07,
42
43 /* GC Registers */
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x0F, 0xFF,
45
46 /* CRTC Registers */
47 0x2D, 0x27, 0x28, 0x90, 0x2B, 0xA0, 0xBF, 0x1F, 0x00, 0x4F, 0x0D, 0x0E,
48 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x14, 0x1F, 0x96, 0xB9, 0xA3,
49 0xFF,
50
51 /* AC Registers */
52 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
53 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x00, 0x0F, 0x08, 0x00
54 };
55
56 static BYTE VideoMode_80x25_text[] =
57 {
58 /* Miscellaneous Register */
59 0x67,
60
61 /* Sequencer Registers */
62 0x00, 0x00, 0x03, 0x00, 0x07,
63
64 /* GC Registers */
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x0F, 0xFF,
66
67 /* CRTC Registers */
68 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F, 0x00, 0x4F, 0x0D, 0x0E,
69 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3,
70 0xFF,
71
72 /* AC Registers */
73 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
74 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x00, 0x0F, 0x08, 0x00
75 };
76
77 static BYTE VideoMode_320x200_4color[] =
78 {
79 /* Miscellaneous Register */
80 0x63,
81
82 /* Sequencer Registers */
83 0x00, 0x09, 0x00, 0x00, 0x02,
84
85 /* GC Registers */
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0F, 0x0F, 0xFF,
87
88 /* CRTC Registers */
89 0x2D, 0x27, 0x28, 0x90, 0x2B, 0x80, 0xBF, 0x1F, 0x00, 0xC1, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x14, 0x00, 0x96, 0xB9, 0xA2,
91 0xFF,
92
93 /* AC Registers */
94 0x00, 0x13, 0x15, 0x17, 0x02, 0x04, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
95 0x14, 0x15, 0x16, 0x17, 0x01, 0x00, 0x0F, 0x00, 0x00
96 };
97
98 static BYTE VideoMode_640x200_2color[] =
99 {
100 /* Miscellaneous Register */
101 0x63,
102
103 /* Sequencer Registers */
104 0x00, 0x09, 0x0F, 0x00, 0x02,
105
106 /* GC Registers */
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0xFF,
108
109 /* CRTC Registers */
110 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F, 0x00, 0xC1, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x28, 0x00, 0x96, 0xB9, 0xC2,
112 0xFF,
113
114 /* AC Registers */
115 0x00, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
116 0x17, 0x17, 0x17, 0x17, 0x01, 0x00, 0x01, 0x00, 0x00
117 };
118
119 static BYTE VideoMode_320x200_16color[] =
120 {
121 /* Miscellaneous Register */
122 0x63,
123
124 /* Sequencer Registers */
125 0x00, 0x09, 0x0F, 0x00, 0x02,
126
127 /* GC Registers */
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0F, 0xFF,
129
130 /* CRTC Registers */
131 0x2D, 0x27, 0x28, 0x90, 0x2B, 0x80, 0xBF, 0x1F, 0x00, 0xC0, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x14, 0x00, 0x96, 0xB9, 0xE3,
133 0xFF,
134
135 /* AC Registers */
136 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
137 0x14, 0x15, 0x16, 0x17, 0x01, 0x00, 0x0F, 0x00, 0x00
138 };
139
140 static BYTE VideoMode_640x200_16color[] =
141 {
142 /* Miscellaneous Register */
143 0x63,
144
145 /* Sequencer Registers */
146 0x00, 0x01, 0x0F, 0x00, 0x02,
147
148 /* GC Registers */
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0F, 0xFF,
150
151 /* CRTC Registers */
152 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F, 0x00, 0xC0, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x28, 0x00, 0x96, 0xB9, 0xE3,
154 0xFF,
155
156 /* AC Registers */
157 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
158 0x14, 0x15, 0x16, 0x17, 0x01, 0x00, 0x0F, 0x00, 0x00
159 };
160
161 static BYTE VideoMode_640x350_16color[] =
162 {
163 /* Miscellaneous Register */
164 0xA3,
165
166 /* Sequencer Registers */
167 0x00, 0x01, 0x0F, 0x00, 0x02,
168
169 /* GC Registers */
170 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0F, 0xFF,
171
172 /* CRTC Registers */
173 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F, 0x00, 0x40, 0x00, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x83, 0x85, 0x5D, 0x28, 0x0F, 0x63, 0xBA, 0xE3,
175 0xFF,
176
177 /* AC Registers */
178 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
179 0x3C, 0x3D, 0x3E, 0x3F, 0x01, 0x00, 0x0F, 0x00, 0x00
180 };
181
182 static BYTE VideoMode_640x480_2color[] =
183 {
184 /* Miscellaneous Register */
185 0xE3,
186
187 /* Sequencer Registers */
188 0x00, 0x01, 0x0F, 0x00, 0x02,
189
190 /* GC Registers */
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0F, 0xFF,
192
193 /* CRTC Registers */
194 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0x0B, 0x3E, 0x00, 0x40, 0x00, 0x00,
195 0x00, 0x00, 0x00, 0x00, 0xEA, 0x8C, 0xDF, 0x28, 0x00, 0xE7, 0x04, 0xC3,
196 0xFF,
197
198 /* AC Registers */
199 0x00, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
200 0x3F, 0x3F, 0x3F, 0x3F, 0x01, 0x00, 0x0F, 0x00, 0x00
201 };
202
203 static BYTE VideoMode_640x480_16color[] =
204 {
205 /* Miscellaneous Register */
206 0xE3,
207
208 /* Sequencer Registers */
209 0x00, 0x01, 0x0F, 0x00, 0x02,
210
211 /* GC Registers */
212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0F, 0xFF,
213
214 /* CRTC Registers */
215 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0x0B, 0x3E, 0x00, 0x40, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0xEA, 0x8C, 0xDF, 0x28, 0x00, 0xE7, 0x04, 0xE3,
217 0xFF,
218
219 /* AC Registers */
220 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
221 0x3C, 0x3D, 0x3E, 0x3F, 0x01, 0x00, 0x0F, 0x00, 0x00
222 };
223
224 static BYTE VideoMode_320x200_256color[] =
225 {
226 /* Miscellaneous Register */
227 0x63,
228
229 /* Sequencer Registers */
230 0x00, 0x01, 0x0F, 0x00, 0x0E,
231
232 /* GC Registers */
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, 0xFF,
234
235 /* CRTC Registers */
236 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F, 0x00, 0x41, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3,
238 0xFF,
239
240 /* AC Registers */
241 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
242 0x0C, 0x0D, 0x0E, 0x0F, 0x41, 0x00, 0x0F, 0x00, 0x00
243 };
244
245 static LPBYTE VideoModes[] =
246 {
247 VideoMode_40x25_text, /* Mode 00h */
248 VideoMode_40x25_text, /* Mode 01h */
249 VideoMode_80x25_text, /* Mode 02h */
250 VideoMode_80x25_text, /* Mode 03h */
251 VideoMode_320x200_4color, /* Mode 04h */
252 VideoMode_320x200_4color, /* Mode 05h */
253 VideoMode_640x200_2color, /* Mode 06h */
254 NULL, /* Mode 07h */
255 NULL, /* Mode 08h */
256 NULL, /* Mode 09h */
257 NULL, /* Mode 0Ah */
258 NULL, /* Mode 0Bh */
259 NULL, /* Mode 0Ch */
260 VideoMode_320x200_16color, /* Mode 0Dh */
261 VideoMode_640x200_16color, /* Mode 0Eh */
262 NULL, /* Mode 0Fh */
263 VideoMode_640x350_16color, /* Mode 10h */
264 VideoMode_640x480_2color, /* Mode 11h */
265 VideoMode_640x480_16color, /* Mode 12h */
266 VideoMode_320x200_256color, /* Mode 13h */
267 };
268
269 /* PRIVATE FUNCTIONS **********************************************************/
270
271 static BOOLEAN BiosKbdBufferPush(WORD Data)
272 {
273 /* Get the location of the element after the tail */
274 WORD NextElement = Bda->KeybdBufferTail + sizeof(WORD);
275
276 /* Wrap it around if it's at or beyond the end */
277 if (NextElement >= Bda->KeybdBufferEnd) NextElement = Bda->KeybdBufferStart;
278
279 /* If it's full, fail */
280 if (NextElement == Bda->KeybdBufferHead) return FALSE;
281
282 /* Put the value in the queue */
283 *((LPWORD)((ULONG_PTR)Bda + Bda->KeybdBufferTail)) = Data;
284 Bda->KeybdBufferTail += sizeof(WORD);
285
286 /* Check if we are at, or have passed, the end of the buffer */
287 if (Bda->KeybdBufferTail >= Bda->KeybdBufferEnd)
288 {
289 /* Return it to the beginning */
290 Bda->KeybdBufferTail = Bda->KeybdBufferStart;
291 }
292
293 /* Return success */
294 return TRUE;
295 }
296
297 static BOOLEAN BiosKbdBufferTop(LPWORD Data)
298 {
299 /* If it's empty, fail */
300 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return FALSE;
301
302 /* Otherwise, get the value and return success */
303 *Data = *((LPWORD)((ULONG_PTR)Bda + Bda->KeybdBufferHead));
304
305 return TRUE;
306 }
307
308 static BOOLEAN BiosKbdBufferPop(VOID)
309 {
310 /* If it's empty, fail */
311 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return FALSE;
312
313 /* Remove the value from the queue */
314 Bda->KeybdBufferHead += sizeof(WORD);
315
316 /* Check if we are at, or have passed, the end of the buffer */
317 if (Bda->KeybdBufferHead >= Bda->KeybdBufferEnd)
318 {
319 /* Return it to the beginning */
320 Bda->KeybdBufferHead = Bda->KeybdBufferStart;
321 }
322
323 /* Return success */
324 return TRUE;
325 }
326
327 static VOID BiosReadWindow(LPWORD Buffer, SMALL_RECT Rectangle, BYTE Page)
328 {
329 INT i, j;
330 INT Counter = 0;
331 WORD Character;
332 DWORD VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
333
334 for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
335 {
336 for (j = Rectangle.Left; j <= Rectangle.Right; j++)
337 {
338 /* Read from video memory */
339 VgaReadMemory(VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
340 (LPVOID)&Character,
341 sizeof(WORD));
342
343 /* Write the data to the buffer in row order */
344 Buffer[Counter++] = Character;
345 }
346 }
347 }
348
349 static VOID BiosWriteWindow(LPWORD Buffer, SMALL_RECT Rectangle, BYTE Page)
350 {
351 INT i, j;
352 INT Counter = 0;
353 WORD Character;
354 DWORD VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
355
356 for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
357 {
358 for (j = Rectangle.Left; j <= Rectangle.Right; j++)
359 {
360 Character = Buffer[Counter++];
361
362 /* Write to video memory */
363 VgaWriteMemory(VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
364 (LPVOID)&Character,
365 sizeof(WORD));
366 }
367 }
368 }
369
370 /* PUBLIC FUNCTIONS ***********************************************************/
371
372 BYTE BiosGetVideoMode(VOID)
373 {
374 return Bda->VideoMode;
375 }
376
377 BOOLEAN BiosSetVideoMode(BYTE ModeNumber)
378 {
379 INT i;
380 COORD Resolution;
381 LPBYTE Values = VideoModes[ModeNumber];
382
383 DPRINT1("Switching to mode %Xh; Values = 0x%p\n", ModeNumber, Values);
384
385 if (Values == NULL) return FALSE;
386
387 /* Write the misc register */
388 VgaWritePort(VGA_MISC_WRITE, *(Values++));
389
390 /* Write the sequencer registers */
391 for (i = 0; i < VGA_SEQ_MAX_REG; i++)
392 {
393 VgaWritePort(VGA_SEQ_INDEX, i);
394 VgaWritePort(VGA_SEQ_DATA, *(Values++));
395 }
396
397 /* Write the GC registers */
398 for (i = 0; i < VGA_GC_MAX_REG; i++)
399 {
400 VgaWritePort(VGA_GC_INDEX, i);
401 VgaWritePort(VGA_GC_DATA, *(Values++));
402 }
403
404 /* Write the CRTC registers */
405 for (i = 0; i < VGA_CRTC_MAX_REG; i++)
406 {
407 VgaWritePort(VGA_CRTC_INDEX, i);
408 VgaWritePort(VGA_CRTC_DATA, *(Values++));
409 }
410
411 /* Write the AC registers */
412 for (i = 0; i < VGA_AC_MAX_REG; i++)
413 {
414 VgaWritePort(VGA_AC_INDEX, i);
415 VgaWritePort(VGA_AC_WRITE, *(Values++));
416 }
417
418 /* Reset the palette */
419 VgaResetPalette();
420
421 /* Update the values in the BDA */
422 Bda->VideoMode = ModeNumber;
423 Bda->VideoPage = 0;
424 Bda->VideoPageSize = BIOS_PAGE_SIZE;
425 Bda->VideoPageOffset = 0;
426
427 /* Get the character height */
428 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_MAX_SCAN_LINE_REG);
429 Bda->CharacterHeight = 1 + (VgaReadPort(VGA_CRTC_DATA) & 0x1F);
430
431 Resolution = VgaGetDisplayResolution();
432 Bda->ScreenColumns = Resolution.X;
433 Bda->ScreenRows = Resolution.Y - 1;
434
435 return TRUE;
436 }
437
438 BOOLEAN BiosSetVideoPage(BYTE PageNumber)
439 {
440 /* Check if the page exists */
441 if (PageNumber >= BIOS_MAX_PAGES) return FALSE;
442
443 /* Check if this is the same page */
444 if (PageNumber == Bda->VideoPage) return TRUE;
445
446 /* Set the values in the BDA */
447 Bda->VideoPage = PageNumber;
448 Bda->VideoPageSize = BIOS_PAGE_SIZE;
449 Bda->VideoPageOffset = PageNumber * BIOS_PAGE_SIZE;
450
451 /* Set the start address in the CRTC */
452 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
453 VgaWritePort(VGA_CRTC_DATA , LOBYTE(Bda->VideoPageOffset));
454 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
455 VgaWritePort(VGA_CRTC_DATA , HIBYTE(Bda->VideoPageOffset));
456
457 return TRUE;
458 }
459
460 BOOLEAN BiosInitialize(VOID)
461 {
462 USHORT i;
463 WORD Offset = 0;
464 LPWORD IntVecTable = (LPWORD)BaseAddress;
465 LPBYTE BiosCode = (LPBYTE)SEG_OFF_TO_PTR(BIOS_SEGMENT, 0);
466
467 /* Initialize the BDA */
468 Bda = (PBIOS_DATA_AREA)SEG_OFF_TO_PTR(BDA_SEGMENT, 0);
469 Bda->EquipmentList = BIOS_EQUIPMENT_LIST;
470 Bda->KeybdBufferStart = FIELD_OFFSET(BIOS_DATA_AREA, KeybdBuffer);
471 Bda->KeybdBufferEnd = Bda->KeybdBufferStart + BIOS_KBD_BUFFER_SIZE * sizeof(WORD);
472
473 /* Generate ISR stubs and fill the IVT */
474 for (i = 0x00; i <= 0xFF; i++)
475 {
476 IntVecTable[i * 2] = Offset;
477 IntVecTable[i * 2 + 1] = BIOS_SEGMENT;
478
479 BiosCode[Offset++] = 0xFB; // sti
480
481 BiosCode[Offset++] = 0x6A; // push i
482 BiosCode[Offset++] = (UCHAR)i;
483
484 BiosCode[Offset++] = 0x6A; // push 0
485 BiosCode[Offset++] = 0x00;
486
487 // BOP_SEQ:
488 BiosCode[Offset++] = 0xF8; // clc
489
490 BiosCode[Offset++] = LOBYTE(EMULATOR_BOP); // BOP sequence
491 BiosCode[Offset++] = HIBYTE(EMULATOR_BOP);
492 BiosCode[Offset++] = EMULATOR_INT_BOP;
493
494 BiosCode[Offset++] = 0x73; // jnc EXIT (offset +3)
495 BiosCode[Offset++] = 0x03;
496
497 // HACK: The following instruction should be HLT!
498 BiosCode[Offset++] = 0x90; // nop
499
500 BiosCode[Offset++] = 0xEB; // jmp BOP_SEQ (offset -9)
501 BiosCode[Offset++] = 0xF7;
502
503 // EXIT:
504 BiosCode[Offset++] = 0x83; // add sp, 4
505 BiosCode[Offset++] = 0xC4;
506 BiosCode[Offset++] = 0x04;
507
508 BiosCode[Offset++] = 0xCF; // iret
509 }
510
511 /* Get the input handle to the real console, and check for success */
512 BiosConsoleInput = CreateFileW(L"CONIN$",
513 GENERIC_READ | GENERIC_WRITE,
514 FILE_SHARE_READ | FILE_SHARE_WRITE,
515 NULL,
516 OPEN_EXISTING,
517 0,
518 NULL);
519 if (BiosConsoleInput == INVALID_HANDLE_VALUE)
520 {
521 return FALSE;
522 }
523
524 /* Get the output handle to the real console, and check for success */
525 BiosConsoleOutput = CreateFileW(L"CONOUT$",
526 GENERIC_READ | GENERIC_WRITE,
527 FILE_SHARE_READ | FILE_SHARE_WRITE,
528 NULL,
529 OPEN_EXISTING,
530 0,
531 NULL);
532 if (BiosConsoleOutput == INVALID_HANDLE_VALUE)
533 {
534 CloseHandle(BiosConsoleInput);
535 return FALSE;
536 }
537
538 /* Save the console screen buffer information */
539 if (!GetConsoleScreenBufferInfo(BiosConsoleOutput, &BiosSavedBufferInfo))
540 {
541 CloseHandle(BiosConsoleOutput);
542 CloseHandle(BiosConsoleInput);
543 return FALSE;
544 }
545
546 /* Initialize VGA */
547 if (!VgaInitialize(BiosConsoleOutput))
548 {
549 CloseHandle(BiosConsoleOutput);
550 CloseHandle(BiosConsoleInput);
551 return FALSE;
552 }
553
554 /* Update the cursor position */
555 BiosSetCursorPosition(BiosSavedBufferInfo.dwCursorPosition.Y,
556 BiosSavedBufferInfo.dwCursorPosition.X,
557 0);
558
559 /* Set the console input mode */
560 SetConsoleMode(BiosConsoleInput, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
561
562 /* Start the input thread */
563 InputThread = CreateThread(NULL, 0, &InputThreadProc, BiosConsoleInput, 0, NULL);
564
565 /* Initialize the PIC */
566 PicWriteCommand(PIC_MASTER_CMD, PIC_ICW1 | PIC_ICW1_ICW4);
567 PicWriteCommand(PIC_SLAVE_CMD , PIC_ICW1 | PIC_ICW1_ICW4);
568
569 /* Set the interrupt offsets */
570 PicWriteData(PIC_MASTER_DATA, BIOS_PIC_MASTER_INT);
571 PicWriteData(PIC_SLAVE_DATA , BIOS_PIC_SLAVE_INT);
572
573 /* Tell the master PIC there is a slave at IRQ 2 */
574 PicWriteData(PIC_MASTER_DATA, 1 << 2);
575 PicWriteData(PIC_SLAVE_DATA , 2);
576
577 /* Make sure the PIC is in 8086 mode */
578 PicWriteData(PIC_MASTER_DATA, PIC_ICW4_8086);
579 PicWriteData(PIC_SLAVE_DATA , PIC_ICW4_8086);
580
581 /* Clear the masks for both PICs */
582 PicWriteData(PIC_MASTER_DATA, 0x00);
583 PicWriteData(PIC_SLAVE_DATA , 0x00);
584
585 PitWriteCommand(0x34);
586 PitWriteData(0, 0x00);
587 PitWriteData(0, 0x00);
588
589 return TRUE;
590 }
591
592 VOID BiosCleanup(VOID)
593 {
594 /* Restore the old screen buffer */
595 SetConsoleActiveScreenBuffer(BiosConsoleOutput);
596
597 /* Restore the screen buffer size */
598 SetConsoleScreenBufferSize(BiosConsoleOutput, BiosSavedBufferInfo.dwSize);
599
600 /* Close the console handles */
601 if (BiosConsoleOutput != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleOutput);
602 if (BiosConsoleInput != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleInput);
603
604 /* Close the input thread handle */
605 if (InputThread != NULL) CloseHandle(InputThread);
606 }
607
608 WORD BiosPeekCharacter(VOID)
609 {
610 WORD CharacterData = 0;
611
612 /* Get the key from the queue, but don't remove it */
613 if (BiosKbdBufferTop(&CharacterData)) return CharacterData;
614 else return 0xFFFF;
615 }
616
617 WORD BiosGetCharacter(VOID)
618 {
619 WORD CharacterData = 0;
620
621 /* Check if there is a key available */
622 if (Bda->KeybdBufferHead != Bda->KeybdBufferTail)
623 {
624 /* Get the key from the queue, and remove it */
625 BiosKbdBufferTop(&CharacterData);
626 BiosKbdBufferPop();
627 }
628 else
629 {
630 /* Set the handler CF to repeat the BOP */
631 EmulatorSetFlag(EMULATOR_FLAG_CF);
632 }
633
634 return CharacterData;
635 }
636
637 VOID BiosSetCursorPosition(BYTE Row, BYTE Column, BYTE Page)
638 {
639 /* Make sure the selected video page is valid */
640 if (Page >= BIOS_MAX_PAGES) return;
641
642 /* Update the position in the BDA */
643 Bda->CursorPosition[Page] = (Row << 8) | Column;
644
645 /* Check if this is the current video page */
646 if (Page == Bda->VideoPage)
647 {
648 WORD Offset = Row * Bda->ScreenColumns + Column;
649
650 /* Modify the CRTC registers */
651 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
652 VgaWritePort(VGA_CRTC_DATA , LOBYTE(Offset));
653 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
654 VgaWritePort(VGA_CRTC_DATA , HIBYTE(Offset));
655 }
656 }
657
658 BOOLEAN BiosScrollWindow(INT Direction,
659 DWORD Amount,
660 SMALL_RECT Rectangle,
661 BYTE Page,
662 BYTE FillAttribute)
663 {
664 DWORD i;
665 LPWORD WindowData;
666 DWORD WindowSize = (Rectangle.Bottom - Rectangle.Top + 1)
667 * (Rectangle.Right - Rectangle.Left + 1);
668
669 /* Allocate a buffer for the window */
670 WindowData = (LPWORD)HeapAlloc(GetProcessHeap(),
671 HEAP_ZERO_MEMORY,
672 WindowSize * sizeof(WORD));
673 if (WindowData == NULL) return FALSE;
674
675 /* Read the window data */
676 BiosReadWindow(WindowData, Rectangle, Page);
677
678 if (Amount == 0)
679 {
680 /* Fill the window */
681 for (i = 0; i < WindowSize; i++)
682 {
683 WindowData[i] = ' ' | (FillAttribute << 8);
684 }
685
686 goto Done;
687 }
688
689 // TODO: Scroll the window!
690
691 Done:
692 /* Write back the window data */
693 BiosWriteWindow(WindowData, Rectangle, Page);
694
695 /* Free the window buffer */
696 HeapFree(GetProcessHeap(), 0, WindowData);
697
698 return TRUE;
699 }
700
701 VOID BiosPrintCharacter(CHAR Character, BYTE Attribute, BYTE Page)
702 {
703 WORD CharData = (Attribute << 8) | Character;
704 BYTE Row, Column;
705
706 /* Make sure the page exists */
707 if (Page >= BIOS_MAX_PAGES) return;
708
709 /* Get the cursor location */
710 Row = HIBYTE(Bda->CursorPosition[Page]);
711 Column = LOBYTE(Bda->CursorPosition[Page]);
712
713 if (Character == '\a')
714 {
715 /* Bell control character */
716 // NOTE: We may use what the terminal emulator offers to us...
717 Beep(800, 200);
718 return;
719 }
720 else if (Character == '\b')
721 {
722 /* Backspace control character */
723 if (Column > 0)
724 {
725 Column--;
726 }
727 else if (Row > 0)
728 {
729 Column = Bda->ScreenColumns - 1;
730 Row--;
731 }
732
733 /* Erase the existing character */
734 CharData = (Attribute << 8) | ' ';
735 VgaWriteMemory(TO_LINEAR(TEXT_VIDEO_SEG,
736 Page * Bda->VideoPageSize
737 + (Row * Bda->ScreenColumns + Column) * sizeof(WORD)),
738 (LPVOID)&CharData,
739 sizeof(WORD));
740 }
741 else if (Character == '\n')
742 {
743 /* Line Feed control character */
744 Row++;
745 }
746 else if (Character == '\r')
747 {
748 /* Carriage Return control character */
749 Column = 0;
750 }
751 else
752 {
753 /* Default character */
754
755 /* Write the character */
756 VgaWriteMemory(TO_LINEAR(TEXT_VIDEO_SEG,
757 Page * Bda->VideoPageSize
758 + (Row * Bda->ScreenColumns + Column) * sizeof(WORD)),
759 (LPVOID)&CharData,
760 sizeof(WORD));
761
762 /* Advance the cursor */
763 Column++;
764 }
765
766 /* Check if it passed the end of the row */
767 if (Column >= Bda->ScreenColumns)
768 {
769 /* Return to the first column and go to the next line */
770 Column = 0;
771 Row++;
772 }
773
774 /* Scroll the screen up if needed */
775 if (Row > Bda->ScreenRows)
776 {
777 /* The screen must be scrolled up */
778 SMALL_RECT Rectangle = { 0, 0, Bda->ScreenColumns - 1, Bda->ScreenRows };
779
780 BiosScrollWindow(SCROLL_DIRECTION_UP,
781 1,
782 Rectangle,
783 Page,
784 DEFAULT_ATTRIBUTE);
785 }
786
787 /* Set the cursor position */
788 BiosSetCursorPosition(Row, Column, Page);
789 }
790
791 VOID BiosVideoService(LPWORD Stack)
792 {
793 switch (getAH())
794 {
795 /* Set Video Mode */
796 case 0x00:
797 {
798 BiosSetVideoMode(getAL());
799 VgaClearMemory();
800 break;
801 }
802
803 /* Set Text-Mode Cursor Shape */
804 case 0x01:
805 {
806 /* Update the BDA */
807 Bda->CursorStartLine = getCH();
808 Bda->CursorEndLine = getCL();
809
810 /* Modify the CRTC registers */
811 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_START_REG);
812 VgaWritePort(VGA_CRTC_DATA , Bda->CursorStartLine);
813 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_END_REG);
814 VgaWritePort(VGA_CRTC_DATA , Bda->CursorEndLine);
815
816 break;
817 }
818
819 /* Set Cursor Position */
820 case 0x02:
821 {
822 BiosSetCursorPosition(getDH(), getDL(), getBH());
823 break;
824 }
825
826 /* Get Cursor Position */
827 case 0x03:
828 {
829 /* Make sure the selected video page exists */
830 if (getBH() >= BIOS_MAX_PAGES) break;
831
832 /* Return the result */
833 setAX(0);
834 setCX(MAKEWORD(Bda->CursorEndLine, Bda->CursorStartLine));
835 setDX(Bda->CursorPosition[getBH()]);
836 break;
837 }
838
839 /* Query Light Pen */
840 case 0x04:
841 {
842 /*
843 * On modern BIOSes, this function returns 0
844 * so that we can ignore the other registers.
845 */
846 setAX(0);
847 break;
848 }
849
850 /* Select Active Display Page */
851 case 0x05:
852 {
853 BiosSetVideoPage(getAL());
854 break;
855 }
856
857 /* Scroll Window Up/Down */
858 case 0x06:
859 case 0x07:
860 {
861 SMALL_RECT Rectangle = { getCL(), getCH(), getDL(), getDH() };
862
863 /* Call the internal function */
864 BiosScrollWindow((getAH() == 0x06) ? SCROLL_DIRECTION_UP
865 : SCROLL_DIRECTION_DOWN,
866 getAL(),
867 Rectangle,
868 Bda->VideoPage,
869 getBH());
870
871 break;
872 }
873
874 /* Read/Write Character From Cursor Position */
875 case 0x08:
876 case 0x09:
877 case 0x0A:
878 {
879 WORD CharacterData = MAKEWORD(getAL(), getBL());
880 BYTE Page = getBH();
881 DWORD Offset;
882
883 /* Check if the page exists */
884 if (Page >= BIOS_MAX_PAGES) break;
885
886 /* Find the offset of the character */
887 Offset = Page * Bda->VideoPageSize +
888 (HIBYTE(Bda->CursorPosition[Page]) * Bda->ScreenColumns +
889 LOBYTE(Bda->CursorPosition[Page])) * 2;
890
891 if (getAH() == 0x08)
892 {
893 /* Read from the video memory */
894 VgaReadMemory(TO_LINEAR(TEXT_VIDEO_SEG, Offset),
895 (LPVOID)&CharacterData,
896 sizeof(WORD));
897
898 /* Return the character in AX */
899 setAX(CharacterData);
900 }
901 else
902 {
903 /* Write to video memory */
904 VgaWriteMemory(TO_LINEAR(TEXT_VIDEO_SEG, Offset),
905 (LPVOID)&CharacterData,
906 (getBH() == 0x09) ? sizeof(WORD) : sizeof(BYTE));
907 }
908
909 break;
910 }
911
912 /* Teletype Output */
913 case 0x0E:
914 {
915 BiosPrintCharacter(getAL(), getBL(), getBH());
916 break;
917 }
918
919 /* Get Current Video Mode */
920 case 0x0F:
921 {
922 setAX(MAKEWORD(Bda->VideoMode, Bda->ScreenColumns));
923 setBX(MAKEWORD(getBL(), Bda->VideoPage));
924 break;
925 }
926
927 /* Palette Control */
928 case 0x10:
929 {
930 switch (getAL())
931 {
932 /* Set Single Palette Register */
933 case 0x00:
934 {
935 /* Reset the flip-flop */
936 VgaReadPort(VGA_STAT_COLOR);
937
938 /* Write the index */
939 VgaWritePort(VGA_AC_INDEX, getBL());
940
941 /* Write the data */
942 VgaWritePort(VGA_AC_WRITE, getBH());
943
944 break;
945 }
946
947 /* Set Overscan Color */
948 case 0x01:
949 {
950 /* Reset the flip-flop */
951 VgaReadPort(VGA_STAT_COLOR);
952
953 /* Write the index */
954 VgaWritePort(VGA_AC_INDEX, VGA_AC_OVERSCAN_REG);
955
956 /* Write the data */
957 VgaWritePort(VGA_AC_WRITE, getBH());
958
959 break;
960 }
961
962 /* Set All Palette Registers */
963 case 0x02:
964 {
965 INT i;
966 LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
967
968 /* Set the palette registers */
969 for (i = 0; i <= VGA_AC_PAL_F_REG; i++)
970 {
971 /* Reset the flip-flop */
972 VgaReadPort(VGA_STAT_COLOR);
973
974 /* Write the index */
975 VgaWritePort(VGA_AC_INDEX, i);
976
977 /* Write the data */
978 VgaWritePort(VGA_AC_WRITE, Buffer[i]);
979 }
980
981 /* Set the overscan register */
982 VgaWritePort(VGA_AC_INDEX, VGA_AC_OVERSCAN_REG);
983 VgaWritePort(VGA_AC_WRITE, Buffer[VGA_AC_PAL_F_REG + 1]);
984
985 break;
986 }
987
988 /* Get Single Palette Register */
989 case 0x07:
990 {
991 /* Reset the flip-flop */
992 VgaReadPort(VGA_STAT_COLOR);
993
994 /* Write the index */
995 VgaWritePort(VGA_AC_INDEX, getBL());
996
997 /* Read the data */
998 setBH(VgaReadPort(VGA_AC_READ));
999
1000 break;
1001 }
1002
1003 /* Get Overscan Color */
1004 case 0x08:
1005 {
1006 /* Reset the flip-flop */
1007 VgaReadPort(VGA_STAT_COLOR);
1008
1009 /* Write the index */
1010 VgaWritePort(VGA_AC_INDEX, VGA_AC_OVERSCAN_REG);
1011
1012 /* Read the data */
1013 setBH(VgaReadPort(VGA_AC_READ));
1014
1015 break;
1016 }
1017
1018 /* Get All Palette Registers */
1019 case 0x09:
1020 {
1021 INT i;
1022 LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
1023
1024 /* Get the palette registers */
1025 for (i = 0; i <= VGA_AC_PAL_F_REG; i++)
1026 {
1027 /* Reset the flip-flop */
1028 VgaReadPort(VGA_STAT_COLOR);
1029
1030 /* Write the index */
1031 VgaWritePort(VGA_AC_INDEX, i);
1032
1033 /* Read the data */
1034 Buffer[i] = VgaReadPort(VGA_AC_READ);
1035 }
1036
1037 /* Get the overscan register */
1038 VgaWritePort(VGA_AC_INDEX, VGA_AC_OVERSCAN_REG);
1039 Buffer[VGA_AC_PAL_F_REG + 1] = VgaReadPort(VGA_AC_READ);
1040
1041 break;
1042 }
1043
1044 /* Set Individual DAC Register */
1045 case 0x10:
1046 {
1047 /* Write the index */
1048 // Certainly in BL and not in BX as said by Ralf Brown...
1049 VgaWritePort(VGA_DAC_WRITE_INDEX, getBL());
1050
1051 /* Write the data in this order: Red, Green, Blue */
1052 VgaWritePort(VGA_DAC_DATA, getDH());
1053 VgaWritePort(VGA_DAC_DATA, getCH());
1054 VgaWritePort(VGA_DAC_DATA, getCL());
1055
1056 break;
1057 }
1058
1059 /* Set Block of DAC Registers */
1060 case 0x12:
1061 {
1062 INT i;
1063 LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
1064
1065 /* Write the index */
1066 // Certainly in BL and not in BX as said by Ralf Brown...
1067 VgaWritePort(VGA_DAC_WRITE_INDEX, getBL());
1068
1069 for (i = 0; i < getCX(); i++)
1070 {
1071 /* Write the data in this order: Red, Green, Blue */
1072 VgaWritePort(VGA_DAC_DATA, *Buffer++);
1073 VgaWritePort(VGA_DAC_DATA, *Buffer++);
1074 VgaWritePort(VGA_DAC_DATA, *Buffer++);
1075 }
1076
1077 break;
1078 }
1079
1080 /* Get Individual DAC Register */
1081 case 0x15:
1082 {
1083 /* Write the index */
1084 VgaWritePort(VGA_DAC_READ_INDEX, getBL());
1085
1086 /* Read the data in this order: Red, Green, Blue */
1087 setDH(VgaReadPort(VGA_DAC_DATA));
1088 setCH(VgaReadPort(VGA_DAC_DATA));
1089 setCL(VgaReadPort(VGA_DAC_DATA));
1090
1091 break;
1092 }
1093
1094 /* Get Block of DAC Registers */
1095 case 0x17:
1096 {
1097 INT i;
1098 LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
1099
1100 /* Write the index */
1101 // Certainly in BL and not in BX as said by Ralf Brown...
1102 VgaWritePort(VGA_DAC_READ_INDEX, getBL());
1103
1104 for (i = 0; i < getCX(); i++)
1105 {
1106 /* Write the data in this order: Red, Green, Blue */
1107 *Buffer++ = VgaReadPort(VGA_DAC_DATA);
1108 *Buffer++ = VgaReadPort(VGA_DAC_DATA);
1109 *Buffer++ = VgaReadPort(VGA_DAC_DATA);
1110 }
1111
1112 break;
1113 }
1114
1115 default:
1116 {
1117 DPRINT1("BIOS Palette Control Sub-command AL = 0x%02X NOT IMPLEMENTED\n",
1118 getAL());
1119 break;
1120 }
1121 }
1122
1123 break;
1124 }
1125
1126 /* Scroll Window */
1127 case 0x12:
1128 {
1129 SMALL_RECT Rectangle = { getCL(), getCH(), getDL(), getDH() };
1130
1131 /* Call the internal function */
1132 BiosScrollWindow(getBL(),
1133 getAL(),
1134 Rectangle,
1135 Bda->VideoPage,
1136 DEFAULT_ATTRIBUTE);
1137
1138 break;
1139 }
1140
1141 /* Display combination code */
1142 case 0x1A:
1143 {
1144 switch(getAL())
1145 {
1146 case 0x00: /* Get Display combiantion code */
1147 setAX(MAKEWORD(0x1A, 0x1A));
1148 setBX(MAKEWORD(0x08, 0x00)); /* VGA w/ color analog display */
1149 break;
1150 case 0x01: /* Set Display combination code */
1151 DPRINT1("Set Display combination code - Unsupported\n");
1152 break;
1153 default:
1154 break;
1155 }
1156 break;
1157 }
1158
1159 default:
1160 {
1161 DPRINT1("BIOS Function INT 10h, AH = 0x%02X NOT IMPLEMENTED\n",
1162 getAH());
1163 }
1164 }
1165 }
1166
1167 VOID BiosKeyboardService(LPWORD Stack)
1168 {
1169 switch (getAH())
1170 {
1171 /* Wait for keystroke and read */
1172 case 0x00:
1173 /* Wait for extended keystroke and read */
1174 case 0x10: // FIXME: Temporarily do the same as INT 16h, 00h
1175 {
1176 /* Read the character (and wait if necessary) */
1177 setAX(BiosGetCharacter());
1178 break;
1179 }
1180
1181 /* Get keystroke status */
1182 case 0x01:
1183 /* Get extended keystroke status */
1184 case 0x11: // FIXME: Temporarily do the same as INT 16h, 01h
1185 {
1186 WORD Data = BiosPeekCharacter();
1187
1188 if (Data != 0xFFFF)
1189 {
1190 /* There is a character, clear ZF and return it */
1191 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_ZF;
1192 setAX(Data);
1193 }
1194 else
1195 {
1196 /* No character, set ZF */
1197 Stack[STACK_FLAGS] |= EMULATOR_FLAG_ZF;
1198 }
1199
1200 break;
1201 }
1202
1203 /* Get shift status */
1204 case 0x02:
1205 {
1206 /* Return the lower byte of the keyboard shift status word */
1207 setAL(LOBYTE(Bda->KeybdShiftFlags));
1208 break;
1209 }
1210
1211 /* Reserved */
1212 case 0x04:
1213 {
1214 DPRINT1("BIOS Function INT 16h, AH = 0x04 is RESERVED\n");
1215 break;
1216 }
1217
1218 /* Push keystroke */
1219 case 0x05:
1220 {
1221 /* Return 0 if success, 1 if failure */
1222 setAL(BiosKbdBufferPush(getCX()) == FALSE);
1223 break;
1224 }
1225
1226 /* Get extended shift status */
1227 case 0x12:
1228 {
1229 /*
1230 * Be careful! The returned word is similar to Bda->KeybdShiftFlags
1231 * but the high byte is organized differently:
1232 * the bytes 2 and 3 of the high byte are not the same...
1233 */
1234 WORD KeybdShiftFlags = (Bda->KeybdShiftFlags & 0xF3FF);
1235
1236 /* Return the extended keyboard shift status word */
1237 setAX(KeybdShiftFlags);
1238 break;
1239 }
1240
1241 default:
1242 {
1243 DPRINT1("BIOS Function INT 16h, AH = 0x%02X NOT IMPLEMENTED\n",
1244 getAH());
1245 }
1246 }
1247 }
1248
1249 VOID BiosTimeService(LPWORD Stack)
1250 {
1251 switch (getAH())
1252 {
1253 case 0x00:
1254 {
1255 /* Set AL to 1 if midnight had passed, 0 otherwise */
1256 setAL(Bda->MidnightPassed ? 0x01 : 0x00);
1257
1258 /* Return the tick count in CX:DX */
1259 setCX(HIWORD(Bda->TickCounter));
1260 setDX(LOWORD(Bda->TickCounter));
1261
1262 /* Reset the midnight flag */
1263 Bda->MidnightPassed = FALSE;
1264
1265 break;
1266 }
1267
1268 case 0x01:
1269 {
1270 /* Set the tick count to CX:DX */
1271 Bda->TickCounter = MAKELONG(getDX(), getCX());
1272
1273 /* Reset the midnight flag */
1274 Bda->MidnightPassed = FALSE;
1275
1276 break;
1277 }
1278
1279 default:
1280 {
1281 DPRINT1("BIOS Function INT 1Ah, AH = 0x%02X NOT IMPLEMENTED\n",
1282 getAH());
1283 }
1284 }
1285 }
1286
1287 VOID BiosSystemTimerInterrupt(LPWORD Stack)
1288 {
1289 /* Increase the system tick count */
1290 Bda->TickCounter++;
1291 }
1292
1293 VOID BiosEquipmentService(LPWORD Stack)
1294 {
1295 /* Return the equipment list */
1296 setAX(Bda->EquipmentList);
1297 }
1298
1299 VOID BiosHandleIrq(BYTE IrqNumber, LPWORD Stack)
1300 {
1301 switch (IrqNumber)
1302 {
1303 /* PIT IRQ */
1304 case 0:
1305 {
1306 /* Perform the system timer interrupt */
1307 EmulatorInterrupt(BIOS_SYS_TIMER_INTERRUPT);
1308 break;
1309 }
1310
1311 /* Keyboard IRQ */
1312 case 1:
1313 {
1314 BYTE ScanCode, VirtualKey;
1315 WORD Character;
1316
1317 /* Loop while there is a scancode available */
1318 do
1319 {
1320 /* Get the scan code and virtual key code */
1321 ScanCode = KeyboardReadData();
1322 VirtualKey = MapVirtualKey(ScanCode & 0x7F, MAPVK_VSC_TO_VK);
1323
1324 /* Check if this is a key press or release */
1325 if (!(ScanCode & (1 << 7)))
1326 {
1327 /* Key press */
1328 if (VirtualKey == VK_NUMLOCK ||
1329 VirtualKey == VK_CAPITAL ||
1330 VirtualKey == VK_SCROLL ||
1331 VirtualKey == VK_INSERT)
1332 {
1333 /* For toggle keys, toggle the lowest bit in the keyboard map */
1334 BiosKeyboardMap[VirtualKey] ^= ~(1 << 0);
1335 }
1336
1337 /* Set the highest bit */
1338 BiosKeyboardMap[VirtualKey] |= (1 << 7);
1339
1340 /* Find out which character this is */
1341 Character = 0;
1342 if (ToAscii(VirtualKey, ScanCode, BiosKeyboardMap, &Character, 0) == 0)
1343 {
1344 /* Not ASCII */
1345 Character = 0;
1346 }
1347
1348 /* Push it onto the BIOS keyboard queue */
1349 BiosKbdBufferPush(MAKEWORD(Character, ScanCode));
1350 }
1351 else
1352 {
1353 /* Key release, unset the highest bit */
1354 BiosKeyboardMap[VirtualKey] &= ~(1 << 7);
1355 }
1356 }
1357 while (KeyboardReadStatus() & 1);
1358
1359 /* Clear the keyboard flags */
1360 Bda->KeybdShiftFlags = 0;
1361
1362 /* Set the appropriate flags based on the state */
1363 if (BiosKeyboardMap[VK_RSHIFT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_RSHIFT;
1364 if (BiosKeyboardMap[VK_LSHIFT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_LSHIFT;
1365 if (BiosKeyboardMap[VK_CONTROL] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_CTRL;
1366 if (BiosKeyboardMap[VK_MENU] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_ALT;
1367 if (BiosKeyboardMap[VK_SCROLL] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_SCROLL_ON;
1368 if (BiosKeyboardMap[VK_NUMLOCK] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_NUMLOCK_ON;
1369 if (BiosKeyboardMap[VK_CAPITAL] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_CAPSLOCK_ON;
1370 if (BiosKeyboardMap[VK_INSERT] & (1 << 0)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_INSERT_ON;
1371 if (BiosKeyboardMap[VK_RMENU] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_RALT;
1372 if (BiosKeyboardMap[VK_LMENU] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_LALT;
1373 if (BiosKeyboardMap[VK_SNAPSHOT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_SYSRQ;
1374 if (BiosKeyboardMap[VK_PAUSE] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_PAUSE;
1375 if (BiosKeyboardMap[VK_SCROLL] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_SCROLL;
1376 if (BiosKeyboardMap[VK_NUMLOCK] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_NUMLOCK;
1377 if (BiosKeyboardMap[VK_CAPITAL] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_CAPSLOCK;
1378 if (BiosKeyboardMap[VK_INSERT] & (1 << 7)) Bda->KeybdShiftFlags |= BDA_KBDFLAG_INSERT;
1379
1380 break;
1381 }
1382 }
1383
1384 /* Send End-of-Interrupt to the PIC */
1385 if (IrqNumber >= 8) PicWriteCommand(PIC_SLAVE_CMD, PIC_OCW2_EOI);
1386 PicWriteCommand(PIC_MASTER_CMD, PIC_OCW2_EOI);
1387 }
1388
1389 /* EOF */