From 5f6ee5837872bd30780cb3cfa990d124b18d3038 Mon Sep 17 00:00:00 2001 From: radumacocian Date: Mon, 30 Jun 2025 14:34:58 +0200 Subject: [PATCH] added half carry flags --- cpu/functions.go | 23 +++++++++++++++++------ cpu/operations/read_byte.go | 10 ++++++++++ cpu/operations/read_byte_test.go | 25 +++++++++++++++++++++++++ journal/References.md | 6 +++++- 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 cpu/operations/read_byte.go create mode 100644 cpu/operations/read_byte_test.go diff --git a/cpu/functions.go b/cpu/functions.go index 50a5481..8031f6a 100644 --- a/cpu/functions.go +++ b/cpu/functions.go @@ -6,8 +6,8 @@ import ( ) type op_context struct { - cpu CPU_struct - addr uint + cpu CPU_struct + immediate byte } func Noop(context op_context) { @@ -16,8 +16,8 @@ func Noop(context op_context) { // LDBCd16 Load the 2 bytes of immediate data into register pair BC. func LDBCd16(context op_context) { - operations.LD(&context.cpu.C, memory.Read8(context.addr)) - operations.LD(&context.cpu.B, memory.Read8(context.addr+8)) + operations.LD(&context.cpu.C, memory.Read8(uint(context.immediate))) + operations.LD(&context.cpu.B, memory.Read8(uint(context.immediate)+8)) } // LDBCa Store the contents of register A in the memory location specified by register pair BC. @@ -34,6 +34,11 @@ func INCBC(context op_context) { // INCB Increment the contents of register B by 1. func INCB(context op_context) { + if context.cpu.B&0x0F == 0x0F { + context.cpu.SetHF(true) + } else { + context.cpu.SetHF(false) + } operations.INC(&context.cpu.B) context.cpu.SetZF(context.cpu.B == byte(0)) @@ -42,6 +47,11 @@ func INCB(context op_context) { // DECB Decrement the contents of register B by 1. func DECB(context op_context) { + if context.cpu.B&0x0F == 0x00 { + context.cpu.SetHF(true) + } else { + context.cpu.SetHF(false) + } operations.DEC(&context.cpu.B) context.cpu.SetZF(context.cpu.B == byte(0)) @@ -50,7 +60,7 @@ func DECB(context op_context) { // LD8B Load the 8-bit immediate operand d8 into register B. func LD8B(context op_context) { - operations.LD(&context.cpu.B, memory.Read8(context.addr)) + operations.LD(&context.cpu.B, memory.Read8(uint(context.immediate))) } // RLCA Rotate the contents of register A to the left. The contents of bit 7 are placed in both the CY flag and bit 0 of register A. @@ -66,5 +76,6 @@ func RLCA(context op_context) { // LDA16 Store the lower byte of stack pointer SP at the address specified by the 16-bit immediate operand a16, and store the upper byte of SP at address a16 + 1. func LDA16(context op_context) { - + operations.LDInMemory8(uint16(context.immediate), operations.GetLowerByte(context.cpu.SP)) + operations.LDInMemory8(uint16(context.immediate+1), operations.GetHigherByte(context.cpu.SP)) } diff --git a/cpu/operations/read_byte.go b/cpu/operations/read_byte.go new file mode 100644 index 0000000..d915b4c --- /dev/null +++ b/cpu/operations/read_byte.go @@ -0,0 +1,10 @@ +package operations + +func GetLowerByte(value uint16) byte { + return (byte)(value) +} + +func GetHigherByte(value uint16) byte { + return (byte)(value >> 8) +} + diff --git a/cpu/operations/read_byte_test.go b/cpu/operations/read_byte_test.go new file mode 100644 index 0000000..befeba7 --- /dev/null +++ b/cpu/operations/read_byte_test.go @@ -0,0 +1,25 @@ +package operations + +import ( + "testing" +) + +func TestReadLowerByte(t *testing.T) { + SP := uint16(0x1234) + + actual := GetLowerByte(SP) + + if actual != 0x34 { + t.Errorf("actual %v != expected %v", actual, true) + } +} + +func TestReadUpperByte(t *testing.T) { + SP := uint16(0x1234) + + actual := GetHigherByte(SP) + + if actual != 0x12 { + t.Errorf("actual %v != expected %v", actual, true) + } +} diff --git a/journal/References.md b/journal/References.md index 4b354ae..f2f2386 100644 --- a/journal/References.md +++ b/journal/References.md @@ -1 +1,5 @@ -[Opcodes Table](https://meganesulli.com/blog/game-boy-opcodes/) \ No newline at end of file +[Opcodes Table](https://meganesulli.com/blog/game-boy-opcodes/) +[Registers & Flags](https://gbdev.io/pandocs/CPU_Registers_and_Flags.html) + +- run command: go run . roms/rom.gb +- test command: go test ./...