Refactor: Use `setInterval` to clock CPU, in order to remove unnecessary use of async/await

This commit is contained in:
n loewen 2023-08-16 12:56:30 +01:00
parent aa6cbc3e6d
commit 1d88d0b5b6
2 changed files with 8 additions and 8 deletions

14
cpu.js
View File

@ -294,18 +294,20 @@ async function stepCPU(debug = false) {
CPU.cycleCounter += 1;
}
}
await logCPUState(debug);
logCPUState(debug);
}
/**
* @param {Uint8Array} code - Machine code to run
* @param {Boolean} [debug] - Enable/disable debugging printouts
**/
exports.runProgram = async (code, debug = false) => {
exports.runProgram = (code, debug = false, framerate = 10) => {
startCPU(code);
while (CPU.running) {
// Animate the output by pausing between steps
const loop = setInterval(async () => {
stepCPU(debug);
};
if (!CPU.running) clearInterval(loop);
}, framerate);
}
@ -314,7 +316,7 @@ exports.runProgram = async (code, debug = false) => {
/**
* @param {Boolean} [debug] - Enable/disable debugging printouts
**/
async function logCPUState(debug = false) {
function logCPUState(debug = false) {
console.group(`Step`);
if (!debug) console.clear();
if (debug) {
@ -328,7 +330,5 @@ async function logCPUState(debug = false) {
console.log();
console.log(`IP: $${num2hex(CPU.IP)} Acc: $${num2hex(CPU.Acc)} NZOC: ${num2bin_4bit(CPU.FLAGS)}  ${CPU.running ? "running" : "halted" }`);
console.log();
// Pause to show animated display:
if (!debug) await new Promise(resolve => setTimeout(resolve, 75));
console.groupEnd();
};

View File

@ -29,5 +29,5 @@ if (mode === "debug") {
computer.runProgram(machineCode, true);
} else {
machineCode = assembler.assemble(inputFile_str);
computer.runProgram(machineCode, false);
computer.runProgram(machineCode, false, 200);
}