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.

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