示例#1
0
class TestMipsPipelineSteps(unittest.TestCase):
    def setUp(self):
        self.mips = Mips()
        self.add = Instruction('00000000001001110100100000100000 ; I1: add R9,R1,R7')
        self.addi = Instruction('00100000000010100000000001100100 ; I2: addi R10,R0,100')
        
    def test_an_intermediary_step(self):
        self.add.instruction_decode(self.mips)
        self.add.execute(self.mips)
        self.addi.instruction_decode(self.mips)

        self.mips.pipeline[2].instruction = self.addi
        self.mips.pipeline[3].instruction = self.add
        self.mips.execute_pipeline()
        self.assertEqual(self.mips.pipeline[2].instruction, self.addi)
        self.assertTrue(self.mips.pipeline[2].done)
        self.assertEqual(self.mips.pipeline[3].instruction, self.add)
        self.assertTrue(self.mips.pipeline[3].done)
示例#2
0
class TestMulInstruction(BaseTestInstruction, unittest.TestCase):
    def setUp(self):
        self.text = "00000000001001110100100000011000 ; I4: mul R9,R1,R7"
        self.instruction = Instruction(self.text)
        
        registers = Registers()
        registers[1] = 3
        registers[7] = 2
        registers[9] = 4
        self._mips = MipsStub(registers, memory=[])
        
    def test_instruction_decode(self):
        BaseTestInstruction.instruction_decode(self)
        self.assertEqual(self.instruction.rs_value, 3)
        self.assertEqual(self.instruction.rt_value, 2)
        
    def test_execute(self):
        BaseTestInstruction.execute(self)
        self.assertEqual(self.instruction.rd_value, 6)
        self.assertEqual(self._mips.registers[1], 3)
        self.assertEqual(self._mips.registers[7], 2)
        
    def test_execute_with_data_forward(self):
        self._mips.data_forwarding = True
        BaseTestInstruction.execute(self)
        self.assertEqual(self._mips.registers[9], 6)
        
    def test_memory_access(self):
        BaseTestInstruction.memory_access(self)
                
    def test_write_back(self):
        BaseTestInstruction.write_back(self)
        self.assertEqual(self._mips.registers[1], 3)
        self.assertEqual(self._mips.registers[7], 2)
        self.assertEqual(self._mips.registers[9], 6)
        
    def test_all_true_returns(self):
        self.assertTrue(self.instruction.instruction_decode(self._mips))
        self.assertFalse(self.instruction.execute(self._mips))
        self.assertTrue(self.instruction.execute(self._mips))
        self.assertTrue(self.instruction.memory_access(self._mips))
        self.assertTrue(self.instruction.write_back(self._mips))

    def test_register_lock(self):
        BaseTestInstruction.instruction_decode(self)
        self.assertRaises(RegisterInUseException, self._mips.registers.__getitem__, 9)
        
    def test_register_unlock(self):
        BaseTestInstruction.write_back(self)
        self._mips.registers[9]