Ejemplo n.º 1
0
        /// <summary>
        ///     Evaluates the given 32-bit operation and parameters to evaluate the status of the Overflow Flag
        /// </summary>
        /// <param name="arithmeticOperation"></param>
        /// <param name="result"></param>
        /// <param name="destination"></param>
        /// <param name="source"></param>
        public void EvaluateOverflow(EnumArithmeticOperation arithmeticOperation, uint result = 0,
                                     uint destination = 0, uint source = 0)
        {
            var setFlag = false;

            switch (arithmeticOperation)
            {
            case EnumArithmeticOperation.Addition:
            {
                //positive+positive==negative
                if (!destination.IsNegative() && !source.IsNegative() &&
                    result.IsNegative())
                {
                    setFlag = true;
                }

                //negative+negative==positive
                if (destination.IsNegative() && source.IsNegative() &&
                    !result.IsNegative())
                {
                    setFlag = true;
                }

                break;
            }

            case EnumArithmeticOperation.Subtraction:
            {
                // negative-positive==positive
                if (destination.IsNegative() && !source.IsNegative() &&
                    !result.IsNegative())
                {
                    setFlag = true;
                }

                // positive-negative==negative
                if (!destination.IsNegative() && source.IsNegative() &&
                    result.IsNegative())
                {
                    setFlag = true;
                }

                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(arithmeticOperation), arithmeticOperation,
                                                      "Unsupported Carry Flag Operation for Evaluation");
            }

            if (setFlag)
            {
                SetFlag(EnumFlags.OF);
            }
            else
            {
                ClearFlag(EnumFlags.OF);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Evaluates the given 32-bit operation and parameters to evaluate the status of the Carry Flag
        /// </summary>
        /// <param name="arithmeticOperation"></param>
        /// <param name="result"></param>
        /// <param name="destination"></param>
        /// <param name="source"></param>
        public void EvaluateCarry(EnumArithmeticOperation arithmeticOperation, uint result = 0,
                                  uint destination = 0, uint source = 0)
        {
            bool setFlag = arithmeticOperation switch
            {
                EnumArithmeticOperation.Addition => ((ulong)source + destination) > uint.MaxValue,
                EnumArithmeticOperation.Subtraction => result > destination,
                EnumArithmeticOperation.ShiftLeft => !result.IsNegative() && destination.IsNegative(),
                _ => throw new ArgumentOutOfRangeException(nameof(arithmeticOperation), arithmeticOperation,
                                                           "Unsupported Carry Flag Operation for Evaluation")
            };

            if (setFlag)
            {
                SetFlag(EnumFlags.CF);
            }
            else
            {
                ClearFlag(EnumFlags.CF);
            }
        }