[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 "bios.h"
14 #include "emulator.h"
15 #include "vga.h"
16 #include "pic.h"
17 #include "ps2.h"
18 #include "timer.h"
19
20 /* PRIVATE VARIABLES **********************************************************/
21
22 static PBIOS_DATA_AREA Bda;
23 static BYTE BiosKeyboardMap[256];
24 static HANDLE BiosConsoleInput = INVALID_HANDLE_VALUE;
25 static HANDLE BiosConsoleOutput = INVALID_HANDLE_VALUE;
26 static CONSOLE_SCREEN_BUFFER_INFO BiosSavedBufferInfo;
27
28 static BYTE VideoMode_40x25_text[] =
29 {
30 /* Miscellaneous Register */
31 0x67,
32
33 /* Sequencer Registers */
34 0x03, 0x08, 0x03, 0x00, 0x02,
35
36 /* GC Registers */
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x0F, 0xFF,
38
39 /* CRTC Registers */
40 0x2D, 0x27, 0x28, 0x90, 0x2B, 0xA0, 0xBF, 0x1F, 0x00, 0x4F, 0x0D, 0x0F,
41 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x14, 0x1F, 0x96, 0xB9, 0xA3,
42 0xFF,
43
44 /* AC Registers */
45 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
46 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x01, 0x0F, 0x13, 0x00
47 };
48
49 static BYTE VideoMode_80x25_text[] =
50 {
51 /* Miscellaneous Register */
52 0x67,
53
54 /* Sequencer Registers */
55 0x03, 0x00, 0x03, 0x00, 0x02,
56
57 /* GC Registers */
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x0F, 0xFF,
59
60 /* CRTC Registers */
61 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F, 0x00, 0x4F, 0x0D, 0x0F,
62 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3,
63 0xFF,
64
65 /* AC Registers */
66 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
67 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x01, 0x0F, 0x13, 0x00
68 };
69
70 static BYTE VideoMode_320x200_4color[] =
71 {
72 /* Miscellaneous Register */
73 0x63,
74
75 /* Sequencer Registers */
76 0x03, 0x09, 0x03, 0x00, 0x02,
77
78 /* GC Registers */
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0F, 0x0F, 0xFF,
80
81 /* CRTC Registers */
82 0x2D, 0x27, 0x28, 0x90, 0x2B, 0x80, 0xBF, 0x1F, 0x00, 0xC1, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x14, 0x00, 0x96, 0xB9, 0xA2,
84 0xFF,
85
86 /* AC Registers */
87 0x00, 0x13, 0x15, 0x17, 0x02, 0x04, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
88 0x14, 0x15, 0x16, 0x17, 0x01, 0x00, 0x03, 0x00, 0x00
89 };
90
91 static BYTE VideoMode_640x480_16color[] =
92 {
93 /* Miscellaneous Register */
94 0xE3,
95
96 /* Sequencer Registers */
97 0x03, 0x01, 0x08, 0x00, 0x06,
98
99 /* GC Registers */
100 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x0F, 0xFF,
101
102 /* CRTC Registers */
103 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0x0B, 0x3E, 0x00, 0x40, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0xEA, 0x0C, 0xDF, 0x28, 0x00, 0xE7, 0x04, 0xE3,
105 0xFF,
106
107 /* AC Registers */
108 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3A, 0x3B,
109 0x3C, 0x3D, 0x3E, 0x3F, 0x01, 0x00, 0x0F, 0x00, 0x00
110 };
111
112 static BYTE VideoMode_320x200_256color[] =
113 {
114 /* Miscellaneous Register */
115 0x63,
116
117 /* Sequencer Registers */
118 0x03, 0x01, 0x0F, 0x00, 0x0E,
119
120 /* GC Registers */
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, 0xFF,
122
123 /* CRTC Registers */
124 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F, 0x00, 0x41, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3,
126 0xFF,
127
128 /* AC Registers */
129 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
130 0x0C, 0x0D, 0x0E, 0x0F, 0x41, 0x00, 0x0F, 0x00, 0x00
131 };
132
133 static LPBYTE VideoModes[] =
134 {
135 VideoMode_40x25_text, /* Mode 00h */
136 VideoMode_40x25_text, /* Mode 01h */
137 VideoMode_80x25_text, /* Mode 02h */
138 VideoMode_80x25_text, /* Mode 03h */
139 VideoMode_320x200_4color, /* Mode 04h */
140 VideoMode_320x200_4color, /* Mode 05h */
141 NULL, /* Mode 06h */
142 NULL, /* Mode 07h */
143 NULL, /* Mode 08h */
144 NULL, /* Mode 09h */
145 NULL, /* Mode 0Ah */
146 NULL, /* Mode 0Bh */
147 NULL, /* Mode 0Ch */
148 NULL, /* Mode 0Dh */
149 NULL, /* Mode 0Eh */
150 NULL, /* Mode 0Fh */
151 NULL, /* Mode 10h */
152 NULL, /* Mode 11h */
153 VideoMode_640x480_16color, /* Mode 12h */
154 VideoMode_320x200_256color, /* Mode 13h */
155 };
156
157 /* PRIVATE FUNCTIONS **********************************************************/
158
159 static BOOLEAN BiosKbdBufferPush(WORD Data)
160 {
161 /* Get the location of the element after the head */
162 WORD NextElement = Bda->KeybdBufferHead + 2;
163
164 /* Wrap it around if it's at or beyond the end */
165 if (NextElement >= Bda->KeybdBufferEnd) NextElement = Bda->KeybdBufferStart;
166
167 /* If it's full, fail */
168 if (NextElement == Bda->KeybdBufferTail) return FALSE;
169
170 /* Put the value in the queue */
171 *((LPWORD)((ULONG_PTR)Bda + Bda->KeybdBufferTail)) = Data;
172 Bda->KeybdBufferTail += sizeof(WORD);
173
174 /* Check if we are at, or have passed, the end of the buffer */
175 if (Bda->KeybdBufferTail >= Bda->KeybdBufferEnd)
176 {
177 /* Return it to the beginning */
178 Bda->KeybdBufferTail = Bda->KeybdBufferStart;
179 }
180
181 /* Return success */
182 return TRUE;
183 }
184
185 static BOOLEAN BiosKbdBufferTop(LPWORD Data)
186 {
187 /* If it's empty, fail */
188 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return FALSE;
189
190 /* Otherwise, get the value and return success */
191 *Data = *((LPWORD)((ULONG_PTR)Bda + Bda->KeybdBufferHead));
192
193 return TRUE;
194 }
195
196 static BOOLEAN BiosKbdBufferPop(VOID)
197 {
198 /* If it's empty, fail */
199 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return FALSE;
200
201 /* Remove the value from the queue */
202 Bda->KeybdBufferHead += sizeof(WORD);
203
204 /* Check if we are at, or have passed, the end of the buffer */
205 if (Bda->KeybdBufferHead >= Bda->KeybdBufferEnd)
206 {
207 /* Return it to the beginning */
208 Bda->KeybdBufferHead = Bda->KeybdBufferStart;
209 }
210
211 /* Return success */
212 return TRUE;
213 }
214
215 static VOID BiosReadWindow(LPWORD Buffer, SMALL_RECT Rectangle, BYTE Page)
216 {
217 INT i, j;
218 INT Counter = 0;
219 WORD Character;
220 DWORD VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
221
222 for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
223 {
224 for (j = Rectangle.Left; j <= Rectangle.Right; j++)
225 {
226 /* Read from video memory */
227 VgaReadMemory(VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
228 (LPVOID)&Character,
229 sizeof(WORD));
230
231 /* Write the data to the buffer in row order */
232 Buffer[Counter++] = Character;
233 }
234 }
235 }
236
237 static VOID BiosWriteWindow(LPWORD Buffer, SMALL_RECT Rectangle, BYTE Page)
238 {
239 INT i, j;
240 INT Counter = 0;
241 WORD Character;
242 DWORD VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
243
244 for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
245 {
246 for (j = Rectangle.Left; j <= Rectangle.Right; j++)
247 {
248 Character = Buffer[Counter++];
249
250 /* Read from video memory */
251 VgaWriteMemory(VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
252 (LPVOID)&Character,
253 sizeof(WORD));
254 }
255 }
256 }
257
258 /* PUBLIC FUNCTIONS ***********************************************************/
259
260 BYTE BiosGetVideoMode(VOID)
261 {
262 return Bda->VideoMode;
263 }
264
265 BOOLEAN BiosSetVideoMode(BYTE ModeNumber)
266 {
267 INT i;
268 COORD Resolution;
269 LPBYTE Values = VideoModes[ModeNumber];
270
271 if (Values == NULL) return FALSE;
272
273 /* Write the misc register */
274 VgaWritePort(VGA_MISC_WRITE, *(Values++));
275
276 /* Write the sequencer registers */
277 for (i = 0; i < VGA_SEQ_MAX_REG; i++)
278 {
279 VgaWritePort(VGA_SEQ_INDEX, i);
280 VgaWritePort(VGA_SEQ_DATA, *(Values++));
281 }
282
283 /* Write the GC registers */
284 for (i = 0; i < VGA_GC_MAX_REG; i++)
285 {
286 VgaWritePort(VGA_GC_INDEX, i);
287 VgaWritePort(VGA_GC_DATA, *(Values++));
288 }
289
290 /* Write the CRTC registers */
291 for (i = 0; i < VGA_CRTC_MAX_REG; i++)
292 {
293 VgaWritePort(VGA_CRTC_INDEX, i);
294 VgaWritePort(VGA_CRTC_DATA, *(Values++));
295 }
296
297 /* Write the AC registers */
298 for (i = 0; i < VGA_AC_MAX_REG; i++)
299 {
300 VgaWritePort(VGA_AC_INDEX, i);
301 VgaWritePort(VGA_AC_WRITE, *(Values++));
302 }
303
304 /* Update the values in the BDA */
305 Bda->VideoMode = ModeNumber;
306 Bda->VideoPage = 0;
307 Bda->VideoPageSize = BIOS_PAGE_SIZE;
308 Bda->VideoPageOffset = 0;
309 Bda->CharacterHeight = 16;
310
311 Resolution = VgaGetDisplayResolution();
312 Bda->ScreenColumns = Resolution.X;
313 Bda->ScreenRows = Resolution.Y - 1;
314
315 return TRUE;
316 }
317
318 BOOLEAN BiosSetVideoPage(BYTE PageNumber)
319 {
320 if (PageNumber >= BIOS_MAX_PAGES) return FALSE;
321
322 /* Set the values in the BDA */
323 Bda->VideoPage = PageNumber;
324 Bda->VideoPageSize = BIOS_PAGE_SIZE;
325 Bda->VideoPageOffset = PageNumber * BIOS_PAGE_SIZE;
326
327 /* Set the start address in the CRTC */
328 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
329 VgaWritePort(VGA_CRTC_DATA , LOBYTE(Bda->VideoPageOffset));
330 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
331 VgaWritePort(VGA_CRTC_DATA , HIBYTE(Bda->VideoPageOffset));
332
333 return TRUE;
334 }
335
336 BOOLEAN BiosInitialize(VOID)
337 {
338 INT i;
339 WORD Offset = 0;
340 LPWORD IntVecTable = (LPWORD)((ULONG_PTR)BaseAddress);
341 LPBYTE BiosCode = (LPBYTE)((ULONG_PTR)BaseAddress + TO_LINEAR(BIOS_SEGMENT, 0));
342
343 /* Initialize the BDA */
344 Bda = (PBIOS_DATA_AREA)((ULONG_PTR)BaseAddress + TO_LINEAR(BDA_SEGMENT, 0));
345 Bda->EquipmentList = BIOS_EQUIPMENT_LIST;
346 Bda->KeybdBufferStart = FIELD_OFFSET(BIOS_DATA_AREA, KeybdBuffer);
347 Bda->KeybdBufferEnd = Bda->KeybdBufferStart + BIOS_KBD_BUFFER_SIZE * sizeof(WORD);
348
349 /* Generate ISR stubs and fill the IVT */
350 for (i = 0; i < 256; i++)
351 {
352 IntVecTable[i * 2] = Offset;
353 IntVecTable[i * 2 + 1] = BIOS_SEGMENT;
354
355 BiosCode[Offset++] = 0xFA; // cli
356
357 BiosCode[Offset++] = 0x6A; // push i
358 BiosCode[Offset++] = (BYTE)i;
359
360 BiosCode[Offset++] = LOBYTE(EMULATOR_BOP); // BOP sequence
361 BiosCode[Offset++] = HIBYTE(EMULATOR_BOP);
362 BiosCode[Offset++] = LOBYTE(EMULATOR_INT_BOP);
363 BiosCode[Offset++] = HIBYTE(EMULATOR_INT_BOP);
364
365 BiosCode[Offset++] = 0x83; // add sp, 2
366 BiosCode[Offset++] = 0xC4;
367 BiosCode[Offset++] = 0x02;
368
369 BiosCode[Offset++] = 0xCF; // iret
370 }
371
372 /* Get the input handle to the real console, and check for success */
373 BiosConsoleInput = CreateFileW(L"CONIN$",
374 GENERIC_READ | GENERIC_WRITE,
375 FILE_SHARE_READ | FILE_SHARE_WRITE,
376 NULL,
377 OPEN_EXISTING,
378 0,
379 NULL);
380 if (BiosConsoleInput == INVALID_HANDLE_VALUE)
381 {
382 return FALSE;
383 }
384
385 /* Get the output handle to the real console, and check for success */
386 BiosConsoleOutput = CreateFileW(L"CONOUT$",
387 GENERIC_READ | GENERIC_WRITE,
388 FILE_SHARE_READ | FILE_SHARE_WRITE,
389 NULL,
390 OPEN_EXISTING,
391 0,
392 NULL);
393 if (BiosConsoleOutput == INVALID_HANDLE_VALUE)
394 {
395 CloseHandle(BiosConsoleInput);
396 return FALSE;
397 }
398
399 /* Save the console screen buffer information */
400 if (!GetConsoleScreenBufferInfo(BiosConsoleOutput, &BiosSavedBufferInfo))
401 {
402 CloseHandle(BiosConsoleOutput);
403 CloseHandle(BiosConsoleInput);
404 return FALSE;
405 }
406
407 /* Initialize VGA */
408 VgaInitialize(BiosConsoleOutput);
409
410 /* Update the cursor position */
411 BiosSetCursorPosition(BiosSavedBufferInfo.dwCursorPosition.Y,
412 BiosSavedBufferInfo.dwCursorPosition.X,
413 0);
414
415 /* Set the console input mode */
416 SetConsoleMode(BiosConsoleInput, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
417
418 /* Initialize the PIC */
419 PicWriteCommand(PIC_MASTER_CMD, PIC_ICW1 | PIC_ICW1_ICW4);
420 PicWriteCommand(PIC_SLAVE_CMD , PIC_ICW1 | PIC_ICW1_ICW4);
421
422 /* Set the interrupt offsets */
423 PicWriteData(PIC_MASTER_DATA, BIOS_PIC_MASTER_INT);
424 PicWriteData(PIC_SLAVE_DATA , BIOS_PIC_SLAVE_INT);
425
426 /* Tell the master PIC there is a slave at IRQ 2 */
427 PicWriteData(PIC_MASTER_DATA, 1 << 2);
428 PicWriteData(PIC_SLAVE_DATA , 2);
429
430 /* Make sure the PIC is in 8086 mode */
431 PicWriteData(PIC_MASTER_DATA, PIC_ICW4_8086);
432 PicWriteData(PIC_SLAVE_DATA , PIC_ICW4_8086);
433
434 /* Clear the masks for both PICs */
435 PicWriteData(PIC_MASTER_DATA, 0x00);
436 PicWriteData(PIC_SLAVE_DATA , 0x00);
437
438 PitWriteCommand(0x34);
439 PitWriteData(0, 0x00);
440 PitWriteData(0, 0x00);
441
442 return TRUE;
443 }
444
445 VOID BiosCleanup(VOID)
446 {
447 /* Restore the old screen buffer */
448 SetConsoleActiveScreenBuffer(BiosConsoleOutput);
449
450 /* Restore the screen buffer size */
451 SetConsoleScreenBufferSize(BiosConsoleOutput, BiosSavedBufferInfo.dwSize);
452
453 /* Close the console handles */
454 if (BiosConsoleOutput != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleOutput);
455 if (BiosConsoleInput != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleInput);
456 }
457
458 WORD BiosPeekCharacter(VOID)
459 {
460 WORD CharacterData;
461
462 /* Check if there is a key available */
463 if (Bda->KeybdBufferHead == Bda->KeybdBufferTail) return 0xFFFF;
464
465 /* Get the key from the queue, but don't remove it */
466 BiosKbdBufferTop(&CharacterData);
467
468 return CharacterData;
469 }
470
471 WORD BiosGetCharacter(VOID)
472 {
473 WORD CharacterData;
474 INPUT_RECORD InputRecord;
475 DWORD Count;
476
477 /* Check if there is a key available */
478 if (Bda->KeybdBufferHead != Bda->KeybdBufferTail)
479 {
480 /* Get the key from the queue, and remove it */
481 BiosKbdBufferTop(&CharacterData);
482 BiosKbdBufferPop();
483 }
484 else
485 {
486 while (TRUE)
487 {
488 /* Wait for a console event */
489 WaitForSingleObject(BiosConsoleInput, INFINITE);
490
491 /* Read the event, and make sure it's a keypress */
492 if (!ReadConsoleInput(BiosConsoleInput, &InputRecord, 1, &Count)) continue;
493 if (InputRecord.EventType != KEY_EVENT) continue;
494 if (!InputRecord.Event.KeyEvent.bKeyDown) continue;
495
496 /* Save the scan code and end the loop */
497 CharacterData = (InputRecord.Event.KeyEvent.wVirtualScanCode << 8)
498 | InputRecord.Event.KeyEvent.uChar.AsciiChar;
499
500 break;
501 }
502 }
503
504 return CharacterData;
505 }
506
507 VOID BiosSetCursorPosition(BYTE Row, BYTE Column, BYTE Page)
508 {
509 /* Make sure the selected video page is valid */
510 if (Page >= BIOS_MAX_PAGES) return;
511
512 /* Update the position in the BDA */
513 Bda->CursorPosition[Page] = (Row << 8) | Column;
514
515 /* Check if this is the current video page */
516 if (Page == Bda->VideoPage)
517 {
518 WORD Offset = Row * Bda->ScreenColumns + Column;
519
520 /* Modify the CRTC registers */
521 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
522 VgaWritePort(VGA_CRTC_DATA , LOBYTE(Offset));
523 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
524 VgaWritePort(VGA_CRTC_DATA , HIBYTE(Offset));
525 }
526 }
527
528 BOOLEAN BiosScrollWindow(INT Direction,
529 DWORD Amount,
530 SMALL_RECT Rectangle,
531 BYTE Page,
532 BYTE FillAttribute)
533 {
534 DWORD i;
535 LPWORD WindowData;
536 DWORD WindowSize = (Rectangle.Bottom - Rectangle.Top + 1)
537 * (Rectangle.Right - Rectangle.Left + 1);
538
539 /* Allocate a buffer for the window */
540 WindowData = (LPWORD)HeapAlloc(GetProcessHeap(),
541 HEAP_ZERO_MEMORY,
542 WindowSize * sizeof(WORD));
543 if (WindowData == NULL) return FALSE;
544
545 /* Read the window data */
546 BiosReadWindow(WindowData, Rectangle, Page);
547
548 if (Amount == 0)
549 {
550 /* Fill the window */
551 for (i = 0; i < WindowSize; i++)
552 {
553 WindowData[i] = ' ' | (FillAttribute << 8);
554 }
555
556 goto Done;
557 }
558
559 // TODO: Scroll the window!
560
561 Done:
562 /* Write back the window data */
563 BiosWriteWindow(WindowData, Rectangle, Page);
564
565 /* Free the window buffer */
566 HeapFree(GetProcessHeap(), 0, WindowData);
567
568 return TRUE;
569 }
570
571 VOID BiosPrintCharacter(CHAR Character, BYTE Attribute, BYTE Page)
572 {
573 WORD CharData = (Attribute << 8) | Character;
574 BYTE Row, Column;
575
576 /* Make sure the page exists */
577 if (Page >= BIOS_MAX_PAGES) return;
578
579 /* Get the cursor location */
580 Row = HIBYTE(Bda->CursorPosition[Page]);
581 Column = LOBYTE(Bda->CursorPosition[Page]);
582
583 /* Write the character */
584 VgaWriteMemory(TO_LINEAR(TEXT_VIDEO_SEG,
585 (Row * Bda->ScreenColumns + Column) * sizeof(WORD)),
586 (LPVOID)&CharData,
587 sizeof(WORD));
588
589 /* Advance the cursor */
590 Column++;
591
592 /* Check if it passed the end of the row */
593 if (Column == Bda->ScreenColumns)
594 {
595 /* Return to the first column */
596 Column = 0;
597
598 if (Row == Bda->ScreenRows)
599 {
600 /* The screen must be scrolled */
601 SMALL_RECT Rectangle = { 0, 0, Bda->ScreenColumns - 1, Bda->ScreenRows };
602
603 BiosScrollWindow(SCROLL_DIRECTION_UP,
604 1,
605 Rectangle,
606 Page,
607 DEFAULT_ATTRIBUTE);
608 }
609 else Row++;
610 }
611
612 /* Set the cursor position */
613 BiosSetCursorPosition(Row, Column, Page);
614 }
615
616 VOID BiosVideoService(LPWORD Stack)
617 {
618 DWORD Eax = EmulatorGetRegister(EMULATOR_REG_AX);
619 DWORD Ecx = EmulatorGetRegister(EMULATOR_REG_CX);
620 DWORD Edx = EmulatorGetRegister(EMULATOR_REG_DX);
621 DWORD Ebx = EmulatorGetRegister(EMULATOR_REG_BX);
622
623 switch (HIBYTE(Eax))
624 {
625 /* Set Video Mode */
626 case 0x00:
627 {
628 BiosSetVideoMode(LOBYTE(Eax));
629 break;
630 }
631
632 /* Set Text-Mode Cursor Shape */
633 case 0x01:
634 {
635 /* Update the BDA */
636 Bda->CursorStartLine = HIBYTE(Ecx);
637 Bda->CursorEndLine = LOBYTE(Ecx);
638
639 /* Modify the CRTC registers */
640 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_START_REG);
641 VgaWritePort(VGA_CRTC_DATA , Bda->CursorStartLine);
642 VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_END_REG);
643 VgaWritePort(VGA_CRTC_DATA , Bda->CursorEndLine);
644
645 break;
646 }
647
648 /* Set Cursor Position */
649 case 0x02:
650 {
651 BiosSetCursorPosition(HIBYTE(Edx), LOBYTE(Edx), HIBYTE(Ebx));
652 break;
653 }
654
655 /* Get Cursor Position */
656 case 0x03:
657 {
658 /* Make sure the selected video page exists */
659 if (HIBYTE(Ebx) >= BIOS_MAX_PAGES) break;
660
661 /* Return the result */
662 EmulatorSetRegister(EMULATOR_REG_AX, 0);
663 EmulatorSetRegister(EMULATOR_REG_CX,
664 (Bda->CursorStartLine << 8) | Bda->CursorEndLine);
665 EmulatorSetRegister(EMULATOR_REG_DX, Bda->CursorPosition[HIBYTE(Ebx)]);
666
667 break;
668 }
669
670 /* Query Light Pen */
671 case 0x04:
672 {
673 /*
674 * On modern BIOSes, this function returns 0
675 * so that we can ignore the other registers.
676 */
677 EmulatorSetRegister(EMULATOR_REG_AX, 0);
678 break;
679 }
680
681 /* Select Active Display Page */
682 case 0x05:
683 {
684 /* Check if the page exists */
685 if (LOBYTE(Eax) >= BIOS_MAX_PAGES) break;
686
687 /* Check if this is the same page */
688 if (LOBYTE(Eax) == Bda->VideoPage) break;
689
690 /* Change the video page */
691 BiosSetVideoPage(LOBYTE(Eax));
692
693 break;
694 }
695
696 /* Scroll Window Up/Down */
697 case 0x06:
698 case 0x07:
699 {
700 SMALL_RECT Rectangle =
701 {
702 LOBYTE(Ecx),
703 HIBYTE(Ecx),
704 LOBYTE(Edx),
705 HIBYTE(Edx)
706 };
707
708 /* Call the internal function */
709 BiosScrollWindow((HIBYTE(Eax)== 0x06)
710 ? SCROLL_DIRECTION_UP : SCROLL_DIRECTION_DOWN,
711 LOBYTE(Eax),
712 Rectangle,
713 Bda->VideoPage,
714 HIBYTE(Ebx));
715
716 break;
717 }
718
719 /* Read/Write Character From Cursor Position */
720 case 0x08:
721 case 0x09:
722 case 0x0A:
723 {
724 WORD CharacterData = MAKEWORD(LOBYTE(Eax), LOBYTE(Ebx));
725 BYTE Page = HIBYTE(Ebx);
726 DWORD Offset;
727
728 /* Check if the page exists */
729 if (Page >= BIOS_MAX_PAGES) break;
730
731 /* Find the offset of the character */
732 Offset = Page * Bda->VideoPageSize
733 + (HIBYTE(Bda->CursorPosition[Page]) * Bda->ScreenColumns
734 + LOBYTE(Bda->CursorPosition[Page])) * 2;
735
736 if (HIBYTE(Eax) == 0x08)
737 {
738 /* Read from the video memory */
739 VgaReadMemory(TO_LINEAR(TEXT_VIDEO_SEG, Offset),
740 (LPVOID)&CharacterData,
741 sizeof(WORD));
742
743 /* Return the character in AX */
744 EmulatorSetRegister(EMULATOR_REG_AX, CharacterData);
745 }
746 else
747 {
748 /* Write to video memory */
749 VgaWriteMemory(TO_LINEAR(TEXT_VIDEO_SEG, Offset),
750 (LPVOID)&CharacterData,
751 (HIBYTE(Ebx) == 0x09) ? sizeof(WORD) : sizeof(BYTE));
752 }
753
754 break;
755 }
756
757 /* Teletype Output */
758 case 0x0E:
759 {
760 BiosPrintCharacter(LOBYTE(Eax), LOBYTE(Ebx), HIBYTE(Ebx));
761 break;
762 }
763
764 /* Get Current Video Mode */
765 case 0x0F:
766 {
767 EmulatorSetRegister(EMULATOR_REG_AX,
768 MAKEWORD(Bda->VideoMode, Bda->ScreenColumns));
769 EmulatorSetRegister(EMULATOR_REG_BX,
770 MAKEWORD(LOBYTE(Ebx), Bda->VideoPage));
771
772 break;
773 }
774
775 /* Scroll Window */
776 case 0x12:
777 {
778 SMALL_RECT Rectangle =
779 {
780 LOBYTE(Ecx),
781 HIBYTE(Ecx),
782 LOBYTE(Edx),
783 HIBYTE(Edx)
784 };
785
786 /* Call the internal function */
787 BiosScrollWindow(LOBYTE(Ebx),
788 LOBYTE(Eax),
789 Rectangle,
790 Bda->VideoPage,
791 DEFAULT_ATTRIBUTE);
792
793 break;
794 }
795
796 default:
797 {
798 DPRINT1("BIOS Function INT 10h, AH = 0x%02X NOT IMPLEMENTED\n",
799 HIBYTE(Eax));
800 }
801 }
802 }
803
804 VOID BiosKeyboardService(LPWORD Stack)
805 {
806 DWORD Eax = EmulatorGetRegister(EMULATOR_REG_AX);
807
808 switch (HIBYTE(Eax))
809 {
810 case 0x00:
811 {
812 /* Read the character (and wait if necessary) */
813 EmulatorSetRegister(EMULATOR_REG_AX, BiosGetCharacter());
814
815 break;
816 }
817
818 case 0x01:
819 {
820 WORD Data = BiosPeekCharacter();
821
822 if (Data != 0xFFFF)
823 {
824 /* There is a character, clear ZF and return it */
825 EmulatorSetRegister(EMULATOR_REG_AX, Data);
826 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_ZF;
827 }
828 else
829 {
830 /* No character, set ZF */
831 Stack[STACK_FLAGS] |= EMULATOR_FLAG_ZF;
832 }
833
834 break;
835 }
836
837 default:
838 {
839 DPRINT1("BIOS Function INT 16h, AH = 0x%02X NOT IMPLEMENTED\n",
840 HIBYTE(Eax));
841 }
842 }
843 }
844
845 VOID BiosTimeService(LPWORD Stack)
846 {
847 DWORD Eax = EmulatorGetRegister(EMULATOR_REG_AX);
848 DWORD Ecx = EmulatorGetRegister(EMULATOR_REG_CX);
849 DWORD Edx = EmulatorGetRegister(EMULATOR_REG_DX);
850
851 switch (HIBYTE(Eax))
852 {
853 case 0x00:
854 {
855 /* Set AL to 1 if midnight had passed, 0 otherwise */
856 Eax &= 0xFFFFFF00;
857 if (Bda->MidnightPassed) Eax |= 1;
858
859 /* Return the tick count in CX:DX */
860 EmulatorSetRegister(EMULATOR_REG_AX, Eax);
861 EmulatorSetRegister(EMULATOR_REG_CX, HIWORD(Bda->TickCounter));
862 EmulatorSetRegister(EMULATOR_REG_DX, LOWORD(Bda->TickCounter));
863
864 /* Reset the midnight flag */
865 Bda->MidnightPassed = FALSE;
866
867 break;
868 }
869
870 case 0x01:
871 {
872 /* Set the tick count to CX:DX */
873 Bda->TickCounter = MAKELONG(LOWORD(Edx), LOWORD(Ecx));
874
875 /* Reset the midnight flag */
876 Bda->MidnightPassed = FALSE;
877
878 break;
879 }
880
881 default:
882 {
883 DPRINT1("BIOS Function INT 1Ah, AH = 0x%02X NOT IMPLEMENTED\n",
884 HIBYTE(Eax));
885 }
886 }
887 }
888
889 VOID BiosSystemTimerInterrupt(LPWORD Stack)
890 {
891 /* Increase the system tick count */
892 Bda->TickCounter++;
893 }
894
895 VOID BiosEquipmentService(LPWORD Stack)
896 {
897 /* Return the equipment list */
898 EmulatorSetRegister(EMULATOR_REG_AX, Bda->EquipmentList);
899 }
900
901 VOID BiosHandleIrq(BYTE IrqNumber, LPWORD Stack)
902 {
903 switch (IrqNumber)
904 {
905 /* PIT IRQ */
906 case 0:
907 {
908 /* Perform the system timer interrupt */
909 EmulatorInterrupt(BIOS_SYS_TIMER_INTERRUPT);
910
911 break;
912 }
913
914 /* Keyboard IRQ */
915 case 1:
916 {
917 BYTE ScanCode, VirtualKey;
918 WORD Character;
919
920 /* Check if there is a scancode available */
921 if (!(KeyboardReadStatus() & 1)) break;
922
923 /* Get the scan code and virtual key code */
924 ScanCode = KeyboardReadData();
925 VirtualKey = MapVirtualKey(ScanCode & 0x7F, MAPVK_VSC_TO_VK);
926
927 /* Check if this is a key press or release */
928 if (!(ScanCode & (1 << 7)))
929 {
930 /* Key press */
931 if (VirtualKey == VK_NUMLOCK
932 || VirtualKey == VK_CAPITAL
933 || VirtualKey == VK_SCROLL)
934 {
935 /* For toggle keys, toggle the lowest bit in the keyboard map */
936 BiosKeyboardMap[VirtualKey] ^= ~(1 << 0);
937 }
938
939 /* Set the highest bit */
940 BiosKeyboardMap[VirtualKey] |= (1 << 7);
941
942 /* Find out which character this is */
943 if (ToAscii(VirtualKey, ScanCode, BiosKeyboardMap, &Character, 0) > 0)
944 {
945 /* Push it onto the BIOS keyboard queue */
946 BiosKbdBufferPush((ScanCode << 8) | (Character & 0xFF));
947 }
948 }
949 else
950 {
951 /* Key release, unset the highest bit */
952 BiosKeyboardMap[VirtualKey] &= ~(1 << 7);
953 }
954
955 break;
956 }
957 }
958
959 /* Send End-of-Interrupt to the PIC */
960 if (IrqNumber > 8) PicWriteCommand(PIC_SLAVE_CMD, PIC_OCW2_EOI);
961 PicWriteCommand(PIC_MASTER_CMD, PIC_OCW2_EOI);
962 }
963
964 /* EOF */