Newer
Older
import { default as wasm, Chip8Neo, Chip8Classic } from "./lib/chip_ahoyto.js";
import info from "./package.json";
const PIXEL_SET_COLOR = 0x50cb93ff;
const PIXEL_UNSET_COLOR = 0x1b1a17ff;
const LOGIC_HZ = 600;
const VISUAL_HZ = 60;
const TIMER_HZ = 60;
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
const FREQUENCY_DELTA = 60;
const DISPLAY_WIDTH = 64;
const DISPLAY_HEIGHT = 32;
const DISPLAY_RATIO = DISPLAY_WIDTH / DISPLAY_HEIGHT;
const SAMPLE_RATE = 2;
const SOUND_DATA =
"data:audio/mpeg;base64,SUQzAwAAAAAAJlRQRTEAAAAcAAAAU291bmRKYXkuY29tIFNvdW5kIEVmZmVjdHMA//uSwAAAAAABLBQAAAL6QWkrN1ADDCBAACAQBAQECQD//2c7OmpoX/btmzIxt4R/7tmdKRqBVldEDICeA2szOT5E0ANLDoERvAwYDvXUwGPgUBhQVAiIAGFQb9toDBQAwSGwMLgECIPAUE/7v4YoAwyHQMSh8BgNl0r//5ofWmt///4swTaBg0CgSAgNoClQMSAwCgBAwiA//t9/GRFBlcXORYXAN8ZQggBgCACBH////4WYFjpmaRcLZcYggswUoBgEEgYPBf////////+VwfOBAwA7llUiIABQAAAgAAAEBgUARBzKEVmNPo26GUFGinz0RnZcAARtaVqlvTwGDx8BvHbgkEQMtcYIQgBjzkgaETYGFhuAEeRQ5m4ZcMEAsmKArYXE7qZFkXGOGkI5L4yqTIqRZNK45ociBkoKE6brSDUgMNi8mkJqHfAwaMBz11/t23+yEgox4FicKWLheWtJMWkAYIGpvvKwpgAQBJxVki+QFZOmhfJkQWCICACENuqdNB1Ba39WSI1wxkIsPSalHkFsZloPyHLBoEwssSa3Xf/7ksBnABz9nUn5qoACZTMov7FQAGsyLZRDwG7X+vJcfAjUzWVJMUz/DadX/DPVVPT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IAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAA";
const BACKGROUNDS = [
"264653",
"1b1a17",
"023047",
"bc6c25",
"283618",
"2a9d8f",
"3a5a40"
];
const KEYS: Record<string, number> = {
"1": 0x01,
"2": 0x02,
"3": 0x03,
"4": 0x0c,
q: 0x04,
w: 0x05,
e: 0x06,
r: 0x0d,
a: 0x07,
s: 0x08,
d: 0x09,
f: 0x0e,
z: 0x0a,
x: 0x00,
c: 0x0b,
v: 0x0f
};
// @ts-ignore: ts(2580)
const ROM_PATH = require("../../res/roms/pong.ch8");
type State = {
chip8: Chip8Neo | Chip8Classic;
engine: string;
logicFrequency: number;
visualFrequency: number;
timerFrequency: number;
canvas: HTMLCanvasElement;
canvasScaled: HTMLCanvasElement;
canvasCtx: CanvasRenderingContext2D;
canvasScaledCtx: CanvasRenderingContext2D;
image: ImageData;
videoBuff: DataView;
toastTimeout: number;
paused: boolean;
background_index: number;
nextTickTime: number;
fps: number;
frameStart: number;
frameCount: number;
romName: string;
romData: Uint8Array;
romSize: number;
};
type Global = {
modalCallback: Function;
};
const state: State = {
chip8: null,
engine: null,
logicFrequency: LOGIC_HZ,
visualFrequency: VISUAL_HZ,
timerFrequency: TIMER_HZ,
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
canvas: null,
canvasScaled: null,
canvasCtx: null,
canvasScaledCtx: null,
image: null,
videoBuff: null,
toastTimeout: null,
paused: false,
background_index: 0,
nextTickTime: 0,
fps: 0,
frameStart: new Date().getTime(),
frameCount: 0,
romName: null,
romData: null,
romSize: 0
};
const global: Global = {
modalCallback: null
};
const sound = ((data = SOUND_DATA, volume = 0.2) => {
const sound = new Audio(data);
sound.volume = volume;
sound.muted = true;
return sound;
})();
// initializes the WASM module, this is required
// so that the global symbols become available
await wasm();
// initializes the complete set of sub-systems
// and registers the event handlers
await init();
await register();
// start the emulator subsystem with the initial
// ROM retrieved from a remote data source
await start({ loadRom: true });
// runs the sequence as an infinite loop, running
// the associated CPU cycles accordingly
while (true) {
// in case the machin is paused we must delay the execution
// a little bit until the paused state is recovered
if (state.paused) {
await new Promise((resolve) => {
setTimeout(resolve, 1000 / state.idleFrequency);
// obtains the current time, this value is going
// to be used to compute the need for tick computation
let currentTime = new Date().getTime();
try {
tick(currentTime);
} catch (err) {
// sets the default error message to be displayed
// to the user
let message = String(err);
// verifies if the current issue is a panic one
// and updates the message value if that's the case
const messageNormalized = (err as Error).message.toLowerCase();
const isPanic =
messageNormalized.startsWith("unreachable") ||
messageNormalized.startsWith("recursive use of an object");
message = "Unrecoverable error, restarting CHIP-8";
// displays the error information to both the end-user
// and the developer (for dianostics)
showToast(message, true, 5000);
console.error(err);
// pauses the machine, allowing the end-user to act
// on the error in a proper fashion
pause();
// if we're talking about a panic proper action must be taken
// which in this case it means restarting both the WASM sub
// system and the machine state (to be able to recover)
// also sets the default color on screen to indicate the issue
if (isPanic) {
await clearCanvas(undefined, {
// @ts-ignore: ts(2580)
image: require("./res/storm.png"),
imageScale: 0.4
});
await wasm();
await start({ restore: false });
}
}
// calculates the amount of time until the next draw operation
// this is the amount of time that is going to be pending
currentTime = new Date().getTime();
const pendingTime = Math.max(state.nextTickTime - currentTime, 0);
// waits a little bit for the next frame to be draw,
// this should control the flow of render
await new Promise((resolve) => {
setTimeout(resolve, pendingTime);
});
}
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
};
const tick = (currentTime: number) => {
// in case the time to draw the next frame has not been
// reached the flush of the "tick" logic is skiped
if (currentTime < state.nextTickTime) return;
// initializes the flag that is going to control is a beep
// is going to be issued
let beepFlag = false;
// calculates the number of ticks that have elapsed since the
// last draw operation, this is critical to be able to properly
// operate the clock of the CPU in frame drop situations
if (state.nextTickTime === 0) state.nextTickTime = currentTime;
let ticks = Math.ceil(
(currentTime - state.nextTickTime) /
((1 / state.visualFrequency) * 1000)
);
ticks = Math.max(ticks, 1);
const ratioLogic = (state.logicFrequency / state.visualFrequency) * ticks;
for (let i = 0; i < ratioLogic; i++) {
state.chip8.clock_ws();
}
const ratioTimer = (state.timerFrequency / state.visualFrequency) * ticks;
for (let i = 0; i < ratioTimer; i++) {
state.chip8.clock_dt_ws();
state.chip8.clock_st_ws();
beepFlag ||= state.chip8.beep_ws();
}
// in case the beep flag is active issue a sound during a bried
// period, to notify the user about a certain event
if (beepFlag) beep();
// updates the canvas object with the new
// visual information coming in
updateCanvas(state.chip8.vram_ws());
// increments the number of frames rendered in the current
// section, this value is going to be used to calculate FPS
state.frameCount += 1;
// in case the target number of frames for FPS control
// has been reached calculates the number of FPS and
// flushes the value to the screen
if (state.frameCount === state.visualFrequency * SAMPLE_RATE) {
const currentTime = new Date().getTime();
const deltaTime = (currentTime - state.frameStart) / 1000;
const fps = Math.round(state.frameCount / deltaTime);
setFps(fps);
state.frameCount = 0;
state.frameStart = currentTime;
}
// updates the next update time reference to the, so that it
// can be used to control the game loop
state.nextTickTime += (1000 / state.visualFrequency) * ticks;
};
const start = async ({
engine = "neo",
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
loadRom = false,
romPath = ROM_PATH,
romName = null as string,
romData = null as Uint8Array
} = {}) => {
// in case a remote ROM loading operation has been
// requested then loads it from the remote origin
if (loadRom) {
[romName, romData] = await fetchRom(romPath);
} else if (romName === null || romData === null) {
[romName, romData] = [state.romName, state.romData];
}
// selects the proper engine for execution
// and builds a new instance of it
switch (engine) {
case "neo":
state.chip8 = new Chip8Neo();
break;
case "classic":
state.chip8 = new Chip8Classic();
break;
default:
if (!state.chip8) {
throw new Error("No engine requested");
}
break;
}
// resets the CHIP-8 engine to restore it into
// a valid state ready to be used
state.chip8.reset_hard_ws();
state.chip8.load_rom_ws(romData);
// updates the name of the currently selected engine
// to the one that has been provided (logic change)
if (engine) state.engine = engine;
// updates the complete set of global information that
// is going to be displayed
setEngine(state.engine);
setRom(romName, romData);
setLogicFrequency(state.logicFrequency);
setFps(state.fps);
// in case the restore (state) flag is set
// then resumes the machine execution
if (restore) resume();
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
};
const register = async () => {
await Promise.all([
registerDrop(),
registerKeys(),
registerButtons(),
registerKeyboard(),
registerCanvas(),
registerToast(),
registerModal()
]);
};
const init = async () => {
await Promise.all([initBase(), initCanvas()]);
};
const registerDrop = () => {
document.addEventListener("drop", async (event) => {
if (
!event.dataTransfer.files ||
event.dataTransfer.files.length === 0
) {
return;
}
event.preventDefault();
event.stopPropagation();
const overlay = document.getElementById("overlay");
overlay.classList.remove("visible");
const file = event.dataTransfer.files[0];
if (!file.name.endsWith(".ch8")) {
showToast("This is probably not a CHIP-8 ROM file!", true);
return;
}
const arrayBuffer = await file.arrayBuffer();
const romData = new Uint8Array(arrayBuffer);
start({ engine: null, romName: file.name, romData: romData });
showToast(`Loaded ${file.name} ROM successfully!`);
});
document.addEventListener("dragover", async (event) => {
if (!event.dataTransfer.items || event.dataTransfer.items[0].type)
return;
event.preventDefault();
const overlay = document.getElementById("overlay");
overlay.classList.add("visible");
});
document.addEventListener("dragenter", async (event) => {
if (!event.dataTransfer.items || event.dataTransfer.items[0].type)
return;
const overlay = document.getElementById("overlay");
overlay.classList.add("visible");
});
document.addEventListener("dragleave", async (event) => {
if (!event.dataTransfer.items || event.dataTransfer.items[0].type)
return;
const overlay = document.getElementById("overlay");
overlay.classList.remove("visible");
});
};
const registerKeys = () => {
document.addEventListener("keydown", (event) => {
const keyCode = KEYS[event.key];
if (keyCode !== undefined) {
state.chip8.key_press_ws(keyCode);
return;
}
switch (event.key) {
case "+":
setLogicFrequency(state.logicFrequency + FREQUENCY_DELTA);
break;
case "-":
setLogicFrequency(state.logicFrequency - FREQUENCY_DELTA);
break;
case "Escape":
minimize();
break;
}
});
document.addEventListener("keyup", (event) => {
const keyCode = KEYS[event.key];
if (keyCode !== undefined) {
state.chip8.key_lift_ws(keyCode);
return;
}
});
};
const registerButtons = () => {
const engine = document.getElementById("engine");
engine.addEventListener("click", () => {
const name = state.engine == "neo" ? "classic" : "neo";
start({ engine: name });
showToast(
`CHIP-8 running in engine "${name.toUpperCase()}" from now on!`
);
});
const logicFrequencyPlus = document.getElementById("logic-frequency-plus");
logicFrequencyPlus.addEventListener("click", () => {
setLogicFrequency(state.logicFrequency + FREQUENCY_DELTA);
});
const logicFrequencyMinus = document.getElementById(
"logic-frequency-minus"
);
logicFrequencyMinus.addEventListener("click", () => {
setLogicFrequency(state.logicFrequency - FREQUENCY_DELTA);
});
const buttonPause = document.getElementById("button-pause");
buttonPause.addEventListener("click", () => {
toggleRunning();
});
const buttonReset = document.getElementById("button-reset");
buttonReset.addEventListener("click", () => {
reset();
});
const buttonBenchmark = document.getElementById("button-benchmark");
buttonBenchmark.addEventListener("click", async () => {
const result = await showModal(
"Are you sure you want to start a benchmark?\nThe benchmark is considered an expensive operation!",
"Confirm"
);
if (!result) return;
buttonBenchmark.classList.add("enabled");
pause();
try {
const initial = Date.now();
const count = 500000000;
for (let i = 0; i < count; i++) {
state.chip8.clock_ws();
}
const delta = (Date.now() - initial) / 1000;
const frequency_mhz = count / delta / 1000 / 1000;
showToast(
`Took ${delta.toFixed(
2
)} seconds to run ${count} ticks (${frequency_mhz.toFixed(
2
)} Mhz)!`,
undefined,
7500
);
} finally {
resume();
buttonBenchmark.classList.remove("enabled");
}
});
const buttonFullscreen = document.getElementById("button-fullscreen");
buttonFullscreen.addEventListener("click", () => {
maximize();
});
const buttonKeyboard = document.getElementById("button-keyboard");
buttonKeyboard.addEventListener("click", () => {
const sectionKeyboard = document.getElementById("section-keyboard");
const separatorKeyboard = document.getElementById("separator-keyboard");
const sectionNarrative = document.getElementById("section-narrative");
const separatorNarrative = document.getElementById(
"separator-narrative"
);
if (buttonKeyboard.classList.contains("enabled")) {
sectionKeyboard.style.display = "none";
separatorKeyboard.style.display = "none";
sectionNarrative.style.display = "block";
separatorNarrative.style.display = "block";
buttonKeyboard.classList.remove("enabled");
} else {
sectionKeyboard.style.display = "block";
separatorKeyboard.style.display = "block";
sectionNarrative.style.display = "none";
separatorNarrative.style.display = "none";
buttonKeyboard.classList.add("enabled");
}
});
const buttonInformation = document.getElementById("button-information");
buttonInformation.addEventListener("click", () => {
const sectionDiag = document.getElementById("section-diag");
const separatorDiag = document.getElementById("separator-diag");
if (buttonInformation.classList.contains("enabled")) {
sectionDiag.style.display = "none";
separatorDiag.style.display = "none";
buttonInformation.classList.remove("enabled");
} else {
sectionDiag.style.display = "block";
separatorDiag.style.display = "block";
buttonInformation.classList.add("enabled");
}
});
const buttonTheme = document.getElementById("button-theme");
buttonTheme.addEventListener("click", () => {
state.background_index =
(state.background_index + 1) % BACKGROUNDS.length;
const background = BACKGROUNDS[state.background_index];
setBackground(background);
});
const buttonUploadFile = document.getElementById(
"button-upload-file"
) as HTMLInputElement;
buttonUploadFile.addEventListener("change", async () => {
if (!buttonUploadFile.files || buttonUploadFile.files.length === 0) {
const file = buttonUploadFile.files[0];
const arrayBuffer = await file.arrayBuffer();
const romData = new Uint8Array(arrayBuffer);
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
start({ engine: null, romName: file.name, romData: romData });
showToast(`Loaded ${file.name} ROM successfully!`);
});
};
const registerKeyboard = () => {
const keyboard = document.getElementById("keyboard");
const keys = keyboard.getElementsByClassName("key");
keyboard.addEventListener("touchstart", function (event) {
event.preventDefault();
event.stopPropagation();
});
keyboard.addEventListener("touchend", function (event) {
event.preventDefault();
event.stopPropagation();
});
Array.prototype.forEach.call(keys, (k: Element) => {
k.addEventListener("mousedown", function (event) {
const keyCode = KEYS[this.textContent.toLowerCase()];
state.chip8.key_press_ws(keyCode);
event.preventDefault();
event.stopPropagation();
});
k.addEventListener("touchstart", function (event) {
const keyCode = KEYS[this.textContent.toLowerCase()];
state.chip8.key_press_ws(keyCode);
event.preventDefault();
event.stopPropagation();
});
k.addEventListener("mouseup", function (event) {
const keyCode = KEYS[this.textContent.toLowerCase()];
state.chip8.key_lift_ws(keyCode);
event.preventDefault();
event.stopPropagation();
});
k.addEventListener("touchend", function (event) {
const keyCode = KEYS[this.textContent.toLowerCase()];
state.chip8.key_lift_ws(keyCode);
event.preventDefault();
event.stopPropagation();
});
});
};
const registerCanvas = () => {
const canvasClose = document.getElementById("canvas-close");
canvasClose.addEventListener("click", () => {
minimize();
});
};
const registerToast = () => {
const toast = document.getElementById("toast");
toast.addEventListener("click", () => {
toast.classList.remove("visible");
});
};
const registerModal = () => {
const modalClose = document.getElementById("modal-close");
modalClose.addEventListener("click", () => {
hideModal(false);
});
const modalCancel = document.getElementById("modal-cancel");
modalCancel.addEventListener("click", () => {
hideModal(false);
});
const modalConfirm = document.getElementById("modal-confirm");
modalConfirm.addEventListener("click", () => {
hideModal(true);
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
hideModal(false);
}
});
};
const initBase = async () => {
const background = BACKGROUNDS[state.background_index];
setBackground(background);
setVersion(info.version);
};
const initCanvas = async () => {
// initializes the off-screen canvas that is going to be
// used in the drawing process
state.canvas = document.createElement("canvas");
state.canvas.width = DISPLAY_WIDTH;
state.canvas.height = DISPLAY_HEIGHT;
state.canvasCtx = state.canvas.getContext("2d");
state.canvasScaled = document.getElementById(
"chip-canvas"
) as HTMLCanvasElement;
state.canvasScaled.width =
state.canvasScaled.width * window.devicePixelRatio;
state.canvasScaled.height =
state.canvasScaled.height * window.devicePixelRatio;
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
state.canvasScaledCtx = state.canvasScaled.getContext("2d");
state.canvasScaledCtx.scale(
state.canvasScaled.width / state.canvas.width,
state.canvasScaled.height / state.canvas.height
);
state.canvasScaledCtx.imageSmoothingEnabled = false;
state.image = state.canvasCtx.createImageData(
state.canvas.width,
state.canvas.height
);
state.videoBuff = new DataView(state.image.data.buffer);
};
const updateCanvas = (pixels: Uint8Array) => {
for (let i = 0; i < pixels.length; i++) {
state.videoBuff.setUint32(
i * 4,
pixels[i] ? PIXEL_SET_COLOR : PIXEL_UNSET_COLOR
);
}
state.canvasCtx.putImageData(state.image, 0, 0);
state.canvasScaledCtx.drawImage(state.canvas, 0, 0);
};
const clearCanvas = async (
color = PIXEL_UNSET_COLOR,
{ image = null as string, imageScale = 1 } = {}
) => {
state.canvasScaledCtx.fillStyle = `#${color.toString(16).toUpperCase()}`;
state.canvasScaledCtx.fillRect(
0,
0,
state.canvasScaled.width,
state.canvasScaled.height
);
// in case an image was requested then uses that to load
// an image at the center of the screen
if (image) {
const img = await new Promise<HTMLImageElement>((resolve) => {
const img = new Image();
img.onload = () => {
resolve(img);
};
img.src = image;
});
const [imgWidth, imgHeight] = [
img.width * imageScale,
img.height * imageScale
];
const [x0, y0] = [
state.canvasScaled.width / 2 - imgWidth / 2,
state.canvasScaled.height / 2 - imgHeight / 2
];
state.canvasScaledCtx.setTransform(1, 0, 0, 1, 0, 0);
try {
state.canvasScaledCtx.drawImage(img, x0, y0, imgWidth, imgHeight);
} finally {
state.canvasScaledCtx.scale(
state.canvasScaled.width / state.canvas.width,
state.canvasScaled.height / state.canvas.height
);
}
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
const showToast = async (message: string, error = false, timeout = 3500) => {
const toast = document.getElementById("toast");
toast.classList.remove("error");
if (error) toast.classList.add("error");
toast.classList.add("visible");
toast.textContent = message;
if (state.toastTimeout) clearTimeout(state.toastTimeout);
state.toastTimeout = setTimeout(() => {
toast.classList.remove("visible");
state.toastTimeout = null;
}, timeout);
};
const showModal = async (
message: string,
title = "Alert"
): Promise<boolean> => {
const modalContainer = document.getElementById("modal-container");
const modalTitle = document.getElementById("modal-title");
const modalText = document.getElementById("modal-text");
modalContainer.classList.add("visible");
modalTitle.textContent = title;
modalText.innerHTML = message.replace(/\n/g, "<br/>");
const result = (await new Promise((resolve) => {
global.modalCallback = resolve;
})) as boolean;
return result;
};
const hideModal = async (result = true) => {
const modalContainer = document.getElementById("modal-container");
modalContainer.classList.remove("visible");
if (global.modalCallback) global.modalCallback(result);
global.modalCallback = null;
};
const setVersion = (value: string) => {
document.getElementById("version").textContent = value;
};
const setEngine = (name: string, upper = true) => {
name = upper ? name.toUpperCase() : name;
document.getElementById("engine").textContent = name;
};
const setRom = (name: string, data: Uint8Array) => {
state.romName = name;
state.romData = data;
state.romSize = data.length;
document.getElementById("rom-name").textContent = name;
document.getElementById("rom-size").textContent = String(data.length);
};
const setLogicFrequency = (value: number) => {
if (value < 0) showToast("Invalid frequency value!", true);
value = Math.max(value, 0);
state.logicFrequency = value;
document.getElementById("logic-frequency").textContent = String(value);
};
const setFps = (value: number) => {
if (value < 0) showToast("Invalid FPS value!", true);
value = Math.max(value, 0);
state.fps = value;
document.getElementById("fps-count").textContent = String(value);
};
const setBackground = (value: string) => {
document.body.style.backgroundColor = `#${value}`;
document.getElementById(
"footer-background"
).style.backgroundColor = `#${value}f2`;
};
const toggleRunning = () => {
if (state.paused) {
resume();
} else {
pause();
}
};
const pause = () => {
state.paused = true;
const buttonPause = document.getElementById("button-pause");
const img = buttonPause.getElementsByTagName("img")[0];
const span = buttonPause.getElementsByTagName("span")[0];
buttonPause.classList.add("enabled");
// @ts-ignore: ts(2580)
img.src = require("./res/play.svg");
span.textContent = "Resume";
};
const resume = () => {
state.paused = false;
state.nextTickTime = new Date().getTime();
const buttonPause = document.getElementById("button-pause");
const img = buttonPause.getElementsByTagName("img")[0];
const span = buttonPause.getElementsByTagName("span")[0];
buttonPause.classList.remove("enabled");
// @ts-ignore: ts(2580)
img.src = require("./res/pause.svg");
span.textContent = "Pause";
};
const toggleWindow = () => {
maximize();
};
const maximize = () => {
const canvasContainer = document.getElementById("canvas-container");
canvasContainer.classList.add("fullscreen");
window.addEventListener("resize", crop);
crop();
};
const minimize = () => {
const canvasContainer = document.getElementById("canvas-container");
const chipCanvas = document.getElementById("chip-canvas");
canvasContainer.classList.remove("fullscreen");
chipCanvas.style.width = null;
chipCanvas.style.height = null;
window.removeEventListener("resize", crop);
};
const crop = () => {
const chipCanvas = document.getElementById("chip-canvas");
// calculates the window ratio as this is fundamental to
// determine the proper way to crop the fulscreen
const windowRatio = window.innerWidth / window.innerHeight;
// in case the window is wider (more horizontal than the base ratio)
// this means that we must crop horizontaly
if (windowRatio > DISPLAY_RATIO) {
chipCanvas.style.width = `${
window.innerWidth * (DISPLAY_RATIO / windowRatio)
}px`;
chipCanvas.style.height = `${window.innerHeight}px`;
} else {
chipCanvas.style.width = `${window.innerWidth}px`;
chipCanvas.style.height = `${
window.innerHeight * (windowRatio / DISPLAY_RATIO)
}px`;
}
};
const reset = () => {
start({ engine: null });
};
const fetchRom = async (romPath: string): Promise<[string, Uint8Array]> => {
// extracts the name of the ROM from the provided
// path by splitting its structure
const romPathS = romPath.split(/\//g);
let romName = romPathS[romPathS.length - 1].split("?")[0];
const romNameS = romName.split(/\./g);
romName = `${romNameS[0]}.${romNameS[romNameS.length - 1]}`;
// loads the ROM data and converts it into the
// target byte array buffer (to be used by WASM)
const response = await fetch(ROM_PATH);
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
const romData = new Uint8Array(arrayBuffer);
// returns both the name of the ROM and the data
// contents as a byte array
return [romName, romData];
};
const beep = async () => {
sound.muted = false;
await sound.play();
};