Пример #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)
Пример #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)
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
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)
Пример #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)
Пример #11
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)
Пример #12
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)
Пример #13
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)
Пример #14
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)
Пример #15
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)
Пример #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)
Пример #17
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)
Пример #18
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)
Пример #19
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()
Пример #20
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])
Пример #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)
Пример #22
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])
Пример #23
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])
Пример #24
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
Пример #25
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