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.

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