setup getters and setters for combined registers

This commit is contained in:
radumaco
2025-04-19 20:27:44 +02:00
parent 43ea4b70e0
commit d26e6b7436
5 changed files with 120 additions and 24 deletions

View File

@@ -4,26 +4,67 @@ import (
"radu.macocian.me/goboy/memory" "radu.macocian.me/goboy/memory"
) )
type cpu_struct struct { type CPU_struct struct {
A int8 A byte
F int8 F byte
B int8 B byte
C int8 C byte
D int8 D byte
E int8 E byte
H int8 H byte
L int8 L byte
IR int8 IR byte
IE int8 IE byte
PC int16 PC uint16
SP int16 SP uint16
} }
var cpu = new(cpu_struct) var cpu = new(CPU_struct)
func Execute(addr int) { func Execute(addr uint) {
instr := memory.Read(addr + int(cpu.PC)) instr := memory.Read(addr + uint(cpu.PC))
cpu.PC++ cpu.PC++
print(string(instr)) 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)
}

46
cpu/cpu_test.go Normal file
View File

@@ -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)
}
}

View File

@@ -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

1
journal/References.md Normal file
View File

@@ -0,0 +1 @@
[Opcodes Table](https://meganesulli.com/blog/game-boy-opcodes/)

View File

@@ -6,30 +6,30 @@ import (
var memory = new([64000]byte) var memory = new([64000]byte)
func Read(addr int) []byte { func Read(addr uint) []byte {
checkInside(addr) checkInside(addr)
checkInside(addr + 8) checkInside(addr + 8)
return memory[addr : addr+9] return memory[addr : addr+9]
} }
func Write(addr int, val [8]byte) { func Write(addr uint, val [8]byte) {
checkInside(addr) checkInside(addr)
for i := 0; i < 8; i++ { 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)
checkInside(addr + len(val)) checkInside(addr + uint(len(val)))
for b := 0; b < len(val); b++ { for b := 0; b < len(val); b++ {
memory[addr+b] = val[b] memory[int(addr)+b] = val[b]
} }
} }
func checkInside(addr int) { func checkInside(addr uint) {
if addr < 0 || addr >= len(memory) { if addr < 0 || addr >= uint(len(memory)) {
panic(errorHandling.OutOfMemoryBoundsError) panic(errorHandling.OutOfMemoryBoundsError)
} }
} }