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.

200 lines
3.6 KiB

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