From eddfb7a0c6b193312d1e50dcbb94110ebf921b8c Mon Sep 17 00:00:00 2001 From: radumacocian Date: Mon, 30 Jun 2025 15:08:25 +0200 Subject: [PATCH] added helper function for flag setting on addition --- cpu/flags.go | 19 +++++++++++++++++++ cpu/functions.go | 32 +++++++++++++++++++------------- 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 cpu/flags.go diff --git a/cpu/flags.go b/cpu/flags.go new file mode 100644 index 0000000..de184c6 --- /dev/null +++ b/cpu/flags.go @@ -0,0 +1,19 @@ +package cpu + +func Set8BitAddFlags(context op_context, byte1 byte, byte2 byte) byte { + result := uint16(byte1) + uint16(byte2) + halfcarry := (byte1&0x0F)+(byte2&0x0F) > 0x0F + context.cpu.SetNF(false) + context.cpu.SetHF(halfcarry) + context.cpu.SetCF(result > 0xFFFF) + return byte(result) +} + +func Add16BitsAndSetFlags(context op_context, op1 uint16, op2 uint16) uint16 { + result := uint32(op1) + uint32(op2) + halfcarry := (op1&0x0FFF)+(op2&0x0FFF) > 0x0FFF + context.cpu.SetNF(false) + context.cpu.SetHF(halfcarry) + context.cpu.SetCF(result > 0xFFFF) + return uint16(result) +} diff --git a/cpu/functions.go b/cpu/functions.go index 8031f6a..162d5bb 100644 --- a/cpu/functions.go +++ b/cpu/functions.go @@ -7,32 +7,32 @@ import ( type op_context struct { cpu CPU_struct - immediate byte + immediate uint16 } +// 0x00 NOOP function func Noop(context op_context) { return } -// LDBCd16 Load the 2 bytes of immediate data into register pair BC. +// 0x01 LDBCd16 Load the 2 bytes of immediate data into register pair BC. func LDBCd16(context op_context) { - operations.LD(&context.cpu.C, memory.Read8(uint(context.immediate))) - operations.LD(&context.cpu.B, memory.Read8(uint(context.immediate)+8)) + context.cpu.SetBC(context.immediate) } -// LDBCa Store the contents of register A in the memory location specified by register pair BC. +// 0x02 LDBCa Store the contents of register A in the memory location specified by register pair BC. func LDBCa(context op_context) { operations.LDInMemory8(context.cpu.BC(), context.cpu.A) return } -// INCBC Increment the contents of register pair BC by 1. +// 0x03 INCBC Increment the contents of register pair BC by 1. func INCBC(context op_context) { operations.INC16(&context.cpu.B, &context.cpu.C) return } -// INCB Increment the contents of register B by 1. +// 0x04 INCB Increment the contents of register B by 1. func INCB(context op_context) { if context.cpu.B&0x0F == 0x0F { context.cpu.SetHF(true) @@ -45,7 +45,7 @@ func INCB(context op_context) { context.cpu.SetNF(false) } -// DECB Decrement the contents of register B by 1. +// 0x05 DECB Decrement the contents of register B by 1. func DECB(context op_context) { if context.cpu.B&0x0F == 0x00 { context.cpu.SetHF(true) @@ -58,12 +58,12 @@ func DECB(context op_context) { context.cpu.SetNF(true) } -// LD8B Load the 8-bit immediate operand d8 into register B. +// 0x06 LD8B Load the 8-bit immediate operand d8 into register B. func LD8B(context op_context) { 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. +// 0x07 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. func RLCA(context op_context) { a7 := context.cpu.A>>7 == 1 operations.Shift(&context.cpu.A) @@ -74,8 +74,14 @@ func RLCA(context op_context) { context.cpu.SetCF(a7) } -// 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. +// 0x08 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)) + operations.LDInMemory8(context.immediate, operations.GetLowerByte(context.cpu.SP)) + operations.LDInMemory8(context.immediate+1, operations.GetHigherByte(context.cpu.SP)) +} + +// 0x09 ADDHLBC Add the contents of register pair BC to the contents of register pair HL, and store the results in register pair HL. +func ADDHLBC(context op_context) { + result := Add16BitsAndSetFlags(context, context.cpu.HL(), context.cpu.BC()) + context.cpu.SetHL(result) }