示例#1
0
        public void TestReturnProcedure()
        {
            var cpu = new CPU();
            cpu.StackPush(10 << 2);

            var instructionAdd = new ReturnProcedure("rp", 0);

            cpu.AddInstruction(instructionAdd);

            cpu.RunClock();
            Assert.AreEqual("rp", instructionAdd.GetFetch(), false);

            cpu.RunClock();
            Assert.AreEqual("RP: ", instructionAdd.GetDecode(), false);

            //Jump should have been taken
            Assert.AreEqual(10 << 2, cpu.GetPC());
        }
示例#2
0
        public void TestJumpProcedure()
        {
            var cpu = new CPU();

            var instructionAdd = new JumpProcedure("jp 10", 0, 10);
            cpu.AddInstruction(instructionAdd);

            cpu.RunClock();
            Assert.AreEqual("jp 10", instructionAdd.GetFetch(), false);

            cpu.RunClock();
            Assert.AreEqual("JP: imm = 10", instructionAdd.GetDecode(), false);

            //Jump should have been taken
            Assert.AreEqual(10 << 2, cpu.GetPC());

            //Check stack contents
            Assert.AreEqual(1 << 2, cpu.StackPeek());
        }
示例#3
0
        public void TestJump()
        {
            var cpu = new CPU();

            var instructionAdd = new J("j 10", 0, 10);
            cpu.AddInstruction(instructionAdd);

            cpu.RunClock();
            Assert.AreEqual("j 10", instructionAdd.GetFetch(), false);

            cpu.RunClock();
            Assert.AreEqual("J: imm = 10", instructionAdd.GetDecode(), false);

            //Jump should have been taken
            Assert.AreEqual(10 << 2, cpu.GetPC());
        }
示例#4
0
        public void TestJr()
        {
            var cpu = new CPU();
            cpu.RegWrite(2, 10 << 2); //$2 = 40

            var instructionAdd = new JR("jr $2", 0, 2);

            cpu.AddInstruction(instructionAdd);

            cpu.RunClock();
            Assert.AreEqual("jr $2", instructionAdd.GetFetch(), false);

            cpu.RunClock();
            Assert.AreEqual("JR: rs = $2", instructionAdd.GetDecode(), false);

            //Jump should have been taken
            Assert.AreEqual(10 << 2, cpu.GetPC());
        }
示例#5
0
        public void TestJal()
        {
            var cpu = new CPU();

            const string instr = "jal 4";

            cpu.AddInstruction(new Jal(instr, 0, 4));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 1)); //Should jump to this instruction

            var expectedRecords = new List<ExecutionRecordList>();
            int clockCycle = 0;

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Fetch, instr, 0, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Decode, "Jal: imm = 4", 0, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Fetch, "nop", 1, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            //Jump should have been taken
            Assert.AreEqual(4 << 2, cpu.GetPC());

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Execute, "None", 0, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Fetch, "nop", 2, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Memory, "None", 0, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Decode, "Nop: ", 2, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Writeback, "Register $15 <= 4", 0, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Execute, "", 2, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            //Test value inside register file
            Assert.AreEqual(1 << 2, cpu.RegRead(15));
            Assert.AreEqual(clockCycle, cpu.ClockCycle);
        }
示例#6
0
        public void TestBleTaken()
        {
            var cpu = new CPU();

            //Set initial values in register file
            cpu.RegWrite(1, 100);
            cpu.RegWrite(2, 200);

            const string instr = "ble $1, $2, 5";

            cpu.AddInstruction(new Ble(instr, 0, 1, 2, 5));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));
            cpu.AddInstruction(new Nop("nop", 0));

            var expectedRecords = new List<ExecutionRecordList>();
            int clockCycle = 0;

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Fetch, instr, 0, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Decode, "Ble: rs = $1, rt = $2, imm = 5", 0, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Fetch, "nop", 1, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            cpu.RunClock();
            expectedRecords.Add(new ExecutionRecordList());
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Execute, "Ble 100 <= 200 = True", 0, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Decode, "Nop: ", 1, null));
            expectedRecords[clockCycle].Add(new ExecutionRecord(ExecutionType.Fetch, "nop", 2, null));
            clockCycle++;
            Assert.IsTrue(expectedRecords.SequenceEqual(cpu.ExecutionRecords));

            Assert.AreEqual(6 << 2, cpu.GetPC());
            Assert.AreEqual(clockCycle, cpu.ClockCycle);
        }