[NTVDM]
[reactos.git] / subsystems / ntvdm / emulator.c
index 4e8f9e8..0732ebb 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "emulator.h"
 
+#include "clock.h"
 #include "bios/bios.h"
 #include "hardware/cmos.h"
 #include "hardware/pic.h"
 #include "vddsup.h"
 #include "io.h"
 
+#include <isvbop.h>
+
 /* PRIVATE VARIABLES **********************************************************/
 
 FAST486_STATE EmulatorContext;
+BOOLEAN CpuSimulate = FALSE;
+
+/* No more than 'MaxCpuCallLevel' recursive CPU calls are allowed */
+const static INT MaxCpuCallLevel = 32;
+static INT CpuCallLevel = 0;
+
 LPVOID  BaseAddress = NULL;
 BOOLEAN VdmRunning  = TRUE;
 
-static BOOLEAN A20Line = FALSE;
+static BOOLEAN A20Line   = FALSE;
 static BYTE Port61hState = 0x00;
 
+static HANDLE InputThread = NULL;
+
 LPCWSTR ExceptionName[] =
 {
     L"Division By Zero",
@@ -54,6 +65,10 @@ VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer
 {
     UNREFERENCED_PARAMETER(State);
 
+    // BIG HACK!!!! To make BIOS images working correctly,
+    // until Aleksander rewrites memory management!!
+    if (Address >= 0xFFFFFFF0) Address -= 0xFFF00000;
+
     /* If the A20 line is disabled, mask bit 20 */
     if (!A20Line) Address &= ~(1 << 20);
 
@@ -70,20 +85,24 @@ VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer
         DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
         DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
                            - VgaAddress + 1;
-        LPBYTE DestBuffer = (LPBYTE)((ULONG_PTR)BaseAddress + VgaAddress);
+        LPBYTE DestBuffer = (LPBYTE)REAL_TO_PHYS(VgaAddress);
 
         /* Read from the VGA memory */
         VgaReadMemory(VgaAddress, DestBuffer, ActualSize);
     }
 
     /* Read the data from the virtual address space and store it in the buffer */
-    RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
+    RtlCopyMemory(Buffer, REAL_TO_PHYS(Address), Size);
 }
 
 VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 {
     UNREFERENCED_PARAMETER(State);
 
+    // BIG HACK!!!! To make BIOS images working correctly,
+    // until Aleksander rewrites memory management!!
+    if (Address >= 0xFFFFFFF0) Address -= 0xFFF00000;
+
     /* If the A20 line is disabled, mask bit 20 */
     if (!A20Line) Address &= ~(1 << 20);
 
@@ -94,7 +113,7 @@ VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffe
     if ((Address + Size) >= ROM_AREA_START && (Address < ROM_AREA_END)) return;
 
     /* Read the data from the buffer and store it in the virtual address space */
-    RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
+    RtlCopyMemory(REAL_TO_PHYS(Address), Buffer, Size);
 
     /*
      * Check if we modified the VGA memory.
@@ -105,7 +124,7 @@ VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffe
         DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
         DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
                            - VgaAddress + 1;
-        LPBYTE SrcBuffer = (LPBYTE)((ULONG_PTR)BaseAddress + VgaAddress);
+        LPBYTE SrcBuffer = (LPBYTE)REAL_TO_PHYS(VgaAddress);
 
         /* Write to the VGA memory */
         VgaWriteMemory(VgaAddress, SrcBuffer, ActualSize);
@@ -120,12 +139,112 @@ UCHAR WINAPI EmulatorIntAcknowledge(PFAST486_STATE State)
     return PicGetInterrupt();
 }
 
-VOID WINAPI EmulatorDebugBreak(LPWORD Stack)
+VOID EmulatorException(BYTE ExceptionNumber, LPWORD Stack)
+{
+    WORD CodeSegment, InstructionPointer;
+    PBYTE Opcode;
+
+    ASSERT(ExceptionNumber < 8);
+
+    /* Get the CS:IP */
+    InstructionPointer = Stack[STACK_IP];
+    CodeSegment = Stack[STACK_CS];
+    Opcode = (PBYTE)SEG_OFF_TO_PTR(CodeSegment, InstructionPointer);
+
+    /* Display a message to the user */
+    DisplayMessage(L"Exception: %s occured at %04X:%04X\n"
+                   L"Opcode: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
+                   ExceptionName[ExceptionNumber],
+                   CodeSegment,
+                   InstructionPointer,
+                   Opcode[0],
+                   Opcode[1],
+                   Opcode[2],
+                   Opcode[3],
+                   Opcode[4],
+                   Opcode[5],
+                   Opcode[6],
+                   Opcode[7],
+                   Opcode[8],
+                   Opcode[9]);
+
+    /* Stop the VDM */
+    VdmRunning = FALSE;
+    return;
+}
+
+// FIXME: This function assumes 16-bit mode!!!
+VOID EmulatorExecute(WORD Segment, WORD Offset)
+{
+    /* Tell Fast486 to move the instruction pointer */
+    Fast486ExecuteAt(&EmulatorContext, Segment, Offset);
+}
+
+VOID EmulatorStep(VOID)
+{
+    /* Dump the state for debugging purposes */
+    // Fast486DumpState(&EmulatorContext);
+
+    /* Execute the next instruction */
+    Fast486StepInto(&EmulatorContext);
+}
+
+VOID EmulatorSimulate(VOID)
+{
+    if (CpuCallLevel > MaxCpuCallLevel)
+    {
+        DisplayMessage(L"Too many CPU levels of recursion (%d, expected maximum %d)",
+                       CpuCallLevel, MaxCpuCallLevel);
+
+        /* Stop the VDM */
+        VdmRunning = FALSE;
+        return;
+    }
+    CpuCallLevel++;
+
+    CpuSimulate = TRUE;
+    while (VdmRunning && CpuSimulate) ClockUpdate();
+
+    CpuCallLevel--;
+    if (CpuCallLevel < 0) CpuCallLevel = 0;
+
+    /* This takes into account for reentrance */
+    CpuSimulate = TRUE;
+}
+
+VOID EmulatorUnsimulate(VOID)
+{
+    /* Stop simulation */
+    CpuSimulate = FALSE;
+}
+
+VOID EmulatorInterrupt(BYTE Number)
+{
+    /* Call the Fast486 API */
+    Fast486Interrupt(&EmulatorContext, Number);
+}
+
+VOID EmulatorInterruptSignal(VOID)
+{
+    /* Call the Fast486 API */
+    Fast486InterruptSignal(&EmulatorContext);
+}
+
+VOID EmulatorSetA20(BOOLEAN Enabled)
+{
+    A20Line = Enabled;
+}
+
+VOID WINAPI EmulatorDebugBreakBop(LPWORD Stack)
 {
     DPRINT1("NTVDM: BOP_DEBUGGER\n");
     DebugBreak();
 }
 
+VOID WINAPI EmulatorUnsimulateBop(LPWORD Stack)
+{
+    EmulatorUnsimulate();
+}
 
 static BYTE WINAPI Port61hRead(ULONG Port)
 {
@@ -211,6 +330,8 @@ static VOID WINAPI PitChan2Out(LPVOID Param, BOOLEAN State)
 
 /* PUBLIC FUNCTIONS ***********************************************************/
 
+DWORD WINAPI PumpConsoleInput(LPVOID Parameter);
+
 BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput)
 {
     /* Allocate memory for the 16-bit address space */
@@ -224,6 +345,13 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput)
     /* Initialize I/O ports */
     /* Initialize RAM */
 
+    /* Initialize the internal clock */
+    if (!ClockInitialize())
+    {
+        wprintf(L"FATAL: Failed to initialize the clock\n");
+        return FALSE;
+    }
+
     /* Initialize the CPU */
     Fast486Initialize(&EmulatorContext,
                       EmulatorReadMemory,
@@ -235,8 +363,7 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput)
                       EmulatorIntAcknowledge,
                       NULL /* TODO: Use a TLB */);
 
-    /* Enable interrupts */
-    setIF(1);
+    /* Initialize DMA */
 
     /* Initialize the PIC, the PIT, the CMOS and the PC Speaker */
     PicInitialize();
@@ -258,12 +385,18 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput)
     /* Set the console input mode */
     // SetConsoleMode(ConsoleInput, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
 
+    /* Start the input thread */
+    InputThread = CreateThread(NULL, 0, &PumpConsoleInput, ConsoleInput, 0, NULL);
+    // if (InputThread == NULL) return FALSE;
+
     /* Initialize the VGA */
     // if (!VgaInitialize(ConsoleOutput)) return FALSE;
     VgaInitialize(ConsoleOutput);
 
-    /* Register the DebugBreak BOP */
-    RegisterBop(BOP_DEBUGGER, EmulatorDebugBreak);
+    /* Initialize the software callback system and register the emulator BOPs */
+    InitializeCallbacks();
+    RegisterBop(BOP_DEBUGGER  , EmulatorDebugBreakBop);
+    RegisterBop(BOP_UNSIMULATE, EmulatorUnsimulateBop);
 
     /* Initialize VDD support */
     VDDSupInitialize();
@@ -274,6 +407,11 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput)
 VOID EmulatorCleanup(VOID)
 {
     // VgaCleanup();
+
+    /* Close the input thread handle */
+    if (InputThread != NULL) CloseHandle(InputThread);
+    InputThread = NULL;
+
     PS2Cleanup();
 
     SpeakerCleanup();
@@ -287,75 +425,15 @@ VOID EmulatorCleanup(VOID)
     if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
 }
 
-VOID EmulatorException(BYTE ExceptionNumber, LPWORD Stack)
-{
-    WORD CodeSegment, InstructionPointer;
-    PBYTE Opcode;
-
-    ASSERT(ExceptionNumber < 8);
-
-    /* Get the CS:IP */
-    InstructionPointer = Stack[STACK_IP];
-    CodeSegment = Stack[STACK_CS];
-    Opcode = (PBYTE)SEG_OFF_TO_PTR(CodeSegment, InstructionPointer);
-
-    /* Display a message to the user */
-    DisplayMessage(L"Exception: %s occured at %04X:%04X\n"
-                   L"Opcode: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
-                   ExceptionName[ExceptionNumber],
-                   CodeSegment,
-                   InstructionPointer,
-                   Opcode[0],
-                   Opcode[1],
-                   Opcode[2],
-                   Opcode[3],
-                   Opcode[4],
-                   Opcode[5],
-                   Opcode[6],
-                   Opcode[7],
-                   Opcode[8],
-                   Opcode[9]);
-
-    /* Stop the VDM */
-    VdmRunning = FALSE;
-    return;
-}
-
-// FIXME: This function assumes 16-bit mode!!!
-VOID EmulatorExecute(WORD Segment, WORD Offset)
-{
-    /* Tell Fast486 to move the instruction pointer */
-    Fast486ExecuteAt(&EmulatorContext, Segment, Offset);
-}
-
-VOID EmulatorInterrupt(BYTE Number)
-{
-    /* Call the Fast486 API */
-    Fast486Interrupt(&EmulatorContext, Number);
-}
-
-VOID EmulatorInterruptSignal(VOID)
-{
-    /* Call the Fast486 API */
-    Fast486InterruptSignal(&EmulatorContext);
-}
-
-VOID EmulatorStep(VOID)
-{
-    /* Dump the state for debugging purposes */
-    // Fast486DumpState(&EmulatorContext);
 
-    /* Execute the next instruction */
-    Fast486StepInto(&EmulatorContext);
-}
 
-VOID EmulatorSetA20(BOOLEAN Enabled)
+VOID
+WINAPI
+VDDSimulate16(VOID)
 {
-    A20Line = Enabled;
+    EmulatorSimulate();
 }
 
-
-
 VOID
 WINAPI
 VDDTerminateVDM(VOID)