[NTVDM]: Implement and export VDDTerminateVDM.
[reactos.git] / subsystems / ntvdm / emulator.c
index 501026c..49a52fb 100644 (file)
 #include "emulator.h"
 #include "bios.h"
 #include "bop.h"
-#include "dos.h"
-#include "speaker.h"
+#include "vddsup.h"
+#include "io.h"
+#include "registers.h"
 #include "vga.h"
 #include "pic.h"
-#include "ps2.h"
-#include "timer.h"
-#include "cmos.h"
+
+// HACK
+typedef INT VDM_MODE;
 
 /* PRIVATE VARIABLES **********************************************************/
 
@@ -27,9 +28,12 @@ FAST486_STATE EmulatorContext;
 
 static BOOLEAN A20Line = FALSE;
 
+/* BOP Identifiers */
+#define BOP_DEBUGGER    0x56    // Break into the debugger from a 16-bit app
+
 /* PRIVATE FUNCTIONS **********************************************************/
 
-static VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
+VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 {
     UNREFERENCED_PARAMETER(State);
 
@@ -39,22 +43,27 @@ static VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID
     /* Make sure the requested address is valid */
     if ((Address + Size) >= MAX_ADDRESS) return;
 
-    /* Read the data from the virtual address space and store it in the buffer */
-    RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
-
-    /* Check if we modified the console video memory */
+    /*
+     * Check if we are going to read the VGA memory and
+     * copy it into the virtual address space if needed.
+     */
     if (((Address + Size) >= VgaGetVideoBaseAddress())
         && (Address < VgaGetVideoLimitAddress()))
     {
         DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
-        LPBYTE VgaBuffer = (LPBYTE)((ULONG_PTR)Buffer + VgaAddress - Address);
+        DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
+                           - VgaAddress + 1;
+        LPBYTE DestBuffer = (LPBYTE)((ULONG_PTR)BaseAddress + VgaAddress);
 
         /* Read from the VGA memory */
-        VgaReadMemory(VgaAddress, VgaBuffer, Size);
+        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);
 }
 
-static VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
+VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 {
     UNREFERENCED_PARAMETER(State);
 
@@ -70,228 +79,34 @@ static VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOI
     /* Read the data from the buffer and store it in the virtual address space */
     RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
 
-    /* Check if we modified the console video memory */
+    /*
+     * Check if we modified the VGA memory.
+     */
     if (((Address + Size) >= VgaGetVideoBaseAddress())
         && (Address < VgaGetVideoLimitAddress()))
     {
         DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
-        LPBYTE VgaBuffer = (LPBYTE)((ULONG_PTR)Buffer + VgaAddress - Address);
+        DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
+                           - VgaAddress + 1;
+        LPBYTE SrcBuffer = (LPBYTE)((ULONG_PTR)BaseAddress + VgaAddress);
 
         /* Write to the VGA memory */
-        VgaWriteMemory(VgaAddress, VgaBuffer, Size);
+        VgaWriteMemory(VgaAddress, SrcBuffer, ActualSize);
     }
 }
 
-static VOID WINAPI EmulatorReadIo(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Size, UCHAR Width)
+UCHAR WINAPI EmulatorIntAcknowledge(PFAST486_STATE State)
 {
-    INT i, j;
-    LPBYTE Address = (LPBYTE)Buffer;
-
-    UNREFERENCED_PARAMETER(State);
-
-    for (i = 0; i < Size; i++) for (j = 0; j < Width; j++)
-    {
-        ULONG CurrentPort = Port + j;
-
-        switch (CurrentPort)
-        {
-            case PIC_MASTER_CMD:
-            case PIC_SLAVE_CMD:
-            {
-                *(Address++) = PicReadCommand(CurrentPort);
-                break;
-            }
-
-            case PIC_MASTER_DATA:
-            case PIC_SLAVE_DATA:
-            {
-                *(Address++) = PicReadData(CurrentPort);
-                break;
-            }
-
-            case PIT_DATA_PORT(0):
-            case PIT_DATA_PORT(1):
-            case PIT_DATA_PORT(2):
-            {
-                *(Address++) = PitReadData(CurrentPort - PIT_DATA_PORT(0));
-                break;
-            }
-
-            case PS2_CONTROL_PORT:
-            {
-                *(Address++) = KeyboardReadStatus();
-                break;
-            }
-
-            case PS2_DATA_PORT:
-            {
-                *(Address++) = KeyboardReadData();
-                break;
-            }
-
-            case CMOS_DATA_PORT:
-            {
-                *(Address++) = CmosReadData();
-                break;
-            }
-
-            case SPEAKER_CONTROL_PORT:
-            {
-                *(Address++) = SpeakerReadStatus();
-                break;
-            }
-
-            case VGA_AC_WRITE:
-            case VGA_AC_READ:
-            case VGA_SEQ_INDEX:
-            case VGA_SEQ_DATA:
-            case VGA_DAC_READ_INDEX:
-            case VGA_DAC_WRITE_INDEX:
-            case VGA_DAC_DATA:
-            case VGA_MISC_READ:
-            case VGA_MISC_WRITE:
-            case VGA_CRTC_INDEX:
-            case VGA_CRTC_DATA:
-            case VGA_GC_INDEX:
-            case VGA_GC_DATA:
-            case VGA_STAT_MONO:
-            case VGA_STAT_COLOR:
-            {
-                *(Address++) = VgaReadPort(CurrentPort);
-                break;
-            }
-
-            default:
-            {
-                DPRINT1("Read from unknown port: 0x%X\n", CurrentPort);
-            }
-        }
-    }
-}
-
-static VOID WINAPI EmulatorWriteIo(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Size, UCHAR Width)
-{
-    INT i, j;
-    LPBYTE Address = (LPBYTE)Buffer;
-
     UNREFERENCED_PARAMETER(State);
 
-    for (i = 0; i < Size; i++) for (j = 0; j < Width; j++)
-    {
-        ULONG CurrentPort = Port + j;
-
-        switch (CurrentPort)
-        {
-            case PIT_COMMAND_PORT:
-            {
-                PitWriteCommand(*(Address++));
-                break;
-            }
-
-            case PIT_DATA_PORT(0):
-            case PIT_DATA_PORT(1):
-            case PIT_DATA_PORT(2):
-            {
-                PitWriteData(CurrentPort - PIT_DATA_PORT(0), *(Address++));
-                break;
-            }
-
-            case PIC_MASTER_CMD:
-            case PIC_SLAVE_CMD:
-            {
-                PicWriteCommand(CurrentPort, *(Address++));
-                break;
-            }
-
-            case PIC_MASTER_DATA:
-            case PIC_SLAVE_DATA:
-            {
-                PicWriteData(CurrentPort, *(Address++));
-                break;
-            }
-
-            case PS2_CONTROL_PORT:
-            {
-                KeyboardWriteCommand(*(Address++));
-                break;
-            }
-
-            case PS2_DATA_PORT:
-            {
-                KeyboardWriteData(*(Address++));
-                break;
-            }
-
-            case CMOS_ADDRESS_PORT:
-            {
-                CmosWriteAddress(*(Address++));
-                break;
-            }
-
-            case CMOS_DATA_PORT:
-            {
-                CmosWriteData(*(Address++));
-                break;
-            }
-
-            case SPEAKER_CONTROL_PORT:
-            {
-                SpeakerWriteCommand(*(Address++));
-                break;
-            }
-
-            case VGA_AC_WRITE:
-            case VGA_AC_READ:
-            case VGA_SEQ_INDEX:
-            case VGA_SEQ_DATA:
-            case VGA_DAC_READ_INDEX:
-            case VGA_DAC_WRITE_INDEX:
-            case VGA_DAC_DATA:
-            case VGA_MISC_READ:
-            case VGA_MISC_WRITE:
-            case VGA_CRTC_INDEX:
-            case VGA_CRTC_DATA:
-            case VGA_GC_INDEX:
-            case VGA_GC_DATA:
-            case VGA_STAT_MONO:
-            case VGA_STAT_COLOR:
-            {
-                VgaWritePort(CurrentPort, *(Address++));
-                break;
-            }
-
-            default:
-            {
-                DPRINT1("Write to unknown port: 0x%X\n", CurrentPort);
-            }
-        }
-    }
-}
-
-static VOID WINAPI EmulatorBiosOperation(PFAST486_STATE State, UCHAR BopCode)
-{
-    WORD StackSegment, StackPointer;
-    LPWORD Stack;
-
-    /* Get the SS:SP */
-    StackSegment = State->SegmentRegs[FAST486_REG_SS].Selector;
-    StackPointer = State->GeneralRegs[FAST486_REG_ESP].LowWord;
-
-    /* Get the stack */
-    Stack = (LPWORD)SEG_OFF_TO_PTR(StackSegment, StackPointer);
-
-    if (BopProc[BopCode] != NULL)
-        BopProc[BopCode](Stack);
-    else
-        DPRINT1("Invalid BOP code %u\n", BopCode);
+    /* Get the interrupt number from the PIC */
+    return PicGetInterrupt();
 }
 
-static UCHAR WINAPI EmulatorIntAcknowledge(PFAST486_STATE State)
+VOID WINAPI EmulatorDebugBreak(LPWORD Stack)
 {
-    UNREFERENCED_PARAMETER(State);
-
-    /* Get the interrupt number from the PIC */
-    return PicGetInterrupt();
+    DPRINT1("NTVDM: BOP_DEBUGGER\n");
+    DebugBreak();
 }
 
 /* PUBLIC FUNCTIONS ***********************************************************/
@@ -310,10 +125,17 @@ BOOLEAN EmulatorInitialize(VOID)
                       EmulatorWriteIo,
                       NULL,
                       EmulatorBiosOperation,
-                      EmulatorIntAcknowledge);
+                      EmulatorIntAcknowledge,
+                      NULL /* TODO: Use a TLB */);
 
     /* Enable interrupts */
-    EmulatorSetFlag(EMULATOR_FLAG_IF);
+    setIF(1);
+
+    /* Initialize VDD support */
+    VDDSupInitialize();
+
+    /* Register the DebugBreak BOP */
+    RegisterBop(BOP_DEBUGGER, EmulatorDebugBreak);
 
     return TRUE;
 }
@@ -324,11 +146,6 @@ VOID EmulatorCleanup(VOID)
     if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
 }
 
-VOID EmulatorSetStack(WORD Segment, DWORD Offset)
-{
-    Fast486SetStack(&EmulatorContext, Segment, Offset);
-}
-
 // FIXME: This function assumes 16-bit mode!!!
 VOID EmulatorExecute(WORD Segment, WORD Offset)
 {
@@ -348,62 +165,90 @@ VOID EmulatorInterruptSignal(VOID)
     Fast486InterruptSignal(&EmulatorContext);
 }
 
-ULONG EmulatorGetRegister(ULONG Register)
+VOID EmulatorStep(VOID)
 {
-    if (Register < EMULATOR_REG_ES)
-    {
-        return EmulatorContext.GeneralRegs[Register].Long;
-    }
-    else
-    {
-        return EmulatorContext.SegmentRegs[Register - EMULATOR_REG_ES].Selector;
-    }
+    /* Dump the state for debugging purposes */
+    // Fast486DumpState(&EmulatorContext);
+
+    /* Execute the next instruction */
+    Fast486StepInto(&EmulatorContext);
 }
 
-VOID EmulatorSetRegister(ULONG Register, ULONG Value)
+VOID EmulatorSetA20(BOOLEAN Enabled)
 {
-    if (Register < EMULATOR_REG_ES)
-    {
-        EmulatorContext.GeneralRegs[Register].Long = Value;
-    }
-    else
-    {
-        Fast486SetSegment(&EmulatorContext, Register - EMULATOR_REG_ES, (USHORT)Value);
-    }
+    A20Line = Enabled;
 }
 
-ULONG EmulatorGetProgramCounter(VOID)
+
+
+VOID
+WINAPI
+VDDTerminateVDM(VOID)
 {
-    return EmulatorContext.InstPtr.Long;
+    /* Stop the VDM */
+    VdmRunning = FALSE;
 }
 
-BOOLEAN EmulatorGetFlag(ULONG Flag)
+PBYTE
+WINAPI
+Sim32pGetVDMPointer(IN ULONG   Address,
+                    IN BOOLEAN ProtectedMode)
 {
-    return (EmulatorContext.Flags.Long & Flag) ? TRUE : FALSE;
+    // FIXME
+    UNREFERENCED_PARAMETER(ProtectedMode);
+
+    /*
+     * HIWORD(Address) == Segment  (if ProtectedMode == FALSE)
+     *                 or Selector (if ProtectedMode == TRUE )
+     * LOWORD(Address) == Offset
+     */
+    return (PBYTE)FAR_POINTER(Address);
 }
 
-VOID EmulatorSetFlag(ULONG Flag)
+PBYTE
+WINAPI
+MGetVdmPointer(IN ULONG   Address,
+               IN ULONG   Size,
+               IN BOOLEAN ProtectedMode)
 {
-    EmulatorContext.Flags.Long |= Flag;
+    UNREFERENCED_PARAMETER(Size);
+    return Sim32pGetVDMPointer(Address, ProtectedMode);
 }
 
-VOID EmulatorClearFlag(ULONG Flag)
+PVOID
+WINAPI
+VdmMapFlat(IN USHORT   Segment,
+           IN ULONG    Offset,
+           IN VDM_MODE Mode)
 {
-    EmulatorContext.Flags.Long &= ~Flag;
+    // FIXME
+    UNREFERENCED_PARAMETER(Mode);
+
+    return SEG_OFF_TO_PTR(Segment, Offset);
 }
 
-VOID EmulatorStep(VOID)
+BOOL 
+WINAPI
+VdmFlushCache(IN USHORT   Segment,
+              IN ULONG    Offset,
+              IN ULONG    Size,
+              IN VDM_MODE Mode)
 {
-    /* Dump the state for debugging purposes */
-    // Fast486DumpState(&EmulatorContext);
-
-    /* Execute the next instruction */
-    Fast486StepInto(&EmulatorContext);
+    // FIXME
+    UNIMPLEMENTED;
+    return TRUE;
 }
 
-VOID EmulatorSetA20(BOOLEAN Enabled)
+BOOL
+WINAPI
+VdmUnmapFlat(IN USHORT   Segment,
+             IN ULONG    Offset,
+             IN PVOID    Buffer,
+             IN VDM_MODE Mode)
 {
-    A20Line = Enabled;
+    // FIXME
+    UNIMPLEMENTED;
+    return TRUE;
 }
 
 /* EOF */