/
ffs.go
210 lines (188 loc) · 4.81 KB
/
ffs.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"syscall/js"
)
type RunState struct {
romLoaded bool
inDebug bool
waitingForKey bool
}
func newRunState() RunState {
rs := RunState{}
js.Global().Get("Chip8").Call("onRunStateInit", rs.toJsObj())
return rs
}
func (rs *RunState) setState(setter func(rs *RunState)) {
setter(rs)
js.Global().Get("Chip8").Call("onRunStateUpdate", rs.toJsObj())
}
func (rs *RunState) toJsObj() map[string]any {
rsObj := make(map[string]any)
rsObj["romLoaded"] = rs.romLoaded
rsObj["inDebug"] = rs.inDebug
rsObj["waitingForKey"] = rs.waitingForKey
return rsObj
}
type JsIO struct {
runState *RunState
keysPressed *[16]bool
lastPressed chan byte
lastReleased chan byte
}
func (jsIO JsIO) Draw(pixels Pixels) {
// convert Pixels type to []any type which JS can only support
pixelsJs := []any{}
for y, row := range pixels {
for x, col := range row {
if col == 1 {
pixelsJs = append(pixelsJs, []any{x, y})
}
}
}
js.Global().Get("Chip8").Call("draw", pixelsJs)
}
func (jsIO JsIO) ClearScreen() {
js.Global().Get("Chip8").Call("clearDisplay")
}
func (jsIO JsIO) WaitKeyPress() (byte, bool) {
select {
case <-jsIO.lastPressed:
jsCall := js.Global().Get("Chip8").Call("waitForKeyPress")
jsIO.runState.setState(func(rs *RunState) { rs.waitingForKey = true })
lastReleased := <-jsIO.lastReleased
jsCall.Call("onRelease", lastReleased)
return lastReleased, true
case <-jsIO.lastReleased: // get rid of any previous key that was pressed
return 0, false
default:
return 0, false
}
}
func (jsIO JsIO) GetKeysPressed() [16]bool {
return *jsIO.keysPressed
}
func setup() {
runState := newRunState()
runParams := RunParams{}
step := make(chan any, 1)
jsIO := JsIO{
runState: &runState,
lastPressed: make(chan byte, 1),
lastReleased: make(chan byte, 1),
}
var vm Vm
js.Global().Set("toggleDebug", js.FuncOf(func(this js.Value, args []js.Value) any {
runState.setState(func(rs *RunState) {
rs.inDebug = !rs.inDebug
if !rs.inDebug {
step <- true
}
})
return runState.inDebug
}))
js.Global().Set("nextInst", js.FuncOf(func(this js.Value, args []js.Value) any {
if runState.waitingForKey || !runState.romLoaded || vm.Done {
return nil
}
step <- true
return nil
}))
js.Global().Set("setKey", js.FuncOf(func(this js.Value, args []js.Value) any {
key := args[0].Int()
pressed := args[1].Bool()
jsIO.keysPressed[key] = pressed
// discard the value of either channel if they are already filled
select {
case <-jsIO.lastPressed:
case <-jsIO.lastReleased:
default:
}
if pressed {
jsIO.lastPressed <- byte(key)
} else {
jsIO.lastReleased <- byte(key)
}
return nil
}))
js.Global().Set("setInstsPerFrame", js.FuncOf(func(this js.Value, args []js.Value) any {
runParams.instCount = args[0].Int()
return nil
}))
// `createNewVm` will be called from JS-space and thus from another goroutine;
// better to keep everything in a single goroutine as much as possible so let's
// make a channel that will expect bytes coming from JS. In effect, this
// function will be a blocking one until `createNewVm` is called from JS.
rom := make(chan []byte, 1)
js.Global().Set("createNewVm", js.FuncOf(func(this js.Value, args []js.Value) any {
// args should be a Uint8Array in the JS-space;
// let's convert them to bytes that can be used by the VM
bytes := make([]byte, len(args))
for i, num := range args {
b := byte(num.Int())
bytes[i] = b
}
rom <- bytes
return nil
}))
loop := make(chan bool, 1)
for {
select {
case newRom := <-rom:
runState = newRunState()
jsIO.keysPressed = &[16]bool{}
newVm, err := NewVm(newRom, jsIO)
vm = newVm
if err != nil {
panic(err)
}
runState.setState(func(rs *RunState) { rs.romLoaded = true })
// start the run loop
select {
case loop <- true:
default: // if the loop channel has already been filled, do nothing
}
case <-loop:
commVmState := vmState(&vm)
if runState.inDebug {
<-step
runParams.instCount = 1
runParams.frameDuration = 1
commVmState()
}
if vm.Done {
fmt.Println("Program executed.")
continue
}
err := vm.Run(runParams)
if err != nil {
fmt.Println(err)
}
loop <- true
}
}
}
func vmState(vm *Vm) func() {
stack := make([]any, len(vm.Stack))
regs := make([]any, len(vm.Regs))
return func() {
for i, v := range vm.Stack {
stack[i] = v
}
for i, v := range vm.Regs {
regs[i] = v
}
state := make(map[string]any)
state["I"] = vm.I
state["Pc"] = vm.Pc
state["Sp"] = vm.Sp
state["DT"] = vm.DT
state["Stack"] = stack
state["Done"] = vm.Done
state["Regs"] = regs
byte1, byte2 := vm.Mem[vm.Pc], vm.Mem[vm.Pc+1]
inst, _ := getInstruction(byte1, byte2)
state["Assembly"] = vm.trace(byte1, byte2)(inst.assembly)
js.Global().Get("Chip8").Call("onVmUpdate", state)
}
}