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); }
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 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); }
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); }
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); }