[NTVDM]
[reactos.git] / reactos / subsystems / mvdm / ntvdm / bios / vidbios.c
index 41d3088..cda45c7 100644 (file)
@@ -1,31 +1,40 @@
 /*
  * COPYRIGHT:       GPL - See COPYING in the top level directory
  * PROJECT:         ReactOS Virtual DOS Machine
- * FILE:            vidbios.c
- * PURPOSE:         VDM Video BIOS Support Library
+ * FILE:            subsystems/mvdm/ntvdm/bios/vidbios.c
+ * PURPOSE:         VDM 32-bit Video BIOS Support Library
  * PROGRAMMERS:     Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
  *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 
+#include "ntvdm.h"
+
 #define NDEBUG
+#include <debug.h>
+
+/* BIOS Version number and Copyright */
+#include <reactos/buildno.h>
+#include <reactos/version.h>
 
-#include "ntvdm.h"
 #include "emulator.h"
 #include "cpu/cpu.h"
 #include "cpu/bop.h"
+#include "memory.h"
 
 #include "bios.h"
+#include "bios32/bios32p.h"
+#include "rom.h"
+#include "bios32/vbe.h"
 // #include "vidbios.h"
+#include "bios32/vidbios32.h"
 
 #include "io.h"
-#include "hardware/video/vga.h"
-
-/* DEFINES ********************************************************************/
-
-/* BOP Identifiers */
-#define BOP_VIDEO_INT   0x10
+#include "hardware/video/svga.h"
+/**/
+#include "../console/video.h"
+/**/
 
 /* MACROS *********************************************************************/
 
 
 /* PRIVATE VARIABLES **********************************************************/
 
+/*
+ * WARNING! For compatibility purposes the string "IBM" should be at C000:001E.
+ */
+static const CHAR BiosInfo[] =
+    "00000000000 Emulation of IBM VGA Compatible ROM\0"
+    "CL-GD5434 VGA BIOS Version 1.41  \r\n"
+    "Copyright (C) ReactOS Team 1996-"COPYRIGHT_YEAR"\r\n"
+    "The original CL-GD5434 card was created by Cirrus Logic, Inc.\r\n\0"
+    "BIOS Date: 06/17/13\0";
+
+C_ASSERT(sizeof(BiosInfo)-1 <= 0xFF-0x05); // Ensures that we won't overflow on the Video Code
+
+
 /*
  * VGA Register Configurations for BIOS Video Modes.
  * The configurations were checked against SeaBIOS VGA BIOS.
@@ -1920,36 +1942,40 @@ typedef struct _VGA_MODE
 {
     PVGA_REGISTERS VgaRegisters;
     WORD PageSize;
+    WORD CharacterWidth;
     WORD CharacterHeight;
     // PCOLORREF Palette;
 } VGA_MODE, *PVGA_MODE;
 
 static CONST VGA_MODE VideoModes[BIOS_MAX_VIDEO_MODE + 1] =
 {
-    {&VideoMode_40x25_text,         0x0800, 16},    /* Mode 00h - 16 color (mono)                       */
-    {&VideoMode_40x25_text,         0x0800, 16},    /* Mode 01h - 16 color                              */
-    {&VideoMode_80x25_text,         0x1000, 16},    /* Mode 02h - 16 color (mono)                       */
-    {&VideoMode_80x25_text,         0x1000, 16},    /* Mode 03h - 16 color                              */
-    {&VideoMode_320x200_4color,     0x4000,  8},    /* Mode 04h - CGA 4 color                           */
-    {&VideoMode_320x200_4color,     0x4000,  8},    /* Mode 05h - CGA same (m) (uses 3rd CGA palette)   */
-    {&VideoMode_640x200_2color,     0x4000,  8},    /* Mode 06h - CGA 640*200 2 color                   */
-    {NULL,                          0x1000,  0},    /* Mode 07h - MDA monochrome text 80*25             */
-    {NULL,                          0x0000,  0},    /* Mode 08h - PCjr                                  */
-    {NULL,                          0x0000,  0},    /* Mode 09h - PCjr                                  */
-    {NULL,                          0x0000,  0},    /* Mode 0Ah - PCjr                                  */
-    {NULL,                          0x0000,  0},    /* Mode 0Bh - Reserved                              */
-    {NULL,                          0x0000,  0},    /* Mode 0Ch - Reserved                              */
-    {&VideoMode_320x200_16color,    0x2000,  8},    /* Mode 0Dh - EGA 320*200 16 color                  */
-    {&VideoMode_640x200_16color,    0x4000,  8},    /* Mode 0Eh - EGA 640*200 16 color                  */
-    {NULL,                          0x8000,  0},    /* Mode 0Fh - EGA 640*350 mono                      */
-    {&VideoMode_640x350_16color,    0x8000, 14},    /* Mode 10h - EGA 640*350 HiRes 16 color            */
-    {&VideoMode_640x480_2color,     0xA000, 16},    /* Mode 11h - VGA 640*480 mono                      */
-    {&VideoMode_640x480_16color,    0xA000, 16},    /* Mode 12h - VGA                                   */
-    {&VideoMode_320x200_256color,   0x2000,  8},    /* Mode 13h - VGA                                   */
+    {&VideoMode_40x25_text,         0x0800, 9, 16},    /* Mode 00h - 16 color (mono)                       */
+    {&VideoMode_40x25_text,         0x0800, 9, 16},    /* Mode 01h - 16 color                              */
+    {&VideoMode_80x25_text,         0x1000, 9, 16},    /* Mode 02h - 16 color (mono)                       */
+    {&VideoMode_80x25_text,         0x1000, 9, 16},    /* Mode 03h - 16 color                              */
+    {&VideoMode_320x200_4color,     0x4000, 8,  8},    /* Mode 04h - CGA 4 color                           */
+    {&VideoMode_320x200_4color,     0x4000, 8,  8},    /* Mode 05h - CGA same (m) (uses 3rd CGA palette)   */
+    {&VideoMode_640x200_2color,     0x4000, 8,  8},    /* Mode 06h - CGA 640*200 2 color                   */
+    {NULL,                          0x1000, 1,  1},    /* Mode 07h - MDA monochrome text 80*25             */
+    {NULL,                          0x0000, 1,  1},    /* Mode 08h - PCjr                                  */
+    {NULL,                          0x0000, 1,  1},    /* Mode 09h - PCjr                                  */
+    {NULL,                          0x0000, 1,  1},    /* Mode 0Ah - PCjr                                  */
+    {NULL,                          0x0000, 1,  1},    /* Mode 0Bh - Reserved                              */
+    {NULL,                          0x0000, 1,  1},    /* Mode 0Ch - Reserved                              */
+    {&VideoMode_320x200_16color,    0x2000, 8,  8},    /* Mode 0Dh - EGA 320*200 16 color                  */
+    {&VideoMode_640x200_16color,    0x4000, 8,  8},    /* Mode 0Eh - EGA 640*200 16 color                  */
+    {NULL,                          0x8000, 1,  1},    /* Mode 0Fh - EGA 640*350 mono                      */
+    {&VideoMode_640x350_16color,    0x8000, 8, 14},    /* Mode 10h - EGA 640*350 HiRes 16 color            */
+    {&VideoMode_640x480_2color,     0xA000, 8, 16},    /* Mode 11h - VGA 640*480 mono                      */
+    {&VideoMode_640x480_16color,    0xA000, 8, 16},    /* Mode 12h - VGA                                   */
+    {&VideoMode_320x200_256color,   0x2000, 8,  8},    /* Mode 13h - VGA                                   */
 };
 
+#define IS_TEXT_MODE(ModeNumber)    \
+    (((ModeNumber) >= 0x00 && (ModeNumber) <= 0x03) || ((ModeNumber) == 0x07))
 
 static PVGA_STATIC_FUNC_TABLE VgaStaticFuncTable;
+static BOOLEAN VbeInitialized = FALSE;
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
@@ -1965,6 +1991,13 @@ static BOOLEAN VidBiosScrollWindow(SCROLL_DIRECTION Direction,
 
     WORD WindowWidth, WindowHeight;
 
+    /* TODO: This function doesn't work in non-alphanumeric modes yet */
+    if (Bda->VideoMode > 3)
+    {
+        DPRINT1("VidBiosScrollWindow: not implemented for mode 0%Xh\n", Bda->VideoMode);
+        return FALSE;
+    }
+
     /* Fixup the rectangle if needed */
     Rectangle.Left   = min(max(Rectangle.Left  , 0), Bda->ScreenColumns - 1);
     Rectangle.Right  = min(max(Rectangle.Right , 0), Bda->ScreenColumns - 1);
@@ -2001,10 +2034,10 @@ static BOOLEAN VidBiosScrollWindow(SCROLL_DIRECTION Direction,
             /* Move text lines up */
             for (i = Rectangle.Top + Amount; i <= Rectangle.Bottom; i++)
             {
-                EmulatorWriteMemory(&EmulatorContext,
-                                    VideoAddress + ((i - Amount) * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD),
-                       REAL_TO_PHYS(VideoAddress + ( i           * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD)),
-                                    (Rectangle.Right - Rectangle.Left + 1) * sizeof(WORD));
+                EmulatorCopyMemory(&EmulatorContext,
+                                   VideoAddress + ((i - Amount) * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD),
+                                   VideoAddress + ( i           * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD),
+                                   (Rectangle.Right - Rectangle.Left + 1) * sizeof(WORD));
             }
 
             /* Fill the bottom of the rectangle */
@@ -2029,10 +2062,10 @@ static BOOLEAN VidBiosScrollWindow(SCROLL_DIRECTION Direction,
             /* Move text lines down */
             for (i = Rectangle.Bottom - Amount; i >= Rectangle.Top; i--)
             {
-                EmulatorWriteMemory(&EmulatorContext,
-                                    VideoAddress + ((i + Amount) * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD),
-                       REAL_TO_PHYS(VideoAddress + ( i           * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD)),
-                                    (Rectangle.Right - Rectangle.Left + 1) * sizeof(WORD));
+                EmulatorCopyMemory(&EmulatorContext,
+                                   VideoAddress + ((i + Amount) * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD),
+                                   VideoAddress + ( i           * Bda->ScreenColumns + Rectangle.Left) * sizeof(WORD),
+                                   (Rectangle.Right - Rectangle.Left + 1) * sizeof(WORD));
             }
 
             /* Fill the top of the rectangle */
@@ -2056,10 +2089,10 @@ static BOOLEAN VidBiosScrollWindow(SCROLL_DIRECTION Direction,
             /* Move text lines left */
             for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
             {
-                EmulatorWriteMemory(&EmulatorContext,
-                                    VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left         ) * sizeof(WORD),
-                       REAL_TO_PHYS(VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left + Amount) * sizeof(WORD)),
-                                    (Rectangle.Right - Rectangle.Left - Amount + 1) * sizeof(WORD));
+                EmulatorCopyMemory(&EmulatorContext,
+                                   VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left         ) * sizeof(WORD),
+                                   VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left + Amount) * sizeof(WORD),
+                                   (Rectangle.Right - Rectangle.Left - Amount + 1) * sizeof(WORD));
             }
 
             /* Fill the right of the rectangle */
@@ -2079,19 +2112,21 @@ static BOOLEAN VidBiosScrollWindow(SCROLL_DIRECTION Direction,
 
         case SCROLL_RIGHT:
         {
+            INT Right;
+
             /* Move text lines right */
             for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
             {
-                EmulatorWriteMemory(&EmulatorContext,
-                                    VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left + Amount) * sizeof(WORD),
-                       REAL_TO_PHYS(VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left         ) * sizeof(WORD)),
-                                    (Rectangle.Right - Rectangle.Left - Amount + 1) * sizeof(WORD));
+                EmulatorCopyMemory(&EmulatorContext,
+                                   VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left + Amount) * sizeof(WORD),
+                                   VideoAddress + (i * Bda->ScreenColumns + Rectangle.Left         ) * sizeof(WORD),
+                                   (Rectangle.Right - Rectangle.Left - Amount + 1) * sizeof(WORD));
             }
 
             /* Fill the left of the rectangle */
+            Right = Rectangle.Left + Amount - 1;
             for (i = Rectangle.Top; i <= Rectangle.Bottom; i++)
             {
-                INT Right = Rectangle.Left + Amount - 1;
                 for (j = Rectangle.Left; j <= Right; j++)
                 {
                     EmulatorWriteMemory(&EmulatorContext,
@@ -2120,7 +2155,7 @@ static __inline VOID VgaSetSinglePaletteRegister(BYTE Index, BYTE Value)
 
 static BOOLEAN VgaSetRegisters(PVGA_REGISTERS Registers)
 {
-    INT i;
+    UINT i;
 
     if (Registers == NULL) return FALSE;
 
@@ -2281,21 +2316,14 @@ static VOID VgaChangePalette(BYTE ModeNumber)
     VgaSetPalette(Palette, Size);
 }
 
-static VOID VidBiosGetCursorPosition(PBYTE Row, PBYTE Column, BYTE Page)
+static __inline VOID VidBiosGetCursorPosition(PBYTE Row, PBYTE Column, BYTE Page)
 {
-    /* Make sure the selected video page is valid */
-    if (Page >= BIOS_MAX_PAGES) return;
-
-    /* Get the cursor location */
     *Row    = HIBYTE(Bda->CursorPosition[Page]);
     *Column = LOBYTE(Bda->CursorPosition[Page]);
 }
 
 static VOID VidBiosSetCursorPosition(BYTE Row, BYTE Column, BYTE Page)
 {
-    /* Make sure the selected video page is valid */
-    if (Page >= BIOS_MAX_PAGES) return;
-
     /* Update the position in the BDA */
     Bda->CursorPosition[Page] = MAKEWORD(Column, Row);
 
@@ -2315,7 +2343,7 @@ static VOID VidBiosSetCursorPosition(BYTE Row, BYTE Column, BYTE Page)
 static VOID VidBiosSetCursorShape(WORD CursorStartEnd)
 {
     /* Only valid in text-mode */
-    if ((Bda->VideoMode > 0x03) && (Bda->VideoMode != 0x07)) return;
+    if (!IS_TEXT_MODE(Bda->VideoMode)) return;
 
     /* Update the BDA */
     Bda->CursorStartLine = HIBYTE(CursorStartEnd) & 0x1F;
@@ -2359,14 +2387,14 @@ static VOID VidBiosSetCursorShape(WORD CursorStartEnd)
     IOWriteB(VGA_CRTC_DATA , LOBYTE(CursorStartEnd));
 }
 
-VOID VidBiosSyncCursorPosition(VOID)
+static VOID VidBiosSyncCursorPosition(VOID)
 {
     BYTE Row, Column;
     BYTE Low, High;
-    SHORT ScreenColumns = VgaGetDisplayResolution().X;
+    SHORT ScreenColumns = Bda->ScreenColumns;
     WORD Offset;
 
-    /* Get the cursor location */
+    /* Get the cursor position */
     IOWriteB(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_LOW_REG);
     Low  = IOReadB(VGA_CRTC_DATA);
     IOWriteB(VGA_CRTC_INDEX, VGA_CRTC_CURSOR_LOC_HIGH_REG);
@@ -2387,6 +2415,44 @@ static inline BYTE VidBiosGetVideoMode(VOID)
     return Bda->VideoMode | (Bda->VGAOptions & 0x80);
 }
 
+static inline VOID VidBiosClearScreen(VOID)
+{
+    static const DWORD MemoryMaps[4]  = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
+    static const DWORD MemorySizes[4] = { 0x20000, 0x10000, 0x08000, 0x08000 };
+
+    DWORD VideoAddress;
+    DWORD BufferSize;
+    BYTE Misc;
+    BYTE Buffer[0x20000];
+
+    /* Read the misc register */
+    IOWriteB(VGA_GC_INDEX, VGA_GC_MISC_REG);
+    Misc = IOReadB(VGA_GC_DATA);
+
+    /* Get the video address and buffer size */
+    VideoAddress = MemoryMaps[(Misc >> 2) & 3];
+    BufferSize  = MemorySizes[(Misc >> 2) & 3];
+
+    // !IS_TEXT_MODE(Bda->VideoMode)
+    if (Misc & 1)
+    {
+        /* Graphics mode */
+        RtlZeroMemory(Buffer, BufferSize);
+    }
+    else
+    {
+        /* Text mode */
+        UINT i;
+        for (i = 0; i < (BufferSize >> 1); i++)
+        {
+            ((PWORD)Buffer)[i] = MAKEWORD(' ', DEFAULT_ATTRIBUTE);
+        }
+    }
+
+    /* Write to video memory */
+    EmulatorWriteMemory(&EmulatorContext, VideoAddress, Buffer, BufferSize);
+}
+
 static BOOLEAN VidBiosSetVideoMode(BYTE ModeNumber)
 {
     BYTE Page;
@@ -2442,36 +2508,45 @@ static BOOLEAN VidBiosSetVideoMode(BYTE ModeNumber)
 
     /* Update the screen size */
     Resolution = VgaGetDisplayResolution();
+    // This could be simplified if the VGA helper always returned the resolution
+    // in number of pixels, instead of in number of cells for text-modes only...
+    if (!IS_TEXT_MODE(ModeNumber))
+    {
+        Resolution.X /= VideoModes[ModeNumber].CharacterWidth ;
+        Resolution.Y /= VideoModes[ModeNumber].CharacterHeight;
+    }
     Bda->ScreenColumns = Resolution.X;
     Bda->ScreenRows    = Resolution.Y - 1;
 
-    /* Adjust the number of columns for graphics modes */
-    if (ModeNumber > 3) Bda->ScreenColumns >>= 3;
-
     /* Update the current font */
     Bda->CharacterHeight = VideoModes[ModeNumber].CharacterHeight;
     switch (Bda->CharacterHeight)
     {
         /*
-         * Write the default font to the VGA font plane
-         * and update the BIOS INT 43h vector (far pointer
-         * to the character range 00h-...)
+         * Write the default font to the VGA font plane for text-modes only.
+         * Update the BIOS INT 43h vector (far pointer to the character range 00h-...).
          */
         case 8:
         {
-            VgaWriteFont(0, Font8x8, ARRAYSIZE(Font8x8) / VGA_FONT_CHARACTERS);
+            if (IS_TEXT_MODE(ModeNumber))
+                VgaWriteTextModeFont(0, Font8x8, ARRAYSIZE(Font8x8) / VGA_FONT_CHARACTERS);
+
             ((PULONG)BaseAddress)[0x43] = MAKELONG(FONT_8x8_OFFSET, VIDEO_BIOS_DATA_SEG);
             break;
         }
         case 14:
         {
-            VgaWriteFont(0, Font8x14, ARRAYSIZE(Font8x14) / VGA_FONT_CHARACTERS);
+            if (IS_TEXT_MODE(ModeNumber))
+                VgaWriteTextModeFont(0, Font8x14, ARRAYSIZE(Font8x14) / VGA_FONT_CHARACTERS);
+
             ((PULONG)BaseAddress)[0x43] = MAKELONG(FONT_8x14_OFFSET, VIDEO_BIOS_DATA_SEG);
             break;
         }
         case 16:
         {
-            VgaWriteFont(0, Font8x16, ARRAYSIZE(Font8x16) / VGA_FONT_CHARACTERS);
+            if (IS_TEXT_MODE(ModeNumber))
+                VgaWriteTextModeFont(0, Font8x16, ARRAYSIZE(Font8x16) / VGA_FONT_CHARACTERS);
+
             ((PULONG)BaseAddress)[0x43] = MAKELONG(FONT_8x16_OFFSET, VIDEO_BIOS_DATA_SEG);
             break;
         }
@@ -2495,7 +2570,7 @@ static BOOLEAN VidBiosSetVideoMode(BYTE ModeNumber)
      * Use the default CGA cursor scanline values,
      * see: http://vitaly_filatov.tripod.com/ng/asm/asm_023.2.html
      */
-    if ((ModeNumber >= 0x00 && ModeNumber <= 0x03) || (ModeNumber == 0x07))
+    if (IS_TEXT_MODE(ModeNumber))
         // FIXME: we might read the CRT registers and do the adjustment?
         VidBiosSetCursorShape(MAKEWORD(0x07, 0x06));
 
@@ -2503,30 +2578,7 @@ static BOOLEAN VidBiosSetVideoMode(BYTE ModeNumber)
     for (Page = 0; Page < BIOS_MAX_PAGES; ++Page)
         VidBiosSetCursorPosition(0, 0, Page);
 
-    // HACK: We clear here all the text memory. TODO: Do it better!
-    if (!DoNotClear && ((ModeNumber >= 0x00 && ModeNumber <= 0x03) || (ModeNumber == 0x07)))
-    {
-        INT i, j;
-        DWORD VideoAddress;
-        WORD FillCharacter = MAKEWORD(' ', DEFAULT_ATTRIBUTE);
-
-        for (Page = 0; Page < BIOS_MAX_PAGES; ++Page)
-        {
-            VideoAddress = TO_LINEAR(TEXT_VIDEO_SEG, Page * Bda->VideoPageSize);
-
-            for (i = 0; i <= Bda->ScreenRows; i++)
-            {
-                for (j = 0; j <= Bda->ScreenColumns - 1; j++)
-                {
-                    /* Write to video memory */
-                    EmulatorWriteMemory(&EmulatorContext,
-                                        VideoAddress + (i * Bda->ScreenColumns + j) * sizeof(WORD),
-                                        (LPVOID)&FillCharacter,
-                                        sizeof(FillCharacter));
-                }
-            }
-        }
-    }
+    if (!DoNotClear) VidBiosClearScreen();
 
     /* Refresh display */
     VgaRefreshDisplay();
@@ -2555,11 +2607,11 @@ static BOOLEAN VidBiosSetVideoPage(BYTE PageNumber)
     IOWriteB(VGA_CRTC_DATA , HIBYTE(Bda->VideoPageOffset));
 
     /*
-     * Get the cursor location (we don't update anything on the BIOS side
-     * but we update the cursor location on the VGA side).
+     * Get the cursor position (we don't update anything on the BIOS side
+     * but we update the cursor position on the VGA side).
      */
     VidBiosGetCursorPosition(&Row, &Column, PageNumber);
-    VidBiosSetCursorPosition(Row, Column, PageNumber);
+    VidBiosSetCursorPosition( Row,  Column, PageNumber);
 
     return TRUE;
 }
@@ -2573,6 +2625,7 @@ static VOID VidBiosDrawGlyph(WORD CharData, BOOLEAN UseAttr, BYTE Page, BYTE Row
         case 0x01:
         case 0x02:
         case 0x03:
+        case 0x07:
         {
             EmulatorWriteMemory(&EmulatorContext,
                                 TO_LINEAR(TEXT_VIDEO_SEG,
@@ -2780,11 +2833,11 @@ static VOID VidBiosDrawGlyph(WORD CharData, BOOLEAN UseAttr, BYTE Page, BYTE Row
             WORD i, j;
             PUCHAR Font = (PUCHAR)FAR_POINTER(((PULONG)BaseAddress)[0x43]);
             PUCHAR Glyph = &Font[LOBYTE(CharData) * Bda->CharacterHeight];
-            BYTE PixelBuffer[8];
+            BYTE PixelBuffer[8]; // 8 == CharacterWidth
 
             for (i = 0; i < Bda->CharacterHeight; i++)
             {
-                for (j = 0; j < 8; j++)
+                for (j = 0; j < ARRAYSIZE(PixelBuffer); j++)
                 {
                     PixelBuffer[j] = (Glyph[i] & (1 << (7 - j))) ? HIBYTE(CharData) : 0;
                 }
@@ -2792,7 +2845,7 @@ static VOID VidBiosDrawGlyph(WORD CharData, BOOLEAN UseAttr, BYTE Page, BYTE Row
                 EmulatorWriteMemory(&EmulatorContext,
                                     TO_LINEAR(GRAPHICS_VIDEO_SEG,
                                               ((Row * Bda->CharacterHeight + i)
-                                              * Bda->ScreenColumns + Column) << 3),
+                                              * Bda->ScreenColumns + Column) * 8),
                                     (LPVOID)PixelBuffer,
                                     sizeof(PixelBuffer));
             }
@@ -2807,15 +2860,12 @@ static VOID VidBiosDrawGlyph(WORD CharData, BOOLEAN UseAttr, BYTE Page, BYTE Row
     }
 }
 
-static VOID VidBiosPrintCharacter(CHAR Character, BYTE Attribute, BYTE Page)
- {
+static VOID VidBiosPrintCharacter(CHAR Character, BYTE Attribute, BOOLEAN UseAttr, BYTE Page)
+{
     WORD CharData = MAKEWORD(Character, Attribute);
     BYTE Row, Column;
 
-    /* Make sure the page exists */
-    if (Page >= BIOS_MAX_PAGES) return;
-
-    /* Get the cursor location */
+    /* Get the cursor position */
     VidBiosGetCursorPosition(&Row, &Column, Page);
 
     if (Character == '\a')
@@ -2840,7 +2890,7 @@ static VOID VidBiosPrintCharacter(CHAR Character, BYTE Attribute, BYTE Page)
 
         /* Erase the existing character */
         CharData = MAKEWORD(' ', Attribute);
-        VidBiosDrawGlyph(CharData, TRUE, Page, Row, Column);
+        VidBiosDrawGlyph(CharData, UseAttr, Page, Row, Column);
     }
     else if (Character == '\t')
     {
@@ -2848,7 +2898,7 @@ static VOID VidBiosPrintCharacter(CHAR Character, BYTE Attribute, BYTE Page)
         do
         {
             // Taken from DOSBox
-            VidBiosPrintCharacter(' ', Attribute, Page);
+            VidBiosPrintCharacter(' ', Attribute, UseAttr, Page);
             VidBiosGetCursorPosition(&Row, &Column, Page);
         } while (Column % 8);
     }
@@ -2867,7 +2917,7 @@ static VOID VidBiosPrintCharacter(CHAR Character, BYTE Attribute, BYTE Page)
         /* Default character */
 
         /* Write the character and advance the cursor */
-        VidBiosDrawGlyph(CharData, TRUE, Page, Row, Column);
+        VidBiosDrawGlyph(CharData, UseAttr, Page, Row, Column);
         Column++;
     }
 
@@ -2915,19 +2965,29 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
         /* Set Cursor Position */
         case 0x02:
         {
-            VidBiosSetCursorPosition(getDH(), getDL(), getBH());
+            BYTE Page = getBH();
+
+            /* Validate the selected video page */
+            if (Page >= BIOS_MAX_PAGES) break;
+
+            VidBiosSetCursorPosition(getDH(), getDL(), Page);
             break;
         }
 
         /* Get Cursor Position and Shape */
         case 0x03:
         {
-            /* Make sure the selected video page exists */
-            if (getBH() >= BIOS_MAX_PAGES) break;
+            BYTE Page = getBH();
+
+            /* Validate the selected video page */
+            if (Page == 0xFF) // Special case: use the current video page
+                Page = Bda->VideoPage;
+            else if (Page >= BIOS_MAX_PAGES)
+                break;
 
             /* Return the result */
             setCX(MAKEWORD(Bda->CursorEndLine, Bda->CursorStartLine));
-            setDX(Bda->CursorPosition[getBH()]);
+            setDX(Bda->CursorPosition[Page]);
             break;
         }
 
@@ -2968,8 +3028,11 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
             BYTE  Page = getBH();
             DWORD Offset;
 
-            /* Check if the page exists */
-            if (Page >= BIOS_MAX_PAGES) break;
+            /* Validate the selected video page */
+            if (Page == 0xFF) // Special case: use the current video page
+                Page = Bda->VideoPage;
+            else if (Page >= BIOS_MAX_PAGES)
+                break;
 
             /* Find the offset of the character */
             Offset = Page * Bda->VideoPageSize +
@@ -2999,13 +3062,14 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
             BYTE Page = getBH();
             BYTE Row, Column;
 
-            /* Check if the page exists */
-            if (Page >= BIOS_MAX_PAGES) break;
+            /* Validate the selected video page */
+            if (Page == 0xFF) // Special case: use the current video page
+                Page = Bda->VideoPage;
+            else if (Page >= BIOS_MAX_PAGES)
+                break;
 
-            /* Get the cursor location */
-            // VidBiosGetCursorPosition(&Row, &Column, Page);
-            Row    = HIBYTE(Bda->CursorPosition[Page]);
-            Column = LOBYTE(Bda->CursorPosition[Page]);
+            /* Get the cursor position */
+            VidBiosGetCursorPosition(&Row, &Column, Page);
 
             /* Write to video memory a certain number of times */
             while (Counter-- > 0)
@@ -3117,7 +3181,15 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
         /* Teletype Output */
         case 0x0E:
         {
-            VidBiosPrintCharacter(getAL(), getBL(), getBH());
+            BYTE Page = getBH();
+
+            /* Validate the selected video page */
+            if (Page == 0xFF) // Special case: use the current video page
+                Page = Bda->VideoPage;
+            else if (Page >= BIOS_MAX_PAGES)
+                break;
+
+            VidBiosPrintCharacter(getAL(), getBL(), !IS_TEXT_MODE(Bda->VideoMode), Page);
             break;
         }
 
@@ -3159,7 +3231,7 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 /* Set All Palette Registers */
                 case 0x02:
                 {
-                    INT i;
+                    UINT i;
                     LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
 
                     /* Set the palette registers */
@@ -3243,7 +3315,7 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 /* Get All Palette Registers */
                 case 0x09:
                 {
-                    INT i;
+                    UINT i;
                     LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
 
                     /* Get the palette registers */
@@ -3285,7 +3357,7 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 /* Set Block of DAC Registers */
                 case 0x12:
                 {
-                    INT i;
+                    UINT i;
                     LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
 
                     /* Write the index */
@@ -3320,7 +3392,7 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 /* Get Block of DAC Registers */
                 case 0x17:
                 {
-                    INT i;
+                    UINT i;
                     LPBYTE Buffer = SEG_OFF_TO_PTR(getES(), getDX());
 
                     /* Write the index */
@@ -3338,6 +3410,20 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                     break;
                 }
 
+                /* Set PEL Mask */
+                case 0x18:
+                {
+                    IOWriteB(VGA_DAC_MASK, getBL());
+                    break;
+                }
+
+                /* Get PEL Mask */
+                case 0x19:
+                {
+                    setBL(IOReadB(VGA_DAC_MASK));
+                    break;
+                }
+
                 default:
                 {
                     DPRINT1("BIOS Palette Control Sub-command AL = 0x%02X NOT IMPLEMENTED\n",
@@ -3356,6 +3442,58 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
             {
                 // FIXME: At the moment we support only graphics-mode functions!
 
+                /* Load User-specified Patterns (Character Set) for Text Mode */
+                case 0x00:
+                case 0x10: // FIXME: 0x1x performs a full mode reset
+                {
+                    // FIXME: BL == ??
+
+                    /* Write the default font to the VGA font plane */
+                    // VgaWriteTextModeFont(0, Font8x8, ARRAYSIZE(Font8x8) / VGA_FONT_CHARACTERS);
+
+                    UNIMPLEMENTED;
+                    break;
+                }
+
+                /* Load ROM Monochrome 8x14 Patterns (Character Set) for Text Mode */
+                case 0x01:
+                case 0x11: // FIXME: 0x1x performs a full mode reset
+                {
+                    // FIXME: BL == ??
+
+                    /* Write the default font to the VGA font plane */
+                    VgaWriteTextModeFont(0, Font8x14, ARRAYSIZE(Font8x14) / VGA_FONT_CHARACTERS);
+
+                    UNIMPLEMENTED;
+                    break;
+                }
+
+                /* Load ROM 8x8 Double-dot Patterns (Character Set) for Text Mode */
+                case 0x02:
+                case 0x12: // FIXME: 0x1x performs a full mode reset
+                {
+                    // FIXME: BL == ??
+
+                    /* Write the default font to the VGA font plane */
+                    VgaWriteTextModeFont(0, Font8x8, ARRAYSIZE(Font8x8) / VGA_FONT_CHARACTERS);
+
+                    UNIMPLEMENTED;
+                    break;
+                }
+
+                /* Load ROM 8x16 Character Set for Text Mode */
+                case 0x04:
+                case 0x14: // FIXME: 0x1x performs a full mode reset
+                {
+                    // FIXME: BL == ??
+
+                    /* Write the default font to the VGA font plane */
+                    VgaWriteTextModeFont(0, Font8x16, ARRAYSIZE(Font8x16) / VGA_FONT_CHARACTERS);
+
+                    UNIMPLEMENTED;
+                    break;
+                }
+
                 /* Set User 8x8 Graphics Chars (Setup INT 1Fh Vector) */
                 case 0x20:
                 {
@@ -3368,10 +3506,10 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 /* Set User Graphics Characters */
                 case 0x21:
                 {
-                    // /* Write the font to the VGA font plane */
-                    // VgaWriteFont(0, Font8x8, ARRAYSIZE(Font8x8) / VGA_FONT_CHARACTERS);
-
-                    /* Update the BIOS INT 43h vector */
+                    /*
+                     * Update the BIOS INT 43h vector (far pointer
+                     * to the character range 00h-...)
+                     */
                     ((PULONG)BaseAddress)[0x43] = MAKELONG(getBP(), getES());
 
                     /* Update BDA */
@@ -3392,11 +3530,9 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 case 0x22:
                 {
                     /*
-                     * Write the default font to the VGA font plane
-                     * and update the BIOS INT 43h vector (far pointer
+                     * Update the BIOS INT 43h vector (far pointer
                      * to the character range 00h-...)
                      */
-                    VgaWriteFont(0, Font8x14, ARRAYSIZE(Font8x14) / VGA_FONT_CHARACTERS);
                     ((PULONG)BaseAddress)[0x43] = MAKELONG(FONT_8x14_OFFSET, VIDEO_BIOS_DATA_SEG);
 
                     /* Update BDA */
@@ -3417,11 +3553,9 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 case 0x23:
                 {
                     /*
-                     * Write the default font to the VGA font plane
-                     * and update the BIOS INT 43h vector (far pointer
+                     * Update the BIOS INT 43h vector (far pointer
                      * to the character range 00h-...)
                      */
-                    VgaWriteFont(0, Font8x8, ARRAYSIZE(Font8x8) / VGA_FONT_CHARACTERS);
                     ((PULONG)BaseAddress)[0x43] = MAKELONG(FONT_8x8_OFFSET, VIDEO_BIOS_DATA_SEG);
 
                     /* Update BDA */
@@ -3442,11 +3576,9 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
                 case 0x24:
                 {
                     /*
-                     * Write the default font to the VGA font plane
-                     * and update the BIOS INT 43h vector (far pointer
-                     * to the character range 00h-...)
+                     * Update the BIOS INT 43h vector (far pointer
+                     * to the character range 00h-...).
                      */
-                    VgaWriteFont(0, Font8x16, ARRAYSIZE(Font8x16) / VGA_FONT_CHARACTERS);
                     ((PULONG)BaseAddress)[0x43] = MAKELONG(FONT_8x16_OFFSET, VIDEO_BIOS_DATA_SEG);
 
                     /* Update BDA */
@@ -3606,7 +3738,44 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
         /* Write String */
         case 0x13:
         {
-            DPRINT1("BIOS Function INT 13h (Write String) is UNIMPLEMENTED\n");
+            PCHAR String = (PCHAR)SEG_OFF_TO_PTR(getES(), getBP());
+            WORD Counter = getCX();
+            BYTE Row, Column;
+            BYTE OldRow, OldColumn;
+            CHAR Character;
+            BYTE Attribute = getBL(); // Default attribute in case the string contains only characters.
+            BYTE Page = getBH();
+            BYTE Flags = getAL();
+
+            /* Validate the selected video page */
+            if (Page == 0xFF) // Special case: use the current video page
+                Page = Bda->VideoPage;
+            else if (Page >= BIOS_MAX_PAGES)
+                break;
+
+            /* Get the original cursor position */
+            VidBiosGetCursorPosition(&OldRow, &OldColumn, Page);
+
+            /* Set the new cursor position */
+            Row    = getDH();
+            Column = getDL();
+            if (Row == 0xFF) // Special case: use the current cursor position
+            {
+                Row    = OldRow;
+                Column = OldColumn;
+            }
+            VidBiosSetCursorPosition(Row, Column, Page);
+
+            while (Counter-- > 0)
+            {
+                Character = *String++;
+                if (Flags & 0x02) Attribute = *String++;
+                VidBiosPrintCharacter(Character, Attribute, TRUE, Page);
+            }
+
+            /* Reset the cursor position to its original value if we don't want to update it */
+            if (!(Flags & 0x01)) VidBiosSetCursorPosition(OldRow, OldColumn, Page);
+
             break;
         }
 
@@ -3683,6 +3852,13 @@ VOID WINAPI VidBiosVideoService(LPWORD Stack)
             break;
         }
 
+        /* VESA BIOS Extensions */
+        case 0x4F:
+        {
+            if (VbeInitialized) VbeService(Stack);
+            break;
+        }
+
         default:
         {
             DPRINT1("BIOS Function INT 10h, AH = 0x%02X, AL = 0x%02X, BH = 0x%02X NOT IMPLEMENTED\n",
@@ -3700,29 +3876,35 @@ static BOOL Attached = TRUE;
 
 VOID VidBiosAttachToConsole(VOID)
 {
-    // VgaRefreshDisplay();
     if (!Attached)
     {
         VgaAttachToConsole();
         Attached = TRUE;
     }
 
+    /* Refresh display */
     VgaRefreshDisplay();
     VidBiosSyncCursorPosition();
 }
 
 VOID VidBiosDetachFromConsole(VOID)
 {
-    /* Perform another screen refresh */
+    if (!Attached) return;
+
+    /* Refresh display */
     VgaRefreshDisplay();
 
     /* Detach from the console */
-    VgaDetachFromConsole(FALSE);
+    VgaDetachFromConsole();
     Attached = FALSE;
 }
 
-BOOLEAN VidBiosInitialize(VOID)
+VOID VidBiosPost(VOID)
 {
+    /*
+     * Initialize VGA BIOS32 RAM dynamic data
+     */
+
     /* Some vectors are in fact addresses to tables */
     ((PULONG)BaseAddress)[0x1D] = (ULONG)NULL; // Video Parameter Tables
     // Far pointer to the 8x8 graphics font for the 8x8 characters 80h-FFh
@@ -3735,6 +3917,56 @@ BOOLEAN VidBiosInitialize(VOID)
     ((PULONG)BaseAddress)[0x42] = (ULONG)NULL; // Relocated Default INT 10h Video Services
     ((PULONG)BaseAddress)[0x6D] = (ULONG)NULL; // Video BIOS Entry Point
 
+    //
+    // FIXME: At the moment we always set a VGA mode. In the future,
+    // we should set this mode **only** when:
+    // - an app starts to use directly the video memory
+    //   (that should be done in emulator.c)
+    // - or starts to use non-stream I/O interrupts
+    //   (that should be done here, or maybe in VGA ??)
+    //
+
+    Bda->CrtModeControl      = 0x00;
+    Bda->CrtColorPaletteMask = 0x00;
+    Bda->VGADccIDActive      = 0x08; // VGA w/ color analog active display
+
+    /* Set the default video mode */
+    VidBiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE);
+
+    /* Synchronize our cursor position with VGA */
+    VidBiosSyncCursorPosition();
+
+    /* Register the BIOS 32-bit Interrupts */
+    RegisterBiosInt32(BIOS_VIDEO_INTERRUPT, VidBiosVideoService);
+
+    /* Vectors that should be implemented */
+    RegisterBiosInt32(0x42, NULL); // Relocated Default INT 10h Video Services
+    RegisterBiosInt32(0x6D, NULL); // Video BIOS Entry Point
+
+    /* Initialize VBE */
+    VbeInitialized = VbeInitialize();
+    if (!VbeInitialized) DPRINT1("Couldn't initialize VBE!\n");
+}
+
+BOOLEAN VidBiosInitialize(VOID)
+{
+    UCHAR Checksum;
+
+    /*
+     * Initialize VGA BIOS32 static data
+     */
+
+    /* This is a ROM of size 'VIDEO_BIOS_ROM_SIZE' */
+    *(PWORD)(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, 0x0000)) = 0xAA55;
+    *(PBYTE)(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, 0x0002)) = VIDEO_BIOS_ROM_SIZE / 512; // Size in blocks of 512 bytes
+
+    /* Bootstrap code */
+    *(PWORD)(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, 0x0003)) = 0x90CB; // retf, nop
+    // RtlCopyMemory(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, 0xFFF0), Bootstrap, sizeof(Bootstrap));
+
+    /* Video BIOS Information */
+    RtlCopyMemory(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, 0x0005), BiosInfo, sizeof(BiosInfo)-1);
+
     /* Initialize the VGA static function table */
     VgaStaticFuncTable = SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, VIDEO_STATE_INFO_OFFSET);
     RtlZeroMemory(VgaStaticFuncTable, sizeof(*VgaStaticFuncTable));
@@ -3755,27 +3987,15 @@ BOOLEAN VidBiosInitialize(VOID)
     RtlMoveMemory(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, FONT_8x14_OFFSET),
                   Font8x14, sizeof(Font8x14));
 
-    //
-    // FIXME: At the moment we always set a VGA mode. In the future,
-    // we should set this mode **only** when:
-    // - an app starts to use directly the video memory
-    //   (that should be done in emulator.c)
-    // - or starts to use non-stream I/O interrupts
-    //   (that should be done here, or maybe in VGA ??)
-    //
-
-    Bda->CrtModeControl      = 0x00;
-    Bda->CrtColorPaletteMask = 0x00;
-    Bda->VGADccIDActive      = 0x08; // VGA w/ color analog active display
+    VidBios32Initialize();
 
-    /* Set the default video mode */
-    VidBiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE);
-
-    /* Synchronize our cursor position with VGA */
-    VidBiosSyncCursorPosition();
+    /* Compute the ROM checksum and store it */
+    *(PBYTE)(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, VIDEO_BIOS_ROM_SIZE - 1)) = 0x00;
+    Checksum = CalcRomChecksum(TO_LINEAR(VIDEO_BIOS_DATA_SEG, 0x0000), VIDEO_BIOS_ROM_SIZE);
+    *(PBYTE)(SEG_OFF_TO_PTR(VIDEO_BIOS_DATA_SEG, VIDEO_BIOS_ROM_SIZE - 1)) = (0xFF - Checksum + 1) & 0xFF;
 
-    /* Register the BIOS support BOPs */
-    RegisterBop(BOP_VIDEO_INT, VidBiosVideoService);
+    WriteProtectRom((PVOID)TO_LINEAR(VIDEO_BIOS_DATA_SEG, 0x0000),
+                    VIDEO_BIOS_ROM_SIZE);
 
     return TRUE;
 }