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) ))); }
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); }
public void DataMovOperationsTests_LOADIntoR0_Success() { string value2 = "00010000"; // LOAD R0 02 MCInstructionF2 i1 = new MCInstructionF2(3, "00001", "000", value2); InstructionSetExe.ExecuteInstruction(i1, micro); }
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) )); }
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)); }
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 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 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)) ); }
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}"); }
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); }