예제 #1
0
        public void FlowControlOperationsTests_CALL_Success()
        {
            //init
            // CALL address {F3}
            // SP <- SP - 2
            // mem[SP] <- PC
            // PC <- address

            ushort expectedSPVal = 98;

            string addressOfCall = UnitConverter.ByteToBinary(33);

            byte initialValueOfPC = 12;

            micro.StackPointer   = 100;
            micro.ProgramCounter = initialValueOfPC;

            // address: 2
            MCInstructionF3 i1 = new MCInstructionF3(30, "11110", addressOfCall);

            // execute
            InstructionSetExe.ExecuteInstruction(i1, micro);

            Assert.AreEqual(expectedSPVal, micro.StackPointer);

            // expected data in Address: 33
            Assert.AreEqual(UnitConverter.ByteToHex(initialValueOfPC), micro.ReadFromMemory(micro.StackPointer));

            // program counter should be equal to 98
            Assert.AreEqual(UnitConverter.BinaryToByte(addressOfCall), micro.ProgramCounter);
        }
예제 #2
0
        public void FlowControlOperationsTests_NEQ_AreNotEqual()
        {
            // NEQ Ra, Rb {F1} Cond <- R[Ra] != R[Rb]
            string ra = "010";          // 2
            string rb = "101";          // 5

            string valInA = "00101101"; // 45
            string valInB = "00101101"; // 45

            bool result = false;

            // set data in register
            micro.MicroRegisters.SetRegisterValue(
                UnitConverter.BinaryToByte(ra),
                UnitConverter.BinaryToHex(valInA));

            micro.MicroRegisters.SetRegisterValue(
                UnitConverter.BinaryToByte(rb),
                UnitConverter.BinaryToHex(valInB));

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual($"Registers[0,0,45,0,0,45,0,0]", micro.MicroRegisters.ToString());

            // start instruction
            MCInstructionF1 instructionF1 = new MCInstructionF1(4, "11100", ra, rb);

            Console.WriteLine(instructionF1);

            InstructionSetExe.ExecuteInstruction(instructionF1, micro);

            Console.WriteLine(micro);

            Assert.AreEqual(result, micro.ConditionalBit);
        }
예제 #3
0
        public MCInstructionF3(ushort decimalAddress, string opCodeBinary, string binaryAddress)
        {
            OpCode = UnitConverter.BinaryToByte(opCodeBinary);

            AddressParamHex = UnitConverter.BinaryToHex(binaryAddress);

            InstructionAddressDecimal = decimalAddress;
        }
예제 #4
0
        public MCInstructionF2(ushort decimalAddress, string opCodeBinary, string Ra, string binaryAddress)
        {
            OpCode = UnitConverter.BinaryToByte(opCodeBinary);

            this.Ra = Ra == null ? (byte)8 : UnitConverter.BinaryToByte(Ra);

            AddressParamHex = UnitConverter.BinaryToHex(binaryAddress);

            InstructionAddressDecimal = decimalAddress;
        }
예제 #5
0
        public MCInstructionF1(ushort decimalAddress, string opCodeBinary, string Ra, string Rb = null, string Rc = null)
        {
            OpCode = (byte)UnitConverter.BinaryToInt(opCodeBinary);

            this.Ra = Ra == null || Ra.Length == 0 ? (byte)0 : UnitConverter.BinaryToByte(Ra);
            this.Rb = Rb == null ? (byte)0 : UnitConverter.BinaryToByte(Rb);
            this.Rc = Rc == null ? (byte)0 : UnitConverter.BinaryToByte(Rc);

            InstructionAddressDecimal = decimalAddress;
        }
예제 #6
0
        public void LogicOperationsTests_ROTAL_Success()
        {
            // ROTAL Ra, Rb, Rc {F1} R[Ra]<- R[Rb] rtl R[Rc]
            string ra1 = "001"; // 1
            string rb1 = "010"; // 2
            string rc1 = "011"; // 3

            string ra2 = "101"; // 5
            string rb2 = "110"; // 6
            string rc2 = "111"; // 7


            MCInstructionF1 i1 = new MCInstructionF1(3, "10011", ra1, rb1, rc1);
            MCInstructionF1 i2 = new MCInstructionF1(3, "10011", ra2, rb2, rc2);

            string valInB1  = "10111100";
            string valInC1  = UnitConverter.ByteToBinary(2);
            string resultA1 = "11110010";

            string valInB2  = "10011100";
            string valInC2  = UnitConverter.ByteToBinary(2);
            string resultA2 = "01110010";


            // set data in register
            ////////////
            /// Test 1
            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rb1),
                UnitConverter.BinaryToHex(valInB1));

            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rc1),
                UnitConverter.BinaryToHex(valInC1));
            ////////////

            ////////////
            /// Test 2
            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rb2),
                UnitConverter.BinaryToHex(valInB2));

            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rc2),
                UnitConverter.BinaryToHex(valInC2));
            ///////////

            Console.WriteLine(micro.MicroRegisters);

            // Assert Registers
            Assert.AreEqual("Registers[0,0,-68,2,0,0,-100,2]",
                            micro.MicroRegisters.ToString());
            Console.WriteLine(i1);
            Console.WriteLine(i2);

            // execute instruction
            InstructionSetExe.ExecuteInstruction(i1, micro);
            InstructionSetExe.ExecuteInstruction(i2, micro);

            Console.WriteLine(micro.MicroRegisters);

            Console.WriteLine($"Expected1: {UnitConverter.BinaryToHex(resultA1)}," +
                              $"Actual: {micro.MicroRegisters.GetRegisterValue(UnitConverter.BinaryToByte(ra1))}");
            Console.WriteLine($"Expected1: {UnitConverter.HexToBinary(micro.MicroRegisters.GetRegisterValue(UnitConverter.BinaryToByte(ra1)))}");

            Console.WriteLine($"Expected2: {UnitConverter.BinaryToHex(resultA2)}," +
                              $"Actual: {micro.MicroRegisters.GetRegisterValue(UnitConverter.BinaryToByte(ra2))}");
            Console.WriteLine($"Expected2: {UnitConverter.HexToBinary(micro.MicroRegisters.GetRegisterValue(UnitConverter.BinaryToByte(ra2)))}");

            Assert.AreEqual(
                UnitConverter.BinaryToHex(resultA1),
                micro.MicroRegisters.GetRegisterValue(UnitConverter.BinaryToByte(ra1))
                );

            Assert.AreEqual(
                UnitConverter.BinaryToHex(resultA2),
                micro.MicroRegisters.GetRegisterValue(UnitConverter.BinaryToByte(ra2))
                );
        }
예제 #7
0
        /// <summary>
        /// get binary format of each instruction
        /// </summary>
        private string GetBinaryFormat(IFormatInstructions _operator)
        {
            int opcode = OperatorsInfo.GetOPCode(_operator.Operator);


            switch (OperatorsInfo.GetInstructionFormat(_operator.Operator))
            {
            case EInstructionFormat.FORMAT_1:
            {
                int Rc = 0;
                int Ra = 0;
                int Rb = 0;
                InstructionFormat1 format = (InstructionFormat1)_operator;

                if (format.RegisterA != null)
                {
                    Ra = (format.RegisterA?.ToString() == "") ? 0 : Convert.ToInt32(Regex.Replace(format.RegisterA.ToString(), @"[.\D+]", ""));
                }
                if (format.RegisterC != null)
                {
                    Rb = (format.RegisterB?.ToString() == "") ? 0 : Convert.ToInt32(Regex.Replace(format.RegisterB.ToString(), @"[.\D+]", ""));
                }
                if (format.RegisterC != null)
                {
                    Rc = (format.RegisterC.ToString() == "") ? 0 : Convert.ToInt32(Regex.Replace(format.RegisterC.ToString(), @"[.\D+]", ""));
                }
                return($"{Convert.ToString(opcode, 2).PadLeft(5, '0')}" +
                       $"{Convert.ToString(Ra, 2).PadLeft(3, '0')}" +
                       $"{Convert.ToString(Rb, 2).PadLeft(3, '0')}" +
                       $"{Convert.ToString(Rc, 2).PadLeft(3, '0')}00");
            }

            case EInstructionFormat.FORMAT_2:
            {
                InstructionFormat2 format = (InstructionFormat2)_operator;
                int Ra = (format.RegisterA.ToString() == "") ? 0 : Convert.ToInt32(Regex.Replace(format.RegisterA.ToString(), @"[.\D+]", ""));

                int constOrAddr;

                if (constants.ContainsKey(format.ConstOrAddress.ToString()))
                {
                    constOrAddr = constants[format.ConstOrAddress.ToString()];
                }
                else if (variables.ContainsKey(format.ConstOrAddress.ToString()))
                {
                    constOrAddr = variables[format.ConstOrAddress.ToString()];
                }
                else if (labels.ContainsKey(format.ConstOrAddress.ToString()))
                {
                    constOrAddr = labels[format.ConstOrAddress.ToString()];
                }
                else
                {
                    //check if the constant or address is a direct input
                    try
                    {
                        constOrAddr = UnitConverter.BinaryToByte(
                            UnitConverter.HexToBinary(
                                format.ConstOrAddress.ToString().Replace("#", "")
                                )
                            );
                    } catch (OverflowException)
                    {
                        //send error message (undefined variable)
                        AsmLogger.Error($"Overflow in instruction: {_operator}",
                                        currentLine.ToString(),
                                        $"Value '{format.ConstOrAddress}'");

                        return(null);
                    }
                    catch
                    {
                        //send error message (undefined variable)
                        AsmLogger.Error($"Variable not defined: {_operator}",
                                        currentLine.ToString(),
                                        "Variable called but never defined");
                        return(null);
                    }
                }
                return($"{UnitConverter.IntToBinary(opcode, defaultWidth: 5)}" +
                       $"{UnitConverter.IntToBinary(Ra, defaultWidth: 3)}" +
                       $"{UnitConverter.IntToBinary(constOrAddr, defaultWidth: 8)}");
            }

            case EInstructionFormat.FORMAT_3:
            {
                InstructionFormat3 format = (InstructionFormat3)_operator;

                int constOrAddr = 0;
                if (constants.ContainsKey(format.ConstOrAddress.ToString()))
                {
                    constOrAddr = constants[format.ConstOrAddress.ToString()];
                }
                else if (variables.ContainsKey(format.ConstOrAddress.ToString()))
                {
                    constOrAddr = variables[format.ConstOrAddress.ToString()];
                }
                else if (labels.ContainsKey(format.ConstOrAddress.ToString()))
                {
                    constOrAddr = labels[format.ConstOrAddress.ToString()];
                }
                else
                {
                    //check if the constant or address is a direct input
                    try
                    {
                        constOrAddr = Convert.ToInt32(format.ConstOrAddress.ToString().Replace("#", ""), 16);
                    }
                    catch
                    {
                        //send error message (undefined variable)
                        return(null);
                    }
                }


                return($"{Convert.ToString(opcode, 2).PadLeft(5, '0')}" +
                       $"{Convert.ToString(constOrAddr, 2).PadLeft(11, '0')}");
            }

            default:
            {
                throw new Exception("Token is not an instruction");
            }
            }
        }