-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
executable file
·157 lines (129 loc) · 3.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// +build wasm
package main
import (
"encoding/base64"
"fmt"
"log"
"strings"
"syscall/js"
"time"
"github.com/djhworld/gomeboycolor-wasm/webworker"
"github.com/djhworld/gomeboycolor/cartridge"
"github.com/djhworld/gomeboycolor/config"
"github.com/djhworld/gomeboycolor/gbc"
"github.com/djhworld/gomeboycolor/saves"
)
const TITLE string = "gomeboycolor"
var BUILD_DATE string
func main() {
fmt.Printf("%s\n", TITLE)
fmt.Printf("Built on: %s\n", BUILD_DATE)
fmt.Println("Copyright (c) 2013-2018. Daniel James Harper.")
fmt.Println("http://djhworld.github.io/gomeboycolor")
fmt.Println(strings.Repeat("*", 120))
var emulatorSetup *EmulatorSetup = new(EmulatorSetup)
webworker.SendLaunchOK()
awaitSetup(emulatorSetup)
emulator, err := gbc.Init(
emulatorSetup.cart,
emulatorSetup.saveStore,
emulatorSetup.config,
NewWebIO(
emulatorSetup.config.FrameRateLock,
emulatorSetup.config.Headless,
emulatorSetup.config.DisplayFPS,
),
)
if err != nil {
log.Fatal(err)
}
frameRunnerWrapper := func(doFrame func()) {
js.Global().Call("setTimeout", js.NewCallback(func(args []js.Value) {
doFrame()
}), 0)
time.Sleep(1 * time.Millisecond)
}
//Starts emulator code
go emulator.Run(frameRunnerWrapper)
//set the IO controller to run indefinitely (it waits for screen updates)
emulator.RunIO()
webworker.SendStopOK()
log.Println("Goodbye!")
}
func awaitSetup(emulatorSetup *EmulatorSetup) {
var done bool = false
var messageCB js.Callback
messageCB = js.NewCallback(func(args []js.Value) {
input := args[0].Get("data")
switch input.Index(0).String() {
case "init":
if err := emulatorSetup.handleInit(input.Index(1)); err != nil {
webworker.SendInitFailed(err)
} else {
webworker.SendInitOK()
}
case "get-game-id":
gameId := emulatorSetup.handleGetGameId()
webworker.SendGotGameId(gameId)
case "load-save":
emulatorSetup.handleLoadGame(input.Index(1))
webworker.SendLoadSaveOK()
case "start":
done = true
}
})
js.Global().Get("self").Call("addEventListener", "message", messageCB, false)
defer func() {
log.Println("Removing temporary message event handler")
js.Global().Get("self").Call("removeEventListener", "message", messageCB, false)
messageCB.Release()
}()
for !done {
time.Sleep(50 * time.Millisecond)
}
}
type EmulatorSetup struct {
saveStore saves.Store
cart *cartridge.Cartridge
config *config.Config
}
func (e *EmulatorSetup) handleInit(initData js.Value) error {
log.Println("Initialising")
initConfig := initData.Get("config")
e.config = &config.Config{
Title: TITLE,
ScreenSize: 1,
SkipBoot: initConfig.Get("skipBoot").Bool(),
DisplayFPS: initConfig.Get("showfps").Bool(),
ColorMode: initConfig.Get("colorMode").Bool(),
Debug: false,
BreakOn: "0x0000",
DumpState: false,
Headless: initConfig.Get("headless").Bool(),
FrameRateLock: int64(initConfig.Get("fpsLock").Int()),
}
romData := initData.Get("romData")
romName := romData.Get("name").String()
romBase64 := romData.Get("base64Bytes").String()
if romBytes, err := base64.StdEncoding.DecodeString(romBase64); err != nil {
return err
} else {
cart, err := cartridge.NewCartridge(romName, romBytes)
if err != nil {
return err
}
e.cart = cart
}
return nil
}
func (e *EmulatorSetup) handleGetGameId() string {
return e.cart.ID
}
func (e *EmulatorSetup) handleLoadGame(saveData js.Value) error {
webStore := NewWebStore()
e.saveStore = webStore
if saveDataStr := saveData.String(); saveDataStr != "" {
return webStore.PutSave(e.cart.ID, saveDataStr)
}
return nil
}