diff --git a/sketches/assembler.js b/sketches/assembler.js index e3374d0..badc7d4 100644 --- a/sketches/assembler.js +++ b/sketches/assembler.js @@ -10,7 +10,6 @@ const printMemory = require('./print-memory.js'); -const { debug } = require("console"); // 0 = silent // 1 = verbose @@ -41,8 +40,6 @@ function decodeInstructions(str) { console.group(`Input line ${i}, cursor ${byteCursor}`); dbg(3, `> ${lines[i]}`); let line = stripWhitespaceFromEnds(stripComments(lines[i])); - // console.log(); - // console.log(`> ${line}`); // Handle blank lines if (line.length === 0) { @@ -66,11 +63,8 @@ function decodeInstructions(str) { bytesToReplace: [], }; } - - dbg(2, ''); - dbg(2, `@label anchor: ${label}`); - dbg(2, labels); - dbg(2, ''); + dbg(2, `pointsToByte: ${labels[label].pointsToByte}`); + dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`); dbg(3, `cursor: ${byteCursor}, new code: none`); console.groupEnd('Input line'); continue; @@ -94,11 +88,8 @@ function decodeInstructions(str) { // TODO: validate label // validateLabel(line); label = arg_str.substring(1); // strip '@' arg_num = 0; - dbg(2, ''); - dbg(2, `@label reference: ${label}`); if (label in labels) { - dbg(2, ''); dbg(1, `'${label}' already in labels object`); labels[label].bytesToReplace.push(byteCursor + 1); } else { @@ -107,11 +98,8 @@ function decodeInstructions(str) { bytesToReplace: [byteCursor + 1], }; } - // dbg(2, labels); - dbg(2, `label pointsToByte: ${labels[label].pointsToByte}`); - dbg(2, `label bytesToReplace: ${labels[label].bytesToReplace}`); - - dbg(2, ''); + dbg(2, `pointsToByte: ${labels[label].pointsToByte}`); + dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`); } else if (arg_str.startsWith("(")) { // Handle indirect expressions addressingMode = "indirect"; @@ -137,12 +125,18 @@ function decodeInstructions(str) { console.groupEnd('Input line'); }; - printMemory.printTable(machineCode); + dbg(2, ''); + dbgGroup(2, 'Memory before filling in label pointers'); + dbgExec(2, () => printMemory.printTable(machineCode)); + dbgGroupEnd(2, 'Memory before filling in label pointers'); // Backfill label pointers for (let k of Object.keys(labels)) { + dbgGroup(2, `@${k}`); let label = labels[k]; - dbg(2, label); + dbg(2, `pointsToByte: ${label.pointsToByte}`); + dbg(2, `bytesToReplace: ${label.bytesToReplace}`); + dbgGroupEnd(2, `label`); for (let j = 0; j < label.bytesToReplace.length; j++) { machineCode[label.bytesToReplace[j]] = label.pointsToByte; } @@ -164,9 +158,11 @@ function stripWhitespaceFromEnds(line) { function hex2num(hex) { return parseInt(hex, 16) }; -function dbg(debugLevel, string) { - if (debugLevel >= DEBUG) console.log(string); -} +// 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