2023-03-25 17:51:46 +00:00
|
|
|
|
2022-10-11 21:40:12 +00:00
|
|
|
import * as Emulator from './moa-genesis.js';
|
|
|
|
|
|
|
|
function initialize_emulator() {
|
|
|
|
let host = Emulator.new_host();
|
|
|
|
let system = Emulator.load_system(host, Emulator.get_load_system_fn());
|
|
|
|
|
|
|
|
let last_update = performance.now();
|
|
|
|
setTimeout(function refreshFrame() {
|
|
|
|
let current = performance.now();
|
2022-10-12 05:36:49 +00:00
|
|
|
let diff = current - last_update;
|
|
|
|
//let remaining = Math.max((16 * Emulator.get_speed()) - diff, 0);
|
2022-10-11 21:40:12 +00:00
|
|
|
last_update = current;
|
|
|
|
|
2022-10-12 05:36:49 +00:00
|
|
|
let runtime = Emulator.run_system_for(system, diff * 1_000_000);
|
2022-10-11 21:40:12 +00:00
|
|
|
if (Emulator.is_running()) {
|
2022-10-12 05:36:49 +00:00
|
|
|
let remaining = Math.max(diff - runtime - (diff * 0.1), 1);
|
|
|
|
setTimeout(refreshFrame, remaining);
|
2022-10-11 21:40:12 +00:00
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
Emulator.host_run_loop(host);
|
|
|
|
}
|
|
|
|
|
2022-10-12 05:36:49 +00:00
|
|
|
window.addEventListener("load", () => {
|
|
|
|
Emulator.default();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load a new ROM file
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onloadend = function (e) {
|
|
|
|
var data = new Uint8Array(reader.result);
|
|
|
|
// If the SMD file magic number is present, then convert it before loading
|
|
|
|
if (data[8] == 0xAA && data[9] == 0xBB)
|
|
|
|
data = Emulator.smd_to_bin(data);
|
|
|
|
Emulator.set_rom_data(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
var file_input = document.getElementById("rom-file");
|
|
|
|
file_input.addEventListener("change", e => {
|
2023-03-25 17:51:46 +00:00
|
|
|
document.getElementById("video").focus();
|
2022-10-12 05:36:49 +00:00
|
|
|
reader.readAsArrayBuffer(file_input.files[0])
|
|
|
|
});
|
|
|
|
|
2022-10-11 21:40:12 +00:00
|
|
|
document.getElementById("reset").addEventListener("click", () => {
|
2023-03-25 17:51:46 +00:00
|
|
|
document.getElementById("video").focus();
|
2022-10-11 21:40:12 +00:00
|
|
|
Emulator.request_stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("power").addEventListener("click", () => {
|
2023-03-25 17:51:46 +00:00
|
|
|
document.getElementById("video").focus();
|
2022-10-11 21:40:12 +00:00
|
|
|
if (Emulator.is_running())
|
|
|
|
Emulator.request_stop();
|
|
|
|
else
|
|
|
|
initialize_emulator();
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("speed").addEventListener("change", (e) => {
|
2023-03-25 17:51:46 +00:00
|
|
|
document.getElementById("video").focus();
|
2022-10-11 21:40:12 +00:00
|
|
|
Emulator.set_speed(e.target.value);
|
|
|
|
});
|
|
|
|
|
2022-10-12 05:36:49 +00:00
|
|
|
// Update the frame rate display
|
2022-10-11 21:40:12 +00:00
|
|
|
var frame_rate_el = document.getElementById("frame-rate");
|
|
|
|
var frame_rate = setInterval(function () {
|
|
|
|
frame_rate_el.value = Emulator.get_frames_since();
|
|
|
|
}, 1000);
|
|
|
|
|