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