def test_init(self): self.assertEqual(cpu.MEMORY_SIZE, len(self.cpu.memory)) self.assertEqual(bytearray(cpu.font_sprites), self.cpu.memory[0:len(cpu.font_sprites)]) self.assertEqual([], self.cpu.stack) self.assertEqual(0x200, self.cpu.pc) self.assertEqual(0x600, CPU(Screen(), Keyboard(), starting_address=0x600).pc) self.assertEqual([0x00] * 16, self.cpu.V) self.assertEqual(0x000, self.cpu.I) self.assertEqual(0x000, self.cpu.I) self.assertEqual(0x00, self.cpu.delay_timer) self.assertEqual(0x00, self.cpu.sound_timer) self.assertEqual(0x042, CPU(Screen(), Keyboard(), starting_address=0x042).pc)
def test_scaling_factor(self): screen = Screen(scaling_factor=3) self.assertEqual(64 * 3, screen.surface.get_width()) self.assertEqual(32 * 3, screen.surface.get_height()) screen.buffer[1][2] = True screen.update() for y in range(screen.surface.get_height()): for x in range(screen.surface.get_width()): if x in [6, 7, 8] and y in [3, 4, 5]: self.assertEqual(WHITE, screen.surface.get_at((x, y))) else: self.assertEqual(BLACK, screen.surface.get_at((x, y)))
def test_update(self): screen = Screen() screen.update() for row in range(32): for col in range(64): self.assertEqual(BLACK, screen.surface.get_at((col, row))) for row in range(32): for col in range(64): screen.buffer[row][col] = True screen.update() for row in range(32): for col in range(64): self.assertEqual(WHITE, screen.surface.get_at((col, row)))
def __init__(self, scaling_factor: int, cycles_per_frame: int, starting_address: int): pygame.init() self.screen = Screen(scaling_factor) self.keyboard = Keyboard() self.sound = Sound() self.cpu = CPU(self.screen, self.keyboard, starting_address) self.cycles_per_frame = cycles_per_frame sixty_hertz_ms = round(1000 / SIXTY_HERTZ) pygame.time.set_timer(SIXTY_HERTZ_CLOCK, sixty_hertz_ms) print( f"Target CPU speed: {cycles_per_frame * SIXTY_HERTZ} instructions per second" ) print(f"Screen scaling factor: {scaling_factor}")
def test_init(self): screen = Screen() self.assertEqual(32, len(screen.buffer)) for row in range(32): self.assertEqual([False] * 64, screen.buffer[row])
def setUp(self): self.screen = Screen() self.keyboard = Keyboard() self.cpu = CPU(self.screen, self.keyboard)
def __init__(self): self.codes = { 0x00E0: self.clear_display, 0x00EE: self.return_subroutine, 0x1: self.jump_subroutine, 0x2: self.call_subroutine, 0x3: self.skip_next_if_equal_address, 0x4: self.skip_next_if_not_equal_address, 0x5: self.skip_next_if_equal_register, 0x6: self.set_register, 0x7: self.add_register, 0x8: self.registers_ops, 0x9: self.skip_if_registers_not_equal, 0xA: self.set_i_to_address_plus, 0xB: self.jump_to_address_plus_i, 0xC: self.set_bitwise_and_random, 0xD: self.draw_sprite, 0xE: self.key_ops, 0xF: self.mem_ops } self.registers_ops = { 0x0: self.assign_registers, 0x1: self.bitwise_or, 0x2: self.bitwise_and, 0x3: self.bitwise_xor, 0x4: self.add_with_carry, 0x5: self.subtract_with_borrow, 0x6: self.shift_right, 0x7: self.reversed_subtraction, 0xE: self.shift_left } self.key_ops = { 0xE: self.skip_if_vx_pressed, 0x1: self.skip_if_not_vx_pressed, } self.mem_ops = { 0x07: self.set_register_to_timer, 0x0A: self.load_key_pressed, 0x15: self.set_delay_timer, 0x18: self.set_sound_timer, 0x1E: self.add_to_memory_index, 0x29: self.set_memory_index_to_sprite, 0x33: self.store_binary_coded_decimal, 0x55: self.dump_registers, 0x65: self.load_registers } self.timers = {'delay': 0, 'sound': 0} self.fonts = (0xF0, 0x90, 0x90, 0x90, 0xF0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xF0, 0x10, 0xF0, 0x80, 0xF0, 0xF0, 0x10, 0xF0, 0x10, 0xF0, 0x90, 0x90, 0xF0, 0x10, 0x10, 0xF0, 0x80, 0xF0, 0x10, 0xF0, 0xF0, 0x80, 0xF0, 0x90, 0xF0, 0xF0, 0x10, 0x20, 0x40, 0x40, 0xF0, 0x90, 0xF0, 0x90, 0xF0, 0xF0, 0x90, 0xF0, 0x10, 0xF0, 0xF0, 0x90, 0xF0, 0x90, 0x90, 0xE0, 0x90, 0xE0, 0x90, 0xE0, 0xF0, 0x80, 0x80, 0x80, 0xF0, 0xE0, 0x90, 0x90, 0x90, 0xE0, 0xF0, 0x80, 0xF0, 0x80, 0xF0, 0xF0, 0x80, 0xF0, 0x80, 0x80) self.stack_pointer = None self.memory = [0] * MEMORY_SIZE self.registers = [0] * REGISTERS_SIZE self.program_counter = 0x200 self.stack = [] self.memory_index = 0 self.vx = 0 self.vy = 0 self.op_code = None for index, font in enumerate(self.fonts): self.memory[index] = self.fonts[index] self.display = Screen() pygame.mixer.music.load(SOUND)