Allow an infinite CYCLE_LIMIT

This commit is contained in:
n loewen 2023-08-02 11:03:24 +01:00
parent 0d050aa968
commit daf3793f90
2 changed files with 16 additions and 8 deletions

View File

@ -14,5 +14,5 @@ let machineCode = assembler.assemble(inputFile_str);
// printMemory.printTable(machineCode); // printMemory.printTable(machineCode);
// console.groupEnd('Machine code output'); // console.groupEnd('Machine code output');
//computer.debugProgram(machineCode); computer.debugProgram(machineCode);
computer.displayProgram(machineCode); //computer.displayProgram(machineCode);

View File

@ -175,21 +175,24 @@ function stepCPU() {
} }
exports.debugProgram = (code) => { 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(); console.log();
let time = new Date(); let time = new Date();
console.log( `┌─────────────────────┐`); console.log( `┌─────────────────────┐`);
// Running at 11:48:15 AM // Running at 11:48:15 AM
console.log( `│ Running at ${time.toLocaleTimeString('en-GB')}` ); console.log( `│ Running at ${time.toLocaleTimeString('en-GB')}` );
console.log( `└─────────────────────┘`); 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; 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.running) break;
if (CPU.IP >= CPU.memory.length) break; if (CPU.IP >= CPU.memory.length) break;
stepCPU();
console.group('Display') console.group('Display')
display.printDisplay(CPU.memory); display.printDisplay(CPU.memory);
console.log(); console.log();
@ -201,7 +204,12 @@ exports.displayProgram = async (code) => {
CPU.loadMemory(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 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; 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.running) break;
if (CPU.IP >= CPU.memory.length) break; if (CPU.IP >= CPU.memory.length) break;
stepCPU(); stepCPU();