[NTVDM]
[reactos.git] / reactos / subsystems / mvdm / ntvdm / hardware / video / vga.c
index 8e84117..c3e0c19 100644 (file)
@@ -10,6 +10,7 @@
 
 #define NDEBUG
 
 
 #define NDEBUG
 
+#include "ntvdm.h"
 #include "emulator.h"
 #include "vga.h"
 #include <bios/vidbios.h>
 #include "emulator.h"
 #include "vga.h"
 #include <bios/vidbios.h>
@@ -21,7 +22,7 @@
 /* PRIVATE VARIABLES **********************************************************/
 
 static CONST DWORD MemoryBase[]  = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
 /* PRIVATE VARIABLES **********************************************************/
 
 static CONST DWORD MemoryBase[]  = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
-static CONST DWORD MemoryLimit[] = { 0xAFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
+static CONST DWORD MemoryLimit[] = { 0xBFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
 
 /*
  * Activate this line if you want to use the real
 
 /*
  * Activate this line if you want to use the real
@@ -356,26 +357,26 @@ __RegisterConsoleVDM(IN DWORD dwRegisterFlags,
         VDMBufferSize = dwVDMBufferSize;
 
         /* HACK: Cache -- to be removed in the real implementation */
         VDMBufferSize = dwVDMBufferSize;
 
         /* HACK: Cache -- to be removed in the real implementation */
-        CharBuff = HeapAlloc(GetProcessHeap(),
-                             HEAP_ZERO_MEMORY,
-                             VDMBufferSize.X * VDMBufferSize.Y
-                                             * sizeof(CHAR_INFO));
+        CharBuff = RtlAllocateHeap(RtlGetProcessHeap(),
+                                   HEAP_ZERO_MEMORY,
+                                   VDMBufferSize.X * VDMBufferSize.Y
+                                                   * sizeof(*CharBuff));
         ASSERT(CharBuff);
 
         ASSERT(CharBuff);
 
-        VDMBuffer = HeapAlloc(GetProcessHeap(),
-                              HEAP_ZERO_MEMORY,
-                              VDMBufferSize.X * VDMBufferSize.Y
-                                              * sizeof(CHAR_CELL));
+        VDMBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                    HEAP_ZERO_MEMORY,
+                                    VDMBufferSize.X * VDMBufferSize.Y
+                                                    * sizeof(*VDMBuffer));
         *lpVDMBuffer = VDMBuffer;
         return (VDMBuffer != NULL);
     }
     else
     {
         /* HACK: Cache -- to be removed in the real implementation */
         *lpVDMBuffer = VDMBuffer;
         return (VDMBuffer != NULL);
     }
     else
     {
         /* HACK: Cache -- to be removed in the real implementation */
-        if (CharBuff) HeapFree(GetProcessHeap(), 0, CharBuff);
+        if (CharBuff) RtlFreeHeap(RtlGetProcessHeap(), 0, CharBuff);
         CharBuff = NULL;
 
         CharBuff = NULL;
 
-        if (VDMBuffer) HeapFree(GetProcessHeap(), 0, VDMBuffer);
+        if (VDMBuffer) RtlFreeHeap(RtlGetProcessHeap(), 0, VDMBuffer);
         VDMBuffer = NULL;
 
         VDMBufferSize.X = VDMBufferSize.Y = 0;
         VDMBuffer = NULL;
 
         VDMBufferSize.X = VDMBufferSize.Y = 0;
@@ -500,10 +501,10 @@ static BOOL VgaAttachToConsoleInternal(PCOORD Resolution)
     }
 
 #ifdef USE_REAL_REGISTERCONSOLEVDM
     }
 
 #ifdef USE_REAL_REGISTERCONSOLEVDM
-    CharBuff = HeapAlloc(GetProcessHeap(),
-                         HEAP_ZERO_MEMORY,
-                         TextResolution.X * TextResolution.Y
-                                          * sizeof(CHAR_INFO));
+    CharBuff = RtlAllocateHeap(RtlGetProcessHeap(),
+                               HEAP_ZERO_MEMORY,
+                               TextResolution.X * TextResolution.Y
+                                                * sizeof(*CharBuff));
     ASSERT(CharBuff);
 #endif
 
     ASSERT(CharBuff);
 #endif
 
@@ -568,7 +569,7 @@ static BOOL VgaAttachToConsoleInternal(PCOORD Resolution)
     }
 
 #ifdef USE_REAL_REGISTERCONSOLEVDM
     }
 
 #ifdef USE_REAL_REGISTERCONSOLEVDM
-    if (CharBuff) HeapFree(GetProcessHeap(), 0, CharBuff);
+    if (CharBuff) RtlFreeHeap(RtlGetProcessHeap(), 0, CharBuff);
 #endif
 
     VgaUpdateCursorPosition();
 #endif
 
     VgaUpdateCursorPosition();
@@ -621,7 +622,7 @@ static inline DWORD VgaGetAddressSize(VOID)
 
 static inline DWORD VgaTranslateReadAddress(DWORD Address)
 {
 
 static inline DWORD VgaTranslateReadAddress(DWORD Address)
 {
-    DWORD Offset = Address - VgaGetVideoBaseAddress();
+    DWORD Offset = LOWORD(Address - VgaGetVideoBaseAddress());
     BYTE Plane;
 
     /* Check for chain-4 and odd-even mode */
     BYTE Plane;
 
     /* Check for chain-4 and odd-even mode */
@@ -643,12 +644,14 @@ static inline DWORD VgaTranslateReadAddress(DWORD Address)
         Plane = VgaGcRegisters[VGA_GC_READ_MAP_SEL_REG] & 0x03;
     }
 
         Plane = VgaGcRegisters[VGA_GC_READ_MAP_SEL_REG] & 0x03;
     }
 
-    return Offset + Plane * VGA_BANK_SIZE;
+    /* Return the offset on plane 0 for read mode 1 */
+    if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_READ) return Offset;
+    else return Offset + Plane * VGA_BANK_SIZE;
 }
 
 static inline DWORD VgaTranslateWriteAddress(DWORD Address)
 {
 }
 
 static inline DWORD VgaTranslateWriteAddress(DWORD Address)
 {
-    DWORD Offset = Address - VgaGetVideoBaseAddress();
+    DWORD Offset = LOWORD(Address - VgaGetVideoBaseAddress());
 
     /* Check for chain-4 and odd-even mode */
     if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
 
     /* Check for chain-4 and odd-even mode */
     if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
@@ -773,14 +776,14 @@ static BOOLEAN VgaInitializePalette(VOID)
     LPLOGPALETTE Palette, TextPalette;
 
     /* Allocate storage space for the palettes */
     LPLOGPALETTE Palette, TextPalette;
 
     /* Allocate storage space for the palettes */
-    Palette = (LPLOGPALETTE)HeapAlloc(GetProcessHeap(),
-                                      HEAP_ZERO_MEMORY,
-                                      sizeof(LOGPALETTE) +
-                                      VGA_MAX_COLORS * sizeof(PALETTEENTRY));
-    TextPalette = (LPLOGPALETTE)HeapAlloc(GetProcessHeap(),
-                                          HEAP_ZERO_MEMORY,
-                                          sizeof(LOGPALETTE) + 
-                                          (VGA_AC_PAL_F_REG + 1) * sizeof(PALETTEENTRY));
+    Palette = RtlAllocateHeap(RtlGetProcessHeap(),
+                              HEAP_ZERO_MEMORY,
+                              sizeof(LOGPALETTE) +
+                                  VGA_MAX_COLORS * sizeof(PALETTEENTRY));
+    TextPalette = RtlAllocateHeap(RtlGetProcessHeap(),
+                                  HEAP_ZERO_MEMORY,
+                                  sizeof(LOGPALETTE) + 
+                                      (VGA_AC_PAL_F_REG + 1) * sizeof(PALETTEENTRY));
     if ((Palette == NULL) || (TextPalette == NULL)) goto Cleanup;
 
     /* Initialize the palettes */
     if ((Palette == NULL) || (TextPalette == NULL)) goto Cleanup;
 
     /* Initialize the palettes */
@@ -813,8 +816,8 @@ static BOOLEAN VgaInitializePalette(VOID)
 
 Cleanup:
     /* Free the palettes */
 
 Cleanup:
     /* Free the palettes */
-    if (Palette) HeapFree(GetProcessHeap(), 0, Palette);
-    if (TextPalette) HeapFree(GetProcessHeap(), 0, TextPalette);
+    if (Palette) RtlFreeHeap(RtlGetProcessHeap(), 0, Palette);
+    if (TextPalette) RtlFreeHeap(RtlGetProcessHeap(), 0, TextPalette);
 
     if (!Result)
     {
 
     if (!Result)
     {
@@ -1899,24 +1902,56 @@ VOID VgaRefreshDisplay(VOID)
 
 VOID NTAPI VgaReadMemory(ULONG Address, PVOID Buffer, ULONG Size)
 {
 
 VOID NTAPI VgaReadMemory(ULONG Address, PVOID Buffer, ULONG Size)
 {
-    DWORD i;
+    DWORD i, j;
     DWORD VideoAddress;
     PUCHAR BufPtr = (PUCHAR)Buffer;
 
     DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n", Address, Size);
     DWORD VideoAddress;
     PUCHAR BufPtr = (PUCHAR)Buffer;
 
     DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n", Address, Size);
-    Address = min(max(Address, VgaGetVideoBaseAddress()), VgaGetVideoLimitAddress());
-    Size = min(Size, VgaGetVideoLimitAddress() - Address + 1);
 
     /* Ignore if video RAM access is disabled */
     if ((VgaMiscRegister & VGA_MISC_RAM_ENABLED) == 0) return;
 
 
     /* Ignore if video RAM access is disabled */
     if ((VgaMiscRegister & VGA_MISC_RAM_ENABLED) == 0) return;
 
-    /* Loop through each byte */
-    for (i = 0; i < Size; i++)
+    if (!(VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_READ))
+    {
+        /* Loop through each byte */
+        for (i = 0; i < Size; i++)
+        {
+            VideoAddress = VgaTranslateReadAddress(Address + i);
+    
+            /* Copy the value to the buffer */
+            BufPtr[i] = VgaMemory[VideoAddress];
+        }
+    }
+    else
     {
     {
-        VideoAddress = VgaTranslateReadAddress(Address + i);
+        /* Loop through each byte */
+        for (i = 0; i < Size; i++)
+        {
+            BYTE Result = 0xFF;
+
+            /* This should always return a plane 0 address for read mode 1 */
+            VideoAddress = VgaTranslateReadAddress(Address + i);
+
+            for (j = 0; j < VGA_NUM_BANKS; j++)
+            {
+                /* Don't consider ignored banks */
+                if (!(VgaGcRegisters[VGA_GC_COLOR_IGNORE_REG] & (1 << j))) continue;
+
+                if (VgaGcRegisters[VGA_GC_COLOR_COMPARE_REG] & (1 << j))
+                {
+                    /* Comparing with 11111111 */
+                    Result &= VgaMemory[j * VGA_BANK_SIZE + LOWORD(VideoAddress)];
+                }
+                else
+                {
+                    /* Comparing with 00000000 */
+                    Result &= ~(VgaMemory[j * VGA_BANK_SIZE + LOWORD(VideoAddress)]);
+                }
+            }
 
 
-        /* Copy the value to the buffer */
-        BufPtr[i] = VgaMemory[VideoAddress];
+            /* Copy the value to the buffer */
+            BufPtr[i] = Result;
+        }
     }
 
     /* Load the latch registers */
     }
 
     /* Load the latch registers */
@@ -1933,8 +1968,6 @@ BOOLEAN NTAPI VgaWriteMemory(ULONG Address, PVOID Buffer, ULONG Size)
     PUCHAR BufPtr = (PUCHAR)Buffer;
 
     DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n", Address, Size);
     PUCHAR BufPtr = (PUCHAR)Buffer;
 
     DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n", Address, Size);
-    Address = min(max(Address, VgaGetVideoBaseAddress()), VgaGetVideoLimitAddress());
-    Size = min(Size, VgaGetVideoLimitAddress() - Address + 1);
 
     /* Ignore if video RAM access is disabled */
     if ((VgaMiscRegister & VGA_MISC_RAM_ENABLED) == 0) return TRUE;
 
     /* Ignore if video RAM access is disabled */
     if ((VgaMiscRegister & VGA_MISC_RAM_ENABLED) == 0) return TRUE;