Пример #1
0
def symbbl(inst, arch="i386", preDefineMem={}, startPC=0):
    assert arch in ["i386", "amd64"]

    instmem = InstMemory()
    for addr in xrange(len(inst)):
        instmem.putchar(addr, inst[addr])

    datamem = DataMemory({'i386': 32, 'amd64': 64}[arch])

    PC0 = startPC
    cpu = Cpu(instmem, datamem, arch)
    cpu.PC = startPC

    for k in preDefineMem.keys():
        _doPreDefineMem(k[0], k[1], preDefineMem[k], datamem, cpu)

    #RUN
    while PC0 <= cpu.PC < len(inst) + PC0:
        if issymbolic(cpu.PC):
            pcs = getallvalues(cpu.PC)
            if len(pcs) == 1:
                cpu.PC = pcs[0]
            else:
                print "Stop Execution because symbolic PC"
                print pcs
                print cpu.PC
                raw_input()
                break
        print cpu.getInstruction(cpu.PC)
        cpu.execute()

    return cpu
Пример #2
0
class CpuTest(unittest.TestCase):

    def setUp(self):
        self.mManager = Mock()
        self.mHandler = Mock()
        self.cpu = Cpu(self.mManager,self.mHandler)
        self.aPcb = Mock()
        when(self.aPcb).getPid().thenReturn(0)
        when(self.aPcb).basePointer().thenReturn(0)
        when(self.aPcb).programCounter().thenReturn(0)
        when(self.aPcb).size().thenReturn(0)
        when(self.aPcb).displazament().thenReturn(0)
        self.instruction = Mock()

    def test_changeRoundRobin(self):
        self.cpu.changeRoundRobin(8)
        assert (self.cpu.roundRobin == 8)

    def test_pcIncrease(self):
        self.cpu.currentPcb = self.aPcb
        self.cpu.pcIncrease()
        verify(self.aPcb).pcIncrease()

    def test_assignPcb(self):
        self.cpu.assignPcb(self.aPcb)
        assert (self.cpu.currentPcb == self.aPcb)

    def test_removePcb(self):
        self.cpu.currentPcb = self.aPcb
        self.cpu.removePcb()
        assert (self.cpu.currentPcb == None)

    def test_havePcbWithPcb(self):
        self.cpu.currentPcb = self.aPcb
        assert (self.cpu.havePcb())

    def test_havePcbWithOutPcb(self):
        self.assertFalse(self.cpu.havePcb())

    def test_executeWithFullQuantum(self):

        when(self.cpu).getCurrentPcb().thenReturn(self.aPcb)

        when(self.cpu).getMemory().thenReturn(self.mManager)
        when(self.mManager).getInstruction(self.aPcb.getPid(),self.aPcb.getDisplacement()).thenReturn(self.instruction)

        when(self.instruction).execute().thenReturn(False)
        when(self.instruction).isIOInstruction().thenReturn(False)
        self.cpu.quantum = 1
        self.cpu.execute()

        verify(self.cpu.handler,times(1)).toWait(self.aPcb)
        verify(self.cpu.handler,times(0)).toKill(self.aPcb)
        verify(self.cpu.handler,times(0)).toIO(self.aPcb)
        assert self.cpu.quantum == 0


    def test_executeWithOutFullQuantum(self):

        when(self.cpu).getCurrentPcb().thenReturn(self.aPcb)

        when(self.cpu).getMemory().thenReturn(self.mManager)
        when(self.mManager).getInstruction(self.aPcb.getPid(),self.aPcb.getDisplacement()).thenReturn(self.instruction)

        when(self.instruction).execute().thenReturn(False)
        when(self.instruction).isIOInstruction().thenReturn(False)
        self.cpu.execute()

        verify(self.aPcb).pcIncrease()
        verify(self.cpu.handler,times(0)).toWait(self.aPcb)
        verify(self.cpu.handler,times(0)).toKill(self.aPcb)
        verify(self.cpu.handler,times(0)).toIO(self.aPcb)
        assert self.cpu.quantum == 1

    def test_executeWithKillIntruction(self):

        when(self.cpu).getCurrentPcb().thenReturn(self.aPcb)

        when(self.cpu).getMemory().thenReturn(self.mManager)
        when(self.mManager).getInstruction(self.aPcb.getPid(),self.aPcb.getDisplacement()).thenReturn(self.instruction)

        when(self.instruction).execute().thenReturn(True)
        when(self.instruction).isIOInstruction().thenReturn(False)
        self.cpu.execute()

        verify(self.cpu.handler,times(0)).toWait(self.aPcb)
        verify(self.cpu.handler,times(1)).toKill(self.aPcb)
        verify(self.cpu.handler,times(0)).toIO(self.aPcb)
        assert self.cpu.quantum == 0

    def test_executeWithIOIntruction(self):

        when(self.cpu).getCurrentPcb().thenReturn(self.aPcb)

        when(self.cpu).getMemory().thenReturn(self.mManager)
        when(self.mManager).getInstruction(self.aPcb.getPid(),self.aPcb.getDisplacement()).thenReturn(self.instruction)

        when(self.instruction).execute().thenReturn(False)
        when(self.instruction).isIOInstruction().thenReturn(True)
        self.cpu.execute()

        verify(self.cpu.handler,times(0)).toWait(self.aPcb)
        verify(self.cpu.handler,times(0)).toKill(self.aPcb)
        verify(self.cpu.handler,times(1)).toIOInput(self.aPcb)
        assert self.cpu.quantum == 0