def test_set_sound_timer(self): machine = Machine() machine.VRegisters[0xC] = 150 set_sound = SetSoundTimer() set_sound.arg_registers.append(0xC) set_sound.execute(machine) self.assertEqual(machine.SoundTimer.get_count(), 150)
def execute(self, machine: Machine): machine.Block = True if not machine.Keyboard.is_any_key_pressed(): return machine.VRegisters[self.vx()] = machine.Keyboard.get_first_pressed() machine.Block = False
def test_set_delay_timer(self): machine = Machine() machine.VRegisters[0xC] = 150 set_delay = SetDelayTimer() set_delay.arg_registers.append(0xC) set_delay.execute(machine) self.assertEqual(machine.DelayTimer.get_count(), 150)
def test_load_char_should_set_right_address(self): machine = Machine() load = LoadChar() load.arg_registers.append(0x5) for digit in range(16): machine.VRegisters[5] = digit load.execute(machine) self.assertEqual(machine.AddressRegister, machine.FontDict[digit])
def test_add_constant_to_register_should_change_value_right(self): machine = Machine() machine.VRegisters[0xA] = 0xFA add = AddConstantToRegister() add.arg_registers.append(0xA) add.arg_constant = 0x3 add.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xFA + 3)
def test_skip_if_key_pressed_should_not_skip_if_key_flag_is_equal_to_0(self): machine = Machine() machine.Keyboard.key_down(7) machine.VRegisters[5] = 4 skip = SkipIfKeyPressed() skip.arg_registers.append(5) skip.execute(machine) self.assertEqual(machine.PC, 2)
def test_skip_if_not_pressed_should_skip_if_key_not_pressed(self): machine = Machine() machine.Keyboard.key_down(8) machine.VRegisters[8] = 3 skip = SkipIfKeyNotPressed() skip.arg_registers.append(8) skip.execute(machine) self.assertEqual(machine.PC, 4)
def test_jmi_should_change_PC_right(self): machine = Machine() machine.VRegisters[0] = 0xAB jmi = Jmi() jmi.arg_constant = 0x111 jmi.execute(machine) self.assertEqual(machine.PC, 0xAB + 0x111) self.assertTrue(isinstance(jmi, JumpInstruction))
def test_mov_constant_to_register_should_change_target_register(self): machine = Machine() machine.VRegisters[0xA] = 15 mov = MovConstantToRegister() mov.arg_registers.append(0xA) mov.arg_constant = 0xAC mov.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xAC)
def test_skne_should_skip_if_not_equals(self): machine = Machine() machine.PC = 10 machine.VRegisters[2] = 0xBC skne = Skne() skne.arg_constant = 0xEE skne.arg_registers.append(2) skne.execute(machine) self.assertTrue(isinstance(skne, JumpInstruction)) self.assertEqual(machine.PC, 14)
def test_bcd_should_get_right_digits(self): machine = Machine() machine.VRegisters[7] = 107 bcd = Bcd() bcd.arg_registers.append(7) bcd.execute(machine) self.assertEqual(machine.Memory[machine.AddressRegister], machine.FontDict[1]) self.assertEqual(machine.Memory[machine.AddressRegister + 1], machine.FontDict[0]) self.assertEqual(machine.Memory[machine.AddressRegister + 2], machine.FontDict[7])
def test_jsr_should_call_subroutine(self): machine = Machine() machine.PC = 10 jsr = Jsr() jsr.arg_constant = 0xEEF jsr.execute(machine) self.assertTrue(isinstance(jsr, JumpInstruction)) self.assertEqual(machine.PC, 0xEEF) self.assertEqual(machine.Stack.top(), 12)
def test_add_constant_to_register_should_change_value_right_with_overflow(self): machine = Machine() machine.VRegisters[0xA] = 0xFA add = AddConstantToRegister() add.arg_registers.append(0xA) add.arg_constant = 0xAC add.execute(machine) self.assertEqual(machine.VRegisters[0xA], (0xFA + 0xAC) % 0x100) self.assertEqual(machine.VRegisters[0xF], 0)
def test_xor(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xEE xor = Xor() xor.arg_registers.append(0xA) xor.arg_registers.append(0xB) xor.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xEE ^ 0xFA) self.assertEqual(machine.VRegisters[0xB], 0xEE)
def test_and(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xEE andi = And() andi.arg_registers.append(0xA) andi.arg_registers.append(0xB) andi.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xEE & 0xFA) self.assertEqual(machine.VRegisters[0xB], 0xEE)
def test_or(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xEE ori = Or() ori.arg_registers.append(0xA) ori.arg_registers.append(0xB) ori.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xEE | 0xFA) self.assertEqual(machine.VRegisters[0xB], 0xEE)
def test_mov_register_to_register(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xEE mov = MovRegisterToRegister() mov.arg_registers.append(0xA) mov.arg_registers.append(0xB) mov.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xEE) self.assertEqual(machine.VRegisters[0xB], 0xEE)
def test_skeq_registers_should_not_skip_if_not_equals(self): machine = Machine() machine.PC = 10 machine.VRegisters[2] = 0xEE machine.VRegisters[5] = 0xE4 skeq = SkeqRegister() skeq.arg_registers.append(2) skeq.arg_registers.append(5) skeq.execute(machine) self.assertTrue(isinstance(skeq, JumpInstruction)) self.assertEqual(machine.PC, 12)
def test_subn_should_set_vf_if_vy_greater_vx(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xFF subn = Subn() subn.arg_registers.append(0xA) subn.arg_registers.append(0xB) subn.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xFF - 0xFA) self.assertEqual(machine.VRegisters[0xB], 0xFF) self.assertEqual(machine.VRegisters[0xF], 1)
def test_load_registers_should_load_registers_and_increase_address_register(self): machine = Machine() for i in range(6): machine.Memory[123 + i] = i machine.AddressRegister = 123 load = LoadRegisters() load.arg_registers.append(5) load.execute(machine) for i in range(6): self.assertEqual(machine.VRegisters[i], machine.Memory[123 + i]) self.assertEqual(machine.AddressRegister, 123 + 5 + 1)
def test_add_register_to_register_shouldnt_set_vf_if_carry_if_sum_less_256(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0x1 add = AddRegisterToRegister() add.arg_registers.append(0xA) add.arg_registers.append(0xB) add.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0x1 + 0xFA) self.assertEqual(machine.VRegisters[0xB], 0x1) self.assertEqual(machine.VRegisters[0xF], 0)
def test_add_register_to_register_should_set_vf_if_carry_if_sum_greater_256(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xEE add = AddRegisterToRegister() add.arg_registers.append(0xA) add.arg_registers.append(0xB) add.execute(machine) self.assertEqual(machine.VRegisters[0xA], (0xEE + 0xFA) % 0x100) self.assertEqual(machine.VRegisters[0xB], 0xEE) self.assertEqual(machine.VRegisters[0xF], 1)
def test_store_registers_should_store_and_increase_address_register(self): machine = Machine() for i in range(6): machine.VRegisters[i] = i machine.AddressRegister = 123 store = StoreRegisters() store.arg_registers.append(5) store.execute(machine) for i in range(6): self.assertEqual(machine.Memory[123 + i], machine.VRegisters[i]) self.assertEqual(machine.AddressRegister, 123 + 5 + 1)
def test_sub_register_to_register_should_set_vf_if_sub_are_positive(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0x1 sub = SubRegisterToRegister() sub.arg_registers.append(0xA) sub.arg_registers.append(0xB) sub.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xFA - 0x1) self.assertEqual(machine.VRegisters[0xB], 0x1) self.assertEqual(machine.VRegisters[0xF], 1)
def test_add_register_to_register_shouldnt_set_vf_if_sub_are_negative(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xFF sub = SubRegisterToRegister() sub.arg_registers.append(0xA) sub.arg_registers.append(0xB) sub.execute(machine) self.assertEqual(machine.VRegisters[0xA], (0xFA - 0xFF) % 0x100) self.assertEqual(machine.VRegisters[0xB], 0xFF) self.assertEqual(machine.VRegisters[0xF], 0)
def test_shr_should_write_least_significant_bit_at_vf(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xFF shr = Shr() shr.arg_registers.append(0xA) shr.arg_registers.append(0xB) shr.execute(machine) self.assertEqual(machine.VRegisters[0xA], 0xFA >> 1) self.assertEqual(machine.VRegisters[0xB], 0xFF) self.assertEqual(machine.VRegisters[0xF], 0)
def test_shr_should_write_most_significant_bit_at_vf(self): machine = Machine() machine.VRegisters[0xA] = 0xFA machine.VRegisters[0xB] = 0xFF shl = Shl() shl.arg_registers.append(0xA) shl.arg_registers.append(0xB) shl.execute(machine) self.assertEqual(machine.VRegisters[0xA], (0xFA << 1) % 0x100) self.assertEqual(machine.VRegisters[0xB], 0xFF) self.assertEqual(machine.VRegisters[0xF], 1)
def test_skne_registers_shouldnt_skip_if_equal(self): machine = Machine() machine.PC = 10 machine.VRegisters[1] = 5 machine.VRegisters[2] = 5 skne = SkneRegisters() skne.arg_registers.append(1) skne.arg_registers.append(2) skne.execute(machine) self.assertTrue(isinstance(skne, JumpInstruction)) self.assertEqual(machine.PC, 12)
def test_subn_should_set_vf_to_0_if_vy_less_vx(self): machine = Machine() machine.VRegisters[0xA] = 0xFF machine.VRegisters[0xB] = 0xFA subn = Subn() subn.arg_registers.append(0xA) subn.arg_registers.append(0xB) subn.execute(machine) self.assertEqual(machine.VRegisters[0xA], (0xFA - 0xFF) % 0x100) self.assertEqual(machine.VRegisters[0xB], 0xFA) self.assertEqual(machine.VRegisters[0xF], 0)
def run(self): machine = Machine(compatibility_load_store=self._compatibility) machine.load_program(self._filename) app = QApplication(sys.argv) if not self._debug: chip8 = Chip8Widget(machine, self._sound, self._instructions_per_second) else: chip8 = Chip8DebugWidget(machine, self._sound) chip8.show() return app.exec_()