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.

288 lines
5.4 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. # TODO: ADC_A_R, ADC_A_N, ADC_A_HL, SUB_A_R, SUB_A_N, SUB_A_HL, SBC_A_R,
  38. # SBC_A_N, SBC_A_HL
  39. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  40. def test_and_a_r(r):
  41. cpu = CPU()
  42. cpu.set_reg8(R8.A, 0x12)
  43. cpu.set_reg8(r, 0x7F)
  44. AND_A_R(r).exec(cpu)
  45. assert cpu.get_reg8(R8.A) == 0x12
  46. assert cpu.carry == 0
  47. assert cpu.cycles == 4
  48. @pytest.mark.parametrize("n", n8())
  49. def test_and_a_n(n):
  50. cpu = CPU()
  51. cpu.set_reg8(R8.A, 0x12)
  52. AND_A_N(n).exec(cpu)
  53. assert cpu.get_reg8(R8.A) == 0x12 & n
  54. assert cpu.carry == 0
  55. assert cpu.cycles == 8
  56. def test_and_a_hl():
  57. cpu = CPU()
  58. cpu.set_reg8(R8.A, 0x12)
  59. cpu.set_reg16(R16.HL, 0x1234)
  60. cpu.set_mem8(0x1234, 0x7F)
  61. AND_A_HL().exec(cpu)
  62. assert cpu.get_reg8(R8.A) == 0x12
  63. assert cpu.carry == 0
  64. assert cpu.cycles == 8
  65. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  66. def test_xor_a_r(r):
  67. cpu = CPU()
  68. cpu.set_reg8(R8.A, 0x12)
  69. cpu.set_reg8(r, 0x7F)
  70. XOR_A_R(r).exec(cpu)
  71. assert cpu.get_reg8(R8.A) == 0x6D
  72. assert cpu.carry == 0
  73. assert cpu.cycles == 4
  74. @pytest.mark.parametrize("n", n8())
  75. def test_xor_a_n(n):
  76. cpu = CPU()
  77. cpu.set_reg8(R8.A, 0x12)
  78. XOR_A_N(n).exec(cpu)
  79. assert cpu.get_reg8(R8.A) == 0x12 ^ n
  80. assert cpu.carry == 0
  81. assert cpu.cycles == 8
  82. def test_xor_a_hl():
  83. cpu = CPU()
  84. cpu.set_reg8(R8.A, 0x12)
  85. cpu.set_reg16(R16.HL, 0x1234)
  86. cpu.set_mem8(0x1234, 0x7F)
  87. XOR_A_HL().exec(cpu)
  88. assert cpu.get_reg8(R8.A) == 0x6D
  89. assert cpu.carry == 0
  90. assert cpu.cycles == 8
  91. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  92. def test_or_a_r(r):
  93. cpu = CPU()
  94. cpu.set_reg8(R8.A, 0x12)
  95. cpu.set_reg8(r, 0x7F)
  96. OR_A_R(r).exec(cpu)
  97. assert cpu.get_reg8(R8.A) == 0x7F
  98. assert cpu.carry == 0
  99. assert cpu.cycles == 4
  100. @pytest.mark.parametrize("n", n8())
  101. def test_or_a_n(n):
  102. cpu = CPU()
  103. cpu.set_reg8(R8.A, 0x12)
  104. OR_A_N(n).exec(cpu)
  105. assert cpu.get_reg8(R8.A) == 0x12 | n
  106. assert cpu.carry == 0
  107. assert cpu.cycles == 8
  108. def test_or_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. OR_A_HL().exec(cpu)
  114. assert cpu.get_reg8(R8.A) == 0x7F
  115. assert cpu.carry == 0
  116. assert cpu.cycles == 8
  117. @pytest.mark.parametrize("r", set(R8) - {R8.A})
  118. def test_cp_a_r(r):
  119. cpu = CPU()
  120. cpu.set_reg8(R8.A, 0x7F)
  121. cpu.set_reg8(r, 0x12)
  122. CP_A_R(r).exec(cpu)
  123. assert cpu.get_reg8(R8.A) == 0x7F
  124. assert cpu.carry == 0
  125. assert cpu.cycles == 4
  126. cpu.set_reg8(r, 0x80)
  127. CP_A_R(r).exec(cpu)
  128. assert cpu.get_reg8(R8.A) == 0x7F
  129. assert cpu.carry == 1
  130. assert cpu.cycles == 8
  131. @pytest.mark.parametrize("n", n8())
  132. def test_cp_a_n(n):
  133. cpu = CPU()
  134. cpu.set_reg8(R8.A, 0x12)
  135. CP_A_N(n).exec(cpu)
  136. assert cpu.get_reg8(R8.A) == 0x12
  137. assert cpu.carry == (1 if 0x12 < n else 0)
  138. assert cpu.cycles == 8
  139. def test_cp_a_hl():
  140. cpu = CPU()
  141. cpu.set_reg8(R8.A, 0x7F)
  142. cpu.set_reg16(R16.HL, 0x1234)
  143. cpu.set_mem8(0x1234, 0x12)
  144. CP_A_HL().exec(cpu)
  145. assert cpu.get_reg8(R8.A) == 0x7F
  146. assert cpu.carry == 0
  147. assert cpu.cycles == 8
  148. cpu.set_mem8(0x1234, 0x80)
  149. CP_A_HL().exec(cpu)
  150. assert cpu.get_reg8(R8.A) == 0x7F
  151. assert cpu.carry == 1
  152. assert cpu.cycles == 16
  153. @pytest.mark.parametrize("r,n", [(r, n) for r in R8 for n in n8()])
  154. def test_inc_r(r, n):
  155. cpu = CPU()
  156. cpu.set_reg8(r, n)
  157. INC_R(r).exec(cpu)
  158. assert cpu.get_reg8(r) == (n + 1) & 0xFF
  159. assert cpu.cycles == 4
  160. @pytest.mark.parametrize("n", n8())
  161. def test_inc_hl(n):
  162. cpu = CPU()
  163. cpu.set_reg16(R16.HL, 0x1234)
  164. cpu.set_mem8(0x1234, n)
  165. INC_HL(n).exec(cpu)
  166. assert cpu.deref_hl() == (n + 1) & 0xFF
  167. assert cpu.cycles == 12
  168. @pytest.mark.parametrize("r,n", [(r, n) for r in R8 for n in n8()])
  169. def test_dec_r(r, n):
  170. cpu = CPU()
  171. cpu.set_reg8(r, n)
  172. DEC_R(r).exec(cpu)
  173. assert cpu.get_reg8(r) == (n - 1) & 0xFF
  174. assert cpu.cycles == 4
  175. @pytest.mark.parametrize("n", n8())
  176. def test_dec_hl(n):
  177. cpu = CPU()
  178. cpu.set_reg16(R16.HL, 0x1234)
  179. cpu.set_mem8(0x1234, n)
  180. DEC_HL(n).exec(cpu)
  181. assert cpu.deref_hl() == (n - 1) & 0xFF
  182. assert cpu.cycles == 12
  183. # TODO: Test DAA after implementing it
  184. @pytest.mark.parametrize("n", n8())
  185. def test_cpl(n):
  186. cpu = CPU()
  187. cpu.set_reg8(R8.A, n)
  188. CPL().exec(cpu)
  189. assert cpu.get_reg8(R8.A) == n ^ 0xFF
  190. assert cpu.cycles == 4