diff --git a/assemble-and-run.js b/assemble-and-run.js index 26cd67d..4594a3a 100644 --- a/assemble-and-run.js +++ b/assemble-and-run.js @@ -14,5 +14,5 @@ let machineCode = assembler.assemble(inputFile_str); // printMemory.printTable(machineCode); // console.groupEnd('Machine code output'); -//computer.debugProgram(machineCode); -computer.displayProgram(machineCode); \ No newline at end of file +computer.debugProgram(machineCode); +//computer.displayProgram(machineCode); \ No newline at end of file diff --git a/simulator.js b/simulator.js index abc3b1e..1d76aca 100644 --- a/simulator.js +++ b/simulator.js @@ -175,21 +175,24 @@ function stepCPU() { } exports.debugProgram = (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(); let time = new Date(); console.log( `┌─────────────────────┐`); // Running at 11:48:15 AM console.log( `│ Running at ${time.toLocaleTimeString('en-GB')} │` ); console.log( `└─────────────────────┘`); - logCPUState(); + 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 + logCPUState(); CPU.running = true; - for (let i = 0; i < CYCLE_LIMIT; i++) { // FIXME: temporary limit as a lazy way to halt infinite loops + let step = 0; + while (true) { + step = step + 1; + // FIXME: temporary limit as a lazy way to halt infinite loops: + if (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; } if (!CPU.running) break; if (CPU.IP >= CPU.memory.length) break; - stepCPU(); console.group('Display') display.printDisplay(CPU.memory); console.log(); @@ -201,7 +204,12 @@ exports.displayProgram = async (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 CPU.running = true; - for (let i = 0; i < CYCLE_LIMIT; i++) { // FIXME: temporary limit as a lazy way to halt infinite loops + + let step = 0; + while (true) { + step = step + 1; + // FIXME: temporary limit as a lazy way to halt infinite loops: + if (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; } if (!CPU.running) break; if (CPU.IP >= CPU.memory.length) break; stepCPU();