[NTVDM]
[reactos.git] / subsystems / ntvdm / bios.c
index a4d1ce1..9a0e953 100644 (file)
@@ -21,7 +21,7 @@
 
 static PBIOS_DATA_AREA Bda;
 static BYTE BiosKeyboardMap[256];
-static HANDLE BiosConsoleInput = INVALID_HANDLE_VALUE;
+static HANDLE BiosConsoleInput  = INVALID_HANDLE_VALUE;
 static HANDLE BiosConsoleOutput = INVALID_HANDLE_VALUE;
 static CONSOLE_SCREEN_BUFFER_INFO BiosSavedBufferInfo;
 
@@ -37,7 +37,7 @@ static BYTE VideoMode_40x25_text[] =
     0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x0F, 0xFF,
 
     /* CRTC Registers */
-    0x2D, 0x27, 0x28, 0x90, 0x2B, 0xA0, 0xBF, 0x1F, 0x00, 0x4F, 0x0E, 0x0F,
+    0x2D, 0x27, 0x28, 0x90, 0x2B, 0xA0, 0xBF, 0x1F, 0x00, 0x4F, 0x0D, 0x0F,
     0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x14, 0x1F, 0x96, 0xB9, 0xA3,
     0xFF,
 
@@ -58,7 +58,7 @@ static BYTE VideoMode_80x25_text[] =
     0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x0F, 0xFF,
 
     /* CRTC Registers */
-    0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F, 0x00, 0x4F, 0x0E, 0x0F,
+    0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F, 0x00, 0x4F, 0x0D, 0x0F,
     0x00, 0x00, 0x00, 0x00, 0x9C, 0x8E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3,
     0xFF,
 
@@ -216,14 +216,13 @@ static VOID BiosReadWindow(LPWORD Buffer, SMALL_RECT Rectangle, BYTE Page)
 {
     INT i, j;
     INT Counter = 0;
+    WORD Character;
     DWORD VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
 
     for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
     {
         for (j = Rectangle.Left; j <= Rectangle.Right; j++)
         {
-            WORD Character;
-
             /* Read from video memory */
             VgaReadMemory(VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
                           (LPVOID)&Character,
@@ -239,13 +238,14 @@ static VOID BiosWriteWindow(LPWORD Buffer, SMALL_RECT Rectangle, BYTE Page)
 {
     INT i, j;
     INT Counter = 0;
+    WORD Character;
     DWORD VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
 
     for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
     {
         for (j = Rectangle.Left; j <= Rectangle.Right; j++)
         {
-            WORD Character = Buffer[Counter++];
+            Character = Buffer[Counter++];
 
             /* Read from video memory */
             VgaWriteMemory(VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
@@ -304,7 +304,7 @@ BOOLEAN BiosSetVideoMode(BYTE ModeNumber)
     /* Update the values in the BDA */
     Bda->VideoMode = ModeNumber;
     Bda->VideoPage = 0;
-    Bda->VideoPageSize = BIOS_PAGE_SIZE;
+    Bda->VideoPageSize   = BIOS_PAGE_SIZE;
     Bda->VideoPageOffset = 0;
     Bda->CharacterHeight = 16;
 
@@ -326,9 +326,9 @@ BOOLEAN BiosSetVideoPage(BYTE PageNumber)
 
     /* Set the start address in the CRTC */
     VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
-    VgaWritePort(VGA_CRTC_DATA, LOBYTE(Bda->VideoPageOffset));
+    VgaWritePort(VGA_CRTC_DATA , LOBYTE(Bda->VideoPageOffset));
     VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
-    VgaWritePort(VGA_CRTC_DATA, HIBYTE(Bda->VideoPageOffset));
+    VgaWritePort(VGA_CRTC_DATA , HIBYTE(Bda->VideoPageOffset));
 
     return TRUE;
 }
@@ -369,64 +369,71 @@ BOOLEAN BiosInitialize(VOID)
         BiosCode[Offset++] = 0xCF; // iret
     }
 
-    /* Get the input and output handles to the real console */
-    BiosConsoleInput = CreateFile(TEXT("CONIN$"),
-                                  GENERIC_READ | GENERIC_WRITE,
-                                  FILE_SHARE_READ | FILE_SHARE_WRITE,
-                                  NULL,
-                                  OPEN_EXISTING,
-                                  0,
-                                  NULL);
-
-    BiosConsoleOutput = CreateFile(TEXT("CONOUT$"),
+    /* Get the input handle to the real console, and check for success */
+    BiosConsoleInput = CreateFileW(L"CONIN$",
                                    GENERIC_READ | GENERIC_WRITE,
                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                                    NULL,
                                    OPEN_EXISTING,
                                    0,
                                    NULL);
+    if (BiosConsoleInput == INVALID_HANDLE_VALUE)
+    {
+        return FALSE;
+    }
 
-    /* Make sure it was successful */
-    if ((BiosConsoleInput == INVALID_HANDLE_VALUE)
-        || (BiosConsoleOutput == INVALID_HANDLE_VALUE))
+    /* Get the output handle to the real console, and check for success */
+    BiosConsoleOutput = CreateFileW(L"CONOUT$",
+                                    GENERIC_READ | GENERIC_WRITE,
+                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
+                                    NULL,
+                                    OPEN_EXISTING,
+                                    0,
+                                    NULL);
+    if (BiosConsoleOutput == INVALID_HANDLE_VALUE)
     {
+        CloseHandle(BiosConsoleInput);
         return FALSE;
     }
 
     /* Save the console screen buffer information */
     if (!GetConsoleScreenBufferInfo(BiosConsoleOutput, &BiosSavedBufferInfo))
     {
+        CloseHandle(BiosConsoleOutput);
+        CloseHandle(BiosConsoleInput);
         return FALSE;
     }
 
-    /* Store the cursor position */
-    Bda->CursorPosition[0] = MAKEWORD(BiosSavedBufferInfo.dwCursorPosition.X,
-                                      BiosSavedBufferInfo.dwCursorPosition.Y);
-    
+    /* Initialize VGA */
     VgaInitialize(BiosConsoleOutput);
 
+    /* Update the cursor position */
+    BiosSetCursorPosition(BiosSavedBufferInfo.dwCursorPosition.X,
+                          BiosSavedBufferInfo.dwCursorPosition.Y,
+                          0);
+
     /* Set the console input mode */
     SetConsoleMode(BiosConsoleInput, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
 
     /* Initialize the PIC */
     PicWriteCommand(PIC_MASTER_CMD, PIC_ICW1 | PIC_ICW1_ICW4);
-    PicWriteCommand(PIC_SLAVE_CMD, PIC_ICW1 | PIC_ICW1_ICW4);
+    PicWriteCommand(PIC_SLAVE_CMD , PIC_ICW1 | PIC_ICW1_ICW4);
 
     /* Set the interrupt offsets */
     PicWriteData(PIC_MASTER_DATA, BIOS_PIC_MASTER_INT);
-    PicWriteData(PIC_SLAVE_DATA, BIOS_PIC_SLAVE_INT);
+    PicWriteData(PIC_SLAVE_DATA , BIOS_PIC_SLAVE_INT);
 
     /* Tell the master PIC there is a slave at IRQ 2 */
     PicWriteData(PIC_MASTER_DATA, 1 << 2);
-    PicWriteData(PIC_SLAVE_DATA, 2);
+    PicWriteData(PIC_SLAVE_DATA , 2);
 
     /* Make sure the PIC is in 8086 mode */
     PicWriteData(PIC_MASTER_DATA, PIC_ICW4_8086);
-    PicWriteData(PIC_SLAVE_DATA, PIC_ICW4_8086);
+    PicWriteData(PIC_SLAVE_DATA , PIC_ICW4_8086);
 
     /* Clear the masks for both PICs */
     PicWriteData(PIC_MASTER_DATA, 0x00);
-    PicWriteData(PIC_SLAVE_DATA, 0x00);
+    PicWriteData(PIC_SLAVE_DATA , 0x00);
 
     PitWriteCommand(0x34);
     PitWriteData(0, 0x00);
@@ -444,8 +451,8 @@ VOID BiosCleanup(VOID)
     SetConsoleScreenBufferSize(BiosConsoleOutput, BiosSavedBufferInfo.dwSize);
 
     /* Close the console handles */
-    if (BiosConsoleInput != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleInput);
     if (BiosConsoleOutput != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleOutput);
+    if (BiosConsoleInput  != INVALID_HANDLE_VALUE) CloseHandle(BiosConsoleInput);
 }
 
 WORD BiosPeekCharacter(VOID)
@@ -512,9 +519,9 @@ VOID BiosSetCursorPosition(BYTE Row, BYTE Column, BYTE Page)
 
         /* Modify the CRTC registers */
         VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
-        VgaWritePort(VGA_CRTC_DATA, LOBYTE(Offset));
+        VgaWritePort(VGA_CRTC_DATA , LOBYTE(Offset));
         VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
-        VgaWritePort(VGA_CRTC_DATA, HIBYTE(Offset));
+        VgaWritePort(VGA_CRTC_DATA , HIBYTE(Offset));
     }
 }
 
@@ -524,7 +531,7 @@ BOOLEAN BiosScrollWindow(INT Direction,
                          BYTE Page,
                          BYTE FillAttribute)
 {
-    INT i;
+    DWORD i;
     LPWORD WindowData;
     DWORD WindowSize = (Rectangle.Bottom - Rectangle.Top + 1)
                        * (Rectangle.Right - Rectangle.Left + 1);
@@ -613,8 +620,6 @@ VOID BiosVideoService(LPWORD Stack)
     DWORD Edx = EmulatorGetRegister(EMULATOR_REG_DX);
     DWORD Ebx = EmulatorGetRegister(EMULATOR_REG_BX);
 
-    UNREFERENCED_PARAMETER(Ecx);
-
     switch (HIBYTE(Eax))
     {
         /* Set Video Mode */
@@ -633,9 +638,9 @@ VOID BiosVideoService(LPWORD Stack)
 
             /* Modify the CRTC registers */
             VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_START_REG);
-            VgaWritePort(VGA_CRTC_DATA, Bda->CursorStartLine);
+            VgaWritePort(VGA_CRTC_DATA , Bda->CursorStartLine);
             VgaWritePort(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_END_REG);
-            VgaWritePort(VGA_CRTC_DATA, Bda->CursorEndLine);
+            VgaWritePort(VGA_CRTC_DATA , Bda->CursorEndLine);
 
             break;
         }