[FAST486]
[reactos.git] / lib / fast486 / extraops.c
index 18dc09a..e83312e 100644 (file)
@@ -31,6 +31,7 @@
 #include <fast486.h>
 #include "opcodes.h"
 #include "common.h"
+#include "opgroups.h"
 #include "extraops.h"
 
 /* PUBLIC VARIABLES ***********************************************************/
@@ -198,34 +199,34 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     Fast486ExtOpcodeConditionalSet,
     Fast486ExtOpcodeConditionalSet,
     Fast486ExtOpcodeConditionalSet,
-    NULL, // TODO: OPCODE 0xA0 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xA1 NOT IMPLEMENTED
+    Fast486ExtOpcodePushFs,
+    Fast486ExtOpcodePopFs,
     NULL, // Invalid
-    NULL, // TODO: OPCODE 0xA3 NOT IMPLEMENTED
+    Fast486ExtOpcodeBitTest,
     NULL, // TODO: OPCODE 0xA4 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xA5 NOT IMPLEMENTED
     NULL, // Invalid
     NULL, // Invalid
-    NULL, // TODO: OPCODE 0xA8 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xA9 NOT IMPLEMENTED
+    Fast486ExtOpcodePushGs,
+    Fast486ExtOpcodePopGs,
     NULL, // TODO: OPCODE 0xAA NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xAB NOT IMPLEMENTED
+    Fast486ExtOpcodeBts,
     NULL, // TODO: OPCODE 0xAC NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xAD NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xAE NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xAF NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB0 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB1 NOT IMPLEMENTED
+    Fast486ExtOpcodeCmpXchgByte,
+    Fast486ExtOpcodeCmpXchg,
     NULL, // TODO: OPCODE 0xB2 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB3 NOT IMPLEMENTED
+    Fast486ExtOpcodeBtr,
     NULL, // TODO: OPCODE 0xB4 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xB5 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xB6 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xB7 NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xB8 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB9 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xBA NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xBB NOT IMPLEMENTED
+    Fast486OpcodeGroup0FB9,
+    Fast486OpcodeGroup0FBA,
+    Fast486ExtOpcodeBtc,
     NULL, // TODO: OPCODE 0xBC NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xBD NOT IMPLEMENTED
     NULL, // TODO: OPCODE 0xBE NOT IMPLEMENTED
@@ -238,14 +239,14 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
-    NULL, // TODO: OPCODE 0xC8 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xC9 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xCA NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xCB NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xCC NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xCD NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xCE NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xCF NOT IMPLEMENTED
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
+    Fast486ExtOpcodeBswap,
     NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
@@ -298,25 +299,541 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
 
 /* PUBLIC FUNCTIONS ***********************************************************/
 
-FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalJmp)
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodePushFs)
 {
-    BOOLEAN Jump = FALSE;
-    LONG Offset = 0;
-    BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+    /* Call the internal API */
+    return Fast486StackPush(State, State->SegmentRegs[FAST486_REG_FS].Selector);
+}
 
-    if (State->PrefixFlags & FAST486_PREFIX_OPSIZE)
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodePopFs)
+{
+    ULONG NewSelector;
+
+    if (!Fast486StackPop(State, &NewSelector))
     {
-        /* The OPSIZE prefix toggles the size */
-        Size = !Size;
+        /* Exception occurred */
+        return FALSE;
     }
 
-    if (State->PrefixFlags & FAST486_PREFIX_LOCK)
+    /* Call the internal API */
+    return Fast486LoadSegment(State, FAST486_REG_FS, LOWORD(NewSelector));
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBitTest)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    UINT DataSize;
+    ULONG BitNumber;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the number of bits */
+    if (OperandSize) DataSize = 32;
+    else DataSize = 16;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
     {
-        /* Invalid prefix */
-        Fast486Exception(State, FAST486_EXCEPTION_UD);
+        /* Exception occurred */
         return FALSE;
     }
 
+    /* Get the bit number */
+    BitNumber = OperandSize ? State->GeneralRegs[ModRegRm.Register].Long
+                            : (ULONG)State->GeneralRegs[ModRegRm.Register].LowWord;
+
+    if (ModRegRm.Memory)
+    {
+        /*
+         * For memory operands, add the bit offset divided by
+         * the data size to the address
+         */
+        ModRegRm.MemoryAddress += BitNumber / DataSize;
+    }
+
+    /* Normalize the bit number */
+    BitNumber &= (1 << DataSize) - 1;
+
+    if (OperandSize)
+    {
+        ULONG Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+    }
+    else
+    {
+        USHORT Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodePushGs)
+{
+    /* Call the internal API */
+    return Fast486StackPush(State, State->SegmentRegs[FAST486_REG_GS].Selector);
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodePopGs)
+{
+    ULONG NewSelector;
+
+    if (!Fast486StackPop(State, &NewSelector))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Call the internal API */
+    return Fast486LoadSegment(State, FAST486_REG_GS, LOWORD(NewSelector));
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBts)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    UINT DataSize;
+    ULONG BitNumber;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the number of bits */
+    if (OperandSize) DataSize = 32;
+    else DataSize = 16;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Get the bit number */
+    BitNumber = OperandSize ? State->GeneralRegs[ModRegRm.Register].Long
+                            : (ULONG)State->GeneralRegs[ModRegRm.Register].LowWord;
+
+    if (ModRegRm.Memory)
+    {
+        /*
+         * For memory operands, add the bit offset divided by
+         * the data size to the address
+         */
+        ModRegRm.MemoryAddress += BitNumber / DataSize;
+    }
+
+    /* Normalize the bit number */
+    BitNumber &= (1 << DataSize) - 1;
+
+    if (OperandSize)
+    {
+        ULONG Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+
+        /* Set the bit */
+        Value |= 1 << BitNumber;
+
+        /* Write back the result */
+        if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        USHORT Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+
+        /* Set the bit */
+        Value |= 1 << BitNumber;
+
+        /* Write back the result */
+        if (!Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchgByte)
+{
+    FAST486_MOD_REG_RM ModRegRm;
+    UCHAR Accumulator = State->GeneralRegs[FAST486_REG_EAX].LowByte;
+    UCHAR Source, Destination, Result;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the operands */
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Source, &Destination))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Compare AL with the destination */
+    Result = Accumulator - Destination;
+
+    /* Update the flags */
+    State->Flags.Cf = Accumulator < Destination;
+    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.Pf = Fast486CalculateParity(Result);
+
+    if (State->Flags.Zf)
+    {
+        /* Load the source operand into the destination */
+        return Fast486WriteModrmByteOperands(State, &ModRegRm, FALSE, Source);
+    }
+    else
+    {
+        /* Load the destination into AL */
+        State->GeneralRegs[FAST486_REG_EAX].LowByte = Destination;
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchg)
+{
+    FAST486_MOD_REG_RM ModRegRm;
+    BOOLEAN OperandSize, AddressSize;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (OperandSize)
+    {
+        ULONG Source, Destination, Result;
+        ULONG Accumulator = State->GeneralRegs[FAST486_REG_EAX].Long;
+
+        /* Read the operands */
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Source, &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Compare EAX with the destination */
+        Result = Accumulator - Destination;
+
+        /* Update the flags */
+        State->Flags.Cf = Accumulator < Destination;
+        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.Pf = Fast486CalculateParity(Result);
+
+        if (State->Flags.Zf)
+        {
+            /* Load the source operand into the destination */
+            return Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Source);
+        }
+        else
+        {
+            /* Load the destination into EAX */
+            State->GeneralRegs[FAST486_REG_EAX].Long = Destination;
+        }
+    }
+    else
+    {
+        USHORT Source, Destination, Result;
+        USHORT Accumulator = State->GeneralRegs[FAST486_REG_EAX].LowWord;
+
+        /* Read the operands */
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Source, &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Compare AX with the destination */
+        Result = Accumulator - Destination;
+
+        /* Update the flags */
+        State->Flags.Cf = Accumulator < Destination;
+        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.Pf = Fast486CalculateParity(Result);
+
+        if (State->Flags.Zf)
+        {
+            /* Load the source operand into the destination */
+            return Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Source);
+        }
+        else
+        {
+            /* Load the destination into AX */
+            State->GeneralRegs[FAST486_REG_EAX].LowWord = Destination;
+        }
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBtr)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    UINT DataSize;
+    ULONG BitNumber;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the number of bits */
+    if (OperandSize) DataSize = 32;
+    else DataSize = 16;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Get the bit number */
+    BitNumber = OperandSize ? State->GeneralRegs[ModRegRm.Register].Long
+                            : (ULONG)State->GeneralRegs[ModRegRm.Register].LowWord;
+
+    if (ModRegRm.Memory)
+    {
+        /*
+         * For memory operands, add the bit offset divided by
+         * the data size to the address
+         */
+        ModRegRm.MemoryAddress += BitNumber / DataSize;
+    }
+
+    /* Normalize the bit number */
+    BitNumber &= (1 << DataSize) - 1;
+
+    if (OperandSize)
+    {
+        ULONG Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+
+        /* Clear the bit */
+        Value &= ~(1 << BitNumber);
+
+        /* Write back the result */
+        if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        USHORT Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+
+        /* Clear the bit */
+        Value &= ~(1 << BitNumber);
+
+        /* Write back the result */
+        if (!Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBtc)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    UINT DataSize;
+    ULONG BitNumber;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the number of bits */
+    if (OperandSize) DataSize = 32;
+    else DataSize = 16;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Get the bit number */
+    BitNumber = OperandSize ? State->GeneralRegs[ModRegRm.Register].Long
+                            : (ULONG)State->GeneralRegs[ModRegRm.Register].LowWord;
+
+    if (ModRegRm.Memory)
+    {
+        /*
+         * For memory operands, add the bit offset divided by
+         * the data size to the address
+         */
+        ModRegRm.MemoryAddress += BitNumber / DataSize;
+    }
+
+    /* Normalize the bit number */
+    BitNumber &= (1 << DataSize) - 1;
+
+    if (OperandSize)
+    {
+        ULONG Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+
+        /* Toggle the bit */
+        Value ^= 1 << BitNumber;
+
+        /* Write back the result */
+        if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        USHORT Dummy, Value;
+
+        /* Read the value */
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Set CF to the bit value */
+        State->Flags.Cf = (Value >> BitNumber) & 1;
+
+        /* Toggle the bit */
+        Value ^= 1 << BitNumber;
+
+        /* Write back the result */
+        if (!Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    /* Return success */
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalJmp)
+{
+    BOOLEAN Jump = FALSE;
+    LONG Offset = 0;
+    BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    TOGGLE_OPSIZE(Size);
+    NO_LOCK_PREFIX();
+
     /* Make sure this is the right instruction */
     ASSERT((Opcode & 0xF0) == 0x80);
 
@@ -424,11 +941,7 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalSet)
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
     FAST486_MOD_REG_RM ModRegRm;
 
-    if (State->PrefixFlags & FAST486_PREFIX_ADSIZE)
-    {
-        /* The OPSIZE prefix toggles the size */
-        AddressSize = !AddressSize;
-    }
+    TOGGLE_ADSIZE(AddressSize);
 
     /* Get the operands */
     if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
@@ -509,6 +1022,23 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalSet)
     return Fast486WriteModrmByteOperands(State, &ModRegRm, FALSE, Value);
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBswap)
+{
+    PUCHAR Pointer;
+
+    NO_LOCK_PREFIX();
+
+    /* Get a pointer to the value */
+    Pointer = (PUCHAR)&State->GeneralRegs[Opcode & 0x07].Long;
+
+    /* Swap the byte order */
+    SWAP(Pointer[0], Pointer[3]);
+    SWAP(Pointer[1], Pointer[2]);
+
+    /* Return success */
+    return TRUE;
+}
+
 FAST486_OPCODE_HANDLER(Fast486OpcodeExtended)
 {
     UCHAR SecondOpcode;