gameboy superoptimizer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
5.9 KiB

  1. import pytest
  2. from tests.insn.helpers import *
  3. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  4. def test_add_a_r(r):
  5. cpu = CPU()
  6. cpu.set_reg8(r, 0x7F)
  7. ADD_A_R(r).exec(cpu)
  8. assert cpu.get_reg8(R8.A) == 0x7F
  9. assert cpu.state.carry == 0
  10. cpu.set_reg8(r, 0x81)
  11. ADD_A_R(r).exec(cpu)
  12. assert cpu.get_reg8(R8.A) == 0x00
  13. assert cpu.state.carry == 1
  14. @pytest.mark.parametrize("n", n8())
  15. def test_add_a_n(n):
  16. cpu = CPU()
  17. cpu.set_reg8(R8.A, 0x7F)
  18. ADD_A_N(n).exec(cpu)
  19. assert cpu.get_reg8(R8.A) == (0x7F + n) & 0xFF
  20. assert cpu.state.carry == (1 if n >= 0x81 else 0)
  21. def test_add_a_hl():
  22. cpu = CPU()
  23. cpu.set_reg16(R16.HL, 0x1234)
  24. cpu.set_mem8(0x1234, 0x7F)
  25. ADD_A_HL().exec(cpu)
  26. assert cpu.get_reg8(R8.A) == 0x7F
  27. assert cpu.state.carry == 0
  28. cpu.set_mem8(0x1234, 0x81)
  29. ADD_A_HL().exec(cpu)
  30. assert cpu.get_reg8(R8.A) == 0
  31. assert cpu.state.carry == 1
  32. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  33. def test_adc_a_r(r):
  34. cpu = CPU()
  35. cpu.set_reg8(r, 0x7F)
  36. ADC_A_R(r).exec(cpu)
  37. assert cpu.get_reg8(R8.A) == 0x7F
  38. assert cpu.state.carry == 0
  39. cpu.set_reg8(r, 0x81)
  40. ADC_A_R(r).exec(cpu)
  41. assert cpu.get_reg8(R8.A) == 0x00
  42. assert cpu.state.carry == 1
  43. cpu.set_reg8(r, 0x00)
  44. ADC_A_R(r).exec(cpu)
  45. assert cpu.get_reg8(R8.A) == 0x01
  46. assert cpu.state.carry == 0
  47. @pytest.mark.parametrize("n", n8())
  48. def test_adc_a_n(n):
  49. cpu = CPU()
  50. cpu.set_reg8(R8.A, 0x7F)
  51. ADC_A_N(n).exec(cpu)
  52. assert cpu.get_reg8(R8.A) == (0x7F + n) & 0xFF
  53. assert cpu.state.carry == (1 if n >= 0x81 else 0)
  54. def test_adc_a_hl():
  55. cpu = CPU()
  56. cpu.set_reg16(R16.HL, 0x1234)
  57. cpu.hl = 0x7F
  58. ADC_A_HL().exec(cpu)
  59. assert cpu.get_reg8(R8.A) == 0x7F
  60. assert cpu.state.carry == 0
  61. cpu.hl = 0x81
  62. ADC_A_HL().exec(cpu)
  63. assert cpu.get_reg8(R8.A) == 0
  64. assert cpu.state.carry == 1
  65. cpu.hl = 0x00
  66. ADC_A_HL().exec(cpu)
  67. assert cpu.get_reg8(R8.A) == 1
  68. assert cpu.state.carry == 0
  69. # TODO: SUB_A_R, SUB_A_N, SUB_A_HL, SBC_A_R, SBC_A_N, SBC_A_HL
  70. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  71. def test_and_a_r(r):
  72. cpu = CPU()
  73. cpu.set_reg8(R8.A, 0x12)
  74. cpu.set_reg8(r, 0x7F)
  75. AND_A_R(r).exec(cpu)
  76. assert cpu.get_reg8(R8.A) == 0x12
  77. assert cpu.state.carry == 0
  78. @pytest.mark.parametrize("n", n8())
  79. def test_and_a_n(n):
  80. cpu = CPU()
  81. cpu.set_reg8(R8.A, 0x12)
  82. AND_A_N(n).exec(cpu)
  83. assert cpu.get_reg8(R8.A) == 0x12 & n
  84. assert cpu.state.carry == 0
  85. def test_and_a_hl():
  86. cpu = CPU()
  87. cpu.set_reg8(R8.A, 0x12)
  88. cpu.set_reg16(R16.HL, 0x1234)
  89. cpu.set_mem8(0x1234, 0x7F)
  90. AND_A_HL().exec(cpu)
  91. assert cpu.get_reg8(R8.A) == 0x12
  92. assert cpu.state.carry == 0
  93. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  94. def test_xor_a_r(r):
  95. cpu = CPU()
  96. cpu.set_reg8(R8.A, 0x12)
  97. cpu.set_reg8(r, 0x7F)
  98. XOR_A_R(r).exec(cpu)
  99. assert cpu.get_reg8(R8.A) == 0x6D
  100. assert cpu.state.carry == 0
  101. @pytest.mark.parametrize("n", n8())
  102. def test_xor_a_n(n):
  103. cpu = CPU()
  104. cpu.set_reg8(R8.A, 0x12)
  105. XOR_A_N(n).exec(cpu)
  106. assert cpu.get_reg8(R8.A) == 0x12 ^ n
  107. assert cpu.state.carry == 0
  108. def test_xor_a_hl():
  109. cpu = CPU()
  110. cpu.set_reg8(R8.A, 0x12)
  111. cpu.set_reg16(R16.HL, 0x1234)
  112. cpu.set_mem8(0x1234, 0x7F)
  113. XOR_A_HL().exec(cpu)
  114. assert cpu.get_reg8(R8.A) == 0x6D
  115. assert cpu.state.carry == 0
  116. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  117. def test_or_a_r(r):
  118. cpu = CPU()
  119. cpu.set_reg8(R8.A, 0x12)
  120. cpu.set_reg8(r, 0x7F)
  121. OR_A_R(r).exec(cpu)
  122. assert cpu.get_reg8(R8.A) == 0x7F
  123. assert cpu.state.carry == 0
  124. @pytest.mark.parametrize("n", n8())
  125. def test_or_a_n(n):
  126. cpu = CPU()
  127. cpu.set_reg8(R8.A, 0x12)
  128. OR_A_N(n).exec(cpu)
  129. assert cpu.get_reg8(R8.A) == 0x12 | n
  130. assert cpu.state.carry == 0
  131. def test_or_a_hl():
  132. cpu = CPU()
  133. cpu.set_reg8(R8.A, 0x12)
  134. cpu.set_reg16(R16.HL, 0x1234)
  135. cpu.set_mem8(0x1234, 0x7F)
  136. OR_A_HL().exec(cpu)
  137. assert cpu.get_reg8(R8.A) == 0x7F
  138. assert cpu.state.carry == 0
  139. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  140. def test_cp_a_r(r):
  141. cpu = CPU()
  142. cpu.set_reg8(R8.A, 0x7F)
  143. cpu.set_reg8(r, 0x12)
  144. CP_A_R(r).exec(cpu)
  145. assert cpu.get_reg8(R8.A) == 0x7F
  146. assert cpu.state.carry == 0
  147. cpu.set_reg8(r, 0x80)
  148. CP_A_R(r).exec(cpu)
  149. assert cpu.get_reg8(R8.A) == 0x7F
  150. assert cpu.state.carry == 1
  151. @pytest.mark.parametrize("n", n8())
  152. def test_cp_a_n(n):
  153. cpu = CPU()
  154. cpu.set_reg8(R8.A, 0x12)
  155. CP_A_N(n).exec(cpu)
  156. assert cpu.get_reg8(R8.A) == 0x12
  157. assert cpu.state.carry == (1 if 0x12 < n else 0)
  158. def test_cp_a_hl():
  159. cpu = CPU()
  160. cpu.set_reg8(R8.A, 0x7F)
  161. cpu.set_reg16(R16.HL, 0x1234)
  162. cpu.set_mem8(0x1234, 0x12)
  163. CP_A_HL().exec(cpu)
  164. assert cpu.get_reg8(R8.A) == 0x7F
  165. assert cpu.state.carry == 0
  166. cpu.set_mem8(0x1234, 0x80)
  167. CP_A_HL().exec(cpu)
  168. assert cpu.get_reg8(R8.A) == 0x7F
  169. assert cpu.state.carry == 1
  170. @pytest.mark.parametrize("r,n", [(r, n) for r in R8 for n in n8()])
  171. def test_inc_r(r, n):
  172. cpu = CPU()
  173. cpu.set_reg8(r, n)
  174. INC_R(r).exec(cpu)
  175. assert cpu.get_reg8(r) == (n + 1) & 0xFF
  176. @pytest.mark.parametrize("n", n8())
  177. def test_inc_hl(n):
  178. cpu = CPU()
  179. cpu.set_reg16(R16.HL, 0x1234)
  180. cpu.set_mem8(0x1234, n)
  181. INC_HL(n).exec(cpu)
  182. assert cpu.hl == (n + 1) & 0xFF
  183. @pytest.mark.parametrize("r,n", [(r, n) for r in R8 for n in n8()])
  184. def test_dec_r(r, n):
  185. cpu = CPU()
  186. cpu.set_reg8(r, n)
  187. DEC_R(r).exec(cpu)
  188. assert cpu.get_reg8(r) == (n - 1) & 0xFF
  189. @pytest.mark.parametrize("n", n8())
  190. def test_dec_hl(n):
  191. cpu = CPU()
  192. cpu.set_reg16(R16.HL, 0x1234)
  193. cpu.set_mem8(0x1234, n)
  194. DEC_HL(n).exec(cpu)
  195. assert cpu.hl == (n - 1) & 0xFF
  196. # TODO: Test DAA after implementing it
  197. @pytest.mark.parametrize("n", n8())
  198. def test_cpl(n):
  199. cpu = CPU()
  200. cpu.set_reg8(R8.A, n)
  201. CPL().exec(cpu)
  202. assert cpu.get_reg8(R8.A) == n ^ 0xFF