diff --git a/assemble-and-run.js b/assemble-and-run.js index 4594a3a..0899f90 100644 --- a/assemble-and-run.js +++ b/assemble-and-run.js @@ -1,12 +1,15 @@ -// Usage: `node assemble-and-run.js assembly.asm` +// Run: `node assemble-and-run.js run assembly.asm` +// Debug: `node assemble-and-run.js debug assembly.asm` const fs = require('fs'); const computer = require('./simulator.js'); const assembler = require('./assembler.js'); const printMemory = require('./print-memory.js'); +const mode = process.argv[2]; + // console.log(`Reading ${filename}`); -const filename = process.argv[2]; +const filename = process.argv[3]; const inputFile_str = fs.readFileSync(filename, 'utf8'); let machineCode = assembler.assemble(inputFile_str); @@ -14,5 +17,8 @@ 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 +if (mode === "debug") { + computer.runProgram(machineCode, true); +} else { + computer.runProgram(machineCode, false); +} \ No newline at end of file diff --git a/package.json b/package.json index 8471f39..bf83aa5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { "name": "paper-computer", "scripts": { - "assemble-and-run": "node assemble-and-run.js", + "display": "node assemble-and-run.js run", + "debug": "node assemble-and-run.js debug", "assemble": "node assemble.js" } } diff --git a/simulator.js b/simulator.js index 1d76aca..00480ea 100644 --- a/simulator.js +++ b/simulator.js @@ -174,49 +174,25 @@ function stepCPU() { // console.groupEnd("Step CPU"); } -exports.debugProgram = (code) => { - console.log(); - let time = new Date(); - console.log( `┌─────────────────────┐`); - // Running at 11:48:15 AM - console.log( `│ Running at ${time.toLocaleTimeString('en-GB')} │` ); - console.log( `└─────────────────────┘`); +exports.runProgram = async (code, debug = false) => { + if (debug) logRunningHeader(); 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(); + if (debug) logCPUState(); CPU.running = true; 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 (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; } // Temporary limit as a lazy way to halt infinite loops: if (!CPU.running) break; if (CPU.IP >= CPU.memory.length) break; + + if (!debug) console.clear(); console.group('Display') display.printDisplay(CPU.memory); - console.log(); console.groupEnd('Display'); - }; -} - -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; - - 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.clear(); - display.printDisplay(CPU.memory); logCPUState(); - await new Promise(resolve => setTimeout(resolve, 75)); + if (!debug) await new Promise(resolve => setTimeout(resolve, 75)); }; } @@ -240,4 +216,12 @@ logTableTitled = (memory, tableTitle) => { console.group(tableTitle); console.table(memory); console.groupEnd(tableTitle); -}; \ No newline at end of file +}; + +function logRunningHeader() { + console.log(); + let time = new Date(); + console.log( `┌─────────────────────┐`); + console.log( `│ Running at ${time.toLocaleTimeString('en-GB')} │` ); + console.log( `└─────────────────────┘`); +} \ No newline at end of file