-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |