示例#1
0
        public void DataMovOperationsTests_LOADRIND_Success()
        {
            // LOADRIND Ra,Rb  {F1} R[Ra] <- mem[R[Rb]]
            byte Ra = 1;
            byte Rb = 2;

            string dataHexRa = "9";
            string dataHexRb = "40";

            string valueInMemory = UnitConverter.IntToHex(52);

            // setting Registers and Memory values
            micro.WriteToMemory(
                UnitConverter.HexToInt(dataHexRb), valueInMemory);

            Assert.AreEqual(valueInMemory, micro.ReadFromMemory(UnitConverter.HexToInt(dataHexRb)));

            micro.MicroRegisters.SetRegisterValue(Ra, dataHexRa);
            micro.MicroRegisters.SetRegisterValue(Rb, dataHexRb);

            Console.WriteLine(micro.MicroRegisters.ToString());
            Assert.AreEqual("Registers[0,9,64,0,0,0,0,0]", micro.MicroRegisters.ToString());

            // starting tests
            MCInstructionF1 i1 = new MCInstructionF1(3, "00101",
                                                     UnitConverter.IntToBinary(Ra),
                                                     UnitConverter.IntToBinary(Rb)
                                                     );

            InstructionSetExe.ExecuteInstruction(i1, micro);

            Console.WriteLine($"Register after execution: {micro.MicroRegisters}");

            Assert.AreEqual(valueInMemory, micro.MicroRegisters.GetRegisterValue(Ra));
        }
示例#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 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);
        }
示例#4
0
        public void FlowControlOperationsTests_GRT_Success2()
        {
            // GRT Ra, Rb {F1} Cond <- R[Ra] > R[Rb]
            string ra = "001";          // 1
            string rb = "010";          // 2

            string valInA = "00000101"; // 5
            string valInB = "00000111"; // 7

            bool result = false;

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

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

            Console.WriteLine(micro.MicroRegisters);

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

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

            Console.WriteLine(instructionF1);

            InstructionSetExe.ExecuteInstruction(instructionF1, micro);

            Console.WriteLine(micro);

            Assert.AreEqual(result, micro.ConditionalBit);
        }
示例#5
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);
        }
示例#6
0
        public void DataMovOperationsTests_STORERIND_Success()
        {
            // STORERIND Ra,Rb  {F1} mem[R[Ra]] <- R[Rb]
            byte registerA = 3;
            byte registerB = 6;

            string expectedValueInMem = UnitConverter.IntToHex(40);

            // data to write in address R[Ra]
            string dataInBHex = UnitConverter.IntToHex(40);

            // addres to write data to
            string dataInAHex = UnitConverter.IntToHex(110);

            // set data in register and VM
            //Ra
            micro.MicroRegisters
            .SetRegisterValue(registerA, dataInAHex);
            // Rb
            micro.MicroRegisters
            .SetRegisterValue(registerB, dataInBHex);

            Console.WriteLine(micro.MicroRegisters);


            // assert that register contain the corret data
            Assert.AreEqual(dataInAHex,
                            micro.MicroRegisters.GetRegisterValue(registerA));

            Assert.AreEqual(dataInBHex,
                            micro.MicroRegisters.GetRegisterValue(registerB));
            ////


            // set instruction
            MCInstructionF1 instructionF1 = new MCInstructionF1(4, "00110",
                                                                UnitConverter.IntToBinary(registerA),
                                                                UnitConverter.IntToBinary(registerB)
                                                                );

            // execute instruction
            InstructionSetExe.ExecuteInstruction(instructionF1, micro);

            Console.WriteLine($"After execution: {micro.MicroRegisters}");
            Console.WriteLine($"Value in address {dataInBHex}: {UnitConverter.HexToInt(expectedValueInMem)}");
            Assert.AreEqual(
                expectedValueInMem,
                micro.ReadFromMemory(UnitConverter.HexToInt(dataInAHex))
                );
        }
        public void LogicOperationsTests_OR_Success()
        {
            // AND Ra, Rb, Rc {F1} R[Ra]<- R[Rb]*R[Rc]
            string ra = "001"; // 1
            string rb = "011"; // 3
            string rc = "111"; // 7

            string valInB = "00100100";

            string valInC = "11101111";

            string resultA = "11101111";

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

            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rc),
                UnitConverter.BinaryToHex(valInC));

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.BinaryToHex(valInB),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rb))
                );

            Assert.AreEqual(
                UnitConverter.BinaryToHex(valInC),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rc))
                );

            MCInstructionF1 i1 = new MCInstructionF1(3, "01100", ra, rb, rc);

            Console.WriteLine(i1);

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

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.BinaryToHex(resultA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );
        }
示例#8
0
        public void FlowControlOperationsTests_NOP_Success()
        {
            //init
            micro.StackPointer   = 100;
            micro.ProgramCounter = 12;

            string savedAddresInMemory = "FF";

            micro.WriteToMemory(100, savedAddresInMemory);
            MCInstructionF1 i1 = new MCInstructionF1(31, "11101", null);

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

            Assert.AreEqual(100, micro.StackPointer);
            Assert.AreEqual(14, micro.ProgramCounter);
        }
        public void InstructionSetExeTester_SUB_Success()
        {
            // ADD Ra, Rb, Rc {F1} R[Ra]<- R[Rb]+R[Rc] 
            string ra = "110"; // 6
            string rb = "011"; // 3
            string rc = "101"; // 5

            sbyte valInB = 20;
            sbyte valInC = 83;

            sbyte resultA = (sbyte)(valInB - valInC);

            // set data in register
            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rb),
                UnitConverter.IntToHex(valInB));

            micro.MicroRegisters.SetRegisterValue(
                (byte)UnitConverter.BinaryToInt(rc),
                UnitConverter.IntToHex(valInC));

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.IntToHex(valInB),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rb))
                );

            Assert.AreEqual(
                UnitConverter.IntToHex(valInC),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rc))
                );

            MCInstructionF1 i1 = new MCInstructionF1(3, "01000", ra, rb, rc);

            InstructionSetExe.ExecuteInstruction(i1, micro);

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.ByteToHex(resultA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );
        }
        public void LogicOperationsTests_NOT_Success()
        {
            // NOT Ra,Rb {F1}   R[Ra] <- Complement(R[Rb])
            string ra = "001"; // 1
            string rb = "011"; // 3

            string valInB = "00100100";

            string resultA = "11011011";

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

            Console.WriteLine(micro.MicroRegisters);

            Assert.AreEqual(
                UnitConverter.BinaryToHex(valInB),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(rb))
                );

            MCInstructionF1 i1 = new MCInstructionF1(3, "01110", ra, rb);

            Console.WriteLine(i1);

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

            Console.WriteLine(micro.MicroRegisters);

            Console.WriteLine($"Expected: {UnitConverter.BinaryToHex(resultA)}," +
                              $"Actual: {micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))}");

            Assert.AreEqual(
                UnitConverter.BinaryToHex(resultA),
                micro.MicroRegisters.GetRegisterValue((byte)UnitConverter.BinaryToInt(ra))
                );
        }
        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))
                );
        }
        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))
                );
        }
示例#13
0
        private IMCInstruction GetNextInstruction()
        {
            ushort currProgramCounter = _simulator.ProgramCounter;

            // even block containing the instruction
            string evenBlock = _vm.GetContentsInBin(currProgramCounter);
            // odd block with data and other params
            string oddBlock = _vm.GetContentsInBin(currProgramCounter + 1);

            // 16-bit instruction block
            string completeBlock = evenBlock + oddBlock;

            // get first 5-bits to use as OpCode
            string opcode = evenBlock.Substring(0, 5);

            // instruction format
            byte instructionFormat = OpCodesInfo.GetInstructionFormat(opcode);
            byte numberOfParams    = OpCodesInfo.GetNumberOfParams(opcode);

            byte count = 0;

            string[] paramList = new string[3];

            IMCInstruction instructionToExecute = null;

            switch (instructionFormat)
            {
            case 1:
                while (count < numberOfParams)
                {
                    paramList[count] = completeBlock.Substring(count * 3 + 5, 3);
                    count++;
                }

                instructionToExecute = new MCInstructionF1(
                    decimalAddress: currProgramCounter,
                    opCodeBinary: opcode,
                    Ra: paramList[0],
                    Rb: paramList[1],
                    Rc: paramList[2]
                    );
                break;

            case 2:
                while (count < numberOfParams)
                {
                    if (count == 0)
                    {
                        // get 'Ra'
                        paramList[count] = completeBlock.Substring(5, 3);
                    }
                    else if (count == 1)
                    {
                        paramList[count] = oddBlock;
                    }

                    count++;
                }

                instructionToExecute = new MCInstructionF2(
                    decimalAddress: currProgramCounter,
                    opCodeBinary: opcode,
                    Ra: paramList[0],
                    binaryAddress: paramList[1]
                    );

                break;

            case 3:
                instructionToExecute = new MCInstructionF3(
                    decimalAddress: currProgramCounter,
                    opCodeBinary: opcode,
                    binaryAddress: completeBlock.Substring(6, 10)
                    );
                break;
            }

            return(instructionToExecute);
        }