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.

220 lines
4.2 KiB

  1. import pytest
  2. from tests.insn.helpers import *
  3. @pytest.mark.parametrize("dst,src", [(x, y) for x in R8 for y in R8])
  4. def test_ld_r_r(dst, src):
  5. cpu = CPU()
  6. cpu.set_reg8(src, 0x7F)
  7. LD_R_R(dst, src).exec(cpu)
  8. assert cpu.get_reg8(src) == 0x7F
  9. assert cpu.state.cycles == 4
  10. @pytest.mark.parametrize("r,imm", [(r, imm) for r in R8 for imm in n8()])
  11. def test_ld_r_n8(r, imm):
  12. cpu = CPU()
  13. LD_R_N8(r, 0x7F).exec(cpu)
  14. assert cpu.get_reg8(r) == 0x7F
  15. assert cpu.state.cycles == 8
  16. @pytest.mark.parametrize("r", R8)
  17. def test_ld_r_hl(r):
  18. cpu = CPU()
  19. cpu.set_reg16(R16.HL, 0x1234)
  20. cpu.set_mem8(0x1234, 0x7F)
  21. LD_R_HL(r).exec(cpu)
  22. assert cpu.get_reg8(r) == 0x7F
  23. assert cpu.state.cycles == 8
  24. @pytest.mark.parametrize("r", R8)
  25. def test_ld_hl_r(r):
  26. cpu = CPU()
  27. cpu.set_reg8(r, 0x7F)
  28. cpu.set_reg16(R16.HL, 0x1234)
  29. LD_HL_R(r).exec(cpu)
  30. if r == R8.H:
  31. assert cpu.hl == 0x12
  32. elif r == R8.L:
  33. assert cpu.hl == 0x34
  34. else:
  35. assert cpu.hl == 0x7F
  36. assert cpu.state.cycles == 8
  37. @pytest.mark.parametrize("imm", n8())
  38. def test_ld_hl_n8(imm):
  39. cpu = CPU()
  40. cpu.set_reg16(R16.HL, 0x1234)
  41. LD_HL_N(imm).exec(cpu)
  42. assert cpu.hl == imm
  43. assert cpu.state.cycles == 12
  44. def test_ld_a_bc():
  45. cpu = CPU()
  46. cpu.set_reg16(R16.BC, 0x1234)
  47. cpu.set_mem8(0x1234, 0x7F)
  48. LD_A_BC().exec(cpu)
  49. assert cpu.get_reg8(R8.A) == 0x7F
  50. assert cpu.state.cycles == 8
  51. def test_ld_a_de():
  52. cpu = CPU()
  53. cpu.set_reg16(R16.DE, 0x1234)
  54. cpu.set_mem8(0x1234, 0x7F)
  55. LD_A_DE().exec(cpu)
  56. assert cpu.get_reg8(R8.A) == 0x7F
  57. assert cpu.state.cycles == 8
  58. # @pytest.mark.parametrize("nn", n16())
  59. def test_ld_a_nn(nn=0x1234):
  60. cpu = CPU()
  61. cpu.set_mem8(nn, 0x7F)
  62. LD_A_NN(nn).exec(cpu)
  63. assert cpu.get_reg8(R8.A) == 0x7F
  64. assert cpu.state.cycles == 16
  65. def test_ld_bc_a():
  66. cpu = CPU()
  67. cpu.set_reg16(R16.BC, 0x1234)
  68. cpu.set_reg8(R8.A, 0x7F)
  69. LD_BC_A().exec(cpu)
  70. assert cpu.get_mem8(0x1234) == 0x7F
  71. assert cpu.state.cycles == 8
  72. def test_ld_de_a():
  73. cpu = CPU()
  74. cpu.set_reg16(R16.DE, 0x1234)
  75. cpu.set_reg8(R8.A, 0x7F)
  76. LD_DE_A().exec(cpu)
  77. assert cpu.get_mem8(0x1234) == 0x7F
  78. assert cpu.state.cycles == 8
  79. # @pytest.mark.parametrize("nn", n16())
  80. def test_ld_nn_a(nn=0x1234):
  81. cpu = CPU()
  82. cpu.set_reg8(R8.A, 0x7F)
  83. LD_NN_A(nn).exec(cpu)
  84. assert cpu.get_mem8(nn) == 0x7F
  85. assert cpu.state.cycles == 16
  86. @pytest.mark.parametrize("n", n8())
  87. def test_ld_a_ff_n(n):
  88. cpu = CPU()
  89. cpu.set_mem8(0xFF00 + n, 0x7F)
  90. LD_A_FF_N(n).exec(cpu)
  91. assert cpu.get_reg8(R8.A) == 0x7F
  92. assert cpu.state.cycles == 12
  93. @pytest.mark.parametrize("n", n8())
  94. def test_ld_ff_n_a(n):
  95. cpu = CPU()
  96. cpu.set_reg8(R8.A, 0x7F)
  97. LD_FF_N_A(n).exec(cpu)
  98. assert cpu.get_mem8(0xFF00 + n) == 0x7F
  99. assert cpu.state.cycles == 12
  100. def test_ld_a_ff_c():
  101. cpu = CPU()
  102. cpu.set_reg8(R8.C, 0x12)
  103. cpu.set_mem8(0xFF12, 0x7F)
  104. LD_A_FF_C().exec(cpu)
  105. assert cpu.get_reg8(R8.A) == 0x7F
  106. assert cpu.state.cycles == 8
  107. def test_ld_ff_c_a():
  108. cpu = CPU()
  109. cpu.set_reg8(R8.C, 0x12)
  110. cpu.set_reg8(R8.A, 0x7F)
  111. LD_FF_C_A().exec(cpu)
  112. assert cpu.get_mem8(0xFF12) == 0x7F
  113. assert cpu.state.cycles == 8
  114. def test_ldi_hl_a():
  115. cpu = CPU()
  116. cpu.set_reg8(R8.A, 0x7F)
  117. cpu.set_reg16(R16.HL, 0x1234)
  118. LDI_HL_A().exec(cpu)
  119. assert cpu.get_reg16(R16.HL) == 0x1235
  120. assert cpu.get_mem8(0x1234) == 0x7F
  121. assert cpu.state.cycles == 8
  122. def test_ldi_a_hl():
  123. cpu = CPU()
  124. cpu.set_mem8(0x1234, 0x7F)
  125. cpu.set_reg16(R16.HL, 0x1234)
  126. LDI_A_HL().exec(cpu)
  127. assert cpu.get_reg16(R16.HL) == 0x1235
  128. assert cpu.get_reg8(R8.A) == 0x7F
  129. assert cpu.state.cycles == 8
  130. def test_ldd_hl_a():
  131. cpu = CPU()
  132. cpu.set_reg8(R8.A, 0x7F)
  133. cpu.set_reg16(R16.HL, 0x1234)
  134. LDD_HL_A().exec(cpu)
  135. assert cpu.get_reg16(R16.HL) == 0x1233
  136. assert cpu.get_mem8(0x1234) == 0x7F
  137. assert cpu.state.cycles == 8
  138. def test_ldd_a_hl():
  139. cpu = CPU()
  140. cpu.set_mem8(0x1234, 0x7F)
  141. cpu.set_reg16(R16.HL, 0x1234)
  142. LDD_A_HL().exec(cpu)
  143. assert cpu.get_reg16(R16.HL) == 0x1233
  144. assert cpu.get_reg8(R8.A) == 0x7F
  145. assert cpu.state.cycles == 8