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)
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)
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)
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)
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)
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)
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)
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)
def test_copyto_direct(self): # arrange sut = Vm() sut.accumulator = 42 sut.rom = [sut.OPC_COPYTO, 2, sut.OPC_NOP] # act sut.next_step() # assert self.assertEqual(sut.program_count, 2) self.assertEqual(sut.accumulator, 42) self.assertEqual(sut.ram[2], 42)
def test_copyfrom_indirect(self): # arrange sut = Vm() sut.rom = [sut.OPC_COPYFROM_I, 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, 4) self.assertEqual(sut.ram, [1, 2, 3, 4])
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)
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])
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])
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
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
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()
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()