[FAST486]: Replace (boolean_condition) ? TRUE : FALSE; by: (boolean_condition) .
[reactos.git] / lib / fast486 / extraops.c
index e83312e..c7ccf51 100644 (file)
@@ -21,8 +21,6 @@
 
 /* INCLUDES *******************************************************************/
 
-// #define WIN32_NO_STATUS
-// #define _INC_WINDOWS
 #include <windef.h>
 
 // #define NDEBUG
@@ -40,12 +38,12 @@ FAST486_OPCODE_HANDLER_PROC
 Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
 {
     NULL, // TODO: OPCODE 0x00 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x01 NOT IMPLEMENTED
+    Fast486OpcodeGroup0F01,
     NULL, // TODO: OPCODE 0x02 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0x03 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0x04 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0x05 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x06 NOT IMPLEMENTED
+    Fast486ExtOpcodeClts,
     NULL, // TODO: OPCODE 0x07 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0x08 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0x09 NOT IMPLEMENTED
@@ -71,10 +69,10 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
-    NULL, // TODO: OPCODE 0x20 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x21 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x22 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x23 NOT IMPLEMENTED
+    Fast486ExtOpcodeStoreControlReg,
+    Fast486ExtOpcodeStoreDebugReg,
+    Fast486ExtOpcodeLoadControlReg,
+    Fast486ExtOpcodeLoadDebugReg,
     NULL, // TODO: OPCODE 0x24 NOT IMPLEMENTED
     NULL, // Invalid
     NULL, // TODO: OPCODE 0x26 NOT IMPLEMENTED
@@ -209,7 +207,7 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     NULL, // Invalid
     Fast486ExtOpcodePushGs,
     Fast486ExtOpcodePopGs,
-    NULL, // TODO: OPCODE 0xAA NOT IMPLEMENTED
+    NULL, // Invalid
     Fast486ExtOpcodeBts,
     NULL, // TODO: OPCODE 0xAC NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xAD NOT IMPLEMENTED
@@ -231,8 +229,8 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     NULL, // TODO: OPCODE 0xBD NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xBE NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xBF NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xC0 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xC1 NOT IMPLEMENTED
+    Fast486ExtOpcodeXaddByte,
+    Fast486ExtOpcodeXadd,
     NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
@@ -299,6 +297,219 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
 
 /* PUBLIC FUNCTIONS ***********************************************************/
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeClts)
+{
+    NO_LOCK_PREFIX();
+
+    /* The current privilege level must be zero */
+    if (Fast486GetCurrentPrivLevel(State) != 0)
+    {
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    /* Clear the task switch bit */
+    State->ControlRegisters[FAST486_REG_CR0] &= ~FAST486_CR0_TS;
+
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeStoreControlReg)
+{
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    NO_LOCK_PREFIX();
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* The current privilege level must be zero */
+    if (Fast486GetCurrentPrivLevel(State) != 0)
+    {
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    if ((ModRegRm.Register == 1) || (ModRegRm.Register > 3))
+    {
+        /* CR1, CR4, CR5, CR6 and CR7 don't exist */
+        Fast486Exception(State, FAST486_EXCEPTION_UD);
+        return FALSE;
+    }
+
+    if (ModRegRm.Register != 0)
+    {
+        /* CR2 and CR3 and are stored in array indexes 1 and 2 */
+        ModRegRm.Register--;
+    }
+
+    /* Store the value of the control register */
+    State->GeneralRegs[ModRegRm.SecondRegister].Long = State->ControlRegisters[ModRegRm.Register];
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeStoreDebugReg)
+{
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    NO_LOCK_PREFIX();
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* The current privilege level must be zero */
+    if (Fast486GetCurrentPrivLevel(State) != 0)
+    {
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    if ((ModRegRm.Register == 6) || (ModRegRm.Register == 7))
+    {
+        /* DR6 and DR7 are aliases to DR4 and DR5 */
+        ModRegRm.Register -= 2;
+    }
+
+    if (State->DebugRegisters[FAST486_REG_DR5] & FAST486_DR5_GD)
+    {
+        /* Disallow access to debug registers */
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    /* Store the value of the debug register */
+    State->GeneralRegs[ModRegRm.SecondRegister].Long = State->DebugRegisters[ModRegRm.Register];
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeLoadControlReg)
+{
+    ULONG Value;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    NO_LOCK_PREFIX();
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* The current privilege level must be zero */
+    if (Fast486GetCurrentPrivLevel(State) != 0)
+    {
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    if ((ModRegRm.Register == 1) || (ModRegRm.Register > 3))
+    {
+        /* CR1, CR4, CR5, CR6 and CR7 don't exist */
+        Fast486Exception(State, FAST486_EXCEPTION_UD);
+        return FALSE;
+    }
+    
+    if (ModRegRm.Register != 0)
+    {
+        /* CR2 and CR3 and are stored in array indexes 1 and 2 */
+        ModRegRm.Register--;
+    }
+
+    /* Get the value */
+    Value = State->GeneralRegs[ModRegRm.SecondRegister].Long;
+
+    if (ModRegRm.Register == (INT)FAST486_REG_CR0)
+    {
+        /* CR0 checks */
+
+        if (((Value & (FAST486_CR0_PG | FAST486_CR0_PE)) == FAST486_CR0_PG)
+            || ((Value & (FAST486_CR0_CD | FAST486_CR0_NW)) == FAST486_CR0_NW))
+        {
+            /* Invalid value */
+            Fast486Exception(State, FAST486_EXCEPTION_GP);
+            return FALSE;
+        }
+    }
+
+    /* Load a value to the control register */
+    State->ControlRegisters[ModRegRm.Register] = Value;
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeLoadDebugReg)
+{
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    NO_LOCK_PREFIX();
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* The current privilege level must be zero */
+    if (Fast486GetCurrentPrivLevel(State) != 0)
+    {
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    if ((ModRegRm.Register == 6) || (ModRegRm.Register == 7))
+    {
+        /* DR6 and DR7 are aliases to DR4 and DR5 */
+        ModRegRm.Register -= 2;
+    }
+
+    if (State->DebugRegisters[FAST486_REG_DR5] & FAST486_DR5_GD)
+    {
+        /* Disallow access to debug registers */
+        Fast486Exception(State, FAST486_EXCEPTION_GP);
+        return FALSE;
+    }
+
+    /* Load a value to the debug register */
+    State->DebugRegisters[ModRegRm.Register] = State->GeneralRegs[ModRegRm.SecondRegister].Long;
+
+    if (ModRegRm.Register == (INT)FAST486_REG_DR4)
+    {
+        /* The reserved bits are 1 */
+        State->DebugRegisters[ModRegRm.Register] |= FAST486_DR4_RESERVED;
+    }
+    else if (ModRegRm.Register == (INT)FAST486_REG_DR5)
+    {
+        /* The reserved bits are 0 */
+        State->DebugRegisters[ModRegRm.Register] &= ~FAST486_DR5_RESERVED;
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodePushFs)
 {
     /* Call the internal API */
@@ -532,8 +743,8 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchgByte)
     State->Flags.Of = ((Accumulator & SIGN_FLAG_BYTE) != (Destination & SIGN_FLAG_BYTE))
                       && ((Accumulator & SIGN_FLAG_BYTE) != (Result & SIGN_FLAG_BYTE));
     State->Flags.Af = (Accumulator & 0x0F) < (Destination & 0x0F);
-    State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-    State->Flags.Sf = (Result & SIGN_FLAG_BYTE) ? TRUE : FALSE;
+    State->Flags.Zf = (Result == 0);
+    State->Flags.Sf = ((Result & SIGN_FLAG_BYTE) != 0);
     State->Flags.Pf = Fast486CalculateParity(Result);
 
     if (State->Flags.Zf)
@@ -588,8 +799,8 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchg)
         State->Flags.Of = ((Accumulator & SIGN_FLAG_LONG) != (Destination & SIGN_FLAG_LONG))
                           && ((Accumulator & SIGN_FLAG_LONG) != (Result & SIGN_FLAG_LONG));
         State->Flags.Af = (Accumulator & 0x0F) < (Destination & 0x0F);
-        State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-        State->Flags.Sf = (Result & SIGN_FLAG_LONG) ? TRUE : FALSE;
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_LONG) != 0);
         State->Flags.Pf = Fast486CalculateParity(Result);
 
         if (State->Flags.Zf)
@@ -623,8 +834,8 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchg)
         State->Flags.Of = ((Accumulator & SIGN_FLAG_WORD) != (Destination & SIGN_FLAG_WORD))
                           && ((Accumulator & SIGN_FLAG_WORD) != (Result & SIGN_FLAG_WORD));
         State->Flags.Af = (Accumulator & 0x0F) < (Destination & 0x0F);
-        State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-        State->Flags.Sf = (Result & SIGN_FLAG_WORD) ? TRUE : FALSE;
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_WORD) != 0);
         State->Flags.Pf = Fast486CalculateParity(Result);
 
         if (State->Flags.Zf)
@@ -1022,6 +1233,165 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalSet)
     return Fast486WriteModrmByteOperands(State, &ModRegRm, FALSE, Value);
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXaddByte)
+{
+    UCHAR Source, Destination, Result;
+    FAST486_MOD_REG_RM ModRegRm;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xC0);
+
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (!Fast486ReadModrmByteOperands(State,
+                                      &ModRegRm,
+                                      &Source,
+                                      &Destination))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Calculate the result */
+    Result = Source + Destination;
+
+    /* Update the flags */
+    State->Flags.Cf = (Result < Source) && (Result < Destination);
+    State->Flags.Of = ((Source & SIGN_FLAG_BYTE) == (Destination & SIGN_FLAG_BYTE))
+                      && ((Source & SIGN_FLAG_BYTE) != (Result & SIGN_FLAG_BYTE));
+    State->Flags.Af = ((((Source & 0x0F) + (Destination & 0x0F)) & 0x10) != 0);
+    State->Flags.Zf = (Result == 0);
+    State->Flags.Sf = ((Result & SIGN_FLAG_BYTE) != 0);
+    State->Flags.Pf = Fast486CalculateParity(Result);
+
+    /* Write the sum to the destination */
+    if (!Fast486WriteModrmByteOperands(State, &ModRegRm, FALSE, Result))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Write the old value of the destination to the source */
+    if (!Fast486WriteModrmByteOperands(State, &ModRegRm, TRUE, Destination))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXadd)
+{
+    FAST486_MOD_REG_RM ModRegRm;
+    BOOLEAN OperandSize, AddressSize;
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xC1);
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    TOGGLE_ADSIZE(AddressSize);
+    TOGGLE_OPSIZE(OperandSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Check the operand size */
+    if (OperandSize)
+    {
+        ULONG Source, Destination, Result;
+
+        if (!Fast486ReadModrmDwordOperands(State,
+                                          &ModRegRm,
+                                          &Source,
+                                          &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    
+        /* Calculate the result */
+        Result = Source + Destination;
+
+        /* Update the flags */
+        State->Flags.Cf = (Result < Source) && (Result < Destination);
+        State->Flags.Of = ((Source & SIGN_FLAG_LONG) == (Destination & SIGN_FLAG_LONG))
+                          && ((Source & SIGN_FLAG_LONG) != (Result & SIGN_FLAG_LONG));
+        State->Flags.Af = ((((Source & 0x0F) + (Destination & 0x0F)) & 0x10) != 0);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_LONG) != 0);
+        State->Flags.Pf = Fast486CalculateParity(Result);
+
+        /* Write the sum to the destination */
+        if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Result))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Write the old value of the destination to the source */
+        if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, TRUE, Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        USHORT Source, Destination, Result;
+
+        if (!Fast486ReadModrmWordOperands(State,
+                                          &ModRegRm,
+                                          &Source,
+                                          &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    
+        /* Calculate the result */
+        Result = Source + Destination;
+
+        /* Update the flags */
+        State->Flags.Cf = (Result < Source) && (Result < Destination);
+        State->Flags.Of = ((Source & SIGN_FLAG_WORD) == (Destination & SIGN_FLAG_WORD))
+                          && ((Source & SIGN_FLAG_WORD) != (Result & SIGN_FLAG_WORD));
+        State->Flags.Af = ((((Source & 0x0F) + (Destination & 0x0F)) & 0x10) != 0);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_WORD) != 0);
+        State->Flags.Pf = Fast486CalculateParity(Result);
+
+        /* Write the sum to the destination */
+        if (!Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Result))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Write the old value of the destination to the source */
+        if (!Fast486WriteModrmWordOperands(State, &ModRegRm, TRUE, Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    return TRUE;
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBswap)
 {
     PUCHAR Pointer;
@@ -1062,3 +1432,4 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeExtended)
         return FALSE;
     }
 }
+