From 34e86cd4f8450e57d0336114eb7ab95bd497d438 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 1 Aug 2023 12:01:50 +0100 Subject: [PATCH] Move cycle limit to a constant --- sketches/simulator-sketch-v3.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sketches/simulator-sketch-v3.js b/sketches/simulator-sketch-v3.js index 7e03d40..bda3fe2 100644 --- a/sketches/simulator-sketch-v3.js +++ b/sketches/simulator-sketch-v3.js @@ -3,6 +3,8 @@ // - instructions are two bytes long: // one byte for the opcode, one for the argument +const CYCLE_LIMIT = 64; // max number of times to step the CPU, to stop endless loops + // STATE const CPU = { @@ -174,7 +176,6 @@ exports.runProgram = (code) => { CPU.loadMemory(code); const initialMemory = JSON.parse(JSON.stringify(CPU.memory)); // Hack to make a copy-by-value -- https://stackoverflow.com/questions/18829099/copy-a-variables-value-into-another console.log(); - console.log("————————————————————————————————————————"); let time = new Date(); console.log( `┌─────────────────────┐`); // Running at 11:48:15 AM @@ -183,7 +184,7 @@ exports.runProgram = (code) => { logCPUState(); CPU.running = true; - for (let i = 0; i < 255; i++) { // FIXME: temporary limit as a lazy way to halt infinite loops + for (let i = 0; i < CYCLE_LIMIT; i++) { // FIXME: temporary limit as a lazy way to halt infinite loops if (!CPU.running) break; if (CPU.IP >= CPU.memory.length) break; stepCPU();