def test_graphics():
    memory = Memory()
    screen = FakeScreen(memory)
    cpu = Cpu(GRAPHICS, screen=screen, memory=memory)
    cpu.run_until_exit_or_halt()
    assert cpu.output == []
    assert memory[210] == 254
Exemplo n.º 2
0
def test_add():
    cpu = Cpu([])
    cpu.registers[0] = 10
    cpu.registers[1] = 20

    Add(0, 1, 2).execute(cpu)

    assert cpu.registers[2] == 30
Exemplo n.º 3
0
def test_add_register_and_number_carry():
    cpu = Cpu([])
    cpu.registers[0] = 250

    AddRegisterAndNumber(50, 0, 1).execute(cpu)

    assert cpu.registers[1] == 44
    assert cpu.carry_flag
Exemplo n.º 4
0
def test_subtract():
    cpu = Cpu([])
    cpu.registers[0] = 30
    cpu.registers[1] = 20

    Sub(0, 1, 2).execute(cpu)

    assert cpu.registers[2] == 10
Exemplo n.º 5
0
def test_subtract_carry():
    cpu = Cpu([])
    cpu.registers[0] = 20
    cpu.registers[1] = 30

    Sub(0, 1, 2).execute(cpu)

    assert cpu.registers[2] == 246
    assert cpu.carry_flag
Exemplo n.º 6
0
def test_add_carry():
    cpu = Cpu([])
    cpu.registers[0] = 250
    cpu.registers[1] = 6

    Add(0, 1, 2).execute(cpu)

    assert cpu.registers[2] == 0
    assert cpu.carry_flag
Exemplo n.º 7
0
def test_subroutine_call_and_return():
    cpu = Cpu([])
    cpu.instruction_pointer = 10

    CallSubroutine(42).execute(cpu)

    assert cpu.instruction_pointer == 42

    Return().execute(cpu)

    assert cpu.instruction_pointer == 10
Exemplo n.º 8
0
def test_memory():
    cpu = Cpu([])

    cpu.registers[1] = 42
    Store(1, 123).execute(cpu)

    assert cpu.memory[123] == 42

    Load(1, 123).execute(cpu)

    assert cpu.registers[1] == 42
Exemplo n.º 9
0
def run_program_from_file(filename: str, program_args: List[int]):
    if filename.endswith(".txt"):
        sprites, program = assembly.load_from_file(filename)
    else:
        program, sprites = binary.load_program_from_file(filename)
    memory = Memory(sprites=sprites)
    screen = PygameScreen(memory, filename)
    cpu = Cpu(program, args=program_args, allow_sleeps=True, screen=screen, memory=memory)
    run_program(cpu)
def test_breakpoint():
    cpu = Cpu(BREAKPOINT)
    cpu.run_until_exit_or_halt()
    assert cpu.output == ["1"]
    assert cpu.halted
    cpu.halted = False
    cpu.run_until_exit_or_halt()
    assert cpu.output == ["1", "2"]
Exemplo n.º 11
0
def run_example(name: str, program_args: List[int]):
    sprites, program = EXAMPLE_PROGRAMS[name]
    memory = Memory(sprites)
    screen = PygameScreen(memory, name)
    cpu = Cpu(program,
              args=program_args,
              allow_sleeps=True,
              screen=screen,
              memory=memory)
    run_program(cpu)
Exemplo n.º 12
0
def test_stack_push_and_pop():
    cpu = Cpu([])

    Push(42).execute(cpu)
    Push(43).execute(cpu)
    Pop(0).execute(cpu)
    Pop(1).execute(cpu)

    assert cpu.registers[0] == 43
    assert cpu.registers[1] == 42
Exemplo n.º 13
0
def run_program(cpu: Cpu):
    try:
        while True:
            cpu.run_until_exit_or_halt()
            if cpu.has_exited:
                print(f"Program exited with code {cpu.exit_code}")
                return
            if cpu.halted:
                print(f"Program halted at {cpu.instruction_pointer}")
                print_debugger_help()
                while cpu.halted:
                    choice = input("> ")
                    if choice.startswith("1"):
                        cpu.halted = False
                    elif choice.startswith("2"):
                        cpu.run_one_cycle()
                        if cpu.has_exited:
                            print(f"Program exited with code {cpu.exit_code}")
                            return
                    elif choice.startswith("3"):
                        print(cpu.dump())
                    else:
                        print_debugger_help()
    except Exception as e:
        raise Exception(f"Program crashed: {e}") from e
Exemplo n.º 14
0
 def execute(self, cpu: Cpu):
     if cpu.registers[self.register_a] > cpu.registers[self.register_b]:
         cpu.instruction_pointer = self.instruction_index
Exemplo n.º 15
0
 def execute(self, cpu: Cpu):
     cpu.refresh_screen()
Exemplo n.º 16
0
 def execute(self, cpu: Cpu):
     cpu.add(cpu.registers[self.source_register], self.number,
             self.destination_register)
Exemplo n.º 17
0
 def execute(self, cpu: Cpu):
     cpu.do_output(cpu.registers[self.register])
Exemplo n.º 18
0
 def __post_init__(self):
     Cpu.assert_fits_in_word(self.address)
Exemplo n.º 19
0
 def execute(self, cpu: Cpu):
     cpu.activate_screen()
def test_add_two_args():
    cpu = Cpu(ADD_TWO_ARGS, args=[38, 4])
    cpu.run_until_exit_or_halt()
    assert cpu.output == ["42"]
def test_1337():
    cpu = Cpu(PRINT_1337)
    cpu.run_until_exit_or_halt()
    assert cpu.output == ["1", "3", "3", "7"]
Exemplo n.º 22
0
 def execute(self, cpu: Cpu):
     return_address = cpu.stack.pop()
     cpu.instruction_pointer = return_address
Exemplo n.º 23
0
 def execute(self, cpu: Cpu):
     cpu.stack.append(cpu.instruction_pointer)
     cpu.instruction_pointer = self.instruction_index
Exemplo n.º 24
0
 def __post_init__(self):
     Cpu.assert_fits_in_word(self.value)
def test_subroutine():
    cpu = Cpu(SUBROUTINE)
    cpu.run_until_exit_or_halt()
    assert cpu.output == ["1", "2", "3"]
Exemplo n.º 26
0
 def execute(self, cpu: Cpu):
     cpu.halted = True
def test_fibonacci():
    cpu = Cpu(FIBONACCI)
    cpu.run_until_exit_or_halt()
    # Fibonacci sequence loops around at 255
    assert cpu.output == ["0", "1", "1", "2", "3", "5", "8", "13", "21", "34", "55", "89", "144", "233", "121", "98"]
Exemplo n.º 28
0
 def execute(self, cpu: Cpu):
     cpu.exit_code = self.exit_code
     cpu.has_exited = True
Exemplo n.º 29
0
 def execute(self, cpu: Cpu):
     cpu.subtract(cpu.registers[self.register_a],
                  cpu.registers[self.register_b], self.destination_register)
Exemplo n.º 30
0
 def execute(self, cpu: Cpu):
     cpu.sleep(self.millis)