diff --git a/cpu.js b/cpu.js index 9f083c4..38b7c90 100644 --- a/cpu.js +++ b/cpu.js @@ -84,7 +84,7 @@ const Instructions = { }, 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.incrementIP(2); }, @@ -333,28 +333,27 @@ async function stepCPU(debugInfo, debug = false, prettyPrintDisplay = false) { CPU.running = false; } } - if (CPU.running) { - if (CPU.IP >= CPU.memory.length) { - console.error('HALTING - IP greater than memory size'); - CPU.running = false; + if (CPU.IP >= CPU.memory.length) { + console.error('HALTING - IP greater than memory size'); + 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(); - } 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); + if (!CPU.running) process.exit(); } /** @@ -374,7 +373,7 @@ exports.runProgram = // Animate the output by pausing between steps const loop = setInterval(async () => { stepCPU(debugInfo, debug, prettyPrint); - if (!CPU.running) clearInterval(loop); + if (!CPU.running) process.exit(); }, clockSpeed); } };