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.1 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.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.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.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. hl = cpu.deref_hl()
  31. if r == R8.H:
  32. assert hl == 0x12
  33. elif r == R8.L:
  34. assert hl == 0x34
  35. else:
  36. assert hl == 0x7F
  37. assert cpu.cycles == 8
  38. @pytest.mark.parametrize("imm", n8())
  39. def test_ld_hl_n8(imm):
  40. cpu = CPU()
  41. cpu.set_reg16(R16.HL, 0x1234)
  42. LD_HL_N(imm).exec(cpu)
  43. assert cpu.deref_hl() == imm
  44. assert cpu.cycles == 12
  45. def test_ld_a_bc():
  46. cpu = CPU()
  47. cpu.set_reg16(R16.BC, 0x1234)
  48. cpu.set_mem8(0x1234, 0x7F)
  49. LD_A_BC().exec(cpu)
  50. assert cpu.get_reg8(R8.A) == 0x7F
  51. assert cpu.cycles == 8
  52. def test_ld_a_de():
  53. cpu = CPU()
  54. cpu.set_reg16(R16.DE, 0x1234)
  55. cpu.set_mem8(0x1234, 0x7F)
  56. LD_A_DE().exec(cpu)
  57. assert cpu.get_reg8(R8.A) == 0x7F
  58. assert cpu.cycles == 8
  59. # @pytest.mark.parametrize("nn", n16())
  60. def test_ld_a_nn(nn=0x1234):
  61. cpu = CPU()
  62. cpu.set_mem8(nn, 0x7F)
  63. LD_A_NN(nn).exec(cpu)
  64. assert cpu.get_reg8(R8.A) == 0x7F
  65. assert cpu.cycles == 16
  66. def test_ld_bc_a():
  67. cpu = CPU()
  68. cpu.set_reg16(R16.BC, 0x1234)
  69. cpu.set_reg8(R8.A, 0x7F)
  70. LD_BC_A().exec(cpu)
  71. assert cpu.get_mem8(0x1234) == 0x7F
  72. assert cpu.cycles == 8
  73. def test_ld_de_a():
  74. cpu = CPU()
  75. cpu.set_reg16(R16.DE, 0x1234)
  76. cpu.set_reg8(R8.A, 0x7F)
  77. LD_DE_A().exec(cpu)
  78. assert cpu.get_mem8(0x1234) == 0x7F
  79. assert cpu.cycles == 8
  80. # @pytest.mark.parametrize("nn", n16())
  81. def test_ld_nn_a(nn=0x1234):
  82. cpu = CPU()
  83. cpu.set_reg8(R8.A, 0x7F)
  84. LD_NN_A(nn).exec(cpu)
  85. assert cpu.get_mem8(nn) == 0x7F
  86. assert cpu.cycles == 16
  87. @pytest.mark.parametrize("n", n8())
  88. def test_ld_a_ff_n(n):
  89. cpu = CPU()
  90. cpu.set_mem8(0xFF00 + n, 0x7F)
  91. LD_A_FF_N(n).exec(cpu)
  92. assert cpu.get_reg8(R8.A) == 0x7F
  93. assert cpu.cycles == 12
  94. @pytest.mark.parametrize("n", n8())
  95. def test_ld_ff_n_a(n):
  96. cpu = CPU()
  97. cpu.set_reg8(R8.A, 0x7F)
  98. LD_FF_N_A(n).exec(cpu)
  99. assert cpu.get_mem8(0xFF00 + n) == 0x7F
  100. assert cpu.cycles == 12
  101. def test_ld_a_ff_c():
  102. cpu = CPU()
  103. cpu.set_reg8(R8.C, 0x12)
  104. cpu.set_mem8(0xFF12, 0x7F)
  105. LD_A_FF_C().exec(cpu)
  106. assert cpu.get_reg8(R8.A) == 0x7F
  107. assert cpu.cycles == 8
  108. def test_ld_ff_c_a():
  109. cpu = CPU()
  110. cpu.set_reg8(R8.C, 0x12)
  111. cpu.set_reg8(R8.A, 0x7F)
  112. LD_FF_C_A().exec(cpu)
  113. assert cpu.get_mem8(0xFF12) == 0x7F
  114. assert cpu.cycles == 8
  115. def test_ldi_hl_a():
  116. cpu = CPU()
  117. cpu.set_reg8(R8.A, 0x7F)
  118. cpu.set_reg16(R16.HL, 0x1234)
  119. LDI_HL_A().exec(cpu)
  120. assert cpu.get_reg16(R16.HL) == 0x1235
  121. assert cpu.get_mem8(0x1234) == 0x7F
  122. assert cpu.cycles == 8
  123. def test_ldi_a_hl():
  124. cpu = CPU()
  125. cpu.set_mem8(0x1234, 0x7F)
  126. cpu.set_reg16(R16.HL, 0x1234)
  127. LDI_A_HL().exec(cpu)
  128. assert cpu.get_reg16(R16.HL) == 0x1235
  129. assert cpu.get_reg8(R8.A) == 0x7F
  130. assert cpu.cycles == 8
  131. def test_ldd_hl_a():
  132. cpu = CPU()
  133. cpu.set_reg8(R8.A, 0x7F)
  134. cpu.set_reg16(R16.HL, 0x1234)
  135. LDD_HL_A().exec(cpu)
  136. assert cpu.get_reg16(R16.HL) == 0x1233
  137. assert cpu.get_mem8(0x1234) == 0x7F
  138. assert cpu.cycles == 8
  139. def test_ldd_a_hl():
  140. cpu = CPU()
  141. cpu.set_mem8(0x1234, 0x7F)
  142. cpu.set_reg16(R16.HL, 0x1234)
  143. LDD_A_HL().exec(cpu)
  144. assert cpu.get_reg16(R16.HL) == 0x1233
  145. assert cpu.get_reg8(R8.A) == 0x7F
  146. assert cpu.cycles == 8