[FAST486]
[reactos.git] / reactos / lib / fast486 / opcodes.c
index 662a5fb..859009c 100644 (file)
@@ -3832,16 +3832,13 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovModrm)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovStoreSeg)
 {
-    BOOLEAN OperandSize, AddressSize;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
     FAST486_MOD_REG_RM ModRegRm;
 
-    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
-
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0x8C);
 
     TOGGLE_ADSIZE(AddressSize);
-    TOGGLE_OPSIZE(OperandSize);
 
     /* Get the operands */
     if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
@@ -3857,20 +3854,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovStoreSeg)
         return;
     }
 
-    if (OperandSize)
-    {
-        Fast486WriteModrmDwordOperands(State,
-                                       &ModRegRm,
-                                       FALSE,
-                                       State->SegmentRegs[ModRegRm.Register].Selector);
-    }
-    else
-    {
-        Fast486WriteModrmWordOperands(State,
-                                      &ModRegRm,
-                                      FALSE,
-                                      State->SegmentRegs[ModRegRm.Register].Selector);
-    }
+    Fast486WriteModrmWordOperands(State,
+                                  &ModRegRm,
+                                  FALSE,
+                                  State->SegmentRegs[ModRegRm.Register].Selector);
 }
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeLea)
@@ -3921,16 +3908,14 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeLea)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovLoadSeg)
 {
-    BOOLEAN OperandSize, AddressSize;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
     FAST486_MOD_REG_RM ModRegRm;
-
-    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    USHORT Selector;
 
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0x8E);
 
     TOGGLE_ADSIZE(AddressSize);
-    TOGGLE_OPSIZE(OperandSize);
 
     /* Get the operands */
     if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
@@ -3947,37 +3932,16 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovLoadSeg)
         return;
     }
 
-    if (OperandSize)
+    if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Selector))
     {
-        ULONG Selector;
-
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Selector))
-        {
-            /* Exception occurred */
-            return;
-        }
-
-        if (!Fast486LoadSegment(State, ModRegRm.Register, LOWORD(Selector)))
-        {
-            /* Exception occurred */
-            return;
-        }
+        /* Exception occurred */
+        return;
     }
-    else
-    {
-        USHORT Selector;
-
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Selector))
-        {
-            /* Exception occurred */
-            return;
-        }
 
-        if (!Fast486LoadSegment(State, ModRegRm.Register, Selector))
-        {
-            /* Exception occurred */
-            return;
-        }
+    if (!Fast486LoadSegment(State, ModRegRm.Register, Selector))
+    {
+        /* Exception occurred */
+        return;
     }
 
     if ((INT)ModRegRm.Register == FAST486_REG_SS)
@@ -4125,8 +4089,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeWait)
         || (!State->FpuControl.Dm && State->FpuStatus.De)
         || (!State->FpuControl.Im && State->FpuStatus.Ie))
     {
-        /* Call the #MF handler */
-        Fast486Exception(State, FAST486_EXCEPTION_MF);
+        Fast486FpuException(State);
     }
 
 #endif
@@ -4155,7 +4118,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodePushFlags)
 FAST486_OPCODE_HANDLER(Fast486OpcodePopFlags)
 {
     BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
-    INT Cpl = Fast486GetCurrentPrivLevel(State);
+    UINT Cpl = Fast486GetCurrentPrivLevel(State);
     FAST486_FLAGS_REG NewFlags;
 
     NO_LOCK_PREFIX();
@@ -4406,16 +4369,24 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeEnter)
     if (NestingLevel > 0) Fast486StackPush(State, FramePointer.Long);
 
     /* Set EBP to the frame pointer */
-    State->GeneralRegs[FAST486_REG_EBP] = FramePointer;
+    if (Size) State->GeneralRegs[FAST486_REG_EBP].Long = FramePointer.Long;
+    else State->GeneralRegs[FAST486_REG_EBP].LowWord = FramePointer.LowWord;
 
     /* Reserve space for the frame */
-    if (Size) State->GeneralRegs[FAST486_REG_ESP].Long -= (ULONG)FrameSize;
-    else State->GeneralRegs[FAST486_REG_ESP].LowWord -= FrameSize;
+    if (State->SegmentRegs[FAST486_REG_SS].Size)
+    {
+        State->GeneralRegs[FAST486_REG_ESP].Long -= (ULONG)FrameSize;
+    }
+    else
+    {
+        State->GeneralRegs[FAST486_REG_ESP].LowWord -= FrameSize;
+    }
 }
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeLeave)
 {
     BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+    ULONG Value;
 
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xC9);
@@ -4423,26 +4394,22 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeLeave)
     NO_LOCK_PREFIX();
     TOGGLE_OPSIZE(Size);
 
-    if (Size)
+    if (State->SegmentRegs[FAST486_REG_SS].Size)
     {
         /* Set the stack pointer (ESP) to the base pointer (EBP) */
         State->GeneralRegs[FAST486_REG_ESP].Long = State->GeneralRegs[FAST486_REG_EBP].Long;
-
-        /* Pop the saved base pointer from the stack */
-        Fast486StackPop(State, &State->GeneralRegs[FAST486_REG_EBP].Long);
     }
     else
     {
-        ULONG Value;
-
         /* Set the stack pointer (SP) to the base pointer (BP) */
         State->GeneralRegs[FAST486_REG_ESP].LowWord = State->GeneralRegs[FAST486_REG_EBP].LowWord;
+    }
 
-        /* Pop the saved base pointer from the stack */
-        if (Fast486StackPop(State, &Value))
-        {
-            State->GeneralRegs[FAST486_REG_EBP].LowWord = LOWORD(Value);
-        }
+    /* Pop the saved base pointer from the stack */
+    if (Fast486StackPop(State, &Value))
+    {
+        if (Size) State->GeneralRegs[FAST486_REG_EBP].Long = Value;
+        else State->GeneralRegs[FAST486_REG_EBP].LowWord = LOWORD(Value);
     }
 }
 
@@ -4488,11 +4455,11 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeRetFar)
 
     if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) && !State->Flags.Vm)
     {
-        INT i;
-        INT OldCpl = Fast486GetCurrentPrivLevel(State);
+        UINT i;
+        UINT OldCpl = Fast486GetCurrentPrivLevel(State);
         ULONG StackPtr;
         ULONG StackSel;
-        
+
         if (GET_SEGMENT_RPL(Segment) > OldCpl)
         {
             /* Pop ESP */
@@ -4659,7 +4626,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeIret)
     /* Check for protected mode */
     if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
     {
-        INT OldCpl = Fast486GetCurrentPrivLevel(State);
+        UINT OldCpl = Fast486GetCurrentPrivLevel(State);
 
         if (State->Flags.Vm)
         {
@@ -5292,7 +5259,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetAl)
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetEax)
 {
     BOOLEAN OperandSize, AddressSize;
-    
+
     OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
     /* Make sure this is the right instruction */
@@ -5865,7 +5832,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeScas)
             || (!AddressSize && (State->GeneralRegs[FAST486_REG_ECX].LowWord == 0)))
         {
             /* Do nothing */
-            return; 
+            return;
         }
     }