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

import pytest
from gbso.insn import *
from gbso.regs import *
def n8():
for i in range(0xFF + 1):
yield i
def n16():
for i in range(0xFFFF + 1):
yield i
@pytest.mark.parametrize("dst,src", [(x, y) for x in R8 for y in R8])
def test_ld_r_r(dst, src):
cpu = CPU()
cpu.set_reg8(src, 0x7F)
LD_R_R(dst, src).exec(cpu)
assert cpu.get_reg8(src) == 0x7F
assert cpu.cycles == 4
@pytest.mark.parametrize("r,imm", [(r, imm) for r in R8 for imm in n8()])
def test_ld_r_n8(r, imm):
cpu = CPU()
LD_R_N8(r, 0x7F).exec(cpu)
assert cpu.get_reg8(r) == 0x7F
assert cpu.cycles == 8
@pytest.mark.parametrize("r", R8)
def test_ld_r_hl(r):
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.set_mem8(0x1234, 0x7F)
LD_R_HL(r).exec(cpu)
assert cpu.get_reg8(r) == 0x7F
assert cpu.cycles == 8
@pytest.mark.parametrize("r", R8)
def test_ld_hl_r(r):
cpu = CPU()
cpu.set_reg8(r, 0x7F)
cpu.set_reg16(R16.HL, 0x1234)
LD_HL_R(r).exec(cpu)
hl = cpu.deref_hl()
if r == R8.H:
assert hl == 0x12
elif r == R8.L:
assert hl == 0x34
else:
assert hl == 0x7F
assert cpu.cycles == 8
@pytest.mark.parametrize("imm", n8())
def test_ld_hl_n8(imm):
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
LD_HL_N(imm).exec(cpu)
assert cpu.deref_hl() == imm
assert cpu.cycles == 12
def test_ld_a_bc():
cpu = CPU()
cpu.set_reg16(R16.BC, 0x1234)
cpu.set_mem8(0x1234, 0x7F)
LD_A_BC().exec(cpu)
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 8
def test_ld_a_de():
cpu = CPU()
cpu.set_reg16(R16.DE, 0x1234)
cpu.set_mem8(0x1234, 0x7F)
LD_A_DE().exec(cpu)
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 8
# @pytest.mark.parametrize("nn", n16())
def test_ld_a_nn(nn=0x1234):
cpu = CPU()
cpu.set_mem8(nn, 0x7F)
LD_A_NN(nn).exec(cpu)
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 16
def test_ld_bc_a():
cpu = CPU()
cpu.set_reg16(R16.BC, 0x1234)
cpu.set_reg8(R8.A, 0x7F)
LD_BC_A().exec(cpu)
assert cpu.get_mem8(0x1234) == 0x7F
assert cpu.cycles == 8
def test_ld_de_a():
cpu = CPU()
cpu.set_reg16(R16.DE, 0x1234)
cpu.set_reg8(R8.A, 0x7F)
LD_DE_A().exec(cpu)
assert cpu.get_mem8(0x1234) == 0x7F
assert cpu.cycles == 8
# @pytest.mark.parametrize("nn", n16())
def test_ld_nn_a(nn=0x1234):
cpu = CPU()
cpu.set_reg8(R8.A, 0x7F)
LD_NN_A(nn).exec(cpu)
assert cpu.get_mem8(nn) == 0x7F
assert cpu.cycles == 16
@pytest.mark.parametrize("n", n8())
def test_ld_a_ff_n(n):
cpu = CPU()
cpu.set_mem8(0xFF00 + n, 0x7F)
LD_A_FF_N(n).exec(cpu)
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 12
@pytest.mark.parametrize("n", n8())
def test_ld_ff_n_a(n):
cpu = CPU()
cpu.set_reg8(R8.A, 0x7F)
LD_FF_N_A(n).exec(cpu)
assert cpu.get_mem8(0xFF00 + n) == 0x7F
assert cpu.cycles == 12
def test_ld_a_ff_c():
cpu = CPU()
cpu.set_reg8(R8.C, 0x12)
cpu.set_mem8(0xFF12, 0x7F)
LD_A_FF_C().exec(cpu)
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 8
def test_ld_ff_c_a():
cpu = CPU()
cpu.set_reg8(R8.C, 0x12)
cpu.set_reg8(R8.A, 0x7F)
LD_FF_C_A().exec(cpu)
assert cpu.get_mem8(0xFF12) == 0x7F
assert cpu.cycles == 8
def test_ldi_hl_a():
cpu = CPU()
cpu.set_reg8(R8.A, 0x7F)
cpu.set_reg16(R16.HL, 0x1234)
LDI_HL_A().exec(cpu)
assert cpu.get_reg16(R16.HL) == 0x1235
assert cpu.get_mem8(0x1234) == 0x7F
assert cpu.cycles == 8
def test_ldi_a_hl():
cpu = CPU()
cpu.set_mem8(0x1234, 0x7F)
cpu.set_reg16(R16.HL, 0x1234)
LDI_A_HL().exec(cpu)
assert cpu.get_reg16(R16.HL) == 0x1235
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 8
def test_ldd_hl_a():
cpu = CPU()
cpu.set_reg8(R8.A, 0x7F)
cpu.set_reg16(R16.HL, 0x1234)
LDD_HL_A().exec(cpu)
assert cpu.get_reg16(R16.HL) == 0x1233
assert cpu.get_mem8(0x1234) == 0x7F
assert cpu.cycles == 8
def test_ldd_a_hl():
cpu = CPU()
cpu.set_mem8(0x1234, 0x7F)
cpu.set_reg16(R16.HL, 0x1234)
LDD_A_HL().exec(cpu)
assert cpu.get_reg16(R16.HL) == 0x1233
assert cpu.get_reg8(R8.A) == 0x7F
assert cpu.cycles == 8