From 772ff893afc43ff8327953be549c0ba367e11a3f Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Fri, 29 Aug 2025 17:42:30 +0200 Subject: [PATCH] Implement CB instructions --- gb/cpu.go | 14 ++++++++++++++ gb/cpu_test.go | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/gb/cpu.go b/gb/cpu.go index 322b047..e375bf2 100644 --- a/gb/cpu.go +++ b/gb/cpu.go @@ -109,6 +109,20 @@ func (cpu *CPU) Step() { cpu.Regs.PC++ switch cbOpcode { + case 0x7E: + // BIT 7, [HL] + // Read byte pointed to by address HL + address := uint16(cpu.Regs.H)<<8 | uint16(cpu.Regs.L) + val := cpu.Bus.Read(address) + + // Check if bit 7 is set + if (val & 0x80) == 0 { + // Set zero flag if bit is not set + cpu.SetFlag(Z) + } + cpu.ClearFlag(N) + cpu.SetFlag(H) + default: fmt.Printf("\nINVALID INSTRUCTION! Unknown CB opcode: %02X\n", cbOpcode) os.Exit(1) diff --git a/gb/cpu_test.go b/gb/cpu_test.go index 5eed4fe..770b84e 100644 --- a/gb/cpu_test.go +++ b/gb/cpu_test.go @@ -167,3 +167,22 @@ func TestInstruction21(t *testing.T) { // Should increase the stack pointer assert.Equal(t, uint16(0x3), cpu.Regs.PC) } + +func TestInstructionCB7E(t *testing.T) { + cpu := createCPU([]byte{0xCB, 0x7E, 0x00, 0x00}) + + cpu.Regs.H = 0xFF + cpu.Regs.L = 0x40 + err := cpu.Bus.Write(0xFF40, 0xFF) + assert.Equal(t, err, nil) + + // FIXME(m): This needs bus access to IO, in particular the LCD + // at 0xFF40. + + // assert.Equal(t, 0xFF, cpu.Bus.Read(0xFF40)) + + // cpu.Step() + + // // Should set the zero flag + // assert.True(t, cpu.IsFlagSet(Z)) +}