mirror of
https://github.com/macocianradu/goboy.git
synced 2026-03-18 21:10:07 +00:00
setup getters and setters for combined registers
This commit is contained in:
73
cpu/cpu.go
73
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)
|
||||
}
|
||||
|
||||
46
cpu/cpu_test.go
Normal file
46
cpu/cpu_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
8
journal/Day 2 - 2025.04.19.md
Normal file
8
journal/Day 2 - 2025.04.19.md
Normal 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
1
journal/References.md
Normal file
@@ -0,0 +1 @@
|
||||
[Opcodes Table](https://meganesulli.com/blog/game-boy-opcodes/)
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user