From 1d88d0b5b6e01270cf180aed4878722e150da177 Mon Sep 17 00:00:00 2001 From: n loewen Date: Wed, 16 Aug 2023 12:56:30 +0100 Subject: [PATCH] Refactor: Use `setInterval` to clock CPU, in order to remove unnecessary use of async/await --- cpu.js | 14 +++++++------- run-cpu.js | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cpu.js b/cpu.js index a2725d3..3a57d9f 100644 --- a/cpu.js +++ b/cpu.js @@ -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(); }; \ No newline at end of file diff --git a/run-cpu.js b/run-cpu.js index 9da11e1..dae2798 100755 --- a/run-cpu.js +++ b/run-cpu.js @@ -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); } \ No newline at end of file