[NTVDM]
[reactos.git] / subsystems / ntvdm / emulator.c
index 67b0cd3..33441a3 100644 (file)
@@ -8,44 +8,60 @@
 
 /* INCLUDES *******************************************************************/
 
+#define NDEBUG
+
 #include "emulator.h"
 #include "bios.h"
 #include "dos.h"
+#include "vga.h"
 #include "pic.h"
 #include "ps2.h"
 #include "timer.h"
 
 /* PRIVATE VARIABLES **********************************************************/
 
-static softx86_ctx EmulatorContext;
-static softx87_ctx FpuEmulatorContext;
+#ifndef NEW_EMULATOR
+softx86_ctx EmulatorContext;
+softx87_ctx FpuEmulatorContext;
+#else
+SOFT386_STATE EmulatorContext;
+#endif
+
 static BOOLEAN A20Line = FALSE;
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
+#ifndef NEW_EMULATOR
+
 static VOID EmulatorReadMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
 {
+    UNREFERENCED_PARAMETER(Context);
+
     /* If the A20 line is disabled, mask bit 20 */
     if (!A20Line) Address &= ~(1 << 20);
 
     /* Make sure the requested address is valid */
     if ((Address + Size) >= MAX_ADDRESS) return;
 
-    /* Are we reading some of the console video memory? */
-    if (((Address + Size) >= CONSOLE_VIDEO_MEM_START)
-        && (Address < CONSOLE_VIDEO_MEM_END))
-    {
-        /* Call the VDM BIOS to update the video memory */
-        BiosUpdateConsole(max(Address, CONSOLE_VIDEO_MEM_START),
-                          min(Address + Size, CONSOLE_VIDEO_MEM_END));
-    }
-
     /* 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 */
+    if (((Address + Size) >= VgaGetVideoBaseAddress())
+        && (Address < VgaGetVideoLimitAddress()))
+    {
+        DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
+        LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
+
+        /* Read from the VGA memory */
+        VgaReadMemory(VgaAddress, VgaBuffer, Size);
+    }
 }
 
 static VOID EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
 {
+    UNREFERENCED_PARAMETER(Context);
+
     /* If the A20 line is disabled, mask bit 20 */
     if (!A20Line) Address &= ~(1 << 20);
 
@@ -59,17 +75,22 @@ static VOID EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT
     RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
 
     /* Check if we modified the console video memory */
-    if (((Address + Size) >= CONSOLE_VIDEO_MEM_START)
-        && (Address < CONSOLE_VIDEO_MEM_END))
+    if (((Address + Size) >= VgaGetVideoBaseAddress())
+        && (Address < VgaGetVideoLimitAddress()))
     {
-        /* Call the VDM BIOS to update the screen */
-        BiosUpdateConsole(max(Address, CONSOLE_VIDEO_MEM_START),
-                          min(Address + Size, CONSOLE_VIDEO_MEM_END));
+        DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
+        LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
+
+        /* Write to the VGA memory */
+        VgaWriteMemory(VgaAddress, VgaBuffer, Size);
     }
 }
 
 static VOID EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
 {
+    UNREFERENCED_PARAMETER(Context);
+    UNREFERENCED_PARAMETER(Size);
+
     switch (Address)
     {
         case PIC_MASTER_CMD:
@@ -105,6 +126,31 @@ static VOID EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
             *Buffer = KeyboardReadData();
             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:
+        {
+            *Buffer = VgaReadPort(Address);
+            break;
+        }
+
+        default:
+        {
+            DPRINT1("Read from unknown port: 0x%X\n", Address);
+        }
     }
 }
 
@@ -112,6 +158,9 @@ static VOID EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size
 {
     BYTE Byte = *Buffer;
 
+    UNREFERENCED_PARAMETER(Context);
+    UNREFERENCED_PARAMETER(Size);
+
     switch (Address)
     {
         case PIT_COMMAND_PORT:
@@ -153,32 +202,60 @@ static VOID EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size
             KeyboardWriteData(Byte);
             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(Address, Byte);
+            break;
+        }
+
+        default:
+        {
+            DPRINT1("Write to unknown port: 0x%X\n", Address);
+        }
     }
 }
 
-static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
+static VOID EmulatorBop(WORD Code)
 {
     WORD StackSegment, StackPointer, CodeSegment, InstructionPointer;
     BYTE IntNum;
+    LPWORD Stack;
 
-    /* Check if this is the special interrupt */
-    if (Number == SPECIAL_INT_NUM)
-    {
-        /* Get the SS:SP */
-        StackSegment = EmulatorContext.state->segment_reg[SX86_SREG_SS].val;
-        StackPointer = EmulatorContext.state->general_reg[SX86_REG_SP].val;
+    /* Get the SS:SP */
+#ifndef NEW_EMULATOR
+    StackSegment = EmulatorContext.state->segment_reg[SX86_SREG_SS].val;
+    StackPointer = EmulatorContext.state->general_reg[SX86_REG_SP].val;
+#else
+    StackSegment = EmulatorContext.SegmentRegs[SOFT386_REG_SS].LowWord;
+    StackPointer = EmulatorContext.SegmentRegs[SOFT386_REG_SP].LowWord;
+#endif
 
-        /* Get the interrupt number */
-        IntNum = *(LPBYTE)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
+    /* Get the stack */
+    Stack = (LPWORD)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
 
-        /* Move the stack pointer forward one word to skip the interrupt number */
-        StackPointer += sizeof(WORD);
+    if (Code == EMULATOR_INT_BOP)
+    {
+        /* Get the interrupt number */
+        IntNum = LOBYTE(Stack[STACK_INT_NUM]);
 
         /* Get the CS:IP */
-        InstructionPointer = *(LPWORD)((ULONG_PTR)BaseAddress
-                             + TO_LINEAR(StackSegment, StackPointer));
-        CodeSegment = *(LPWORD)((ULONG_PTR)BaseAddress
-                      + TO_LINEAR(StackSegment, StackPointer + sizeof(WORD)));
+        InstructionPointer = Stack[STACK_IP];
+        CodeSegment = Stack[STACK_CS];
 
         /* Check if this was an exception */
         if (IntNum < 8)
@@ -198,59 +275,107 @@ static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
         if (IntNum >= BIOS_PIC_MASTER_INT && IntNum < BIOS_PIC_MASTER_INT + 8)
         {
             /* It was an IRQ from the master PIC */
-            BiosHandleIrq(IntNum - BIOS_PIC_MASTER_INT);
+            BiosHandleIrq(IntNum - BIOS_PIC_MASTER_INT, Stack);
+            return;
         }
         else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
         {
             /* It was an IRQ from the slave PIC */
-            BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8);
+            BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8, Stack);
+            return;
         }
 
         switch (IntNum)
         {
-            case VIDEO_BIOS_INTERRUPT:
+            case BIOS_VIDEO_INTERRUPT:
             {
                 /* This is the video BIOS interrupt, call the BIOS */
-                BiosVideoService();
+                BiosVideoService(Stack);
+                break;
+            }
+            case BIOS_EQUIPMENT_INTERRUPT:
+            {
+                /* This is the BIOS "get equipment" command, call the BIOS */
+                BiosEquipmentService(Stack);
+                break;
+            }
+            case BIOS_KBD_INTERRUPT:
+            {
+                /* This is the keyboard BIOS interrupt, call the BIOS */
+                BiosKeyboardService(Stack);
+                break;
+            }
+            case BIOS_TIME_INTERRUPT:
+            {
+                /* This is the time BIOS interrupt, call the BIOS */
+                BiosTimeService(Stack);
+                break;
+            }
+            case BIOS_SYS_TIMER_INTERRUPT:
+            {
+                /* BIOS timer update */
+                BiosSystemTimerInterrupt(Stack);
                 break;
             }
             case 0x20:
             {
-                DosInt20h(CodeSegment);
+                DosInt20h(Stack);
                 break;
             }
             case 0x21:
             {
-                DosInt21h(CodeSegment);
+                DosInt21h(Stack);
                 break;
             }
             case 0x23:
             {
-                DosBreakInterrupt();
+                DosBreakInterrupt(Stack);
+                break;
+            }
+            default:
+            {
+                DPRINT1("Unhandled interrupt: 0x%02X\n", IntNum);
                 break;
             }
         }
     }
 }
 
+static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
+{
+    UNREFERENCED_PARAMETER(Context);
+    UNREFERENCED_PARAMETER(Number);
+
+    /* Do nothing */
+}
+
 static VOID EmulatorHardwareInt(PVOID Context, BYTE Number)
 {
+    UNREFERENCED_PARAMETER(Context);
+    UNREFERENCED_PARAMETER(Number);
+
     /* Do nothing */
 }
 
 static VOID EmulatorHardwareIntAck(PVOID Context, BYTE Number)
 {
+    UNREFERENCED_PARAMETER(Context);
+    UNREFERENCED_PARAMETER(Number);
+
     /* Do nothing */
 }
 
+#endif
+
 /* PUBLIC FUNCTIONS ***********************************************************/
 
 BOOLEAN EmulatorInitialize()
 {
     /* Allocate memory for the 16-bit address space */
-    BaseAddress = HeapAlloc(GetProcessHeap(), 0, MAX_ADDRESS);
+    BaseAddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_ADDRESS);
     if (BaseAddress == NULL) return FALSE;
 
+#ifndef NEW_EMULATOR
     /* Initialize the softx86 CPU emulator */
     if (!softx86_init(&EmulatorContext, SX86_CPULEVEL_80286))
     {
@@ -281,6 +406,9 @@ BOOLEAN EmulatorInitialize()
 
     /* Connect the emulated FPU to the emulated CPU */
     softx87_connect_to_CPU(&EmulatorContext, &FpuEmulatorContext);
+#else
+    // TODO: NOT IMPLEMENTED
+#endif
 
     /* Enable interrupts */
     EmulatorSetFlag(EMULATOR_FLAG_IF);
@@ -288,16 +416,24 @@ BOOLEAN EmulatorInitialize()
     return TRUE;
 }
 
-VOID EmulatorSetStack(WORD Segment, WORD Offset)
+VOID EmulatorSetStack(WORD Segment, DWORD Offset)
 {
+#ifndef NEW_EMULATOR
     /* Call the softx86 API */
     softx86_set_stack_ptr(&EmulatorContext, Segment, Offset);
+#else
+    // TODO: NOT IMPLEMENTED
+#endif
 }
 
 VOID EmulatorExecute(WORD Segment, WORD Offset)
 {
+#ifndef NEW_EMULATOR
     /* Call the softx86 API */
     softx86_set_instruction_ptr(&EmulatorContext, Segment, Offset);
+#else
+    // TODO: NOT IMPLEMENTED
+#endif
 }
 
 VOID EmulatorInterrupt(BYTE Number)
@@ -309,18 +445,27 @@ VOID EmulatorInterrupt(BYTE Number)
     Segment = HIWORD(IntVecTable[Number]);
     Offset = LOWORD(IntVecTable[Number]);
 
+#ifndef NEW_EMULATOR
     /* Call the softx86 API */
     softx86_make_simple_interrupt_call(&EmulatorContext, &Segment, &Offset);
+#else
+    UNREFERENCED_PARAMETER(Segment);
+    UNREFERENCED_PARAMETER(Offset);
+    // TODO: NOT IMPLEMENTED
+#endif
 }
 
 VOID EmulatorExternalInterrupt(BYTE Number)
 {
+#ifndef NEW_EMULATOR
     /* Call the softx86 API */
     softx86_ext_hw_signal(&EmulatorContext, Number);
+#endif
 }
 
 ULONG EmulatorGetRegister(ULONG Register)
 {
+#ifndef NEW_EMULATOR
     if (Register < EMULATOR_REG_ES)
     {
         return EmulatorContext.state->general_reg[Register].val;
@@ -329,53 +474,118 @@ ULONG EmulatorGetRegister(ULONG Register)
     {
         return EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val;
     }
+#else
+    if (Register < EMULATOR_REG_ES)
+    {
+        return EmulatorContext.GeneralRegs[Register].Long;
+    }
+    else
+    {
+        return EmulatorContext.SegmentRegs[Register - EMULATOR_REG_ES].Selector;
+    }
+#endif
+}
+
+ULONG EmulatorGetProgramCounter(VOID)
+{
+#ifndef NEW_EMULATOR
+    return EmulatorContext.state->reg_ip;
+#else
+    return EmulatorContext.InstPtr.Long;
+#endif
 }
 
 VOID EmulatorSetRegister(ULONG Register, ULONG Value)
 {
+#ifndef NEW_EMULATOR
     if (Register < EMULATOR_REG_CS)
     {
         EmulatorContext.state->general_reg[Register].val = Value;
     }
     else
     {
-        EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val = Value;
+        EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val = (WORD)Value;
     }
+#else
+    // TODO: NOT IMPLEMENTED
+#endif
 }
 
 BOOLEAN EmulatorGetFlag(ULONG Flag)
 {
-    return (EmulatorContext.state->reg_flags.val & Flag);
+#ifndef NEW_EMULATOR
+    return (EmulatorContext.state->reg_flags.val & Flag) ? TRUE : FALSE;
+#else
+    return (EmulatorContext.Flags.Long & Flag) ? TRUE : FALSE;
+#endif
 }
 
 VOID EmulatorSetFlag(ULONG Flag)
 {
+#ifndef NEW_EMULATOR
     EmulatorContext.state->reg_flags.val |= Flag;
+#else
+    EmulatorContext.Flags.Long |= Flag;
+#endif
 }
 
 VOID EmulatorClearFlag(ULONG Flag)
 {
+#ifndef NEW_EMULATOR
     EmulatorContext.state->reg_flags.val &= ~Flag;
+#else
+    EmulatorContext.Flags.Long &= ~Flag;
+#endif
 }
 
-VOID EmulatorStep()
+VOID EmulatorStep(VOID)
 {
+#ifndef NEW_EMULATOR
+    LPWORD Instruction;
+
+    /* Print the current position - useful for debugging */
+    DPRINT("Executing at CS:IP = %04X:%04X\n",
+           EmulatorGetRegister(EMULATOR_REG_CS),
+           EmulatorContext.state->reg_ip);
+
+    Instruction = (LPWORD)((ULONG_PTR)BaseAddress
+                           + TO_LINEAR(EmulatorGetRegister(EMULATOR_REG_CS),
+                           EmulatorContext.state->reg_ip));
+
+    /* Check for the BIOS operation (BOP) sequence */
+    if (Instruction[0] == EMULATOR_BOP)
+    {
+        /* Skip the opcodes */
+        EmulatorContext.state->reg_ip += 4;
+
+        // HACK: Refresh the display because the called function may wait.
+        VgaRefreshDisplay();
+
+        /* Call the BOP handler */
+        EmulatorBop(Instruction[1]);
+    }
+
     /* Call the softx86 API */
     if (!softx86_step(&EmulatorContext))
     {
         /* Invalid opcode */
         EmulatorInterrupt(EMULATOR_EXCEPTION_INVALID_OPCODE);
     }
+#else
+    // TODO: NOT IMPLEMENTED
+#endif
 }
 
-VOID EmulatorCleanup()
+VOID EmulatorCleanup(VOID)
 {
-    /* Free the memory allocated for the 16-bit address space */
-    if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
-
+#ifndef NEW_EMULATOR
     /* Free the softx86 CPU and FPU emulator */
-    softx86_free(&EmulatorContext);
     softx87_free(&FpuEmulatorContext);
+    softx86_free(&EmulatorContext);
+#endif
+
+    /* Free the memory allocated for the 16-bit address space */
+    if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
 }
 
 VOID EmulatorSetA20(BOOLEAN Enabled)