Beispiel #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));
        }
Beispiel #2
0
        public void DataMovOperationsTests_StoreValueInRegisterToMemory_Success()
        {
            // Set register state to Registers[0,0,4,16,0,0,0,0]
            micro.MicroRegisters.SetRegisterValue(2, "04");
            micro.MicroRegisters.SetRegisterValue(3, "10");

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

            string memoryAddress1 = UnitConverter.IntToBinary(50);
            string memoryAddress2 = UnitConverter.IntToBinary(51);

            Console.WriteLine($"Memory1: {UnitConverter.BinaryToInt(memoryAddress1)}");
            Console.WriteLine($"Memory1: {UnitConverter.BinaryToInt(memoryAddress2)}");

            // {F2} STORE mem, Ra    [mem] <- R[Ra]
            MCInstructionF2 i1 = new MCInstructionF2(3, "00011", "010", memoryAddress1);
            MCInstructionF2 i2 = new MCInstructionF2(3, "00011", "011", memoryAddress2);

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

            Console.WriteLine($"VirtualMemory #{UnitConverter.BinaryToInt(memoryAddress1)}: " +
                              $"{UnitConverter.HexToInt(micro.ReadFromMemory(UnitConverter.BinaryToInt(memoryAddress1)))}");
            Console.WriteLine($"VirtualMemory #{UnitConverter.BinaryToInt(memoryAddress2)}: " +
                              $"{UnitConverter.HexToInt(micro.ReadFromMemory(UnitConverter.BinaryToInt(memoryAddress2)))}");

            Assert.AreEqual(4, UnitConverter.HexToInt(micro.ReadFromMemory(
                                                          UnitConverter.BinaryToInt(memoryAddress1)
                                                          )));
            Assert.AreEqual(16, UnitConverter.HexToInt(micro.ReadFromMemory(
                                                           UnitConverter.BinaryToInt(memoryAddress2)
                                                           )));
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
0
        public void DataMovOperationsTests_LOADIntoR0_Success()
        {
            string value2 = "00010000";

            // LOAD R0 02
            MCInstructionF2 i1 = new MCInstructionF2(3, "00001", "000", value2);

            InstructionSetExe.ExecuteInstruction(i1, micro);
        }
Beispiel #9
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);
        }
Beispiel #10
0
        public void DataMovOperationsTests_STOREFrom0_Success()
        {
            string addrs = "00000010";
            // STORE R0 02
            MCInstructionF2 i1 = new MCInstructionF2(3, "00011", "000", addrs);

            InstructionSetExe.ExecuteInstruction(i1, micro);

            Assert.AreEqual("00", micro.ReadFromMemory(
                                UnitConverter.BinaryToInt(addrs)
                                ));
        }
Beispiel #11
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))
                );
        }
Beispiel #12
0
        public void DataMovOperationsTests_PUSH2_Success()
        {
            //init
            micro.StackPointer = 0;
            micro.MicroRegisters.SetRegisterValue(4, "05");
            MCInstructionF2 i1         = new MCInstructionF2(3, "00100", "100", null);
            ushort          SPNewValue = 4095;

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

            Assert.AreEqual(SPNewValue, micro.StackPointer);
            Assert.AreEqual("05", micro.ReadFromMemory(micro.StackPointer));
        }
Beispiel #13
0
        public void DataMovOperationsTests_POP3_Success()
        {
            //init
            micro.StackPointer = 4095;
            micro.WriteToMemory(4095, "FF");
            MCInstructionF2 i1         = new MCInstructionF2(3, "00010", "010", null);
            ushort          SPNewValue = 0;

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

            Assert.AreEqual(SPNewValue, micro.StackPointer);
            Assert.AreEqual("FF", micro.MicroRegisters.GetRegisterValue(2));
        }
        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))
                );
        }
Beispiel #15
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);
        }
Beispiel #16
0
        public void FlowControlOperationsTests_RETURN_Success()
        {
            //init
            micro.StackPointer   = 100;
            micro.ProgramCounter = 12;

            string savedAddresInMemory = "FF";

            micro.WriteToMemory(100, savedAddresInMemory);
            MCInstructionF3 i1         = new MCInstructionF3(31, "11111", null);
            ushort          SPNewValue = 102;

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

            Assert.AreEqual(SPNewValue, micro.StackPointer);
            Assert.AreEqual(UnitConverter.HexToInt(savedAddresInMemory) + 2, 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))
                );
        }
Beispiel #18
0
        public void FlowControlOperationsTests_JCONDADDR_Success()
        {
            // JCONDADDR addr {F3} If cond then [pc] <- address

            ushort a1 = 233;
            ushort a2 = 325;

            string addressBin1 = UnitConverter.IntToBinary(a1);
            string addressBin2 = UnitConverter.IntToBinary(a2);

            // start instruction
            MCInstructionF3 i1 = new MCInstructionF3(4, "10111", addressBin1);
            MCInstructionF3 i2 = new MCInstructionF3(4, "10111", addressBin2);

            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(a1, micro.ProgramCounter);

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

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

            InstructionSetExe.ExecuteInstruction(i2, micro);

            Console.WriteLine(micro);
            Assert.AreEqual(a2, micro.ProgramCounter);
        }
Beispiel #19
0
        public void DataMovOperationsTests_LoadConstantValueToRegister_Success()
        {
            string value1 = "00000100";
            string value2 = "00010000";

            // LOADIM R2 02
            MCInstructionF2 i1 = new MCInstructionF2(3, "00001", "010", value1);

            // LOAD R2 03
            MCInstructionF2 i2 = new MCInstructionF2(3, "00001", "011", value2);

            InstructionSetExe.ExecuteInstruction(i1, micro);
            Console.WriteLine($"Registers after Execution #1 -> {micro.MicroRegisters}");

            InstructionSetExe.ExecuteInstruction(i2, micro);
            Console.WriteLine($"Registers after Execution #2 -> {micro.MicroRegisters}");

            Assert.AreEqual(UnitConverter.BinaryToHex(value1), micro.MicroRegisters.GetRegisterValue(2));
            Assert.AreEqual(UnitConverter.BinaryToHex(value2), micro.MicroRegisters.GetRegisterValue(3));
        }
        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 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))
                );
        }
Beispiel #22
0
        public void FlowControlOperationsTests_JMPADDR_Success()
        {
            // JMPADDR addr {F3} [pc] <- address
            string addressHex1 = UnitConverter.ByteToHex(6);
            string addressHex2 = UnitConverter.ByteToHex(44);

            // start instruction
            MCInstructionF3 i1 = new MCInstructionF3(4, "10101", UnitConverter.HexToBinary(addressHex1));
            MCInstructionF3 i2 = new MCInstructionF3(4, "10101", UnitConverter.HexToBinary(addressHex2));

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

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

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

            InstructionSetExe.ExecuteInstruction(i2, micro);
            Assert.AreEqual(44, micro.ProgramCounter);
        }
        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))
                );
        }
Beispiel #24
0
        public void DataMovOperationsTests_LoadVMValueToRegister_Success()
        {
            micro.WriteToMemory(2, "05");
            micro.WriteToMemory(3, "07");

            // LOAD R2 02
            MCInstructionF2 i1 = new MCInstructionF2(3, "00000", "010", "00000010");

            // LOAD R2 03
            MCInstructionF2 i2 = new MCInstructionF2(3, "00000", "011", "00000011");

            InstructionSetExe.ExecuteInstruction(i1, micro);

            Console.WriteLine($"Registers after Execution #1 -> {micro.MicroRegisters}");

            Assert.AreEqual("05", micro.MicroRegisters.GetRegisterValue(2));

            InstructionSetExe.ExecuteInstruction(i2, micro);


            Assert.AreEqual("07", micro.MicroRegisters.GetRegisterValue(3));

            Console.WriteLine($"Registers after Execution #2 -> {micro.MicroRegisters}");
        }
        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))
                );
        }