/// <summary>
 /// Creates a new TestOperation. (INCFSZ f,d / DECFSZ f,d)
 /// </summary>
 /// <param name="_sourceAddress">f</param>
 /// <param name="_op">INC / DEC</param>
 /// <param name="_targetAddress">the address where the result should be stored in</param>
 /// <param name="_registerFileMap"></param>
 /// <param name="_address">code address</param>
 public TestOperation(short _sourceAddress, TestOperator _op, short _targetAddress, Register.ProgramCounter _programCounter, Register.RegisterFileMap _registerFileMap, short _address)
     : base(_registerFileMap, CYCLES, _address)
 {
     op = _op;
     sourceAddress = _sourceAddress;
     targetAddress = _targetAddress;
     programCounter = _programCounter;
 }
Esempio n. 2
0
        static void DisplayBasics()
        {
            int test = -1;

            while (test != 0)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                Console.Write("Basics - Enter exercise no: (1 to 6) ");
                string input = Console.ReadLine();

                Console.Clear();
                int.TryParse(input, out test);
                Console.ForegroundColor = ConsoleColor.Green;
                switch (test)
                {
                case 0:
                    break;

                case 1:
                    TypeCodeExample tc = new TypeCodeExample("ex. 1.1 - TypeCode Enumeration");
                    NullCoalescingOperatorExample nco = new NullCoalescingOperatorExample("ex. 1.2 - Null Coalescing Operator");
                    TestOperator    to  = new TestOperator("Ex. 1.3 - Test Operator", null);
                    FormatExample   fe  = new FormatExample("ex. 1.4 - Format");
                    DelegateExample del = new DelegateExample(); del.Example("ex. 1.5 - Delegates");
                    del.Example();
                    break;

                case 2:
                    DelegateExample del2 = new DelegateExample(); del2.ActionExample("ex. 2.1 - Actions / Delegates\n");
                    break;

                case 3:
                    DelegateExample del3 = new DelegateExample(); del3.AnonymousExample("ex.3.2 - Actions / Delegates - anonymous\n");
                    break;

                case 4:
                    DelegateExample del4 = new DelegateExample(); del4.LambdaExample("ex. 4.1 - Actions / Delegates - lambdas\n");
                    break;

                case 5:
                    ConstructorChain cc1 = new ConstructorChain("ex. 5.1 - Constructor Chain:");
                    ConstructorChain cc2 = new ConstructorChain();
                    break;

                case 6:
                    string s = "Hello Extension Methods";
                    int    i = s.WordCount();
                    Console.WriteLine($"\tex. 6.1 - Extension Methods: The text \"{s}\" has {i} words");
                    break;

                default:
                    Console.WriteLine("Not implemented!");
                    break;
                }
            }
        }
Esempio n. 3
0
        public void TestOperator01()
        {
            int a = 1 + 2;

            Assert.IsTrue(a == 3);
            Console.WriteLine("Test 1 (int a = new TestOperator(2) + 1):");
            a = new TestOperator(2) + 1;
            Assert.IsTrue(a == 3);

            Console.WriteLine("Test 2 (TestOperator b = 1 + 2):");
            TestOperator b = 1 + 2;

            Assert.IsTrue(a == 3);

            Console.WriteLine("Test 3 (TestOperator c = 2 + new TestOperator(1)):");
            TestOperator c = 2 + new TestOperator(1);

            Assert.IsTrue(c == 3);

            Console.WriteLine("Test 4 (int c = 2 + new TestOperator(1)):");
            int d = 2 + new TestOperator(1);

            Assert.IsTrue(d == 3);

            Console.WriteLine("Test 5 (int c = new TestOperator(1) + 2):");
            int e = new TestOperator(1) + 2;

            Assert.IsTrue(e == 3);

            Console.WriteLine("Test 6 (int c = new TestOperator(1) + 2):");
            int f = (byte)1 + new TestOperator(2);

            Assert.IsTrue(f == 3);

            Console.WriteLine("Methods:");
            Type t = typeof(TestOperator);

            foreach (MethodInfo mi in t.GetMethods(BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.DeclaredOnly))
            {
                Console.WriteLine(mi.Name);
            }
        } // proc TestOperator01
Esempio n. 4
0
        public BaseOperation getNextOperation(short _codeAdress)
        {
            this.address = _codeAdress;

            // mask Operation-Byte --> xxxx xxxx 0000 0000
            short operation = (short)((short)programMemory[_codeAdress] & 0xFF00);
            // mask Parameter-Byte --> 0000 0000 xxxx xxxx
            short parameter = (short)((short)programMemory[_codeAdress] & 0x00FF);

            if (parameter < 0)
                throw new Exception("negative parameter");

            switch (operation)
            {
                /* ------------------------------------------------------ */
                /* -------- ARITHMETIC OPERATIONS ----------------------- */
                case ParserConstants.ADDWF:
                    // arithmetical operator
                    ArithOp = ArithmeticOperator.PLUS;
                    // target address
                    target = getTargetAddress(parameter);
                    // operating bytes
                    byte1 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    byte2 = registerFileMap.Get(getAddressFromParameter(parameter));
                    return new ArithmeticOperation(byte1, byte2, ArithOp, target, registerFileMap, address);
                case ParserConstants.ADDLW_1:
                case ParserConstants.ADDLW_2:
                    ArithOp = ArithmeticOperator.PLUS;
                    target = RegisterConstants.WORKING_REGISTER_ADDRESS;
                    byte1 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    byte2 = getLiteralFromParameter(parameter);
                    return new ArithmeticOperation(byte1, byte2, ArithOp, target, registerFileMap, address);
                case ParserConstants.INCF:
                    ArithOp = ArithmeticOperator.PLUS;
                    target = getTargetAddress(parameter);
                    byte1 = 0x01;
                    byte2 = registerFileMap.Get(getAddressFromParameter(parameter));
                    return new ArithmeticOperation(byte1, byte2, ArithOp, target, registerFileMap, address);
                case ParserConstants.SUBWF:
                    ArithOp = ArithmeticOperator.MINUS;
                    target = getTargetAddress(parameter);
                    byte1 = registerFileMap.Get(getAddressFromParameter(parameter));
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new ArithmeticOperation(byte1, byte2, ArithOp, target, registerFileMap, address);
                case ParserConstants.SUBLW_1:
                case ParserConstants.SUBLW_2:
                    ArithOp = ArithmeticOperator.MINUS;
                    target = RegisterConstants.WORKING_REGISTER_ADDRESS;
                    byte1 = getLiteralFromParameter(parameter);
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new ArithmeticOperation(byte1, byte2, ArithOp, target, registerFileMap, address);
                case ParserConstants.DECF:
                    ArithOp = ArithmeticOperator.MINUS;
                    target = getTargetAddress(parameter);
                    byte1 = registerFileMap.Get(getAddressFromParameter(parameter));
                    byte2 = 0x01;
                    return new ArithmeticOperation(byte1, byte2, ArithOp, target, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- BIT OPERATIONS ------------------------------ */
                case ParserConstants.BCF_1:
                case ParserConstants.BCF_2:
                case ParserConstants.BCF_3:
                case ParserConstants.BCF_4:
                    // bit operator
                    BitOp = BitOperator.BITCLEAR;
                    // target address
                    target = getAddressFromParameter(parameter);
                    // bit-number
                    bit = getBitNumberFromOperationCall(operation, parameter);
                    return new BitOperation(target, bit, BitOp, registerFileMap, address);
                case ParserConstants.BSF_1:
                case ParserConstants.BSF_2:
                case ParserConstants.BSF_3:
                case ParserConstants.BSF_4:
                    BitOp = BitOperator.BITSET;
                    target = getAddressFromParameter(parameter);
                    bit = getBitNumberFromOperationCall(operation, parameter);
                    return new BitOperation(target, bit, BitOp, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- CALL OPERATIONS ----------------------------- */
                case ParserConstants.CALL_1:
                case ParserConstants.CALL_2:
                case ParserConstants.CALL_3:
                case ParserConstants.CALL_4:
                case ParserConstants.CALL_5:
                case ParserConstants.CALL_6:
                case ParserConstants.CALL_7:
                case ParserConstants.CALL_8:
                    target = getTargetAddress(operation, parameter);
                    return new CallOperation(target, operationStack, programCounter, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- GOTO OPERATION ------------------------------ */
                case ParserConstants.GOTO_1:
                case ParserConstants.GOTO_2:
                case ParserConstants.GOTO_3:
                case ParserConstants.GOTO_4:
                case ParserConstants.GOTO_5:
                case ParserConstants.GOTO_6:
                case ParserConstants.GOTO_7:
                case ParserConstants.GOTO_8:
                    target = getTargetAddress(operation, parameter);
                    return new GotoOperation(target, programCounter, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- CLEAR OPERATIONS ---------------------------- */
                case ParserConstants.CLRF_CLRW:
                    target = getTargetAddress(parameter);
                    return new ClearOperation(target, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- COMPLEMENT OPERATIONS ----------------------- */
                case ParserConstants.COMF:
                    target = getTargetAddress(parameter);
                    if (target == RegisterConstants.WORKING_REGISTER_ADDRESS)
                        return new ComplementOperation(getAddressFromParameter(parameter), target, registerFileMap, address);
                    else
                        return new ComplementOperation(target, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- LOGIC OPERATIONS ---------------------------- */
                case ParserConstants.ANDWF:
                    LogOp = LogicOperator.AND;
                    target = getTargetAddress(parameter);
                    byte1 = registerFileMap.Get(getAddressFromParameter(parameter));
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new LogicOperation(byte1, byte2, LogOp, target, registerFileMap, address);
                case ParserConstants.ANDLW:
                    LogOp = LogicOperator.AND;
                    target = RegisterConstants.WORKING_REGISTER_ADDRESS;
                    byte1 = getLiteralFromParameter(parameter);
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new LogicOperation(byte1, byte2, LogOp, target, registerFileMap, address);
                case ParserConstants.IORWF:
                    LogOp = LogicOperator.IOR;
                    target = getTargetAddress(parameter);
                    byte1 = registerFileMap.Get(getAddressFromParameter(parameter));
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new LogicOperation(byte1, byte2, LogOp, target, registerFileMap, address);
                case ParserConstants.IORLW:
                    LogOp = LogicOperator.IOR;
                    target = RegisterConstants.WORKING_REGISTER_ADDRESS;
                    byte1 = getLiteralFromParameter(parameter);
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new LogicOperation(byte1, byte2, LogOp, target, registerFileMap, address);
                case ParserConstants.XORWF:
                    LogOp = LogicOperator.XOR;
                    target = getTargetAddress(parameter);
                    byte1 = registerFileMap.Get(getAddressFromParameter(parameter));
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new LogicOperation(byte1, byte2, LogOp, target, registerFileMap, address);
                case ParserConstants.XORLW:
                    LogOp = LogicOperator.XOR;
                    target = RegisterConstants.WORKING_REGISTER_ADDRESS;
                    byte1 = getLiteralFromParameter(parameter);
                    byte2 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                    return new LogicOperation(byte1, byte2, LogOp, target, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- MOVE OPERATIONS ----------------------------- */
                case ParserConstants.MOVF:
                    target = getTargetAddress(parameter);
                    source = getAddressFromParameter(parameter);
                    return new MoveOperation(source, target, registerFileMap, address);
                case ParserConstants.MOVLW_1:
                case ParserConstants.MOVLW_2:
                case ParserConstants.MOVLW_3:
                case ParserConstants.MOVLW_4:
                    byte1 = getLiteralFromParameter(parameter);
                    target = RegisterConstants.WORKING_REGISTER_ADDRESS;
                    return new MoveOperation(byte1, target, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- ROTATE OPERATIONS --------------------------- */
                case ParserConstants.RLF:
                    RotDir = RotationDirection.LEFT;
                    target = getTargetAddress(parameter);
                    source = getAddressFromParameter(parameter);
                    return new RotateOperation(source, target, RotDir, registerFileMap, address);
                case ParserConstants.RRF:
                    RotDir = RotationDirection.RIGHT;
                    target = getTargetAddress(parameter);
                    source = getAddressFromParameter(parameter);
                    return new RotateOperation(source, target, RotDir, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- SWAP OPERATIONS ----------------------------- */
                case ParserConstants.SWAPF:
                    target = getTargetAddress(parameter);
                    source = getAddressFromParameter(parameter);
                    return new SwapOperation(source, target, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- BIT TEST OPERATIONS ----------------------------- */
                case ParserConstants.DECFSZ:
                    TestOp = TestOperator.DECFSZ;
                    target = getTargetAddress(parameter);
                    source = getAddressFromParameter(parameter);
                    return new TestOperation(source, TestOp, target, programCounter, registerFileMap, address);
                case ParserConstants.INCFSZ:
                    TestOp = TestOperator.INCFSZ;
                    target = getTargetAddress(parameter);
                    source = getAddressFromParameter(parameter);
                    return new TestOperation(source, TestOp, target, programCounter, registerFileMap, address);
                case ParserConstants.BTFSC_1:
                case ParserConstants.BTFSC_2:
                case ParserConstants.BTFSC_3:
                case ParserConstants.BTFSC_4:
                    BitTestOp = BitTestOperator.BTFSC;
                    source = getAddressFromParameter(parameter);
                    bit = getBitNumberFromOperationCall(operation, parameter);
                    return new BitTestOperation(source, bit, BitTestOp, programCounter, registerFileMap, address);
                case ParserConstants.BTFSS_1:
                case ParserConstants.BTFSS_2:
                case ParserConstants.BTFSS_3:
                case ParserConstants.BTFSS_4:
                    BitTestOp = BitTestOperator.BTFSS;
                    source = getAddressFromParameter(parameter);
                    bit = getBitNumberFromOperationCall(operation, parameter);
                    return new BitTestOperation(source, bit, BitTestOp, programCounter, registerFileMap, address);
                /* ------------------------------------------------------ */

                /* ------------------------------------------------------ */
                /* -------- RETURN OPERATIONS --------------------------- */
                case ParserConstants.RETLW_1:
                case ParserConstants.RETLW_2:
                case ParserConstants.RETLW_3:
                case ParserConstants.RETLW_4:
                    RetOp = ReturnOperator.RETLW;
                    byte1 = getLiteralFromParameter(parameter);
                    return new ReturnOperation(programCounter, operationStack, RetOp, byte1, registerFileMap, address);
                /* ------------------------------------------------------ */

                case 0x0000:
                    if(parameter > 127)
                    {
                        // MOVWF
                        target = getAddressFromParameter(parameter);
                        byte1 = registerFileMap.Get(RegisterConstants.WORKING_REGISTER_ADDRESS);
                        return new MoveOperation(byte1, target, registerFileMap, address);
                    }
                    switch (parameter)
                    {
                        case ParserConstants.CLRWDT:
                            return new ClearWdtOperation(pic, registerFileMap, address);
                        case ParserConstants.RETFIE:
                            RetOp = ReturnOperator.RETFIE;
                            return new ReturnOperation(programCounter, operationStack, RetOp, registerFileMap, address);
                        case ParserConstants.RETURN:
                            RetOp = ReturnOperator.RETURN;
                            return new ReturnOperation(programCounter, operationStack, RetOp, registerFileMap, address);
                        case ParserConstants.SLEEP:
                            return new SleepOperation(pic, registerFileMap, address);
                        case ParserConstants.NOP_1:
                        case ParserConstants.NOP_2:
                        case ParserConstants.NOP_3:
                        case ParserConstants.NOP_4:
                            return new NopOperation(registerFileMap, address);
                        default:
                            break;
                    }
                    break;

                default:
                    throw new Exception("unknown operation");
            }

            throw new Exception("unknown error");
        }