/// <summary> /// Substract two Hexadecimal numbers. /// </summary> /// <param name="minuend">The number it is subtracted from</param> /// <param name="subtrahend">The number being subtracted</param> /// <returns>The difference between the minuend and subtrahend</returns> public static string HexSubstract(string minuend, string subtrahend) { sbyte element1 = UnitConverter.HexToSByte(minuend); sbyte element2 = UnitConverter.HexToSByte(subtrahend); return(UnitConverter.ByteToHex((byte)(element1 - element2))); }
/// <summary> /// Division of two Hexadecimal Numbers /// </summary> /// <param name="dividend">The number being divided</param> /// <param name="divisor">The number divided by</param> /// <returns>The quotient</returns> public static string HexDivide(string dividend, string divisor) { sbyte element1 = UnitConverter.HexToSByte(dividend); sbyte element2 = UnitConverter.HexToSByte(divisor); return(UnitConverter.ByteToHex((byte)(element1 - element2))); }
/// <summary> /// Multiply two decimal numbers /// </summary> /// <param name="hex1">Multiplier</param> /// <param name="hex2">Multiplicand</param> /// <returns>Product</returns> public static string HexMultiply(string hex1, string hex2) { sbyte element1 = UnitConverter.HexToSByte(hex1); sbyte element2 = UnitConverter.HexToSByte(hex2); return(UnitConverter.ByteToHex((byte)(element1 * element2))); }
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_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); }
public string ReadFromPort(int port) { if (_buffer.Count == 0) { return(UnitConverter.ByteToHex(0)); } return(_buffer.Dequeue()); }
/// <summary> /// Access contents in the registers in Hexadecimal format /// </summary> /// <param name="registerNumber">Register Number</param> /// <exception cref="InvalidCastException">If the data is invalid for the size of the register</exception> /// <returns>Contents of the registers in Hexadecimal format</returns> public string GetRegisterValue(byte registerNumber) { if (!IsValidAction(registerNumber, isWrite: false)) { throw new IndexOutOfRangeException($"Read Action: Invalid Register '{registerNumber}'"); } return(UnitConverter.ByteToHex((byte)registers[registerNumber]).Replace("0x", "")); }
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); }
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)) ); }
/// <summary> /// Reading the data contained in the ASCII Display's port. /// </summary> /// <param name="port">Port whose data we wish to read.</param> /// <returns>String representation of the data stored in the port.</returns> public string ReadFromPort(int port) { if (IsValidPort(port)) { if (DisplaySlots[ConvertPortToIndex((short)port)].Length > 0) { char c = DisplaySlots[ConvertPortToIndex((short)port)].ToCharArray()[0]; return(UnitConverter.ByteToHex((byte)c)); } else { return(UnitConverter.ByteToHex((byte)0)); } } else { throw new ArgumentException($"Invalid port \n"); } }
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 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)) ); }
string IIODevice.ReadFromPort(int port) { return(UnitConverter.ByteToHex(binaryData)); }