* Sync up to trunk head (r64921).
[reactos.git] / lib / fast486 / fast486.c
index dcb98b3..12a3768 100644 (file)
@@ -2,7 +2,7 @@
  * Fast486 386/486 CPU Emulation Library
  * fast486.c
  *
- * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
+ * Copyright (C) 2014 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -29,6 +29,7 @@
 #include <fast486.h>
 #include "common.h"
 #include "opcodes.h"
+#include "fpu.h"
 
 /* DEFINES ********************************************************************/
 
@@ -42,86 +43,90 @@ typedef enum
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
-static
-inline
 VOID
 NTAPI
-Fast486ExecutionControl(PFAST486_STATE State, INT Command)
+Fast486ExecutionControl(PFAST486_STATE State, FAST486_EXEC_CMD Command)
 {
     UCHAR Opcode;
+    FAST486_OPCODE_HANDLER_PROC CurrentHandler;
     INT ProcedureCallCount = 0;
 
     /* Main execution loop */
     do
     {
-        /* Check if this is a new instruction */
-        if (State->PrefixFlags == 0)
+        if (!State->Halted)
         {
-            State->SavedInstPtr = State->InstPtr;
+NextInst:
+            /* Check if this is a new instruction */
+            if (State->PrefixFlags == 0) State->SavedInstPtr = State->InstPtr;
 
-            /*
-             * Check if there is an interrupt to execute, or a hardware interrupt signal
-             * while interrupts are enabled.
-             */
-            if ((State->IntStatus == FAST486_INT_EXECUTE)
-                || (State->Flags.If
-                && (State->IntAckCallback != NULL)
-                && (State->IntStatus == FAST486_INT_SIGNAL)))
+            /* Perform an instruction fetch */
+            if (!Fast486FetchByte(State, &Opcode))
             {
-                FAST486_IDT_ENTRY IdtEntry;
-
-                if (State->IntStatus == FAST486_INT_SIGNAL)
-                {
-                    /* Acknowledge the interrupt to get the number */
-                    State->PendingIntNum = State->IntAckCallback(State);
-                }
-
-                /* Get the interrupt vector */
-                if (Fast486GetIntVector(State, State->PendingIntNum, &IdtEntry))
-                {
-                    /* Perform the interrupt */
-                    Fast486InterruptInternal(State,
-                                             IdtEntry.Selector,
-                                             MAKELONG(IdtEntry.Offset, IdtEntry.OffsetHigh),
-                                             IdtEntry.Type);
-                }
-
-                /* Clear the interrupt status */
-                State->IntStatus = FAST486_INT_NONE;
+                /* Exception occurred */
+                State->PrefixFlags = 0;
+                continue;
             }
-        }
-
-        /* Perform an instruction fetch */
-        if (!Fast486FetchByte(State, &Opcode)) continue;
 
-        // TODO: Check for CALL/RET to update ProcedureCallCount.
+            // TODO: Check for CALL/RET to update ProcedureCallCount.
 
-        if (Fast486OpcodeHandlers[Opcode] != NULL)
-        {
             /* Call the opcode handler */
-            Fast486OpcodeHandlers[Opcode](State, Opcode);
+            CurrentHandler = Fast486OpcodeHandlers[Opcode];
+            CurrentHandler(State, Opcode);
+
+            /* If this is a prefix, go to the next instruction immediately */
+            if (CurrentHandler == Fast486OpcodePrefix) goto NextInst;
+
+            /* A non-prefix opcode has been executed, reset the prefix flags */
+            State->PrefixFlags = 0;
         }
-        else
+
+        /*
+         * Check if there is an interrupt to execute, or a hardware interrupt signal
+         * while interrupts are enabled.
+         */
+        if (State->Flags.Tf && !State->Halted)
         {
-            /* This is not a valid opcode */
-            Fast486Exception(State, FAST486_EXCEPTION_UD);
+            /* Perform the interrupt */
+            Fast486PerformInterrupt(State, 0x01);
+
+            /*
+             * Flags and TF are pushed on stack so we can reset TF now,
+             * to not break into the INT 0x01 handler.
+             * After the INT 0x01 handler returns, the flags and therefore
+             * TF are popped back off the stack and restored, so TF will be
+             * automatically reset to its previous state.
+             */
+            State->Flags.Tf = FALSE;
         }
+        else if (State->IntStatus == FAST486_INT_EXECUTE)
+        {
+            /* No longer halted */
+            State->Halted = FALSE;
+
+            /* Perform the interrupt */
+            Fast486PerformInterrupt(State, State->PendingIntNum);
 
-        if (Fast486OpcodeHandlers[Opcode] != Fast486OpcodePrefix)
+            /* Clear the interrupt status */
+            State->IntStatus = FAST486_INT_NONE;
+        }
+        else if (State->Flags.If && (State->IntStatus == FAST486_INT_SIGNAL))
         {
-            /* A non-prefix opcode has been executed, reset the prefix flags */
-            State->PrefixFlags = 0;
+            /* Acknowledge the interrupt to get the number */
+            State->PendingIntNum = State->IntAckCallback(State);
+
+            /* Set the interrupt status to execute on the next instruction */
+            State->IntStatus = FAST486_INT_EXECUTE;
         }
-        else
+        else if (State->IntStatus == FAST486_INT_DELAYED)
         {
-            /* This is a prefix, go to the next instruction immediately */
-            continue;
+            /* Restore the old state */
+            State->IntStatus = FAST486_INT_EXECUTE;
         }
     }
-    while ((Command == FAST486_CONTINUE)
-           || (Command == FAST486_STEP_OVER && ProcedureCallCount > 0)
-           || (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0)
-           || (Fast486OpcodeHandlers[Opcode] == Fast486OpcodePrefix));
+    while ((Command == FAST486_CONTINUE) ||
+           (Command == FAST486_STEP_OVER && ProcedureCallCount > 0) ||
+           (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0));
 }
 
 /* DEFAULT CALLBACKS **********************************************************/
@@ -131,7 +136,6 @@ NTAPI
 Fast486MemReadCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 {
     UNREFERENCED_PARAMETER(State);
-
     RtlMoveMemory(Buffer, (PVOID)Address, Size);
 }
 
@@ -140,13 +144,12 @@ NTAPI
 Fast486MemWriteCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 {
     UNREFERENCED_PARAMETER(State);
-
     RtlMoveMemory((PVOID)Address, Buffer, Size);
 }
 
 static VOID
 NTAPI
-Fast486IoReadCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
+Fast486IoReadCallback(PFAST486_STATE State, USHORT Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
 {
     UNREFERENCED_PARAMETER(State);
     UNREFERENCED_PARAMETER(Port);
@@ -157,7 +160,7 @@ Fast486IoReadCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Data
 
 static VOID
 NTAPI
-Fast486IoWriteCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
+Fast486IoWriteCallback(PFAST486_STATE State, USHORT Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
 {
     UNREFERENCED_PARAMETER(State);
     UNREFERENCED_PARAMETER(Port);
@@ -166,13 +169,6 @@ Fast486IoWriteCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Dat
     UNREFERENCED_PARAMETER(DataSize);
 }
 
-static VOID
-NTAPI
-Fast486IdleCallback(PFAST486_STATE State)
-{
-    UNREFERENCED_PARAMETER(State);
-}
-
 static VOID
 NTAPI
 Fast486BopCallback(PFAST486_STATE State, UCHAR BopCode)
@@ -187,8 +183,8 @@ Fast486IntAckCallback(PFAST486_STATE State)
 {
     UNREFERENCED_PARAMETER(State);
 
-    /* Return something... */
-    return 0;
+    /* Return something... defaulted to single-step interrupt */
+    return 0x01;
 }
 
 /* PUBLIC FUNCTIONS ***********************************************************/
@@ -200,19 +196,21 @@ Fast486Initialize(PFAST486_STATE         State,
                   FAST486_MEM_WRITE_PROC MemWriteCallback,
                   FAST486_IO_READ_PROC   IoReadCallback,
                   FAST486_IO_WRITE_PROC  IoWriteCallback,
-                  FAST486_IDLE_PROC      IdleCallback,
                   FAST486_BOP_PROC       BopCallback,
-                  FAST486_INT_ACK_PROC   IntAckCallback)
+                  FAST486_INT_ACK_PROC   IntAckCallback,
+                  PULONG                 Tlb)
 {
     /* Set the callbacks (or use default ones if some are NULL) */
     State->MemReadCallback  = (MemReadCallback  ? MemReadCallback  : Fast486MemReadCallback );
     State->MemWriteCallback = (MemWriteCallback ? MemWriteCallback : Fast486MemWriteCallback);
     State->IoReadCallback   = (IoReadCallback   ? IoReadCallback   : Fast486IoReadCallback  );
     State->IoWriteCallback  = (IoWriteCallback  ? IoWriteCallback  : Fast486IoWriteCallback );
-    State->IdleCallback     = (IdleCallback     ? IdleCallback     : Fast486IdleCallback    );
     State->BopCallback      = (BopCallback      ? BopCallback      : Fast486BopCallback     );
     State->IntAckCallback   = (IntAckCallback   ? IntAckCallback   : Fast486IntAckCallback  );
 
+    /* Set the TLB (if given) */
+    State->Tlb = Tlb;
+
     /* Reset the CPU */
     Fast486Reset(State);
 }
@@ -223,13 +221,14 @@ Fast486Reset(PFAST486_STATE State)
 {
     FAST486_SEG_REGS i;
 
+    /* Save the callbacks and TLB */
     FAST486_MEM_READ_PROC  MemReadCallback  = State->MemReadCallback;
     FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
     FAST486_IO_READ_PROC   IoReadCallback   = State->IoReadCallback;
     FAST486_IO_WRITE_PROC  IoWriteCallback  = State->IoWriteCallback;
-    FAST486_IDLE_PROC      IdleCallback     = State->IdleCallback;
     FAST486_BOP_PROC       BopCallback      = State->BopCallback;
     FAST486_INT_ACK_PROC   IntAckCallback   = State->IntAckCallback;
+    PULONG                 Tlb              = State->Tlb;
 
     /* Clear the entire structure */
     RtlZeroMemory(State, sizeof(*State));
@@ -238,6 +237,9 @@ Fast486Reset(PFAST486_STATE State)
     State->Flags.AlwaysSet = 1;
     State->InstPtr.LowWord = 0xFFF0;
 
+    /* Set the CPL to 0 */
+    State->Cpl = 0;
+
     /* Initialize segments */
     for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
     {
@@ -262,133 +264,24 @@ Fast486Reset(PFAST486_STATE State)
     State->Idtr.Size = 0x3FF;
     State->Idtr.Address = 0;
 
+#ifndef FAST486_NO_FPU
     /* Initialize CR0 */
     State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
 
-    /* Restore the callbacks */
+    /* Initialize the FPU control and tag registers */
+    State->FpuControl.Value = FAST486_FPU_DEFAULT_CONTROL;
+    State->FpuStatus.Value = 0;
+    State->FpuTag = 0xFFFF;
+#endif
+
+    /* Restore the callbacks and TLB */
     State->MemReadCallback  = MemReadCallback;
     State->MemWriteCallback = MemWriteCallback;
     State->IoReadCallback   = IoReadCallback;
     State->IoWriteCallback  = IoWriteCallback;
-    State->IdleCallback     = IdleCallback;
     State->BopCallback      = BopCallback;
     State->IntAckCallback   = IntAckCallback;
-}
-
-VOID
-NTAPI
-Fast486DumpState(PFAST486_STATE State)
-{
-    DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
-            (State->ControlRegisters[0] & FAST486_CR0_PE) ? "protected" : "real",
-            State->SegmentRegs[FAST486_REG_CS].Selector,
-            State->InstPtr.Long);
-    DPRINT1("\nGeneral purpose registers:\n"
-            "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
-            "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
-            State->GeneralRegs[FAST486_REG_EAX].Long,
-            State->GeneralRegs[FAST486_REG_ECX].Long,
-            State->GeneralRegs[FAST486_REG_EDX].Long,
-            State->GeneralRegs[FAST486_REG_EBX].Long,
-            State->GeneralRegs[FAST486_REG_ESP].Long,
-            State->GeneralRegs[FAST486_REG_EBP].Long,
-            State->GeneralRegs[FAST486_REG_ESI].Long,
-            State->GeneralRegs[FAST486_REG_EDI].Long);
-    DPRINT1("\nSegment registers:\n"
-            "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
-            "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
-            "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
-            "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
-            "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
-            "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
-            State->SegmentRegs[FAST486_REG_ES].Selector,
-            State->SegmentRegs[FAST486_REG_ES].Base,
-            State->SegmentRegs[FAST486_REG_ES].Limit,
-            State->SegmentRegs[FAST486_REG_ES].Dpl,
-            State->SegmentRegs[FAST486_REG_CS].Selector,
-            State->SegmentRegs[FAST486_REG_CS].Base,
-            State->SegmentRegs[FAST486_REG_CS].Limit,
-            State->SegmentRegs[FAST486_REG_CS].Dpl,
-            State->SegmentRegs[FAST486_REG_SS].Selector,
-            State->SegmentRegs[FAST486_REG_SS].Base,
-            State->SegmentRegs[FAST486_REG_SS].Limit,
-            State->SegmentRegs[FAST486_REG_SS].Dpl,
-            State->SegmentRegs[FAST486_REG_DS].Selector,
-            State->SegmentRegs[FAST486_REG_DS].Base,
-            State->SegmentRegs[FAST486_REG_DS].Limit,
-            State->SegmentRegs[FAST486_REG_DS].Dpl,
-            State->SegmentRegs[FAST486_REG_FS].Selector,
-            State->SegmentRegs[FAST486_REG_FS].Base,
-            State->SegmentRegs[FAST486_REG_FS].Limit,
-            State->SegmentRegs[FAST486_REG_FS].Dpl,
-            State->SegmentRegs[FAST486_REG_GS].Selector,
-            State->SegmentRegs[FAST486_REG_GS].Base,
-            State->SegmentRegs[FAST486_REG_GS].Limit,
-            State->SegmentRegs[FAST486_REG_GS].Dpl);
-    DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
-            State->Flags.Long,
-            State->Flags.Cf ? "CF" : "cf",
-            State->Flags.Pf ? "PF" : "pf",
-            State->Flags.Af ? "AF" : "af",
-            State->Flags.Zf ? "ZF" : "zf",
-            State->Flags.Sf ? "SF" : "sf",
-            State->Flags.Tf ? "TF" : "tf",
-            State->Flags.If ? "IF" : "if",
-            State->Flags.Df ? "DF" : "df",
-            State->Flags.Of ? "OF" : "of",
-            State->Flags.Nt ? "NT" : "nt",
-            State->Flags.Rf ? "RF" : "rf",
-            State->Flags.Vm ? "VM" : "vm",
-            State->Flags.Ac ? "AC" : "ac",
-            State->Flags.Vif ? "VIF" : "vif",
-            State->Flags.Vip ? "VIP" : "vip",
-            State->Flags.Iopl);
-    DPRINT1("\nControl Registers:\n"
-            "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
-            State->ControlRegisters[FAST486_REG_CR0],
-            State->ControlRegisters[FAST486_REG_CR2],
-            State->ControlRegisters[FAST486_REG_CR3]);
-    DPRINT1("\nDebug Registers:\n"
-            "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
-            "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
-            State->DebugRegisters[FAST486_REG_DR0],
-            State->DebugRegisters[FAST486_REG_DR1],
-            State->DebugRegisters[FAST486_REG_DR2],
-            State->DebugRegisters[FAST486_REG_DR3],
-            State->DebugRegisters[FAST486_REG_DR4],
-            State->DebugRegisters[FAST486_REG_DR5]);
-}
-
-VOID
-NTAPI
-Fast486Continue(PFAST486_STATE State)
-{
-    /* Call the internal function */
-    Fast486ExecutionControl(State, FAST486_CONTINUE);
-}
-
-VOID
-NTAPI
-Fast486StepInto(PFAST486_STATE State)
-{
-    /* Call the internal function */
-    Fast486ExecutionControl(State, FAST486_STEP_INTO);
-}
-
-VOID
-NTAPI
-Fast486StepOver(PFAST486_STATE State)
-{
-    /* Call the internal function */
-    Fast486ExecutionControl(State, FAST486_STEP_OVER);
-}
-
-VOID
-NTAPI
-Fast486StepOut(PFAST486_STATE State)
-{
-    /* Call the internal function */
-    Fast486ExecutionControl(State, FAST486_STEP_OUT);
+    State->Tlb              = Tlb;
 }
 
 VOID