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.

232 lines
4.2 KiB

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