-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
325 lines (291 loc) · 15.1 KB
/
Copy pathmain.js
File metadata and controls
325 lines (291 loc) · 15.1 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* References:
* 1. https://www.html5rocks.com/en/tutorials/file/dndfiles/
* - Used for reading in files
* 2. https://blog.teamtreehouse.com/reading-files-using-the-html5-filereader-api
* - Used for reading in files
*/
let stepBackward = new Array();
var runEmulator = null;
var pauseflag = false;
var loadFlag = null;
var logToggle = false;
function main(usrFile) {
document.getElementById("runTest").onclick = function () { runTest()};
document.getElementById("startEmulator").onclick = function () {
if (loadFlag != false) {
startEmulator(usrFile);
loadFlag = false;
document.getElementById("startEmulator").classList.add("emulatorRunning");
if (logToggle) {
chip.logToggle = true;
}
}
};
document.getElementById("logToggle").onclick = function () { toggleLog()};
}
function pushThisChip() {
if (stepBackward.length > 200) {
stepBackward.shift();
}
let newChip = new Chip8();
newChip = chip.deepCopy(newChip);
stepBackward.push(newChip);
}
function callSetInt(){
runEmulator =
setInterval(function() {
if (chip.getWaitKeyFlag() == true || pauseflag == true) {
window.clearInterval(runEmulator);
}
else {
chip.runEmulator();
}
}, 1);
}
function startEmulator(usrFile) {
chip = new Chip8();
chip.reset();
document.getElementById("log").innerHTML = "";
if (usrFile && !snakeOK && !floppyOK) {
chip.loadProgram(prog);
}
if (snakeOK) {
//snake program hardcoded.
chip.program = [0x13, 0x6a, 0xa3, 0x25, 0xd4, 0x51, 0x3f, 0x01, 0x00, 0xee, 0x54, 0x80, 0x12, 0xde, 0x55, 0x90
, 0x12, 0xde, 0x60, 0x02, 0xf0, 0x18, 0x22, 0xc6, 0x76, 0x01, 0x22, 0xc6, 0x60, 0xff, 0x61, 0xff
, 0x7b, 0xfe, 0xa3, 0x90, 0xfb, 0x1e, 0xf1, 0x55, 0x7b, 0xfe, 0xa3, 0x90, 0xfb, 0x1e, 0xf1, 0x55
, 0xa3, 0x25, 0xd4, 0x51, 0x12, 0xa0, 0x00, 0xee, 0x80, 0x70, 0xb2, 0x5a, 0x00, 0xee, 0x60, 0x05
, 0xe0, 0xa1, 0x67, 0x00, 0x60, 0x07, 0xe0, 0xa1, 0x67, 0x0c, 0x60, 0x08, 0xe0, 0xa1, 0x67, 0x08
, 0x60, 0x09, 0xe0, 0xa1, 0x67, 0x04, 0x00, 0xee, 0x00, 0xee, 0x75, 0xff, 0x00, 0xee, 0x74, 0x01
, 0x00, 0xee, 0x75, 0x01, 0x00, 0xee, 0x74, 0xff, 0x00, 0xee, 0x00, 0xee, 0xa3, 0x90, 0x7a, 0x02
, 0xfa, 0x1e, 0x80, 0x40, 0x81, 0x50, 0xf1, 0x55, 0x00, 0xee, 0x00, 0xee, 0xa3, 0x90, 0xfb, 0x1e
, 0xf1, 0x65, 0xa3, 0x25, 0xd0, 0x11, 0x7b, 0x02, 0x00, 0xee, 0x00, 0xee, 0x44, 0xff, 0x12, 0xde
, 0x44, 0x40, 0x12, 0xde, 0x45, 0xff, 0x12, 0xde, 0x45, 0x20, 0x12, 0xde, 0x00, 0xee, 0x00, 0xee
, 0xc8, 0x3f, 0xc9, 0x1f, 0x60, 0x07, 0x80, 0x97, 0x3f, 0x00, 0x12, 0xb8, 0x60, 0x36, 0x80, 0x87
, 0x3f, 0x01, 0x12, 0xb8, 0x12, 0xa0, 0x00, 0xee, 0xa3, 0x25, 0xd8, 0x91, 0x4f, 0x00, 0x00, 0xee
, 0xd8, 0x91, 0x12, 0xa0, 0x00, 0xee, 0xa3, 0x28, 0xf6, 0x33, 0xf2, 0x65, 0x60, 0x37, 0x63, 0x00
, 0xf1, 0x29, 0xd0, 0x35, 0x70, 0x05, 0xf2, 0x29, 0xd0, 0x35, 0x00, 0xee, 0x00, 0xee, 0x60, 0x0f
, 0xf0, 0x18, 0x22, 0xe8, 0x23, 0x1e, 0x00, 0xee, 0x00, 0xe0, 0x60, 0x0f, 0x61, 0x08, 0xa3, 0x2b
, 0xd0, 0x17, 0x70, 0x08, 0xa3, 0x34, 0xd0, 0x17, 0x70, 0x08, 0xa3, 0x3d, 0xd0, 0x17, 0x70, 0x08
, 0xa3, 0x46, 0xd0, 0x17, 0x60, 0x0f, 0x71, 0x08, 0xa3, 0x4f, 0xd0, 0x17, 0x70, 0x08, 0xa3, 0x58
, 0xd0, 0x17, 0x70, 0x08, 0xa3, 0x46, 0xd0, 0x17, 0x70, 0x08, 0xa3, 0x61, 0xd0, 0x17, 0x13, 0x1e
, 0x00, 0xee, 0xe0, 0x00, 0xee, 0x80, 0x00, 0xee, 0xc0, 0x00, 0x00, 0x7e, 0x42, 0x40, 0x66, 0x62
, 0x62, 0x7e, 0x00, 0xee, 0x3c, 0x24, 0x24, 0x7e, 0x62, 0x62, 0x62, 0x00, 0xee, 0x7f, 0x49, 0x49
, 0x6d, 0x6d, 0x6d, 0x6d, 0x00, 0xee, 0x7e, 0x40, 0x40, 0x7c, 0x60, 0x60, 0x7e, 0x00, 0xee, 0x7e
, 0x46, 0x46, 0x42, 0x42, 0x42, 0x7e, 0x00, 0xee, 0x61, 0x61, 0x61, 0x61, 0x23, 0x22, 0x3e, 0x00
, 0xee, 0x7e, 0x42, 0x42, 0x7f, 0x61, 0x61, 0x61, 0x00, 0xee, 0x6a, 0x08, 0x6b, 0x04, 0x66, 0x00
, 0x67, 0x04, 0xa3, 0x90, 0xfb, 0x1e, 0xf5, 0x65, 0xa3, 0x22, 0xd0, 0x11, 0x22, 0xc6, 0x22, 0xa0
, 0x22, 0x3e, 0x22, 0x38, 0x22, 0x8c, 0x22, 0x6c, 0x22, 0x02, 0x22, 0x7c, 0x13, 0x80, 0x00, 0xee
, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0b, 0x0a, 0x0c, 0x0a, 0x00, 0xee ];
chip.loadProgram(chip.program);
} else if(floppyOK) {
//flappy bird hardcoded
chip.program = [0x14, 0x81, 0xF0, 0x90, 0xF7, 0x84, 0x84, 0xEF, 0x88, 0xEF, 0x81, 0xEF, 0xF0, 0x80, 0xF0, 0x10
, 0xF0, 0xC3, 0xC3, 0xDB, 0xDB, 0xFF, 0xFF, 0x81, 0x81, 0x81, 0x81, 0x8F, 0x81, 0x81, 0x81, 0x81
, 0x8F, 0x89, 0x89, 0x89, 0xF9, 0xFF, 0x02, 0x02, 0x02, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04
, 0x04, 0x04, 0x04, 0x04, 0xFF, 0x01, 0x01, 0x01, 0x81, 0x01, 0x01, 0x01, 0x01, 0x71, 0x01, 0x01
, 0x01, 0x01, 0x01, 0xFF, 0x01, 0x01, 0x01, 0x11, 0x11, 0x11, 0x01, 0x01, 0x01, 0x01, 0x1F, 0x11
, 0x11, 0xF1, 0xFF, 0x11, 0x11, 0x11, 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xE1, 0x01, 0x01
, 0xFF, 0xFF, 0x81, 0x81, 0x81, 0x99, 0x81, 0x81, 0x83, 0x81, 0x81, 0x81, 0x99, 0x81, 0x81, 0xFF
, 0xF0, 0x10, 0x10, 0xF0, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xF0, 0xFF
, 0x01, 0x01, 0x01, 0x11, 0x11, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xF0, 0xFF, 0x11
, 0x11, 0x11, 0xF1, 0x81, 0x81, 0x81, 0x91, 0x91, 0x91, 0x81, 0x81, 0x81, 0xFF, 0xC0, 0xC0, 0xC0
, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xEF, 0x89, 0x8F, 0xA9
, 0xE9, 0xDB, 0xAA, 0xAB, 0x8A, 0x8B, 0x4A, 0xAA, 0xAA, 0xAA, 0x44, 0xEF, 0x89, 0xEE, 0x89, 0xE9
, 0x00, 0x00, 0x00, 0xF0, 0x15, 0xF0, 0x07, 0x30, 0x00, 0x12, 0xC5, 0x00, 0xEE, 0x60, 0x00, 0x61
, 0x01, 0xA2, 0x16, 0xD0, 0x1F, 0x70, 0x08, 0xA2, 0x25, 0xD0, 0x1F, 0x70, 0x08, 0xA2, 0x34, 0xD0
, 0x1F, 0x70, 0x08, 0xA2, 0x43, 0xD0, 0x1F, 0x70, 0x08, 0xD0, 0x1F, 0x70, 0x08, 0xA2, 0x52, 0xD0
, 0x1F, 0x71, 0x09, 0x70, 0x08, 0xA4, 0x91, 0xD0, 0x1F, 0x70, 0x08, 0xA4, 0xA0, 0xD0, 0x1F, 0x60
, 0x01, 0x61, 0x10, 0xA2, 0x61, 0xD0, 0x1F, 0x70, 0x08, 0xA2, 0x70, 0xD0, 0x1F, 0x70, 0x04, 0xA2
, 0x7F, 0xD0, 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, 0x05, 0x33, 0x3F, 0x13, 0xC7, 0x63
, 0x2C, 0x64, 0x00, 0xA2, 0x9D, 0xD3, 0x42, 0x74, 0x02, 0x34, 0x20, 0x13, 0xD5, 0x23, 0x53, 0x23
, 0x59, 0xA4, 0xAF, 0xDE, 0xD7, 0xA2, 0x9D, 0x00, 0xEE, 0x64, 0x0D, 0x65, 0x00, 0xA2, 0xC0, 0xF2
, 0x65, 0x63, 0x30, 0xF0, 0x29, 0xD3, 0x45, 0x73, 0x05, 0xF1, 0x29, 0xD3, 0x45, 0x73, 0x05, 0xF2
, 0x29, 0xD3, 0x45, 0x75, 0x01, 0xA2, 0xC0, 0xFC, 0x33, 0x35, 0x02, 0x13, 0xEF, 0xA2, 0x9D, 0x00
, 0xEE, 0x00, 0xE0, 0x60, 0x1D, 0x61, 0x05, 0x3C, 0xFF, 0x23, 0x37, 0x6E, 0x00, 0x6F, 0x00, 0x6D
, 0x00, 0x60, 0x78, 0x22, 0xC3, 0xF0, 0x0A, 0x00, 0xE0, 0x00, 0xEE, 0x23, 0x59, 0x61, 0x02, 0x86
, 0x15, 0x23, 0x59, 0x46, 0x00, 0x23, 0x59, 0x46, 0x00, 0x23, 0x53, 0x46, 0x28, 0x23, 0x59, 0xA4
, 0xAF, 0xDE, 0xD7, 0x61, 0x05, 0x62, 0x01, 0xE1, 0x9E, 0x8D, 0x24, 0xE1, 0xA1, 0x8D, 0x25, 0xDE
, 0xD7, 0xA2, 0x9D, 0x4F, 0x01, 0x24, 0x11, 0x4D, 0x1F, 0x24, 0x11, 0x4D, 0xFF, 0x24, 0x11, 0x4C
, 0xFF, 0x24, 0x11, 0x9E, 0x60, 0x7C, 0x01, 0x00, 0xEE, 0x00, 0xE0, 0x23, 0xB7, 0x89, 0xC0, 0x24
, 0x2B, 0x4E, 0x00, 0x00, 0xEE, 0x59, 0xC0, 0x23, 0xE9, 0x60, 0x02, 0x22, 0xC3, 0x14, 0x6D, 0x00
, 0xEE, 0x22, 0xCD, 0x23, 0x1B, 0x60, 0x08, 0x22, 0xC3, 0x63, 0x05, 0xE3, 0xA1, 0x24, 0x69, 0x14
, 0x83, 0x00, 0x00, 0x07, 0x38, 0x21, 0x21, 0x71, 0x88, 0x84, 0x84, 0x88, 0x70, 0x20, 0x1F, 0x00
, 0x00, 0x00, 0xE0, 0x90, 0x08, 0x14, 0x14, 0x84, 0x7E, 0x01, 0x3E, 0x02, 0x3C, 0xE0, 0x00, 0x8C
, 0xDE, 0xFB, 0xFE, 0x7E, 0x1C, 0x14, 0x00, 0xFF, 0x83, 0x83, 0xC7, 0x46, 0x46, 0x46, 0x46, 0x46
, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0xC7
, 0x83, 0x83, 0xFF, 0x00];
chip.loadProgram(chip.program);
}
callSetInt();
}
//If click pause -> clear setinterval
document.getElementById("pause").onclick = function() {
if (pauseflag === false) {
chip.updateHTMLLogMessage("Emulator Paused");
//for the UI
document.getElementById("resume").classList.add("stepControlActive");
document.getElementById("stepForward").classList.add("stepControlActive");
document.getElementById("stepBack").classList.add("stepControlActive");
document.getElementById("resume").classList.remove("stepControlInactive");
document.getElementById("stepBack").classList.remove("stepControlInactive");
document.getElementById("stepForward").classList.remove("stepControlInactive");
pauseflag = true;
}
};
//If click resume -> run emulator is true
document.getElementById("resume").onclick = function() {
if (pauseflag == true){
callSetInt();
chip.updateHTMLLogMessage("Emulator Resumed");
//for the UI
document.getElementById("resume").classList.remove("stepControlActive");
document.getElementById("stepForward").classList.remove("stepControlActive");
document.getElementById("stepBack").classList.remove("stepControlActive");
document.getElementById("resume").classList.add("stepControlInactive");
document.getElementById("stepBack").classList.add("stepControlInactive");
document.getElementById("stepForward").classList.add("stepControlInactive");
pauseflag = false;
}
};
//If click step forward -> move forward one opcode
document.getElementById("stepForward").onclick = function() {
if (pauseflag === false) return;
chip.runEmulator();
chip.updateHTMLLogMessage("Stepped Forward");
};
document.getElementById("stepBack").onclick = function() {
if (pauseflag === false) return;
window.clearInterval(runEmulator);
chip.updateHTMLLogMessage("Stepped Backwards");
stepBackward.pop(); //get rid of one (cause we run through one at the end)
let otherChip = stepBackward.pop();
console.log(otherChip.register.toString());
console.log(otherChip.stack.toString());
console.log(otherChip.programCounter.toString(16));
this.chip = otherChip.deepCopy(chip);
pauseflag = true;
chip.runEmulator(); //run that once (updates emulator every time you step back)
};
var translateKeys = {
49: 1,
50: 2,
51: 3,
52: 12,
81: 4,
87: 5,
69: 6,
82: 13,
65: 7,
83: 8,
68: 9,
70: 14,
90: 10,
88: 0,
67: 11,
86: 15,
};
document.addEventListener("keydown", function(event) {
chip.keydown(translateKeys[event.keyCode]);
if (chip.getWaitKeyFlag() == true && pauseflag == false) {
chip.runEmulator();
callSetInt();
}
});
document.addEventListener("keyup", function(event) {
chip.keyup(translateKeys[event.keyCode]);
});
function runTest() {
opCoTest();
}
function toggleLog() {
if (!logToggle) {
//turns on
logToggle = true;
if (chip!= null) {
chip.logToggle = true;
document.getElementById("logToggle").innerText = "Toggle Log Off";
document.getElementById("log").innerHTML = "";
chip.logCount = 0;
}
} else {
//turns off
logToggle = false;
if (chip!= null) {
chip.logToggle = false;
document.getElementById("logToggle").innerText = "Toggle Log On";
chip.updateHTMLLogMessage("Click on \"Toggle Log On\" to bring back the logs");
document.getElementById("log").removeChild(document.getElementById("log").lastElementChild);
}
}
}
//function to update game rom name and input to snake or flappy bird when select is chosen
let snakeOK, floppyOK = false;
function updateGameFromSelect() {
if (snakeOK || floppyOK)window.clearInterval(runEmulator);
snakeOK = false;
floppyOK = false;
var lol = document.getElementById("games");
if (lol.options[lol.selectedIndex].value !== "default") {
if (lol.options[lol.selectedIndex].value === "snake") {
//play snake
snakeOK = true;
alert("Snake has been loaded, please press \"Start Emulation\"");
document.getElementById("fileUploadBox").innerText = "Snake Clone loaded";
} else {
//play floppy
floppyOK = true;
alert("Flappy Bird has been loaded, please press \"Start Emulation\"");
document.getElementById("fileUploadBox").innerText = "Flappy Bird Clone loaded";
}
}
loadFlag = true; //makes sure runEmulator only runs once
pauseflag = false; //resets the pause flag when new game is put in
document.getElementById("startEmulator").classList.remove("emulatorRunning");
main(true); //call main, with true boolean to show it should load a file
}
let inputElement = document.getElementById("file");
inputElement.addEventListener("change", handleFiles, false);
let file;
let prog;
function handleFiles() {
window.clearInterval(runEmulator);
file = this.files[0];
let reader = new FileReader();
let result;
reader.onload = function(event) {
result = event.target.result; //result = contents of file
result = result.replace(/\n/g, " "); //replace any newline characters with spaces
let arr = result.split(" "); //string to array, split by spaces
prog = new Uint8Array(arr.length*2); //Array to hold program
let j = 0;
for (let i = 0; j <= arr.length; i += 2) { //loop to split opcodes into 1 byte
prog[i] = (parseInt(arr[j], 16) & 0xFF00) >>> 8; //first byte (parse int --> convert string to int)
prog[i+1] = (parseInt(arr[j], 16) & 0x00FF); //second byte
j++;
}
alert("Your file has been loaded, please press \"Start Emulation\"");
//input name
var fileElement = document.getElementById('file');
var filename = fileElement.files[0].name;
document.getElementById("fileUploadBox").innerText = filename + " uploaded";
loadFlag = true; //makes sure runEmulator only runs once
pauseflag = false; //resets the pause flag when new game is put in
document.getElementById("startEmulator").classList.remove("emulatorRunning");
main(true); //call main, with true boolean to show it should load a file
};
reader.readAsBinaryString(file);
}
main(false);