cpu - Fix: Quit the process when the CPU halts

This commit is contained in:
n loewen 2023-08-26 13:26:26 +01:00
parent 41632b0a0f
commit edad9ecbb8
1 changed files with 19 additions and 20 deletions

39
cpu.js
View File

@ -84,7 +84,7 @@ const Instructions = {
}, },
store_addr: (addr) => { store_addr: (addr) => {
CPU.currentInstruction.mnemonic = 'STO addr'; CPU.currentInstruction.mnemonic = `STO addr; @addr: ${num2hex(CPU.memory[addr])}`;
CPU.memory[CPU.memory[addr]] = CPU.Acc; CPU.memory[CPU.memory[addr]] = CPU.Acc;
CPU.incrementIP(2); CPU.incrementIP(2);
}, },
@ -333,28 +333,27 @@ async function stepCPU(debugInfo, debug = false, prettyPrintDisplay = false) {
CPU.running = false; CPU.running = false;
} }
} }
if (CPU.running) { if (CPU.IP >= CPU.memory.length) {
if (CPU.IP >= CPU.memory.length) { console.error('HALTING - IP greater than memory size');
console.error('HALTING - IP greater than memory size'); CPU.running = false;
CPU.running = false; process.exit();
} else {
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
CPU.currentInstruction.operand = CPU.memory[CPU.IP+1];
let executeInstruction = opcodes2mnemonics[CPU.currentInstruction.opcode];
if (typeof executeInstruction === 'undefined') {
let info = debugInfo[CPU.previousIP];
console.error();
console.error(`Error: Invalid opcode`);
console.error(` Executing $${num2hex(info.machine[0])} $${num2hex(info.machine[1])}`);
console.error(` from line ${info.lineNumber}: ${info.source}`);
process.exit(); process.exit();
} else {
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
CPU.currentInstruction.operand = CPU.memory[CPU.IP+1];
let executeInstruction = opcodes2mnemonics[CPU.currentInstruction.opcode];
if (typeof executeInstruction === 'undefined') {
let info = debugInfo[CPU.previousIP];
console.error();
console.error(`Error: Invalid opcode`);
console.error(` Executing $${num2hex(info.machine[0])} $${num2hex(info.machine[1])}`);
console.error(` from line ${info.lineNumber}: ${info.source}`);
process.exit();
}
executeInstruction(CPU.currentInstruction.operand);
CPU.cycleCounter += 1;
} }
executeInstruction(CPU.currentInstruction.operand);
CPU.cycleCounter += 1;
} }
logCPUState(debugInfo, debug, prettyPrintDisplay); logCPUState(debugInfo, debug, prettyPrintDisplay);
if (!CPU.running) process.exit();
} }
/** /**
@ -374,7 +373,7 @@ exports.runProgram =
// Animate the output by pausing between steps // Animate the output by pausing between steps
const loop = setInterval(async () => { const loop = setInterval(async () => {
stepCPU(debugInfo, debug, prettyPrint); stepCPU(debugInfo, debug, prettyPrint);
if (!CPU.running) clearInterval(loop); if (!CPU.running) process.exit();
}, clockSpeed); }, clockSpeed);
} }
}; };