import pytest from tests.cpu.insn.helpers import * @pytest.mark.parametrize("r", set(R8) - {R8.A}) def test_add_a_r(r): cpu = CPU() cpu.set_reg8(r, 0x7F) ADD_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 cpu.set_reg8(r, 0x81) ADD_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x00 assert cpu.state.carry == 1 @pytest.mark.parametrize("n", n8()) def test_add_a_n(n): cpu = CPU() cpu.set_reg8(R8.A, 0x7F) ADD_A_N(n).exec(cpu) assert cpu.get_reg8(R8.A) == (0x7F + n) & 0xFF assert cpu.state.carry == (1 if n >= 0x81 else 0) def test_add_a_hl(): cpu = CPU() cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, 0x7F) ADD_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 cpu.set_mem8(0x1234, 0x81) ADD_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0 assert cpu.state.carry == 1 @pytest.mark.parametrize("r", set(R8) - {R8.A}) def test_adc_a_r(r): cpu = CPU() cpu.set_reg8(r, 0x7F) ADC_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 cpu.set_reg8(r, 0x81) ADC_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x00 assert cpu.state.carry == 1 cpu.set_reg8(r, 0x00) ADC_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x01 assert cpu.state.carry == 0 @pytest.mark.parametrize("n", n8()) def test_adc_a_n(n): cpu = CPU() cpu.set_reg8(R8.A, 0x7F) ADC_A_N(n).exec(cpu) assert cpu.get_reg8(R8.A) == (0x7F + n) & 0xFF assert cpu.state.carry == (1 if n >= 0x81 else 0) def test_adc_a_hl(): cpu = CPU() cpu.set_reg16(R16.HL, 0x1234) cpu.hl = 0x7F ADC_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 cpu.hl = 0x81 ADC_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0 assert cpu.state.carry == 1 cpu.hl = 0x00 ADC_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 1 assert cpu.state.carry == 0 # TODO: SUB_A_R, SUB_A_N, SUB_A_HL, SBC_A_R, SBC_A_N, SBC_A_HL @pytest.mark.parametrize("r", set(R8) - {R8.A}) def test_and_a_r(r): cpu = CPU() cpu.set_reg8(R8.A, 0x12) cpu.set_reg8(r, 0x7F) AND_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x12 assert cpu.state.carry == 0 @pytest.mark.parametrize("n", n8()) def test_and_a_n(n): cpu = CPU() cpu.set_reg8(R8.A, 0x12) AND_A_N(n).exec(cpu) assert cpu.get_reg8(R8.A) == 0x12 & n assert cpu.state.carry == 0 def test_and_a_hl(): cpu = CPU() cpu.set_reg8(R8.A, 0x12) cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, 0x7F) AND_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x12 assert cpu.state.carry == 0 @pytest.mark.parametrize("r", set(R8) - {R8.A}) def test_xor_a_r(r): cpu = CPU() cpu.set_reg8(R8.A, 0x12) cpu.set_reg8(r, 0x7F) XOR_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x6D assert cpu.state.carry == 0 @pytest.mark.parametrize("n", n8()) def test_xor_a_n(n): cpu = CPU() cpu.set_reg8(R8.A, 0x12) XOR_A_N(n).exec(cpu) assert cpu.get_reg8(R8.A) == 0x12 ^ n assert cpu.state.carry == 0 def test_xor_a_hl(): cpu = CPU() cpu.set_reg8(R8.A, 0x12) cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, 0x7F) XOR_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x6D assert cpu.state.carry == 0 @pytest.mark.parametrize("r", set(R8) - {R8.A}) def test_or_a_r(r): cpu = CPU() cpu.set_reg8(R8.A, 0x12) cpu.set_reg8(r, 0x7F) OR_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 @pytest.mark.parametrize("n", n8()) def test_or_a_n(n): cpu = CPU() cpu.set_reg8(R8.A, 0x12) OR_A_N(n).exec(cpu) assert cpu.get_reg8(R8.A) == 0x12 | n assert cpu.state.carry == 0 def test_or_a_hl(): cpu = CPU() cpu.set_reg8(R8.A, 0x12) cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, 0x7F) OR_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 @pytest.mark.parametrize("r", set(R8) - {R8.A}) def test_cp_a_r(r): cpu = CPU() cpu.set_reg8(R8.A, 0x7F) cpu.set_reg8(r, 0x12) CP_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 cpu.set_reg8(r, 0x80) CP_A_R(r).exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 1 @pytest.mark.parametrize("n", n8()) def test_cp_a_n(n): cpu = CPU() cpu.set_reg8(R8.A, 0x12) CP_A_N(n).exec(cpu) assert cpu.get_reg8(R8.A) == 0x12 assert cpu.state.carry == (1 if 0x12 < n else 0) def test_cp_a_hl(): cpu = CPU() cpu.set_reg8(R8.A, 0x7F) cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, 0x12) CP_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 0 cpu.set_mem8(0x1234, 0x80) CP_A_HL().exec(cpu) assert cpu.get_reg8(R8.A) == 0x7F assert cpu.state.carry == 1 @pytest.mark.parametrize("r,n", [(r, n) for r in R8 for n in n8()]) def test_inc_r(r, n): cpu = CPU() cpu.set_reg8(r, n) INC_R(r).exec(cpu) assert cpu.get_reg8(r) == (n + 1) & 0xFF @pytest.mark.parametrize("n", n8()) def test_inc_hl(n): cpu = CPU() cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, n) INC_HL().exec(cpu) assert cpu.hl == (n + 1) & 0xFF @pytest.mark.parametrize("r,n", [(r, n) for r in R8 for n in n8()]) def test_dec_r(r, n): cpu = CPU() cpu.set_reg8(r, n) DEC_R(r).exec(cpu) assert cpu.get_reg8(r) == (n - 1) & 0xFF @pytest.mark.parametrize("n", n8()) def test_dec_hl(n): cpu = CPU() cpu.set_reg16(R16.HL, 0x1234) cpu.set_mem8(0x1234, n) DEC_HL().exec(cpu) assert cpu.hl == (n - 1) & 0xFF # TODO: Test DAA after implementing it @pytest.mark.parametrize("n", n8()) def test_cpl(n): cpu = CPU() cpu.set_reg8(R8.A, n) CPL().exec(cpu) assert cpu.get_reg8(R8.A) == n ^ 0xFF