Beispiel #1
0
        /// <summary>
        /// Validates the instruction.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="undecoded">The undecoded.</param>
        /// <returns></returns>
        private bool ValidateInstruction(InstructionTyp type, string undecoded)
        {
            if (instructionParameter[type] == 0)
            {
                return(true);
            }

            try
            {
                if (!Regex.IsMatch(undecoded.Trim(), instructionValidationPattern[type]))
                {
                    return(false);
                }
            }
            catch { return(false); }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Executes the specified a.
        /// </summary>
        /// <param name="a">A.</param>
        /// <param name="b">The b.</param>
        /// <param name="instruction">The instruction.</param>
        public void Execute(int a, int b, InstructionTyp instruction)
        {
            switch (instruction)
            {
            case InstructionTyp.AND:
                R = a & b;
                break;

            case InstructionTyp.OR:
                R = a | b;
                break;

            case InstructionTyp.XOR:
                R = a ^ b;
                break;

            case InstructionTyp.ADD:
                try
                {
                    checked
                    {
                        R = a + b;
                    }
                    Carry = 0;
                }
                catch (OverflowException oEx)
                {
                    //Carry wenn Ergebnis Ueberlauf verursacht
                    Carry = 1;
                }
                catch {; }

                break;

            case InstructionTyp.SUB:
                try
                {
                    checked
                    {
                        R = a - b;
                    }
                    Carry = 0;
                }
                catch (OverflowException oEx)
                {
                    //Carry wenn Ergebnis UEberlauf verursacht
                    Carry = 1;
                }
                catch {; }

                break;

            case InstructionTyp.SHR:
                R = a >> b;
                //Carry haelt letztes rausgeschobenes Bit
                try
                {
                    Carry = Convert.ToString(a, 2).ToCharArray()[(b - 1)];
                }
                catch {; }
                break;

            case InstructionTyp.SHL:
                R = a << b;
                //Carry haelt letztes rausgeschobenes Bit
                try
                {
                    string bits = Convert.ToString(a, 2);
                    Carry = bits.ToCharArray()[bits.Length - b];
                }
                catch {; }
                break;

            case InstructionTyp.RR:
                R = (a >> b) | (a << (32 - b));

                try
                {
                    //C haelt letztes rotiertes Bit (Result Bit 31)
                    Carry = Convert.ToInt32(Convert.ToString(R, 2)[31]);
                }
                catch {; }
                break;

            case InstructionTyp.RL:
                R = (a << b) | (a >> (32 - b));
                try
                {
                    //C haelt letztes rotiertes Bit (Result Bit 0)
                    Carry = Convert.ToInt32(Convert.ToString(R, 2)[0]);
                }
                catch {; }
                break;

            case InstructionTyp.RRC:
                //rotate right with carry
                //result = (a >> b + 1) | (a << (32 - b + 1))
                R = RotateRightWithCarry(a, b);
                break;

            case InstructionTyp.RLC:
                // rotate left with carry
                //result = (a << b + 1) | (a >> (32 - b + 1));
                R = RotateLeftWithCarry(a, b);
                break;

            case InstructionTyp.DIV:
                double d    = (double)a / b;
                int    even = (int)d;

                Carry = 0;
                if (d - even > 0)
                {
                    Carry = 1;
                }

                //int / int => int
                R = a / b;
                break;

            case InstructionTyp.MUL:
                try
                {
                    checked
                    {
                        R = a * b;
                    }
                    Carry = 0;
                }
                catch
                {
                    Carry = 1;
                }

                break;
            }

            Negative = (R < 0) ? 1 : 0;
            Zero     = (R == 0) ? 1 : 0;
        }