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.1 KiB

  1. import pytest
  2. from tests.insn.helpers import *
  3. def test_rlca():
  4. cpu = CPU()
  5. cpu.set_reg8(R8.A, 0x80)
  6. RLCA().exec(cpu)
  7. assert cpu.get_reg8(R8.A) == 0x01
  8. assert cpu.carry == 1
  9. assert cpu.cycles == 4
  10. RLCA().exec(cpu)
  11. assert cpu.get_reg8(R8.A) == 0x02
  12. assert cpu.carry == 0
  13. assert cpu.cycles == 8
  14. def test_rla():
  15. cpu = CPU()
  16. cpu.set_reg8(R8.A, 0x80)
  17. RLA().exec(cpu)
  18. assert cpu.get_reg8(R8.A) == 0x00
  19. assert cpu.carry == 1
  20. assert cpu.cycles == 4
  21. RLA().exec(cpu)
  22. assert cpu.get_reg8(R8.A) == 0x01
  23. assert cpu.carry == 0
  24. assert cpu.cycles == 8
  25. def test_rrca():
  26. cpu = CPU()
  27. cpu.set_reg8(R8.A, 0x01)
  28. RRCA().exec(cpu)
  29. assert cpu.get_reg8(R8.A) == 0x80
  30. assert cpu.carry == 1
  31. assert cpu.cycles == 4
  32. RRCA().exec(cpu)
  33. assert cpu.get_reg8(R8.A) == 0x40
  34. assert cpu.carry == 0
  35. assert cpu.cycles == 8
  36. def test_rra():
  37. cpu = CPU()
  38. cpu.set_reg8(R8.A, 0x01)
  39. RRA().exec(cpu)
  40. assert cpu.get_reg8(R8.A) == 0x00
  41. assert cpu.carry == 1
  42. assert cpu.cycles == 4
  43. RRA().exec(cpu)
  44. assert cpu.get_reg8(R8.A) == 0x80
  45. assert cpu.carry == 0
  46. assert cpu.cycles == 8
  47. @pytest.mark.parametrize("r", R8)
  48. def test_rlc_r(r):
  49. cpu = CPU()
  50. cpu.set_reg8(r, 0x80)
  51. RLC_R(r).exec(cpu)
  52. assert cpu.get_reg8(r) == 0x01
  53. assert cpu.carry == 1
  54. assert cpu.cycles == 8
  55. RLC_R(r).exec(cpu)
  56. assert cpu.get_reg8(r) == 0x02
  57. assert cpu.carry == 0
  58. assert cpu.cycles == 16
  59. def test_rlc_hl():
  60. cpu = CPU()
  61. cpu.deref_hl_set(0x80)
  62. RLC_HL().exec(cpu)
  63. assert cpu.deref_hl() == 0x01
  64. assert cpu.carry == 1
  65. assert cpu.cycles == 16
  66. RLC_HL().exec(cpu)
  67. assert cpu.deref_hl() == 0x02
  68. assert cpu.carry == 0
  69. assert cpu.cycles == 32
  70. @pytest.mark.parametrize("r", R8)
  71. def test_rl_r(r):
  72. cpu = CPU()
  73. cpu.set_reg8(r, 0x80)
  74. RL_R(r).exec(cpu)
  75. assert cpu.get_reg8(r) == 0x00
  76. assert cpu.carry == 1
  77. assert cpu.cycles == 8
  78. RL_R(r).exec(cpu)
  79. assert cpu.get_reg8(r) == 0x01
  80. assert cpu.carry == 0
  81. assert cpu.cycles == 16
  82. def test_rl_hl():
  83. cpu = CPU()
  84. cpu.deref_hl_set(0x80)
  85. RL_HL().exec(cpu)
  86. assert cpu.deref_hl() == 0x00
  87. assert cpu.carry == 1
  88. assert cpu.cycles == 16
  89. RL_HL().exec(cpu)
  90. assert cpu.deref_hl() == 0x01
  91. assert cpu.carry == 0
  92. assert cpu.cycles == 32
  93. @pytest.mark.parametrize("r", R8)
  94. def test_rrc_r(r):
  95. cpu = CPU()
  96. cpu.set_reg8(r, 0x01)
  97. RRC_R(r).exec(cpu)
  98. assert cpu.get_reg8(r) == 0x80
  99. assert cpu.carry == 1
  100. assert cpu.cycles == 8
  101. RRC_R(r).exec(cpu)
  102. assert cpu.get_reg8(r) == 0x40
  103. assert cpu.carry == 0
  104. assert cpu.cycles == 16
  105. def test_rrc_hl():
  106. cpu = CPU()
  107. cpu.set_reg16(R16.HL, 0x1234)
  108. cpu.deref_hl_set(0x01)
  109. RRC_HL().exec(cpu)
  110. assert cpu.deref_hl() == 0x80
  111. assert cpu.carry == 1
  112. assert cpu.cycles == 16
  113. RRC_HL().exec(cpu)
  114. assert cpu.deref_hl() == 0x40
  115. assert cpu.carry == 0
  116. assert cpu.cycles == 32
  117. @pytest.mark.parametrize("r", R8)
  118. def test_rr_r(r):
  119. cpu = CPU()
  120. cpu.set_reg8(r, 0x01)
  121. RR_R(r).exec(cpu)
  122. assert cpu.get_reg8(r) == 0x00
  123. assert cpu.carry == 1
  124. assert cpu.cycles == 8
  125. RR_R(r).exec(cpu)
  126. assert cpu.get_reg8(r) == 0x80
  127. assert cpu.carry == 0
  128. assert cpu.cycles == 16
  129. def test_rr_hl():
  130. cpu = CPU()
  131. cpu.set_reg16(R16.HL, 0x1234)
  132. cpu.deref_hl_set(0x01)
  133. RR_HL().exec(cpu)
  134. assert cpu.deref_hl() == 0x00
  135. assert cpu.carry == 1
  136. assert cpu.cycles == 16
  137. RR_HL().exec(cpu)
  138. assert cpu.deref_hl() == 0x80
  139. assert cpu.carry == 0
  140. assert cpu.cycles == 32
  141. @pytest.mark.parametrize("r", R8)
  142. def test_sla_r(r):
  143. cpu = CPU()
  144. cpu.set_reg8(r, 0xFF)
  145. SLA_R(r).exec(cpu)
  146. assert cpu.get_reg8(r) == 0xFE
  147. assert cpu.carry == 1
  148. assert cpu.cycles == 8
  149. cpu.set_reg8(r, 0x01)
  150. SLA_R(r).exec(cpu)
  151. assert cpu.get_reg8(r) == 0x02
  152. assert cpu.carry == 0
  153. assert cpu.cycles == 16
  154. def test_sla_hl():
  155. cpu = CPU()
  156. cpu.set_reg16(R16.HL, 0x1234)
  157. cpu.deref_hl_set(0xFF)
  158. SLA_HL().exec(cpu)
  159. assert cpu.deref_hl() == 0xFE
  160. assert cpu.carry == 1
  161. assert cpu.cycles == 16
  162. cpu.deref_hl_set(0x01)
  163. SLA_HL().exec(cpu)
  164. assert cpu.deref_hl() == 0x02
  165. assert cpu.carry == 0
  166. assert cpu.cycles == 32
  167. @pytest.mark.parametrize("r", R8)
  168. def test_swap_r(r):
  169. cpu = CPU()
  170. cpu.set_reg8(r, 0xAB)
  171. SWAP_R(r).exec(cpu)
  172. assert cpu.get_reg8(r) == 0xBA
  173. assert cpu.carry == 0
  174. assert cpu.cycles == 8
  175. def test_swap_hl():
  176. cpu = CPU()
  177. cpu.set_reg16(R16.HL, 0x1234)
  178. cpu.deref_hl_set(0xAB)
  179. SWAP_HL().exec(cpu)
  180. assert cpu.deref_hl() == 0xBA
  181. assert cpu.carry == 0
  182. assert cpu.cycles == 16
  183. @pytest.mark.parametrize("r", R8)
  184. def test_sra_r(r):
  185. cpu = CPU()
  186. cpu.set_reg8(r, 0xFF)
  187. SRA_R(r).exec(cpu)
  188. assert cpu.get_reg8(r) == 0xFF
  189. assert cpu.carry == 1
  190. assert cpu.cycles == 8
  191. cpu.set_reg8(r, 0x02)
  192. SRA_R(r).exec(cpu)
  193. assert cpu.get_reg8(r) == 0x01
  194. assert cpu.carry == 0
  195. assert cpu.cycles == 16
  196. def test_sra_hl():
  197. cpu = CPU()
  198. cpu.set_reg16(R16.HL, 0x1234)
  199. cpu.deref_hl_set(0xFF)
  200. SRA_HL().exec(cpu)
  201. assert cpu.deref_hl() == 0xFF
  202. assert cpu.carry == 1
  203. assert cpu.cycles == 16
  204. cpu.deref_hl_set(0x02)
  205. SRA_HL().exec(cpu)
  206. assert cpu.deref_hl() == 0x01
  207. assert cpu.carry == 0
  208. assert cpu.cycles == 32
  209. @pytest.mark.parametrize("r", R8)
  210. def test_srl_r(r):
  211. cpu = CPU()
  212. cpu.set_reg8(r, 0xFF)
  213. SRL_R(r).exec(cpu)
  214. assert cpu.get_reg8(r) == 0x7F
  215. assert cpu.carry == 1
  216. assert cpu.cycles == 8
  217. cpu.set_reg8(r, 0x02)
  218. SRL_R(r).exec(cpu)
  219. assert cpu.get_reg8(r) == 0x01
  220. assert cpu.carry == 0
  221. assert cpu.cycles == 16
  222. def test_srl_hl():
  223. cpu = CPU()
  224. cpu.set_reg16(R16.HL, 0x1234)
  225. cpu.deref_hl_set(0xFF)
  226. SRL_HL().exec(cpu)
  227. assert cpu.deref_hl() == 0x7F
  228. assert cpu.carry == 1
  229. assert cpu.cycles == 16
  230. cpu.deref_hl_set(0x02)
  231. SRL_HL().exec(cpu)
  232. assert cpu.deref_hl() == 0x01
  233. assert cpu.carry == 0
  234. assert cpu.cycles == 32