Example #1
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)
Example #2
0
    def test_next_step_no_rom_raise_NoRomException(self):
        # arrange
        sut = Vm()

        # act
        action = sut.next_step

        # assert
        self.assertRaises(NoRomException, action)
Example #3
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)
Example #4
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)
Example #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)
Example #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)
Example #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)
Example #8
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)
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
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)
Example #13
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)
Example #14
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)
Example #15
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)
Example #16
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)
Example #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)
Example #18
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)
Example #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)
Example #20
0
    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)
Example #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)
Example #22
0
    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])
Example #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])
Example #24
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])
Example #25
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()
Example #26
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
Example #27
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
Example #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()
Example #29
0
    def test_initialization_custom_ram_size(self):
        # arrange / act
        sut = Vm(10)

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