Exemple #1
0
    def test_display_sprite(self):
        chip = Chip8()
        program = BytesIO(b'\xD2\x33\xD2\x31')
        chip.registers.i = 80
        chip.memory.set(80, b'\x3C\xC3\xFF')
        chip.load_game(program)
        chip.emulate_cycle()

        gfx_line1 = chip.graphics.get_gfx_state(2, 3, 8)
        gfx_line2 = chip.graphics.get_gfx_state(2, 4, 8)
        gfx_line3 = chip.graphics.get_gfx_state(2, 5, 8)

        assert gfx_line1 == [0, 0, 1, 1, 1, 1, 0, 0]
        assert gfx_line2 == [1, 1, 0, 0, 0, 0, 1, 1]
        assert gfx_line3 == [1, 1, 1, 1, 1, 1, 1, 1]
        assert chip.registers.v[15] == 0

        chip.memory.set_byte(84, 0x24)
        chip.registers.i = 84
        chip.emulate_cycle()

        gfx_line1 = chip.graphics.get_gfx_state(2, 3, 8)

        assert gfx_line1 == [0, 0, 0, 1, 1, 0, 0, 0]
        assert chip.registers.v[15] == 1
Exemple #2
0
    def test_set_program_counter(self):
        chip = Chip8()
        program = BytesIO(b'\x15\x42')
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.program_counter.value == 0x0542
Exemple #3
0
    def test_set_v_register(self):
        chip = Chip8()
        program = BytesIO(b'\x67\x42')
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x0042
Exemple #4
0
    def test_set_index_register(self):
        chip = Chip8()
        program = BytesIO(b'\xA0\x01')
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.registers.i == 1
Exemple #5
0
 def __init__(self, file):
     self.height = 500
     self.width = 1000
     self.blockSize = self.height / 32 #15
     self.window = pygame.display.set_mode((self.width, self.height))
     self.clock = pygame.time.Clock()
     self.chip8 = Chip8()
     self.file = file
Exemple #6
0
    def __init__(self, rom_file):
        self.chip = Chip8(rom_file)
        self.paused = False
        self.manual_step_mode = False
        self.should_step = False

        pyxel.init(64, 64, fps=100, scale=10)
        pyxel.run(self.update, self.draw)
Exemple #7
0
    def test_jump_w_offset(self):
        chip = Chip8()
        program = BytesIO(b'\xB1\x11')
        chip.registers.v[0] = 1
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.program_counter.value == 0x112
Exemple #8
0
    def test_set_random(self):
        chip = Chip8()
        program = BytesIO(b'\xC1\x0D')
        random.seed(2)
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.registers.v[1] == 0xC
Exemple #9
0
    def test_return(self):
        chip = Chip8()
        stack_pc = 100
        chip.stack.append(stack_pc)
        program = BytesIO(b'\x00\xEE')
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.program_counter.value == stack_pc + 2
Exemple #10
0
    def test_bitwise_and(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x42')
        chip.load_game(program)

        chip.registers.v[4] = 0x0F
        chip.registers.v[7] = 0x13
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x03
Exemple #11
0
    def test_call_subroutine(self):
        chip = Chip8()
        program = BytesIO(b'\x25\x42')
        chip.load_game(program)

        orig_pc = chip.program_counter.value
        chip.emulate_cycle()

        assert chip.program_counter.value == 0x0542
        assert chip.stack.pop() == orig_pc
Exemple #12
0
    def test_move_to_char_address(self):
        chip = Chip8()
        program = BytesIO(b'\xF4\x29')
        chip.registers.v[4] = 0xA
        orig_pc = chip.program_counter.value
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.registers.i == 50
        assert chip.program_counter.value == orig_pc + 2
Exemple #13
0
    def test_xor(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x43')
        chip.load_game(program)

        chip.registers.v[4] = 0x03
        chip.registers.v[7] = 0x11
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x12
Exemple #14
0
    def test_add_to_v_register(self):
        chip = Chip8()
        program = BytesIO(b'\x78\x05\x78\xFD')
        chip.load_game(program)

        chip.emulate_cycle()
        assert chip.registers.v[8] == 0x0005

        chip.emulate_cycle()
        assert chip.registers.v[8] == 0x0002
Exemple #15
0
    def test_nonequal_condition(self):
        chip = Chip8()
        program = BytesIO(b'\x41\x01')
        chip.load_game(program)

        chip.registers.v[1] = 0
        orig_pc = chip.program_counter.value
        chip.emulate_cycle()

        assert chip.program_counter.value == orig_pc + 4
Exemple #16
0
    def test_clear_screen(self):
        chip = Chip8()
        test_pixel = chip.graphics.pixel_at(5, 6)
        test_pixel.set()

        program = BytesIO(b'\x00\xE0')
        chip.load_game(program)
        chip.emulate_cycle()

        assert (not test_pixel.is_on)
Exemple #17
0
    def test_assignment(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x40')
        chip.load_game(program)

        chip.registers.v[4] = 1
        chip.registers.v[7] = 0
        chip.emulate_cycle()

        assert chip.registers.v[7] == 1
Exemple #18
0
    def test_set_sound_timer(self):
        chip = Chip8()
        program = BytesIO(b'\xF2\x18')
        chip.registers.v[2] = 5
        orig_pc = chip.program_counter.value
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.timers.sound_timer == 4
        assert chip.program_counter.value == orig_pc + 2
Exemple #19
0
    def test_get_delay_timer(self):
        chip = Chip8()
        program = BytesIO(b'\xF6\x07')
        chip.timers.delay_timer = 7
        orig_pc = chip.program_counter.value
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.registers.v[6] == 7
        assert chip.program_counter.value == orig_pc + 2
Exemple #20
0
    def test_add_to_address_register(self):
        chip = Chip8()
        program = BytesIO(b'\xF3\x1E')
        chip.registers.v[3] = 16
        chip.registers.i = 250
        orig_pc = chip.program_counter.value
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.registers.i == 10
        assert chip.program_counter.value == orig_pc + 2
Exemple #21
0
    def test_equal_condition(self):
        chip = Chip8()
        program = BytesIO(b'\x90\x10')
        chip.load_game(program)

        chip.registers.v[0] = 1
        chip.registers.v[1] = 1
        orig_pc = chip.program_counter.value
        chip.emulate_cycle()

        assert chip.program_counter.value == orig_pc + 2
Exemple #22
0
    def test_shift_left(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x4E\x87\x4E')
        chip.load_game(program)

        chip.registers.v[7] = 0x81
        chip.emulate_cycle()

        assert chip.registers.v[7] == 2
        assert chip.registers.v[0xF] == 1

        chip.emulate_cycle()

        assert chip.registers.v[7] == 4
        assert chip.registers.v[0xF] == 0
Exemple #23
0
    def __init__(self, file):
        print("Loading from \"{}\"".format(file))
        self.chip8 = Chip8()
        self.rom = self.load_from_file(file)

        self.TITLE = "CHIP8 Emulator"
        self.PIXEL_SIZE = 10
        self.WIDTH = 64
        self.HEIGHT = 32
        self.SIZE = [
            self.WIDTH * self.PIXEL_SIZE, self.HEIGHT * self.PIXEL_SIZE
        ]
        self.MAX_TICKS_PER_SECOND = 60

        pygame.init()
Exemple #24
0
    def test_shift_right(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x46\x87\x46')
        chip.load_game(program)

        chip.registers.v[7] = 2
        chip.emulate_cycle()

        assert chip.registers.v[7] == 1
        assert chip.registers.v[0xF] == 0

        chip.emulate_cycle()

        assert chip.registers.v[7] == 0
        assert chip.registers.v[0xF] == 1
Exemple #25
0
    def test_jump_when_key_not_pressed(self):
        chip = Chip8()
        program = BytesIO(b'\xE2\xA1\xE2\xA1')
        chip.registers.v[2] = 5
        chip.keys.press_key(5)
        orig_pc = chip.program_counter.value
        chip.load_game(program)
        chip.emulate_cycle()

        assert chip.program_counter.value == orig_pc + 2

        chip.keys.release_key(5)
        orig_pc = chip.program_counter.value
        chip.emulate_cycle()

        assert chip.program_counter.value == orig_pc + 4
Exemple #26
0
    def test_addition(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x44\x87\x44')
        chip.load_game(program)

        chip.registers.v[4] = 0x01
        chip.registers.v[7] = 0x10
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x11
        assert chip.registers.v[0xF] == 0

        chip.registers.v[4] = 0xFF
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x10
        assert chip.registers.v[0xF] == 1
Exemple #27
0
    def test_sub_x_y(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x45\x87\x45')
        chip.load_game(program)

        chip.registers.v[4] = 0x01
        chip.registers.v[7] = 0x10
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x0F
        assert chip.registers.v[0xF] == 0

        chip.registers.v[4] = 0x10
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0xFF
        assert chip.registers.v[0xF] == 1
Exemple #28
0
    def test_get_key(self):
        chip = Chip8()
        program = BytesIO(b'\xF3\x0A')
        chip.registers.v[3] = 9
        orig_pc = chip.program_counter.value
        chip.load_game(program)
        chip.emulate_cycle()

        # With no key pressed operation should "block"
        assert chip.program_counter.value == orig_pc
        assert chip.registers.v[3] == 9

        chip.keys.press_key(13)
        chip.emulate_cycle()

        assert chip.registers.v[3] == 13
        assert chip.program_counter.value == orig_pc + 2
Exemple #29
0
    def test_sub_y_x(self):
        chip = Chip8()
        program = BytesIO(b'\x87\x47\x87\x47')
        chip.load_game(program)

        chip.registers.v[4] = 0x01
        chip.registers.v[7] = 0x10
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0xF1
        assert chip.registers.v[0xF] == 1

        chip.registers.v[4] = 0x20
        chip.registers.v[7] = 0x10
        chip.emulate_cycle()

        assert chip.registers.v[7] == 0x10
        assert chip.registers.v[0xF] == 0
Exemple #30
0
def main(chip_program):
    with open(chip_program, "rb") as chip_file:
        grid_rect = graphic_grid(SIZE, MODIFIER)

        # initialize pygame
        pygame.mixer.pre_init(44100, -16, 1)
        pygame.init()
        screen = pygame.display.set_mode((WIDTH * MODIFIER, HEIGHT * MODIFIER))
        pygame.display.set_caption("Chip8 Interpreter")
        pygame.mouse.set_visible(0)
        # background = pygame.Surface(screen.get_size())
        # background = background.convert()
        clock = pygame.time.Clock()
        font = pygame.font.SysFont("monospace", 24)

        # startup chip8
        chip8 = Chip8(chip_file)

        # Change to boot screen
        active_scene = scenes.BootScene(pygame.time.get_ticks(), chip8)
        active_scene = active_scene.next

        # Event loop
        while active_scene != None:
            pressed_keys = pygame.key.get_pressed()
            filtered_events = []
            for event in pygame.event.get():
                quit_attempt = False
                if event.type == pygame.QUIT:
                    quit_attempt = True
                if quit_attempt:
                    active_scene.terminate()
                else:
                    filtered_events.append(event)

            active_scene.process_input(filtered_events, pressed_keys)
            active_scene.update()
            active_scene.render(screen, grid_rect, font, clock)
            active_scene = active_scene.next

            pygame.display.update()
            clock.tick(60)