-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.js
104 lines (91 loc) · 2.83 KB
/
Runner.js
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
(() => {
class Runner {
constructor(opts = {}) {
this.chip8 = new opts.chip8()
this.screen = new opts.screen()
this.input = new opts.input()
this.speaker = new opts.speaker()
this.intervalId = 0
this.fps = opts.fps || 10
this.resumeCb = () => {}
this.load = this.load.bind(this)
this.pause = this.pause.bind(this)
this.start = this.start.bind(this)
this.step = this.step.bind(this)
this.initEvents()
this.initInputs()
}
initEvents() {
this.chip8
.on('debug', ({ target, detail: { opName } }) => {
chip8.Debugger.trace({
opName,
pcAddr: target.pc.addr,
stack: target.stack,
v: target.v
})
})
.on('draw', ({ target, detail: { payload } }) => {
this.screen.draw({
...payload,
vMemory: target.vMemory,
})
})
.on('clear', () => {
this.screen.clear()
})
.on('wait', ({ detail: { payload } }) => {
this.resumeCb = (key) => {
payload.cb(key)
this.resumeCb = () => {}
this.resume()
}
this.pause()
})
.on('beepRun', () => {
console.log('beep')
this.speaker.play()
})
.on('beepStop', () => {
this.speaker.stop()
})
}
initInputs() {
this.input
.on('pause', () => {
this.pause()
})
.on('resume', () => {
this.resume()
})
.on('keyDown', ({ detail: { key }}) => {
this.chip8.setKey(key)
this.resumeCb(key)
})
.on('keyUp', ({ detail: { key }}) => {
this.chip8.unsetKey(key)
})
}
load(rom) {
this.pause()
this.chip8.reset()
this.screen.clear()
this.chip8.load(rom)
return Promise.resolve()
}
pause() {
clearInterval(this.intervalId)
}
resume() {
this.intervalId = setInterval(this.step, 16)
}
start() {
this.screen.clear()
this.resume()
}
step() {
this.chip8.step()
}
}
window.chip8.Runner = Runner
})()