From d26e6b743686fc1a177bbab9c6abf314ba535e73 Mon Sep 17 00:00:00 2001 From: radumaco Date: Sat, 19 Apr 2025 20:27:44 +0200 Subject: [PATCH] setup getters and setters for combined registers --- cpu/cpu.go | 73 +++++++++++++++++++++++++++-------- cpu/cpu_test.go | 46 ++++++++++++++++++++++ journal/Day 2 - 2025.04.19.md | 8 ++++ journal/References.md | 1 + memory/memory.go | 16 ++++---- 5 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 cpu/cpu_test.go create mode 100644 journal/Day 2 - 2025.04.19.md create mode 100644 journal/References.md diff --git a/cpu/cpu.go b/cpu/cpu.go index 9651473..5de7f43 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -4,26 +4,67 @@ import ( "radu.macocian.me/goboy/memory" ) -type cpu_struct struct { - A int8 - F int8 - B int8 - C int8 - D int8 - E int8 - H int8 - L int8 - IR int8 - IE int8 +type CPU_struct struct { + A byte + F byte + B byte + C byte + D byte + E byte + H byte + L byte + IR byte + IE byte - PC int16 - SP int16 + PC uint16 + SP uint16 } -var cpu = new(cpu_struct) +var cpu = new(CPU_struct) -func Execute(addr int) { - instr := memory.Read(addr + int(cpu.PC)) +func Execute(addr uint) { + instr := memory.Read(addr + uint(cpu.PC)) cpu.PC++ print(string(instr)) } + +func readDoubleRegis(r1 byte, r2 byte) uint16 { + return uint16(r1)<<8 | uint16(r2) +} + +func setDoubleRegis(r1 *byte, r2 *byte, value uint16) { + *r1 = byte((value & 0xFF00) >> 8) + *r2 = byte(value & 0x00FF) +} + +func (cpu *CPU_struct) AF() uint16 { + return readDoubleRegis(cpu.A, cpu.F) +} + +func (cpu *CPU_struct) SetAF(value uint16) { + setDoubleRegis(&cpu.A, &cpu.F, value) +} + +func (cpu *CPU_struct) BC() uint16 { + return readDoubleRegis(cpu.B, cpu.C) +} + +func (cpu *CPU_struct) SetBC(value uint16) { + setDoubleRegis(&cpu.B, &cpu.C, value) +} + +func (cpu *CPU_struct) DE() uint16 { + return readDoubleRegis(cpu.D, cpu.E) +} + +func (cpu *CPU_struct) SetDE(value uint16) { + setDoubleRegis(&cpu.D, &cpu.E, value) +} + +func (cpu *CPU_struct) HL() uint16 { + return readDoubleRegis(cpu.H, cpu.L) +} + +func (cpu *CPU_struct) SetHL(value uint16) { + setDoubleRegis(&cpu.H, &cpu.L, value) +} diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go new file mode 100644 index 0000000..6f2844e --- /dev/null +++ b/cpu/cpu_test.go @@ -0,0 +1,46 @@ +package cpu + +import ( + "testing" +) + +func TestCPUAF(t *testing.T) { + cpu := CPU_struct{} + cpu.A = 0x0A + cpu.F = 0x0B + + expected := uint16(0x0A0B) + actual := cpu.AF() + + if actual != expected { + t.Errorf("actual %x != expected %x", actual, expected) + } +} + +func TestCPU_struct_SetAF(t *testing.T) { + cpu := CPU_struct{} + cpu.SetAF(0xABCD) + + expectedA := uint8(0xAB) + expectedF := uint8(0xCD) + actualA := cpu.A + actualF := cpu.F + if actualA != expectedA { + t.Errorf("actual %x != expected %x", actualA, expectedA) + } + if actualF != expectedF { + t.Errorf("actual %x != expected %x", actualF, expectedF) + } +} + +func TestCPU_struct_GetAndSetAF(t *testing.T) { + cpu := CPU_struct{} + cpu.SetAF(0xABCD) + + expectedAF := uint16(0xABCD) + actualAF := cpu.AF() + + if actualAF != expectedAF { + t.Errorf("actual %x != expected %x", actualAF, expectedAF) + } +} diff --git a/journal/Day 2 - 2025.04.19.md b/journal/Day 2 - 2025.04.19.md new file mode 100644 index 0000000..93a155e --- /dev/null +++ b/journal/Day 2 - 2025.04.19.md @@ -0,0 +1,8 @@ +# For the day: +- Setup registers reads and writes + +# Conclusion +- Can read and write to combined registers +- Using a private function in the cpu to work with combined registers +- Have named functions for the used combined registers for ease of use +- Added unit tests for the combined registers \ No newline at end of file diff --git a/journal/References.md b/journal/References.md new file mode 100644 index 0000000..4b354ae --- /dev/null +++ b/journal/References.md @@ -0,0 +1 @@ +[Opcodes Table](https://meganesulli.com/blog/game-boy-opcodes/) \ No newline at end of file diff --git a/memory/memory.go b/memory/memory.go index 402c6db..c8c2fa4 100644 --- a/memory/memory.go +++ b/memory/memory.go @@ -6,30 +6,30 @@ import ( var memory = new([64000]byte) -func Read(addr int) []byte { +func Read(addr uint) []byte { checkInside(addr) checkInside(addr + 8) return memory[addr : addr+9] } -func Write(addr int, val [8]byte) { +func Write(addr uint, val [8]byte) { checkInside(addr) for i := 0; i < 8; i++ { - memory[addr+i] = val[i] + memory[int(addr)+i] = val[i] } } -func WriteAll(addr int, val []byte) { +func WriteAll(addr uint, val []byte) { checkInside(addr) - checkInside(addr + len(val)) + checkInside(addr + uint(len(val))) for b := 0; b < len(val); b++ { - memory[addr+b] = val[b] + memory[int(addr)+b] = val[b] } } -func checkInside(addr int) { - if addr < 0 || addr >= len(memory) { +func checkInside(addr uint) { + if addr < 0 || addr >= uint(len(memory)) { panic(errorHandling.OutOfMemoryBoundsError) } }