2023-03-25 17:51:46 +00:00
|
|
|
|
2022-10-11 21:40:12 +00:00
|
|
|
import * as Emulator from './moa-genesis.js';
|
|
|
|
|
2023-05-07 21:57:45 +00:00
|
|
|
window.addEventListener("load", () => {
|
|
|
|
Emulator.default();
|
|
|
|
});
|
|
|
|
|
2022-10-11 21:40:12 +00:00
|
|
|
function initialize_emulator() {
|
2023-05-07 21:57:45 +00:00
|
|
|
const host = Emulator.new_host();
|
|
|
|
const system = Emulator.load_system(host, Emulator.get_load_system_fn());
|
2022-10-11 21:40:12 +00:00
|
|
|
|
2023-05-06 17:04:44 +00:00
|
|
|
//Emulator.start_system(system);
|
2022-10-11 21:40:12 +00:00
|
|
|
let last_update = performance.now();
|
|
|
|
setTimeout(function refreshFrame() {
|
2023-05-07 21:57:45 +00:00
|
|
|
// Calculate the time difference since the last update cycle
|
|
|
|
const current = performance.now();
|
|
|
|
const diff = current - last_update;
|
2022-10-11 21:40:12 +00:00
|
|
|
last_update = current;
|
|
|
|
|
2023-05-07 21:57:45 +00:00
|
|
|
// Run the system for the difference, and get the realtime runtime in millis
|
|
|
|
const runtime = Emulator.run_system_for(system, diff * 1_000_000);
|
|
|
|
|
2022-10-11 21:40:12 +00:00
|
|
|
if (Emulator.is_running()) {
|
2023-05-07 21:57:45 +00:00
|
|
|
// Calculate the timeout needed to fill the time that was *not* taken by the sim
|
|
|
|
const remaining = Math.max(diff - runtime - (diff * 0.1), 1);
|
2022-10-12 05:36:49 +00:00
|
|
|
setTimeout(refreshFrame, remaining);
|
2022-10-11 21:40:12 +00:00
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
|
2023-05-07 21:57:45 +00:00
|
|
|
const controllers = Emulator.get_controllers(host);
|
|
|
|
function button_event(e) {
|
|
|
|
let state;
|
|
|
|
if (e.type == 'mousedown' || e.type == 'touchstart') {
|
|
|
|
state = true;
|
|
|
|
} else {
|
|
|
|
state = false;
|
|
|
|
}
|
|
|
|
Emulator.button_press(controllers, e.target.name, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById("controller").querySelectorAll('button').forEach(function (button) {
|
|
|
|
button.addEventListener('mousedown', button_event);
|
|
|
|
button.addEventListener('mouseup', button_event);
|
|
|
|
button.addEventListener('touchstart', button_event);
|
|
|
|
button.addEventListener('touchend', button_event);
|
|
|
|
});
|
|
|
|
|
2022-10-11 21:40:12 +00:00
|
|
|
Emulator.host_run_loop(host);
|
|
|
|
}
|
|
|
|
|
2023-05-06 17:04:44 +00:00
|
|
|
// Update the frame rate display
|
2023-05-07 21:57:45 +00:00
|
|
|
const frame_rate_el = document.getElementById("frame-rate");
|
|
|
|
const frame_rate = setInterval(function () {
|
2023-05-06 17:04:44 +00:00
|
|
|
frame_rate_el.value = Emulator.get_frames_since();
|
|
|
|
}, 1000);
|
|
|
|
|
2022-10-12 05:36:49 +00:00
|
|
|
// Load a new ROM file
|
2023-05-07 21:57:45 +00:00
|
|
|
const reader = new FileReader();
|
2022-10-12 05:36:49 +00:00
|
|
|
reader.onloadend = function (e) {
|
2023-05-07 21:57:45 +00:00
|
|
|
let data = new Uint8Array(reader.result);
|
2022-10-12 05:36:49 +00:00
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
2023-05-07 21:57:45 +00:00
|
|
|
const file_input = document.getElementById("rom-file");
|
2022-10-12 05:36:49 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2023-05-07 21:57:45 +00:00
|
|
|
let mute_state = false;
|
|
|
|
const mute = document.getElementById("mute");
|
2023-05-06 17:04:44 +00:00
|
|
|
mute.addEventListener("click", () => {
|
|
|
|
mute_state = !mute_state;
|
|
|
|
if (mute_state) {
|
|
|
|
mute.value = "\uD83D\uDD07";
|
|
|
|
} else {
|
|
|
|
mute.value = "\uD83D\uDD08";
|
|
|
|
}
|
|
|
|
Emulator.set_mute(mute_state);
|
2022-10-11 21:40:12 +00:00
|
|
|
});
|
|
|
|
|