|
203 | 203 | console.error(err);
|
204 | 204 | } else {
|
205 | 205 | Module.callMain([file_name]);
|
| 206 | + setSpeed(); |
206 | 207 | }
|
207 | 208 | });
|
208 | 209 | }
|
|
215 | 216 | }
|
216 | 217 | }
|
217 | 218 |
|
| 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 | + |
218 | 253 | function updateSpeed() {
|
219 | 254 | var n = document.getElementById("speed").value;
|
220 | 255 | Module.ccall('set_instructions_per_step', 'void', '[number]', [n]);
|
221 | 256 | }
|
222 | 257 |
|
| 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 | + |
223 | 268 | document.getElementById("speed").addEventListener("input", updateSpeed, false);
|
224 | 269 | document.getElementById("rom").addEventListener("change", loadROM, false);
|
| 270 | + window.addEventListener("load", preloadROM, false); |
225 | 271 | </script>
|
226 | 272 | </body>
|
227 | 273 | </html>
|
0 commit comments