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
def test_add(): cpu = Cpu([]) cpu.registers[0] = 10 cpu.registers[1] = 20 Add(0, 1, 2).execute(cpu) assert cpu.registers[2] == 30
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
def test_subtract(): cpu = Cpu([]) cpu.registers[0] = 30 cpu.registers[1] = 20 Sub(0, 1, 2).execute(cpu) assert cpu.registers[2] == 10
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
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
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
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
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"]
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)
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
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
def execute(self, cpu: Cpu): if cpu.registers[self.register_a] > cpu.registers[self.register_b]: cpu.instruction_pointer = self.instruction_index
def execute(self, cpu: Cpu): cpu.refresh_screen()
def execute(self, cpu: Cpu): cpu.add(cpu.registers[self.source_register], self.number, self.destination_register)
def execute(self, cpu: Cpu): cpu.do_output(cpu.registers[self.register])
def __post_init__(self): Cpu.assert_fits_in_word(self.address)
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"]
def execute(self, cpu: Cpu): return_address = cpu.stack.pop() cpu.instruction_pointer = return_address
def execute(self, cpu: Cpu): cpu.stack.append(cpu.instruction_pointer) cpu.instruction_pointer = self.instruction_index
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"]
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"]
def execute(self, cpu: Cpu): cpu.exit_code = self.exit_code cpu.has_exited = True
def execute(self, cpu: Cpu): cpu.subtract(cpu.registers[self.register_a], cpu.registers[self.register_b], self.destination_register)
def execute(self, cpu: Cpu): cpu.sleep(self.millis)