Skip to content

Commit

Permalink
add debugging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Jun 6, 2024
1 parent 78638a7 commit 0d53acb
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ build: clean
run: build
./Sapphire.app/Contents/MacOS/sapphire

.PHONY: run-debug
run-debug:
go run -tags debug ./debugger

.PHONY: package
package: build
zip -r build/sapphire.zip Sapphire.app
Expand Down
22 changes: 22 additions & 0 deletions debugger/debugger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build debug

package main

import (
"os"

"github.com/dbut2/sapphire/gba"
)

func main() {
gameData, err := os.ReadFile("sapphire.gba")
if err != nil {
panic(err.Error())
}
e := gba.NewEmu(gameData)

e.Hooks.RegisterHook(gba.PreStepCPUEmuHook, func(emulator *gba.Emulator) {
// Do something before CPU step
})
e.Boot()
}
19 changes: 19 additions & 0 deletions debugger/hooks/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hooks

type HookService[T ~int, V any] map[T]func(V)

func (h *HookService[T, V]) RegisterHook(hook T, f func(V)) {
if *h == nil {
*h = make(HookService[T, V])
}
(*h)[hook] = f
}

func (h *HookService[T, V]) Hook(hook T, v V) {
if *h == nil {
*h = make(HookService[T, V])
}
if f, ok := (*h)[hook]; ok {
f(v)
}
}
15 changes: 9 additions & 6 deletions gba/emu.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package gba

import (
"image"
"time"
)

type Emulator struct {
*Motherboard
}

func NewEmu(gamepak []byte) *Emulator {
return &Emulator{Motherboard: NewMotherboard(gamepak)}
motherboard := NewMotherboard(gamepak)

img := image.NewRGBA(image.Rect(0, 0, 240, 160))
motherboard.LCD.SetImage(img)
motherboard.LCD.SetDraw(func() {})

return &Emulator{Motherboard: motherboard}
}

func (e *Emulator) Boot() {
Expand Down Expand Up @@ -74,7 +77,7 @@ func (e *Emulator) step() {
SetIORegister(e.Memory, DISPSTAT, dispstat)

preCount := e.CPU.cycles
e.CPU.Step()
e.stepCPU()
postCount := e.CPU.cycles

e.Timer.Tick(postCount - preCount)
Expand Down
26 changes: 26 additions & 0 deletions gba/emu_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build debug

package gba

import (
"github.com/dbut2/sapphire/debugger/hooks"
)

type Emulator struct {
*Motherboard

Hooks hooks.HookService[EmuHook, *Emulator]
}

type EmuHook int

const (
PreStepCPUEmuHook EmuHook = iota
PostStepCPUEmuHook
)

func (e *Emulator) stepCPU() {
e.Hooks.Hook(PreStepCPUEmuHook, e)
e.CPU.Step()
e.Hooks.Hook(PostStepCPUEmuHook, e)
}
11 changes: 11 additions & 0 deletions gba/emu_prod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !debug

package gba

type Emulator struct {
*Motherboard
}

func (e *Emulator) stepCPU() {
e.CPU.Step()
}

0 comments on commit 0d53acb

Please sign in to comment.