[FAST486]
[reactos.git] / lib / fast486 / opcodes.c
index c61de2b..07e870c 100644 (file)
@@ -684,7 +684,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeShortConditionalJmp)
 
     if (Jump)
     {
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.Long += Offset;
     }
 
@@ -997,7 +997,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeOutByte)
 
     /* Read the value from AL */
     Data = State->GeneralRegs[FAST486_REG_EAX].LowByte;
-    
+
     /* Write the byte to the I/O port */
     State->IoWriteCallback(State, Port, &Data, 1, sizeof(UCHAR));
 
@@ -1041,7 +1041,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeOut)
         ULONG Data = State->GeneralRegs[FAST486_REG_EAX].Long;
 
         /* Write a dword to the I/O port */
-        State->IoReadCallback(State, Port, &Data, 1, sizeof(ULONG));
+        State->IoWriteCallback(State, Port, &Data, 1, sizeof(ULONG));
     }
     else
     {
@@ -1069,7 +1069,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeShortJump)
         return FALSE;
     }
 
-    /* Move the instruction pointer */        
+    /* Move the instruction pointer */
     State->InstPtr.Long += Offset;
 
     return TRUE;
@@ -1231,7 +1231,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeAddModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue + SecondValue;
 
@@ -1262,7 +1262,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeAddModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue + SecondValue;
 
@@ -1465,7 +1465,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeOrModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue | SecondValue;
 
@@ -1494,7 +1494,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeOrModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue | SecondValue;
 
@@ -1689,7 +1689,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeAndModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue & SecondValue;
 
@@ -1718,7 +1718,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeAndModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue & SecondValue;
 
@@ -1908,7 +1908,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeXorModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue ^ SecondValue;
 
@@ -1937,7 +1937,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeXorModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue ^ SecondValue;
 
@@ -2128,7 +2128,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeTestModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue & SecondValue;
 
@@ -2151,7 +2151,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeTestModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue & SecondValue;
 
@@ -2375,7 +2375,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeXchgModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Write the value from the register to the R/M */
         if (!Fast486WriteModrmWordOperands(State,
                                            &ModRegRm,
@@ -2510,7 +2510,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeAdcModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue + SecondValue + State->Flags.Cf;
 
@@ -2545,7 +2545,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeAdcModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = FirstValue + SecondValue + State->Flags.Cf;
 
@@ -2803,7 +2803,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeSbbModrm)
             /* Swap the order */
             SWAP(FirstValue, SecondValue);
         }
-    
+
         /* Calculate the result */
         Result = FirstValue - SecondValue - Carry;
 
@@ -2834,14 +2834,14 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeSbbModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Check if this is the instruction that writes to R/M */
         if (!(Opcode & FAST486_OPCODE_WRITE_REG))
         {
             /* Swap the order */
             SWAP(FirstValue, SecondValue);
         }
-    
+
         /* Calculate the result */
         Result = FirstValue - SecondValue - Carry;
 
@@ -3129,7 +3129,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeCmpSubModrm)
             /* Swap the order */
             SWAP(FirstValue, SecondValue);
         }
-    
+
         /* Calculate the result */
         Result = FirstValue - SecondValue;
 
@@ -3169,14 +3169,14 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeCmpSubModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Check if this is the instruction that writes to R/M */
         if (!(Opcode & FAST486_OPCODE_WRITE_REG))
         {
             /* Swap the order */
             SWAP(FirstValue, SecondValue);
         }
-    
+
         /* Calculate the result */
         Result = FirstValue - SecondValue;
 
@@ -3791,7 +3791,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovModrm)
 
         if (Opcode & FAST486_OPCODE_WRITE_REG) Result = SecondValue;
         else Result = FirstValue;
-    
+
         /* Write back the result */
         return Fast486WriteModrmDwordOperands(State,
                                               &ModRegRm,
@@ -3810,7 +3810,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovModrm)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         if (Opcode & FAST486_OPCODE_WRITE_REG) Result = SecondValue;
         else Result = FirstValue;
 
@@ -4234,7 +4234,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodePopFlags)
             /* Call the VM86 monitor */
             Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, 0);
         }
-        
+
     }
 
     return TRUE;
@@ -4314,6 +4314,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeLdsLes)
 
     OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
+    TOGGLE_OPSIZE(OperandSize);
     TOGGLE_ADSIZE(AddressSize);
 
     /* Get the operands */
@@ -4975,7 +4976,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeJecxz)
 
     if (Condition)
     {
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.Long += Offset;
     }
 
@@ -5010,7 +5011,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeCall)
             return FALSE;
         }
 
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.Long += Offset;
     }
     else
@@ -5031,7 +5032,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeCall)
             return FALSE;
         }
 
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.LowWord += Offset;
     }
 
@@ -5059,7 +5060,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeJmp)
             return FALSE;
         }
 
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.Long += Offset;
     }
     else
@@ -5073,7 +5074,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeJmp)
             return FALSE;
         }
 
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.LowWord += Offset;
     }
 
@@ -5133,15 +5134,15 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeJmpAbs)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovAlOffset)
 {
-    BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
     ULONG Offset;
 
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xA0);
 
-    TOGGLE_OPSIZE(Size);
+    TOGGLE_ADSIZE(AddressSize);
 
-    if (Size)
+    if (AddressSize)
     {
         if (!Fast486FetchDword(State, &Offset))
         {
@@ -5174,14 +5175,17 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovAlOffset)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovEaxOffset)
 {
-    BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+    BOOLEAN OperandSize, AddressSize;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xA1);
 
-    TOGGLE_OPSIZE(Size);
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
 
-    if (Size)
+    if (AddressSize)
     {
         ULONG Offset;
 
@@ -5192,13 +5196,26 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovEaxOffset)
         }
 
         /* Read from memory */
-        return Fast486ReadMemory(State,
-                                 (State->PrefixFlags & FAST486_PREFIX_SEG) ?
-                                 State->SegmentOverride : FAST486_REG_DS,
-                                 Offset,
-                                 FALSE,
-                                 &State->GeneralRegs[FAST486_REG_EAX].Long,
-                                 sizeof(ULONG));
+        if (OperandSize)
+        {
+            return Fast486ReadMemory(State,
+                                     (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                     State->SegmentOverride : FAST486_REG_DS,
+                                     Offset,
+                                     FALSE,
+                                     &State->GeneralRegs[FAST486_REG_EAX].Long,
+                                     sizeof(ULONG));
+        }
+        else
+        {
+            return Fast486ReadMemory(State,
+                                     (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                     State->SegmentOverride : FAST486_REG_DS,
+                                     Offset,
+                                     FALSE,
+                                     &State->GeneralRegs[FAST486_REG_EAX].LowWord,
+                                     sizeof(USHORT));
+        }
     }
     else
     {
@@ -5211,27 +5228,40 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovEaxOffset)
         }
 
         /* Read from memory */
-        return Fast486ReadMemory(State,
-                                 (State->PrefixFlags & FAST486_PREFIX_SEG) ?
-                                 State->SegmentOverride : FAST486_REG_DS,
-                                 Offset,
-                                 FALSE,
-                                 &State->GeneralRegs[FAST486_REG_EAX].LowWord,
-                                 sizeof(USHORT));
+        if (OperandSize)
+        {
+            return Fast486ReadMemory(State,
+                                     (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                     State->SegmentOverride : FAST486_REG_DS,
+                                     Offset,
+                                     FALSE,
+                                     &State->GeneralRegs[FAST486_REG_EAX].Long,
+                                     sizeof(ULONG));
+        }
+        else
+        {
+            return Fast486ReadMemory(State,
+                                     (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                     State->SegmentOverride : FAST486_REG_DS,
+                                     Offset,
+                                     FALSE,
+                                     &State->GeneralRegs[FAST486_REG_EAX].LowWord,
+                                     sizeof(USHORT));
+        }
     }
 }
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetAl)
 {
-    BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
     ULONG Offset;
 
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xA2);
 
-    TOGGLE_OPSIZE(Size);
+    TOGGLE_ADSIZE(AddressSize);
 
-    if (Size)
+    if (AddressSize)
     {
         if (!Fast486FetchDword(State, &Offset))
         {
@@ -5263,14 +5293,17 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetAl)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetEax)
 {
-    BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+    BOOLEAN OperandSize, AddressSize;
+    
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xA3);
 
-    TOGGLE_OPSIZE(Size);
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
 
-    if (Size)
+    if (AddressSize)
     {
         ULONG Offset;
 
@@ -5281,12 +5314,24 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetEax)
         }
 
         /* Write to memory */
-        return Fast486WriteMemory(State,
-                                  (State->PrefixFlags & FAST486_PREFIX_SEG) ?
-                                  State->SegmentOverride : FAST486_REG_DS,
-                                  Offset,
-                                  &State->GeneralRegs[FAST486_REG_EAX].Long,
-                                  sizeof(ULONG));
+        if (OperandSize)
+        {
+            return Fast486WriteMemory(State,
+                                      (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                      State->SegmentOverride : FAST486_REG_DS,
+                                      Offset,
+                                      &State->GeneralRegs[FAST486_REG_EAX].Long,
+                                      sizeof(ULONG));
+        }
+        else
+        {
+            return Fast486WriteMemory(State,
+                                      (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                      State->SegmentOverride : FAST486_REG_DS,
+                                      Offset,
+                                      &State->GeneralRegs[FAST486_REG_EAX].LowWord,
+                                      sizeof(USHORT));
+        }
     }
     else
     {
@@ -5299,12 +5344,24 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeMovOffsetEax)
         }
 
         /* Write to memory */
-        return Fast486WriteMemory(State,
-                                  (State->PrefixFlags & FAST486_PREFIX_SEG) ?
-                                  State->SegmentOverride : FAST486_REG_DS,
-                                  Offset,
-                                  &State->GeneralRegs[FAST486_REG_EAX].LowWord,
-                                  sizeof(USHORT));
+        if (OperandSize)
+        {
+            return Fast486WriteMemory(State,
+                                      (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                      State->SegmentOverride : FAST486_REG_DS,
+                                      Offset,
+                                      &State->GeneralRegs[FAST486_REG_EAX].Long,
+                                      sizeof(ULONG));
+        }
+        else
+        {
+            return Fast486WriteMemory(State,
+                                      (State->PrefixFlags & FAST486_PREFIX_SEG) ?
+                                      State->SegmentOverride : FAST486_REG_DS,
+                                      Offset,
+                                      &State->GeneralRegs[FAST486_REG_EAX].LowWord,
+                                      sizeof(USHORT));
+        }
     }
 }
 
@@ -5607,7 +5664,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeCmps)
         || (State->PrefixFlags & FAST486_PREFIX_REPNZ))
     {
         BOOLEAN Repeat = TRUE;
-        
+
         if (OperandSize)
         {
             if ((--State->GeneralRegs[FAST486_REG_ECX].Long) == 0)
@@ -5915,7 +5972,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeScas)
         || (State->PrefixFlags & FAST486_PREFIX_REPNZ))
     {
         BOOLEAN Repeat = TRUE;
-        
+
         if (OperandSize)
         {
             if ((--State->GeneralRegs[FAST486_REG_ECX].Long) == 0)