Exemplo n.º 1
0
    def test_helper_functions(self):

        r_mem = RealMemory()

        assert r_mem.get_address_tuple((0, 0)) == (0, 0)
        assert r_mem.get_address_tuple(0) == (0, 0)
        assert r_mem.get_address_tuple(203) == (12, 11)
        assert r_mem.get_address_tuple((12, 11)) == (12, 11)

        assert r_mem.get_address_int((0, 0)) == 0
        assert r_mem.get_address_int(0) == 0
        assert r_mem.get_address_int(203) == 203
        assert r_mem.get_address_int((12, 11)) == 203
Exemplo n.º 2
0
 def __init__(self, cell_handler=None):
     u""" Inicializuoja mašinos objektą.
     """
     
     self.real_memory = RealMemory(cell_handler)
     self.processor = Processor(self.real_memory)
     self.virtual_memory_data = None
     self.virtual_memory_code = None
Exemplo n.º 3
0
    def test_cell_access(self):

        r_mem = RealMemory()

        assert r_mem[356] == '00000000'
        r_mem[12, 11] = u'ačiū'
        assert r_mem[203] == '  a\xc4\x8di\xc5\xab'
                                        # 12 * 16 + 11 == 203

        word = '01234567'
        r_mem.put_data((0, 0), word * 16)
        r_mem.put_data((1, 0), word * 5 + 'aaa')
        r_mem.put_data((1, 15), word)
        try:
            r_mem.put_data((1, 16), word)
        except ValueError, e:
            assert unicode(e) == u'Duomenys netelpa į bloką.'
Exemplo n.º 4
0
    def test_creating_virtual_memory(self):

        r_mem = RealMemory()

        code = [
                'namo:LR1 00a',
                'LR2 00a\n',
                'CMP    ',
                u'JE «namo»  \n'.encode('utf-8'),
                ]
        data = [
                '[a]:labas\n\n\n\n',
                ]
        vmcode, vmdata = r_mem.create_virtual_memory(code, 1, data, 1)
        assert vmcode[0] == 'LR1 00a '
        assert r_mem.get_data((16, 0), 8 * 16) == (
                'LR1 00a '
                'LR2 00a '
                'CMP     '
                'JE 000  ' + '00000000' * 12)
        assert vmdata[10] == 'labas   '
        assert r_mem.get_data((17, 0), 8 * 16) == (
                '00000000' * 10 + 'labas   ' + '00000000' * 5)
Exemplo n.º 5
0
class RealMachine(object):
    u""" Realią mašiną simuliuojantis objektas.
    """

    def __init__(self, cell_handler=None):
        u""" Inicializuoja mašinos objektą.
        """
        
        self.real_memory = RealMemory(cell_handler)
        self.processor = Processor(self.real_memory)
        self.virtual_memory_data = None
        self.virtual_memory_code = None

    def passHandler(self, cell_handler):
        self.real_memory.SetHandler(cell_handler)

    def load_virtual_machine(
            self, file, stdin_handler=None, stdout_handler=None):
        u""" Pakrauna virtualią mašiną.
        """

        code = []
        code_size = None
        data = []
        data_size = None

        if isinstance(file, str) or isinstance(file, unicode):
            with open(file) as fp:
                code_segment = False
                data_segment = False
                for line in fp:

                    if not code_segment and not data_segment and \
                            line == 'CODE\n':
                        code_segment = True
                        continue
                    elif code_segment and not data_segment and \
                            line == 'ENDCODE\n':
                        code_segment = False
                        continue
                    elif not code_segment and not data_segment and \
                            line.startswith('DATA '):
                        data_segment = True
                        data_size = int(line[4:])
                        continue
                    elif not code_segment and data_segment and \
                            line == 'ENDDATA\n':
                        data_segment = False
                        continue

                    if code_segment:
                        code.append(line)
                    if data_segment:
                        data.append(line)
                code_size = int(ceil(float(len(code)) / BLOCK_SIZE))

            self.virtual_memory_code, self.virtual_memory_data = \
                    self.real_memory.create_virtual_memory(
                            code, code_size, data, data_size,
                            stdin_handler, stdout_handler)
            self.pager = self.virtual_memory_code.pager
            self.processor.PLR = self.virtual_memory_code.pager.PLR
            self.processor.PLBR = self.virtual_memory_code.pager.PLBR
            self.processor.set_virtual_memory(
                    self.virtual_memory_code, self.virtual_memory_data,
                    self.pager)
            self.processor.IC = 0
            # TODO: Atidaryti išorinius failus.
        else:
            # Šitas if iš esmės skirtas tam, jei kartais butų visgi 
            # nuspręsta pasinaudoti „#!/usr/bin/pyemu“ funkcionalumu.
            raise Exception('Not implemented!')