Exemplo n.º 1
0
    def test_next_step_invalid_opcode_UnknownOpcodeException(self):
        # arrange
        sut = Vm()
        sut.rom = [255]

        # act
        action = sut.next_step

        # assert
        self.assertRaises(UnknownOpCodeException, action)
Exemplo n.º 2
0
    def text_next_step_everything_okay(self):
        # arrange
        sut = Vm()
        sut.rom = [7, 7, 7]

        # action
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 1)
Exemplo n.º 3
0
    def test_load(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_LOAD, 42, sut.OPC_NOP]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 2)
        self.assertEqual(sut.accumulator, 42)
Exemplo n.º 4
0
    def test_copyfrom_direct_invalid_adress(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_COPYFROM, 4, sut.OPC_NOP]
        sut.ram = [1, 2, 3, 4]

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidRamAdressException, action)
Exemplo n.º 5
0
    def test_bumpdwn_indirect_invalid_adress(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_BUMPDWN_I, 3, sut.OPC_NOP]
        sut.ram = [1, 2, 3, 4]

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidRamAdressException, action)
Exemplo n.º 6
0
    def test_next_step_pc_negative_InvalidPcException(self):
        # arrange
        sut = Vm()
        sut.rom = [7, 7, 7]
        sut.program_count = -1

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidPcException, action)
Exemplo n.º 7
0
    def test_next_step_pc_larger_than_rom_InvalidPcException(self):
        # arrange
        sut = Vm()
        sut.rom = [7, 7, 7]
        sut.program_count = 4

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidPcException, action)
Exemplo n.º 8
0
    def test_jump_invalid_target(self):
        # arrange
        sut = Vm()
        sut.program_count = 1
        sut.rom = [sut.OPC_NOP, sut.OPC_JUMP, 3]

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidPcException, action)
Exemplo n.º 9
0
    def test_sub_indirect_invalid_adress(self):
        # arrange
        sut = Vm()
        sut.accumulator = 1
        sut.rom = [sut.OPC_SUB_I, 2, sut.OPC_NOP]
        sut.ram = [1, 2, 3]

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidRamAdressException, action)
Exemplo n.º 10
0
    def test_jumpa_invalid_target(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_NOP, sut.OPC_NOP, sut.OPC_JUMPA]
        sut.accumulator = 5
        sut.program_count = 2

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidPcException, action)
Exemplo n.º 11
0
    def test_jumpn_invalid_target_fail(self):
        # arrange
        sut = Vm()
        sut.accumulator = -4
        sut.rom = [sut.OPC_NOP, sut.OPC_JUMPN, 4, sut.OPC_NOP]
        sut.program_count = 1

        # act
        action = sut.next_step

        # assert
        self.assertRaises(InvalidPcException, action)
Exemplo n.º 12
0
    def __init__(self, vm_ram_size, rom):
        super().__init__()

        self.title = 'HRM VM'
        self.left = 10
        self.top = 10
        self.width = 350
        self.height = 500

        self.vm = Vm(ram_size=vm_ram_size)
        self.vm.rom = rom
        self.vm.ostream_subscribers.append(self.ostream_append)

        self.initUI()
Exemplo n.º 13
0
    def test_initialization_default_values(self):
        # arrange / act
        sut = Vm()

        # assert
        self.assertEqual(sut.accumulator, 0)
        self.assertEqual(sut.program_count, 0)
        self.assertEqual(len(sut.ram), 255)
        self.assertIsNone(sut.rom)
Exemplo n.º 14
0
    def test_next_step_no_rom_raise_NoRomException(self):
        # arrange
        sut = Vm()

        # act
        action = sut.next_step

        # assert
        self.assertRaises(NoRomException, action)
Exemplo n.º 15
0
    def test_pop_inbox(self):
        # arrange
        mock_store = [-111]  # random number

        def mock(x):
            mock_store[0] = x

        sut = Vm()
        sut.rom = [sut.OPC_INBOX, sut.OPC_NOP]
        sut.istream.put(42)

        sut.istream_subscribers.append(mock)

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 1)
        self.assertEqual(sut.accumulator, 42)
        self.assertEqual(mock_store[0], 42)  # mock called
Exemplo n.º 16
0
    def test_add_direct(self):
        # arrange
        sut = Vm()
        sut.accumulator = 1
        sut.rom = [sut.OPC_ADD, 0, sut.OPC_NOP]
        sut.ram = [1, 2, 3]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.accumulator, 2)
Exemplo n.º 17
0
    def test_jumpn_dont_jump(self):
        # arrange
        sut = Vm()
        sut.accumulator = 1
        sut.rom = [sut.OPC_NOP, sut.OPC_JUMPN, 0, sut.OPC_NOP]
        sut.program_count = 1

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 3)
Exemplo n.º 18
0
    def test_sub_indirect(self):
        # arrange
        sut = Vm()
        sut.accumulator = 1
        sut.rom = [sut.OPC_SUB_I, 0, sut.OPC_NOP]
        sut.ram = [1, 2, 3]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.accumulator, -1)
Exemplo n.º 19
0
    def test_jumpa(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_NOP, sut.OPC_NOP, sut.OPC_JUMPA]
        sut.accumulator = 1
        sut.program_count = 2

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 1)
Exemplo n.º 20
0
    def test_jumpn_invalid_target_dont_fail(self):
        # arrange
        sut = Vm()
        sut.accumulator = 4
        sut.rom = [sut.OPC_NOP, sut.OPC_JUMPN, 4, sut.OPC_NOP]
        sut.program_count = 1

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 3)
Exemplo n.º 21
0
    def test_ldpc(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_NOP, sut.OPC_LDPC, sut.OPC_NOP]
        sut.program_count = 1
        sut.accumulator = -4

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.accumulator, 1)
        self.assertEqual(sut.program_count, 2)
Exemplo n.º 22
0
    def test_bumpdwn_indirect(self):
        # arrange
        sut = Vm()
        sut.accumulator = 42
        sut.ram = [1, 2, 3, 4]
        sut.rom = [sut.OPC_BUMPDWN_I, 2, sut.OPC_NOP]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 2)
        self.assertEqual(sut.accumulator, 3)
        self.assertEqual(sut.ram, [1, 2, 3, 3])
Exemplo n.º 23
0
    def test_nop(self):
        # arrange
        sut = Vm()
        sut.accumulator = 42
        sut.ram = [1, 2, 3, 4]
        sut.rom = [sut.OPC_NOP, sut.OPC_NOP]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 1)
        self.assertEqual(sut.accumulator, 42)
        self.assertEqual(sut.ram, [1, 2, 3, 4])
Exemplo n.º 24
0
    def test_copyto_indirect(self):
        # arrange
        sut = Vm()
        sut.accumulator = 42
        sut.ram = [1, 2, 3, 4]
        sut.rom = [sut.OPC_COPYTO_I, 2, sut.OPC_NOP]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 2)
        self.assertEqual(sut.accumulator, 42)
        self.assertEqual(sut.ram, [1, 2, 3, 42])
Exemplo n.º 25
0
    def test_jump(self):
        # arrange
        sut = Vm()
        sut.program_count = 1
        sut.rom = [sut.OPC_NOP, sut.OPC_JUMP, 0]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 0)
Exemplo n.º 26
0
    def test_copyfrom_direct(self):
        # arrange
        sut = Vm()
        sut.rom = [sut.OPC_COPYFROM, 2, sut.OPC_NOP]
        sut.ram = [1, 2, 3, 4]

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 2)
        self.assertEqual(sut.accumulator, 3)
        self.assertEqual(sut.ram, [1, 2, 3, 4])
Exemplo n.º 27
0
    def test_push_outbox(self):
        # arrange
        mock_store = [-143]  # random number

        def mock(x):
            mock_store[0] = x

        sut = Vm()
        sut.rom = [sut.OPC_OUTBOX, sut.OPC_NOP]
        sut.ostream_subscribers.append(mock)
        sut.accumulator = 42

        # act
        sut.next_step()

        # assert
        self.assertEqual(sut.program_count, 1)
        self.assertEqual(sut.accumulator, 0)
        self.assertEqual(mock_store[0], 42)  # mock called
Exemplo n.º 28
0
def main():
    vm = Vm(ram_size=10)

    vm.rom = [8, 0, 8, 1, 10, 2, 16, 0]

    #vm.rom = [2,11,5,0, 2,13,5,1, 2,-20,5,2, # load initial
    #          2,2,5,4, # load 2 to [4]
    #          9,4,     # incr [[4]]
    #          10,4,    # decr [4]
    #          18,12,   # jumpn 12
    #          16,16]   # jump 16

    data = readbin(sys.argv[1])
    vm.rom = data

    vm.ostream_subscribers.append(ostream_emulation)

    while (True):
        print_vm(vm)
        #input("enter for next step")
        time.sleep(0.1)
        vm.next_step()
Exemplo n.º 29
0
class App(QMainWindow):
    def __init__(self, vm_ram_size, rom):
        super().__init__()

        self.title = 'HRM VM'
        self.left = 10
        self.top = 10
        self.width = 350
        self.height = 500

        self.vm = Vm(ram_size=vm_ram_size)
        self.vm.rom = rom
        self.vm.ostream_subscribers.append(self.ostream_append)

        self.initUI()

    def ostream_append(self, out):
        self.ostream_val.setText(self.ostream_val.text() + " " + str(out))

    def initUI(self):

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # immutable components
        acc_lbl = QLabel(self)
        acc_lbl.move(130, 100)
        acc_lbl.setText("ACC:")
        pc_lbl = QLabel(self)
        pc_lbl.move(130, 70)
        pc_lbl.setText("PC:")

        ostream_lbl = QLabel(self)
        ostream_lbl.move(20, 40)
        ostream_lbl.setText("Out:")

        istream_lbl = QLabel(self)
        istream_lbl.move(20, 15)
        istream_lbl.setText("In:")

        ram_lbl = QLabel(self)
        ram_lbl.move(240, 70)
        ram_lbl.setText("RAM:")

        ram_lbls = [QLabel(self) for i in range(len(self.vm.ram))]
        for i in range(len(self.vm.ram)):
            ram_lbls[i].move(240, 95 + 15 * i)
            ram_lbls[i].setText(bytestr(i) + ":")

        rom_vals = [QLabel(self) for i in range(len(self.vm.rom))]
        for i in range(len(self.vm.rom)):
            rom_vals[i].move(80, 95 + 15 * i)
            rom_vals[i].setText(bytestr(self.vm.rom[i]))

        # mutable components
        self.ostream_val = QLabel(self)
        self.ostream_val.move(50, 40)

        self.istream_val = QLabel(self)
        self.istream_val.move(50, 15)
        self.istream_val.setText("dummy")

        self.acc_val = QLabel(self)
        self.acc_val.move(170, 100)

        self.pc_val = QLabel(self)
        self.pc_val.move(170, 70)

        self.ram_vals = [QLabel(self) for i in range(len(self.vm.ram))]
        for i in range(len(self.vm.ram)):
            self.ram_vals[i].move(300, 95 + 15 * i)

        rom_lbl = QLabel(self)
        rom_lbl.move(30, 70)
        rom_lbl.setText("ROM:")

        self.rom_lbls = [QLabel(self) for i in range(len(self.vm.rom))]
        for i in range(len(self.vm.rom)):
            self.rom_lbls[i].move(30, 95 + 15 * i)

        self.update_vm_values()

        # Create textbox
        #self.textbox = QLineEdit(self)
        #self.textbox.move(20, 20)
        #self.textbox.resize(280,40)

        # Create a button in the window
        self.but_next = QPushButton('Next Step', self)
        self.but_next.move(110, 460)
        self.but_next.clicked.connect(self.next_step_click)

        self.show()

    def update_vm_values(self):
        for i in range(len(self.vm.ram)):
            val = self.ram_vals[i]
            val.setText(bytestr(self.vm.ram[i]))

        for i in range(len(self.vm.rom)):
            romlbl = self.rom_lbls[i]
            if i == self.vm.program_count:
                romlbl.setText(bytestr(i) + ":>")
            else:
                romlbl.setText(bytestr(i) + ":")

        self.acc_val.setText(bytestr(self.vm.accumulator))
        self.pc_val.setText(bytestr(self.vm.program_count))

    @pyqtSlot()
    def next_step_click(self):
        self.vm.next_step()
        self.update_vm_values()
Exemplo n.º 30
0
    def test_initialization_custom_ram_size(self):
        # arrange / act
        sut = Vm(10)

        # assert
        self.assertEqual(len(sut.ram), 10)