Add 'debug' flag when calling assembler
This commit is contained in:
parent
aa08e50eda
commit
97ab0c6ee0
21
assemble.js
21
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 fs = require('fs');
|
||||||
const assembler = require('./assembler.js');
|
const assembler = require('./assembler.js');
|
||||||
const printMemory = require('./print-memory.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');
|
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||||
let machineCode = assembler.assemble(inputFile_str);
|
|
||||||
console.log();
|
let machineCode;
|
||||||
console.group("Machine code output");
|
|
||||||
|
if (mode === "debug") {
|
||||||
|
machineCode = assembler.assemble(inputFile_str, true);
|
||||||
|
console.group("Machine code output");
|
||||||
printMemory.printTable(machineCode);
|
printMemory.printTable(machineCode);
|
||||||
console.groupEnd('Machine code output');
|
console.groupEnd('Machine code output');
|
||||||
|
} else {
|
||||||
|
machineCode = assembler.assemble(inputFile_str);
|
||||||
|
console.log(machineCode); // TODO print just the numbers
|
||||||
|
}
|
||||||
24
assembler.js
24
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
|
// 2 = what i'm currently focusing on
|
||||||
// 3 = always print
|
// 3 = always print
|
||||||
// 4 = silent
|
// 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 mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp'];
|
||||||
const mnemonics2opcodes = {
|
const mnemonics2opcodes = {
|
||||||
|
|
@ -124,7 +130,7 @@ function decodeInstructions(str) {
|
||||||
|
|
||||||
dbg(1, '');
|
dbg(1, '');
|
||||||
dbgGroup(1, 'Memory before filling in label pointers');
|
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');
|
dbgGroupEnd(1, 'Memory before filling in label pointers');
|
||||||
|
|
||||||
// Backfill label pointers
|
// Backfill label pointers
|
||||||
|
|
@ -156,13 +162,7 @@ function stripWhitespaceFromEnds(line) {
|
||||||
function hex2num(hex) { return parseInt(hex, 16) };
|
function hex2num(hex) { return parseInt(hex, 16) };
|
||||||
|
|
||||||
// Debug helpers
|
// Debug helpers
|
||||||
const dbg = (lvl, s) => { if (lvl >= DEBUG) console.log(s) };
|
const dbg = (lvl, s) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) console.log(s) };
|
||||||
const dbgGroup = (lvl, s) => { if (lvl >= DEBUG) console.group(s) };
|
const dbgGroup = (lvl, s) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) console.group(s) };
|
||||||
const dbgGroupEnd = (lvl, s) => { if (lvl >= DEBUG) console.groupEnd(s) };
|
const dbgGroupEnd = (lvl, s) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) console.groupEnd(s) };
|
||||||
const dbgExec = (lvl, func) => { if (lvl >= DEBUG) func(); }
|
const dbgExec = (lvl, func) => { if (DEBUG && (lvl >= DEBUG_LEVEL)) func(); }
|
||||||
|
|
||||||
// RUN IT
|
|
||||||
|
|
||||||
exports.assemble = (str) => {
|
|
||||||
return decodeInstructions(str);
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
{
|
{
|
||||||
"name": "paper-computer",
|
"name": "paper-computer",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"display": "node assemble-and-run.js run",
|
"rundisplay": "node assemble-and-run.js run",
|
||||||
"debug": "node assemble-and-run.js debug",
|
"rundebug": "node assemble-and-run.js debug",
|
||||||
"assemble": "node assemble.js"
|
"asm": "node assemble.js run",
|
||||||
|
"asmdebug": "node assemble.js debug"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue