public void TestIFormat() { IFormatInstruction test = new IFormatInstruction(9, 12, 16, 1343); IFormatInstruction decodedTest = IFormatInstruction.Decode(test.Encode()); Assert.AreEqual(test.OpCode, decodedTest.OpCode); Assert.AreEqual(test.RegisterA, decodedTest.RegisterA); Assert.AreEqual(test.RegisterB, decodedTest.RegisterB); Assert.AreEqual(test.Immediate, decodedTest.Immediate); Assert.AreEqual(9, decodedTest.OpCode); Assert.AreEqual(12, decodedTest.RegisterA); Assert.AreEqual(16, decodedTest.RegisterB); Assert.AreEqual(1343, decodedTest.Immediate); test = new IFormatInstruction(25, 12, 16, -1); decodedTest = IFormatInstruction.Decode(test.Encode()); Assert.AreEqual(test.OpCode, decodedTest.OpCode); Assert.AreEqual(test.RegisterA, decodedTest.RegisterA); Assert.AreEqual(test.RegisterB, decodedTest.RegisterB); Assert.AreEqual(test.SignedImmediate, decodedTest.SignedImmediate); Assert.AreEqual(25, decodedTest.OpCode); Assert.AreEqual(12, decodedTest.RegisterA); Assert.AreEqual(16, decodedTest.RegisterB); Assert.AreEqual(-1, decodedTest.SignedImmediate); test = new IFormatInstruction(25, 12, 16, -256); decodedTest = IFormatInstruction.Decode(test.Encode()); Assert.AreEqual(test.OpCode, decodedTest.OpCode); Assert.AreEqual(test.RegisterA, decodedTest.RegisterA); Assert.AreEqual(test.RegisterB, decodedTest.RegisterB); Assert.AreEqual(test.SignedImmediate, decodedTest.SignedImmediate); Assert.AreEqual(25, decodedTest.OpCode); Assert.AreEqual(12, decodedTest.RegisterA); Assert.AreEqual(16, decodedTest.RegisterB); Assert.AreEqual(-256, decodedTest.SignedImmediate); test = new IFormatInstruction(OperationCodes.Bne.Code(), Registers.R0.Number(), Registers.R8.Number(), -8); decodedTest = IFormatInstruction.Decode(test.Encode()); Assert.AreEqual(test.OpCode, decodedTest.OpCode); Assert.AreEqual(test.RegisterA, decodedTest.RegisterA); Assert.AreEqual(test.RegisterB, decodedTest.RegisterB); Assert.AreEqual(test.SignedImmediate, decodedTest.SignedImmediate); Assert.AreEqual(OperationCodes.Bne.Code(), decodedTest.OpCode); Assert.AreEqual(0, decodedTest.RegisterA); Assert.AreEqual(8, decodedTest.RegisterB); Assert.AreEqual(-8, decodedTest.SignedImmediate); test = new IFormatInstruction(25, 12, 16, 40512); decodedTest = IFormatInstruction.Decode(test.Encode()); Assert.AreEqual(test.Immediate, decodedTest.Immediate); Assert.AreEqual(40512, decodedTest.Immediate); }
public void TestAddi() { this.virtualMachine.SetRegisterValue(Registers.R8, 2); IFormatInstruction slliInstruction = new IFormatInstruction( OperationCodes.Addi.Code(), Registers.R8.Number(), Registers.R9.Number(), 15); this.virtualMachine.ExecuteInstruction(slliInstruction.Encode()); Assert.AreEqual(17, this.virtualMachine.GetRegisterValue(Registers.R9)); }
public void TestSth() { uint addr = 1024; short value = 6424; this.virtualMachine.SetRegisterValue(Registers.R1, (int)addr); this.virtualMachine.SetRegisterValue(Registers.R2, value); IFormatInstruction ldbInstruction = new IFormatInstruction( OperationCodes.Sth.Code(), 1, 2, 0); this.virtualMachine.ExecuteInstruction(ldbInstruction.Encode()); Assert.AreEqual(value, this.virtualMachine.ReadHalfWordFromMemory(addr)); }
public void TestLdh() { uint addr = 1024; short value = 4251; this.virtualMachine.SetRegisterValue(Registers.R1, (int)addr); this.virtualMachine.WriteHalfWordToMemory(addr, value); IFormatInstruction ldbInstruction = new IFormatInstruction( OperationCodes.Ldh.Code(), 1, 2, 0); this.virtualMachine.ExecuteInstruction(ldbInstruction.Encode()); Assert.AreEqual(value, this.virtualMachine.GetRegisterValue(Registers.R2)); }
public void TestInstructions() { //Test the I-format this.virtualMachine.SetRegisterValue(Registers.R8, 17); IFormatInstruction addiInst = new IFormatInstruction( OperationCodes.Addi.Code(), Registers.R8.Number(), Registers.R9.Number(), 278); this.virtualMachine.ExecuteInstruction(addiInst.Encode()); Assert.AreEqual(278 + 17, this.virtualMachine.GetRegisterValue(Registers.R9)); this.virtualMachine.SetRegisterValue(Registers.R8, 17); addiInst = new IFormatInstruction( OperationCodes.Addi.Code(), Registers.R8.Number(), Registers.R9.Number(), -234); this.virtualMachine.ExecuteInstruction(addiInst.Encode()); Assert.AreEqual(-234 + 17, this.virtualMachine.GetRegisterValue(Registers.R9)); //Test the R-format this.virtualMachine.SetRegisterValue(Registers.R8, 17); this.virtualMachine.SetRegisterValue(Registers.R9, 4717); RFormatInstruction addInst = new RFormatInstruction( OperationCodes.Add.Code(), OperationXCodes.Add, Registers.R8.Number(), Registers.R9.Number(), Registers.R10.Number()); this.virtualMachine.ExecuteInstruction(addInst.Encode()); Assert.AreEqual(4717 + 17, this.virtualMachine.GetRegisterValue(Registers.R10)); //Tests memory instructions this.virtualMachine.SetRegisterValue(Registers.R8, 1024); this.virtualMachine.SetRegisterValue(Registers.R9, 4711); IFormatInstruction storeInst = new IFormatInstruction( OperationCodes.Stw.Code(), Registers.R8.Number(), Registers.R9.Number(), 0); this.virtualMachine.ExecuteInstruction(storeInst.Encode()); int value = this.virtualMachine.ReadWordFromMemory(1024); Assert.AreEqual(4711, value); IFormatInstruction loadInst = new IFormatInstruction( OperationCodes.Ldw.Code(), Registers.R8.Number(), Registers.R10.Number(), 0); this.virtualMachine.ExecuteInstruction(loadInst.Encode()); Assert.AreEqual(4711, this.virtualMachine.GetRegisterValue(Registers.R10)); //Tests branch instructions IFormatInstruction branchInst = new IFormatInstruction( OperationCodes.Br.Code(), 0, 0, 4 * 4); this.virtualMachine.ExecuteInstruction(branchInst.Encode()); Assert.AreEqual((uint)(4 * 4), this.virtualMachine.ProgramCounter); }