Browse Source

Finish rotate & shift tests

master
Forest Belton 3 years ago
parent
commit
57fe6b9a29
2 changed files with 214 additions and 1 deletions
  1. +8
    -0
      gbso/insn.py
  2. +206
    -1
      tests/insn/test_shift.py

+ 8
- 0
gbso/insn.py View File

@ -902,6 +902,7 @@ class SLA_R(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
r = cpu.get_reg8(self.r) r = cpu.get_reg8(self.r)
cpu.carry = 1 if r & (1 << 7) else 0
cpu.set_reg8(self.r, (r << 1) & 0xFF) cpu.set_reg8(self.r, (r << 1) & 0xFF)
cpu.cycles += 8 cpu.cycles += 8
@ -913,6 +914,7 @@ class SLA_R(Insn):
class SLA_HL(Insn): class SLA_HL(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
hl = cpu.deref_hl() hl = cpu.deref_hl()
cpu.carry = 1 if hl & (1 << 7) else 0
cpu.deref_hl_set((hl << 1) & 0xFF) cpu.deref_hl_set((hl << 1) & 0xFF)
cpu.cycles += 16 cpu.cycles += 16
@ -927,6 +929,7 @@ class SWAP_R(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
r = cpu.get_reg8(self.r) r = cpu.get_reg8(self.r)
cpu.set_reg8(self.r, ((r << 4) & 0xFF) | (r >> 4)) cpu.set_reg8(self.r, ((r << 4) & 0xFF) | (r >> 4))
cpu.carry = 0
cpu.cycles += 8 cpu.cycles += 8
def pretty(self) -> str: def pretty(self) -> str:
@ -938,6 +941,7 @@ class SWAP_HL(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
hl = cpu.deref_hl() hl = cpu.deref_hl()
cpu.deref_hl_set(((hl << 4) & 0xFF) | (hl >> 4)) cpu.deref_hl_set(((hl << 4) & 0xFF) | (hl >> 4))
cpu.carry = 0
cpu.cycles += 16 cpu.cycles += 16
def pretty(self) -> str: def pretty(self) -> str:
@ -950,6 +954,7 @@ class SRA_R(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
r = cpu.get_reg8(self.r) r = cpu.get_reg8(self.r)
cpu.carry = 1 if r & (1 << 0) else 0
cpu.set_reg8(self.r, (r >> 1) | (r & (1 << 7))) cpu.set_reg8(self.r, (r >> 1) | (r & (1 << 7)))
cpu.cycles += 8 cpu.cycles += 8
@ -961,6 +966,7 @@ class SRA_R(Insn):
class SRA_HL(Insn): class SRA_HL(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
hl = cpu.deref_hl() hl = cpu.deref_hl()
cpu.carry = 1 if hl & (1 << 0) else 0
cpu.deref_hl_set((hl >> 1) | (hl & (1 << 7))) cpu.deref_hl_set((hl >> 1) | (hl & (1 << 7)))
cpu.cycles += 16 cpu.cycles += 16
@ -974,6 +980,7 @@ class SRL_R(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
r = cpu.get_reg8(self.r) r = cpu.get_reg8(self.r)
cpu.carry = 1 if r & (1 << 0) else 0
cpu.set_reg8(self.r, r >> 1) cpu.set_reg8(self.r, r >> 1)
cpu.cycles += 8 cpu.cycles += 8
@ -985,6 +992,7 @@ class SRL_R(Insn):
class SRL_HL(Insn): class SRL_HL(Insn):
def exec(self, cpu: CPU) -> None: def exec(self, cpu: CPU) -> None:
hl = cpu.deref_hl() hl = cpu.deref_hl()
cpu.carry = 1 if hl & (1 << 0) else 0
cpu.deref_hl_set(hl >> 1) cpu.deref_hl_set(hl >> 1)
cpu.cycles += 16 cpu.cycles += 16

+ 206
- 1
tests/insn/test_shift.py View File

@ -89,6 +89,23 @@ def test_rlc_r(r):
assert cpu.cycles == 16 assert cpu.cycles == 16
def test_rlc_hl():
cpu = CPU()
cpu.deref_hl_set(0x80)
RLC_HL().exec(cpu)
assert cpu.deref_hl() == 0x01
assert cpu.carry == 1
assert cpu.cycles == 16
RLC_HL().exec(cpu)
assert cpu.deref_hl() == 0x02
assert cpu.carry == 0
assert cpu.cycles == 32
@pytest.mark.parametrize("r", R8) @pytest.mark.parametrize("r", R8)
def test_rl_r(r): def test_rl_r(r):
cpu = CPU() cpu = CPU()
@ -107,6 +124,23 @@ def test_rl_r(r):
assert cpu.cycles == 16 assert cpu.cycles == 16
def test_rl_hl():
cpu = CPU()
cpu.deref_hl_set(0x80)
RL_HL().exec(cpu)
assert cpu.deref_hl() == 0x00
assert cpu.carry == 1
assert cpu.cycles == 16
RL_HL().exec(cpu)
assert cpu.deref_hl() == 0x01
assert cpu.carry == 0
assert cpu.cycles == 32
@pytest.mark.parametrize("r", R8) @pytest.mark.parametrize("r", R8)
def test_rrc_r(r): def test_rrc_r(r):
cpu = CPU() cpu = CPU()
@ -125,6 +159,24 @@ def test_rrc_r(r):
assert cpu.cycles == 16 assert cpu.cycles == 16
def test_rrc_hl():
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.deref_hl_set(0x01)
RRC_HL().exec(cpu)
assert cpu.deref_hl() == 0x80
assert cpu.carry == 1
assert cpu.cycles == 16
RRC_HL().exec(cpu)
assert cpu.deref_hl() == 0x40
assert cpu.carry == 0
assert cpu.cycles == 32
@pytest.mark.parametrize("r", R8) @pytest.mark.parametrize("r", R8)
def test_rr_r(r): def test_rr_r(r):
cpu = CPU() cpu = CPU()
@ -143,4 +195,157 @@ def test_rr_r(r):
assert cpu.cycles == 16 assert cpu.cycles == 16
# TODO: Test RLC_HL, RL_HL, RRC_HL, RR_HL
def test_rr_hl():
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.deref_hl_set(0x01)
RR_HL().exec(cpu)
assert cpu.deref_hl() == 0x00
assert cpu.carry == 1
assert cpu.cycles == 16
RR_HL().exec(cpu)
assert cpu.deref_hl() == 0x80
assert cpu.carry == 0
assert cpu.cycles == 32
@pytest.mark.parametrize("r", R8)
def test_sla_r(r):
cpu = CPU()
cpu.set_reg8(r, 0xFF)
SLA_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0xFE
assert cpu.carry == 1
assert cpu.cycles == 8
cpu.set_reg8(r, 0x01)
SLA_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0x02
assert cpu.carry == 0
assert cpu.cycles == 16
def test_sla_hl():
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.deref_hl_set(0xFF)
SLA_HL().exec(cpu)
assert cpu.deref_hl() == 0xFE
assert cpu.carry == 1
assert cpu.cycles == 16
cpu.deref_hl_set(0x01)
SLA_HL().exec(cpu)
assert cpu.deref_hl() == 0x02
assert cpu.carry == 0
assert cpu.cycles == 32
@pytest.mark.parametrize("r", R8)
def test_swap_r(r):
cpu = CPU()
cpu.set_reg8(r, 0xAB)
SWAP_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0xBA
assert cpu.carry == 0
assert cpu.cycles == 8
def test_swap_hl():
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.deref_hl_set(0xAB)
SWAP_HL().exec(cpu)
assert cpu.deref_hl() == 0xBA
assert cpu.carry == 0
assert cpu.cycles == 16
@pytest.mark.parametrize("r", R8)
def test_sra_r(r):
cpu = CPU()
cpu.set_reg8(r, 0xFF)
SRA_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0xFF
assert cpu.carry == 1
assert cpu.cycles == 8
cpu.set_reg8(r, 0x02)
SRA_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0x01
assert cpu.carry == 0
assert cpu.cycles == 16
def test_sra_hl():
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.deref_hl_set(0xFF)
SRA_HL().exec(cpu)
assert cpu.deref_hl() == 0xFF
assert cpu.carry == 1
assert cpu.cycles == 16
cpu.deref_hl_set(0x02)
SRA_HL().exec(cpu)
assert cpu.deref_hl() == 0x01
assert cpu.carry == 0
assert cpu.cycles == 32
@pytest.mark.parametrize("r", R8)
def test_srl_r(r):
cpu = CPU()
cpu.set_reg8(r, 0xFF)
SRL_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0x7F
assert cpu.carry == 1
assert cpu.cycles == 8
cpu.set_reg8(r, 0x02)
SRL_R(r).exec(cpu)
assert cpu.get_reg8(r) == 0x01
assert cpu.carry == 0
assert cpu.cycles == 16
def test_srl_hl():
cpu = CPU()
cpu.set_reg16(R16.HL, 0x1234)
cpu.deref_hl_set(0xFF)
SRL_HL().exec(cpu)
assert cpu.deref_hl() == 0x7F
assert cpu.carry == 1
assert cpu.cycles == 16
cpu.deref_hl_set(0x02)
SRL_HL().exec(cpu)
assert cpu.deref_hl() == 0x01
assert cpu.carry == 0
assert cpu.cycles == 32

Loading…
Cancel
Save