Skip to content

Commit 1c9acfb

Browse files
committed
Add preloadROM and setSpeed functions
1 parent cd08493 commit 1c9acfb

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

chip8.html

+46
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
console.error(err);
204204
} else {
205205
Module.callMain([file_name]);
206+
setSpeed();
206207
}
207208
});
208209
}
@@ -215,13 +216,58 @@
215216
}
216217
}
217218

219+
function preloadROM() {
220+
var searchParams = new URLSearchParams(window.location.search);
221+
var romUrl = searchParams.get("rom");
222+
if (romUrl == null)
223+
return;
224+
225+
var req = new XMLHttpRequest();
226+
req.open("GET", romUrl);
227+
req.responseType = "arraybuffer";
228+
req.onload = function() {
229+
if (this.status != 200)
230+
alert(this.status + " " + this.statusText + ": Error loading ROM from " + romUrl);
231+
232+
var contents = this.response;
233+
console.log(contents);
234+
var file_name = "chip8_rom";
235+
FS.writeFile(file_name, new Uint8Array(contents), {encoding: "binary"});
236+
237+
// Make sure any saved games are loaded before running
238+
FS.syncfs(true, function(err) {
239+
if (err) {
240+
console.error(err);
241+
} else {
242+
Module.callMain([file_name]);
243+
setSpeed();
244+
}
245+
});
246+
};
247+
req.send();
248+
249+
document.getElementById("rom").style.display = "none";
250+
document.getElementById("controls").style.display = "block";
251+
}
252+
218253
function updateSpeed() {
219254
var n = document.getElementById("speed").value;
220255
Module.ccall('set_instructions_per_step', 'void', '[number]', [n]);
221256
}
222257

258+
function setSpeed() {
259+
var searchParams = new URLSearchParams(window.location.search);
260+
var speed = searchParams.get("speed");
261+
if (speed == null)
262+
speed = "10";
263+
264+
document.getElementById("speed").value = speed;
265+
updateSpeed();
266+
}
267+
223268
document.getElementById("speed").addEventListener("input", updateSpeed, false);
224269
document.getElementById("rom").addEventListener("change", loadROM, false);
270+
window.addEventListener("load", preloadROM, false);
225271
</script>
226272
</body>
227273
</html>

0 commit comments

Comments
 (0)