-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
135 lines (113 loc) · 3.22 KB
/
test.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
133
134
135
var Hack = require('./index')
window.onload = () => {
// Hack special keys compared to ascii
var keys = {
13: 128,
8: 129,
37: 130,
38: 131,
39: 132,
40: 133,
36: 134,
35: 135,
33: 136,
24: 137,
45: 138,
46: 139,
27: 140,
112: 141,
113: 142,
114: 143,
115: 144,
116: 145,
117: 146,
118: 147,
119: 148,
120: 149,
121: 150,
122: 151,
123: 152
}
function handkeKeyDown(e) {
var keyCode = parseInt(e.keyCode)
if (keyCode in keys) {
keyCode = keys[keyCode]
}
hack.RAM[24576] = keyCode
}
function handkeKeyUp() {
hack.RAM[24576] = 0
}
var hack
var intervalID
var running = false
var opcodes = parseInt(document.getElementById('opcodes').value)
var milli = parseInt(document.getElementById('milli').value)
document.getElementById('opcodes').addEventListener('input', function (e) {
opcodes = this.value
})
document.getElementById('milli').addEventListener('input', function (e) {
milli = this.value
})
document.getElementById("run").addEventListener("click", () => {
if (running) {
window.removeEventListener("keydown", handkeKeyDown, true)
window.removeEventListener("keyup", handkeKeyUp, true)
stopAnimation()
clearInterval(intervalID)
running = false
}
var asm = document.getElementById('asm').value
setupHack(asm)
})
document.getElementById("stop").addEventListener("click", () => {
if (running) {
window.removeEventListener("keydown", handkeKeyDown, true)
window.removeEventListener("keyup", handkeKeyUp, true)
stopAnimation()
clearInterval(intervalID)
hack = null
running = false
}
})
function setupHack(asm) {
if (running) {
return
}
hack = new Hack()
hack.CANVAS = document.getElementById("screen")
hack.CANVAS_CTX = hack.CANVAS.getContext("2d")
hack.CANVAS_DATA = hack.CANVAS_CTX.getImageData(0, 0, hack.CANVAS.width, hack.CANVAS.height)
hack.loadROM(asm)
hack.debug = 0
intervalID = setInterval(function () {
for (var i = 0; i < opcodes; i++) {
hack.cycle()
}
if (hack.cyclesDone % 100000 == 0) {
document.getElementById('numOpcodes').innerHTML = hack.cyclesDone
}
}, milli)
window.addEventListener("keydown", handkeKeyDown)
window.addEventListener("keyup", handkeKeyUp)
startAnmation()
running = true
}
var requestId
function loopAnimation(time) {
requestId = undefined
hack.updateCanvas()
startAnmation();
}
function startAnmation() {
if (!requestId) {
requestId = requestAnimationFrame(loopAnimation);
}
}
function stopAnimation() {
if (requestId) {
cancelAnimationFrame(requestId);
requestId = undefined;
}
}
}