Ejemplo n.º 1
0
        public override string ToString()
        {
            if (IMCInstruction.AsmTextPrint)
            {
                string itr = $"{OpCodesInfo.GetOpName(UnitConverter.ByteToBinary(OpCode, defaultWidth: 5))}";

                if (Ra >= 0 && Ra <= 7)
                {
                    itr += $" R{Ra}";
                }

                if (Rb >= 0 && Rb <= 7)
                {
                    itr += $" R{Rb}";
                }

                if (Rc >= 0 && Rc <= 7)
                {
                    itr += $" R{Rc}";
                }

                return(itr);
            }
            return($"MCInstructionF1[InstructionAddressDecimal: (decimal)'{InstructionAddressDecimal}', opcode:'{OpCode}', Ra:'{Ra}', Rb:'{Rb}', Rc:'{Rc}']");
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void FlowControlOperationsTests_JMPRIND_Success()
        {
            // JMPRIND Ra {F1} [pc] <- [R[ra]]
            byte ra = 1;
            byte rb = 3;

            string addressInRa = UnitConverter.ByteToHex(100);
            string addressInRb = UnitConverter.ByteToHex(8);

            // set data in Register
            micro.MicroRegisters.SetRegisterValue(ra, addressInRa);
            micro.MicroRegisters.SetRegisterValue(rb, addressInRb);

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual("Registers[0,100,0,8,0,0,0,0]", micro.MicroRegisters.ToString());

            // start instruction
            MCInstructionF1 i1 = new MCInstructionF1(4, "10100", UnitConverter.ByteToBinary(ra));
            MCInstructionF1 i2 = new MCInstructionF1(4, "10100", UnitConverter.ByteToBinary(rb));

            Console.WriteLine(i1);
            Console.WriteLine(i2);

            // assert that program counter starts at 0
            Assert.AreEqual(0, micro.ProgramCounter);

            InstructionSetExe.ExecuteInstruction(i1, micro);
            Assert.AreEqual(100, micro.ProgramCounter);

            InstructionSetExe.ExecuteInstruction(i2, micro);
            Assert.AreEqual(8, micro.ProgramCounter);
        }
Ejemplo n.º 4
0
        public void FlowControlOperationsTests_LOOP_Success()
        {
            // LOOP Ra, address {F2}
            // [R[ra]] <- [R[ra]] – 1
            //If R[Ra] != 0 [pc] <- address

            byte  ra    = 1;
            sbyte value = 100;

            string dataInRaBin = UnitConverter.ByteToBinary(value);

            string addressToGoHex = "AF";

            // set register
            micro.MicroRegisters.SetRegisterValue(ra, UnitConverter.BinaryToHex(dataInRaBin));

            Console.WriteLine(micro.MicroRegisters);

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

            // start instruction
            MCInstructionF2 i1 = new MCInstructionF2(2, "11000", UnitConverter.ByteToBinary(ra),
                                                     UnitConverter.HexToBinary(addressToGoHex));

            InstructionSetExe.ExecuteInstruction(i1, micro);

            Console.WriteLine($"Expected PC: {UnitConverter.HexToInt(addressToGoHex)}");
            Console.WriteLine(micro);
            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(value - 1, UnitConverter.HexToInt(micro.MicroRegisters.GetRegisterValue(ra)));
            Assert.AreEqual(UnitConverter.HexToInt(addressToGoHex), micro.ProgramCounter);
        }
Ejemplo n.º 5
0
 public override string ToString()
 {
     if (IMCInstruction.AsmTextPrint)
     {
         return($"{OpCodesInfo.GetOpName(UnitConverter.ByteToBinary(OpCode, defaultWidth: 5))} {AddressParamHex}");
     }
     return($"MCInstructionF3[InstructionAddressDecimal: (decimal)'{InstructionAddressDecimal}', opcode:'{OpCode}', AddressParamHex:'{AddressParamHex}']");
 }
Ejemplo n.º 6
0
        public void FlowControlOperationsTests_JCONDRIN_Success()
        {
            // JCONDRIN Ra {F3} If cond then [pc] <- [R[ra]]
            byte ra = 1;
            byte rb = 3;

            byte v1 = 100;
            byte v2 = 8;

            string addressInRa = UnitConverter.ByteToHex(v1);
            string addressInRb = UnitConverter.ByteToHex(v2);

            // set data in Register
            micro.MicroRegisters.SetRegisterValue(ra, addressInRa);
            micro.MicroRegisters.SetRegisterValue(rb, addressInRb);

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual($"Registers[0,{v1},0,{v2},0,0,0,0]", micro.MicroRegisters.ToString());

            // start instruction
            MCInstructionF1 i1 = new MCInstructionF1(4, "10110", UnitConverter.ByteToBinary(ra));
            MCInstructionF1 i2 = new MCInstructionF1(4, "10110", UnitConverter.ByteToBinary(rb));

            Console.WriteLine(i1);
            Console.WriteLine(i2);

            // assert that program counter starts at 0
            Assert.AreEqual(0, micro.ProgramCounter);

            // set conditional to true
            micro.ConditionalBit = true;
            Console.WriteLine(micro);

            // when instructions executes PC will change
            InstructionSetExe.ExecuteInstruction(i1, micro);

            Console.WriteLine(micro);
            Assert.AreEqual(v1, micro.ProgramCounter);

            // cond bit is false, pc will not change
            InstructionSetExe.ExecuteInstruction(i2, micro);
            Console.WriteLine($"CondBit False: {micro}");
            Assert.AreEqual(v1 + 2, micro.ProgramCounter);

            // now set to true
            micro.ConditionalBit = true;

            InstructionSetExe.ExecuteInstruction(i2, micro);

            Console.WriteLine(micro);
            Assert.AreEqual(v2, micro.ProgramCounter);
        }
Ejemplo n.º 7
0
        public override string ToString()
        {
            if (IMCInstruction.AsmTextPrint)
            {
                if (OpCodesInfo.GetOpName(UnitConverter.ByteToBinary(OpCode, defaultWidth: 5)).ToLower().Equals("return"))
                {
                    return("RETURN");
                }

                string itr = $"{OpCodesInfo.GetOpName(UnitConverter.ByteToBinary(OpCode, defaultWidth: 5))}";

                if (Ra >= 0 && Ra <= 7)
                {
                    itr += $" R{Ra}";
                }

                return(itr);
            }

            return($"MCInstructionF2[InstructionAddressDecimal: (decimal)'{InstructionAddressDecimal}', opcode:'{OpCode}', Ra:'{Ra}', AddressParamHex:'{AddressParamHex}']");
        }
        public void InstructionSetExeTester_SUBIM_Success()
        {
            // SUBIM Ra,  cons {F2} R[Ra] <- R[Ra]-cons 
            string ra = "001"; // 1

            sbyte valInA = 20;
            sbyte constVal = -33;

            sbyte resultA = (sbyte)(valInA - constVal);

            // set data in register
            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(ra),
                UnitConverter.ByteToHex(valInA));

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.IntToHex(valInA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );

            MCInstructionF2 i2 = new MCInstructionF2(3, "01010", ra,
                UnitConverter.ByteToBinary(constVal)
                );
            Console.WriteLine(i2);

            // execute instruction

            InstructionSetExe.ExecuteInstruction(i2, micro);

            Console.WriteLine(micro.MicroRegisters);
            Console.WriteLine($"Result in 0x{UnitConverter.ByteToHex((byte)resultA)}");
            Console.WriteLine($"Result in binary: {UnitConverter.ByteToHex((byte)resultA)}");

            Assert.AreEqual(
                UnitConverter.ByteToHex((byte)resultA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );
        }
        public void InstructionSetExeTester_ADDIM_Success()
        {
            // ADDIM Ra, cons {F2} R[Ra] <- R[Ra]+cons
            string ra = "110"; // 6

            sbyte valInA = 20;
            sbyte constVal = -33;

            sbyte resultA = (sbyte)(valInA + constVal);

            // set data in register
            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(ra),
                UnitConverter.ByteToHex(valInA));

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.ByteToHex(valInA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );

            MCInstructionF2 i2 = new MCInstructionF2(3, "01001", ra,
                UnitConverter.ByteToBinary(constVal)
                );

            Console.WriteLine(i2);

            InstructionSetExe.ExecuteInstruction(i2, micro);

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.ByteToHex(resultA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );
        }
Ejemplo n.º 10
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))
                );
        }
Ejemplo n.º 11
0
        public void LogicOperationsTests_NEG_Success()
        {
            // NEG Ra,Rb {F1} R[Ra]<- - R[Rb]
            string ra1 = "001"; // 1
            string rb1 = "011"; // 3

            string ra2 = "110"; // 6
            string rb2 = "111"; // 7


            string valInB1  = UnitConverter.ByteToBinary(-13);
            string resultA1 = UnitConverter.ByteToBinary(13);

            string valInB2  = UnitConverter.ByteToBinary(-45);
            string resultA2 = UnitConverter.ByteToBinary(45);


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

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

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.BinaryToHex(valInB1),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rb1))
                );

            Assert.AreEqual(
                UnitConverter.BinaryToHex(valInB2),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rb2))
                );

            MCInstructionF1 i1 = new MCInstructionF1(3, "01111", ra1, rb1);
            MCInstructionF1 i2 = new MCInstructionF1(3, "01111", ra2, rb2);

            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((byte)UnitConverter.BinaryToInt(ra1))}");
            Console.WriteLine($"Expected2: {UnitConverter.BinaryToHex(resultA2)}," +
                              $"Actual: {micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra2))}");

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

            Assert.AreEqual(
                UnitConverter.BinaryToHex(resultA2),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra2))
                );
        }