diff --git a/assemble.js b/assemble.js index 76d2749..7f71dab 100644 --- a/assemble.js +++ b/assemble.js @@ -1,13 +1,22 @@ -// Usage: `node assemble.js assembly.asm` +// Run: `node assemble.js run assembly.asm` +// Debug: `node assemble.js debug assembly.asm` const fs = require('fs'); const assembler = require('./assembler.js'); const printMemory = require('./print-memory.js'); -const filename = process.argv[2]; +const mode = process.argv[2]; +const filename = process.argv[3]; const inputFile_str = fs.readFileSync(filename, 'utf8'); -let machineCode = assembler.assemble(inputFile_str); -console.log(); -console.group("Machine code output"); + +let machineCode; + +if (mode === "debug") { + machineCode = assembler.assemble(inputFile_str, true); + console.group("Machine code output"); printMemory.printTable(machineCode); -console.groupEnd('Machine code output'); \ No newline at end of file + console.groupEnd('Machine code output'); +} else { + machineCode = assembler.assemble(inputFile_str); + console.log(machineCode); // TODO print just the numbers +} \ No newline at end of file diff --git a/assembler.js b/assembler.js index b7dd9c7..12bd9fe 100644 --- a/assembler.js +++ b/assembler.js @@ -5,7 +5,13 @@ const { INITIAL_IP_ADDRESS, START_OF_DISPLAY_MEM } = require('./machine.config.j // 2 = what i'm currently focusing on // 3 = always print // 4 = silent -const DEBUG = 4; +const DEBUG_LEVEL = 2; +let DEBUG = false; // Turn debugging on/off -- set by assemble() + +exports.assemble = (str, debug = false) => { + DEBUG = debug; + return decodeInstructions(str); +} const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp']; const mnemonics2opcodes = { @@ -124,7 +130,7 @@ function decodeInstructions(str) { dbg(1, ''); dbgGroup(1, 'Memory before filling in label pointers'); - dbgExec(1, () => printMemory.printTable(machineCode)); + dbgExec(1, () => logMemory(machineCode)); dbgGroupEnd(1, 'Memory before filling in label pointers'); // Backfill label pointers @@ -156,13 +162,7 @@ function stripWhitespaceFromEnds(line) { function hex2num(hex) { return parseInt(hex, 16) }; // Debug helpers -const dbg = (lvl, s) => { if (lvl >= DEBUG) console.log(s) }; -const dbgGroup = (lvl, s) => { if (lvl >= DEBUG) console.group(s) }; -const dbgGroupEnd = (lvl, s) => { if (lvl >= DEBUG) console.groupEnd(s) }; -const dbgExec = (lvl, func) => { if (lvl >= DEBUG) func(); } - -// RUN IT - -exports.assemble = (str) => { - return decodeInstructions(str); -} \ No newline at end of file +const dbg = (lvl, s) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) console.log(s) }; +const dbgGroup = (lvl, s) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) console.group(s) }; +const dbgGroupEnd = (lvl, s) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) console.groupEnd(s) }; +const dbgExec = (lvl, func) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) func(); } \ No newline at end of file diff --git a/package.json b/package.json index bf83aa5..445aa83 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "paper-computer", "scripts": { - "display": "node assemble-and-run.js run", - "debug": "node assemble-and-run.js debug", - "assemble": "node assemble.js" + "rundisplay": "node assemble-and-run.js run", + "rundebug": "node assemble-and-run.js debug", + "asm": "node assemble.js run", + "asmdebug": "node assemble.js debug" } -} +} \ No newline at end of file