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

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