-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (110 loc) · 3.5 KB
/
index.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
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
import init, { Emulator } from './static/wasm.js'
init().then(() => {
const evaluator = new Emulator()
evaluator.reset()
const input = document.getElementById('input')
const loadButton = document.getElementById('load')
const runButton = document.getElementById('run')
const resetButton = document.getElementById('reset')
const output = document.getElementById('output')
const memoryDump = document.getElementById('memorydump')
const range_start = document.getElementById('range_start')
const range_end = document.getElementById('range_end')
const dumpButton = document.getElementById('dump')
const stepButton = document.getElementById('step')
const clearButton = document.getElementById('clear')
const debug_output = document.getElementById('debug_output')
evaluator.set_cpu_debug_callback((msg) => {
debug_output.value += `[CPU Debug] : ${msg}\n`
debug_output.scrollTop = debug_output.scrollHeight
})
evaluator.set_memory_debug_callback((msg) => {
debug_output.value += `[Memory Debug] : ${msg}\n`
debug_output.scrollTop = debug_output.scrollHeight
})
evaluator.set_registers_debug_callback((msg) => {
debug_output.value += `[Register Debug] : ${msg}\n`
debug_output.scrollTop = debug_output.scrollHeight
})
input.value = `
LDA #$02
CMP #$01
BNE FOO
LDA #$01
STA $00
BRK
FOO:
LDA #$01
STA $01
BRK
`.trim()
const update_output = () => {
output.value = evaluator.cpu_status()
const start = parseInt(range_start.value, 16)
const end = parseInt(range_end.value, 16)
console.log(start, end)
if (isNaN(start) || isNaN(end)) {
memoryDump.value = 'Invalid range'
return
}
memoryDump.value = evaluator.memory_hexdump(start, end)
}
update_output()
loadButton.addEventListener('click', () => {
try {
evaluator.reset()
let input = document.getElementById('input').value
console.log(input)
let assembled = evaluator.assemble(input)
if (assembled.kind() === 1) {
update_output()
output.value = `Error: ${assembled.error()}`
} else {
evaluator.load(assembled.value())
update_output()
}
} catch (e) {
output.value = e
}
})
runButton.addEventListener('click', () => {
try {
evaluator.execute()
update_output()
} catch (e) {
output.value = e
}
})
resetButton.addEventListener('click', () => {
try {
evaluator.reset()
update_output()
} catch (e) {
output.value = e
}
})
dumpButton.addEventListener('click', () => {
try {
update_output()
} catch (e) {
output.value = e
}
})
stepButton.addEventListener('click', () => {
try {
evaluator.step()
update_output()
} catch (e) {
output.value = e
}
})
clearButton.addEventListener('click', () => {
debug_output.value = ''
})
document.getElementById('presets').addEventListener('change', (e) => {
const split = e.target.value.split(',')
document.getElementById('range_start').value = split[0]
document.getElementById('range_end').value = split[1]
update_output()
})
})