Turn print-memory.js into a more general-purpose 'printing utils' library -- export num2hex + rename function that prints a table of memory
This commit is contained in:
parent
97ab0c6ee0
commit
9a3d9cffdc
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
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 { logMemory } = require('./print-memory.js');
|
||||||
|
|
||||||
const mode = process.argv[2];
|
const mode = process.argv[2];
|
||||||
const filename = process.argv[3];
|
const filename = process.argv[3];
|
||||||
|
|
@ -14,7 +14,7 @@ let machineCode;
|
||||||
if (mode === "debug") {
|
if (mode === "debug") {
|
||||||
machineCode = assembler.assemble(inputFile_str, true);
|
machineCode = assembler.assemble(inputFile_str, true);
|
||||||
console.group("Machine code output");
|
console.group("Machine code output");
|
||||||
printMemory.printTable(machineCode);
|
logMemory(machineCode);
|
||||||
console.groupEnd('Machine code output');
|
console.groupEnd('Machine code output');
|
||||||
} else {
|
} else {
|
||||||
machineCode = assembler.assemble(inputFile_str);
|
machineCode = assembler.assemble(inputFile_str);
|
||||||
|
|
|
||||||
10
assembler.js
10
assembler.js
|
|
@ -1,4 +1,4 @@
|
||||||
const printMemory = require('./print-memory.js');
|
const { logMemory, num2hex } = require('./print-memory.js');
|
||||||
const { INITIAL_IP_ADDRESS, START_OF_DISPLAY_MEM } = require('./machine.config.js');
|
const { INITIAL_IP_ADDRESS, START_OF_DISPLAY_MEM } = require('./machine.config.js');
|
||||||
|
|
||||||
// 1 = verbose
|
// 1 = verbose
|
||||||
|
|
@ -36,13 +36,13 @@ function decodeInstructions(str) {
|
||||||
let IP = INITIAL_IP_ADDRESS;
|
let IP = INITIAL_IP_ADDRESS;
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
dbg(1, '');
|
dbg(1, '');
|
||||||
dbgGroup(1, `Input line ${i}, IP ${IP}`);
|
dbgGroup(1, `Input line ${i}, IP ${num2hex(IP)}`);
|
||||||
dbg(3, `> ${lines[i]}`);
|
dbg(3, `> ${lines[i]}`);
|
||||||
let line = stripWhitespaceFromEnds(stripComments(lines[i]));
|
let line = stripWhitespaceFromEnds(stripComments(lines[i]));
|
||||||
|
|
||||||
// Handle blank lines
|
// Handle blank lines
|
||||||
if (line.length === 0) {
|
if (line.length === 0) {
|
||||||
dbg(3, `IP: ${IP}, new code: none`);
|
dbg(3, `IP: $${num2hex(IP)}, new code: none`);
|
||||||
dbg(1, 'blank');
|
dbg(1, 'blank');
|
||||||
dbgGroupEnd(1, 'Input line');
|
dbgGroupEnd(1, 'Input line');
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -64,7 +64,7 @@ function decodeInstructions(str) {
|
||||||
}
|
}
|
||||||
dbg(2, `pointsToByte: ${labels[label].pointsToByte}`);
|
dbg(2, `pointsToByte: ${labels[label].pointsToByte}`);
|
||||||
dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`);
|
dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`);
|
||||||
dbg(3, `IP: ${IP}, new code: none`);
|
dbg(3, `IP: $${num2hex(IP)}, new code: none`);
|
||||||
dbgGroupEnd(1, 'Input line');
|
dbgGroupEnd(1, 'Input line');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -123,8 +123,8 @@ function decodeInstructions(str) {
|
||||||
|
|
||||||
machineCode.push(op);
|
machineCode.push(op);
|
||||||
machineCode.push(arg_num);
|
machineCode.push(arg_num);
|
||||||
|
dbg(3, `IP: $${num2hex(IP)}, new code: $${num2hex(op)} $${num2hex(arg_num)}`);
|
||||||
IP += 2;
|
IP += 2;
|
||||||
dbg(3, `IP: ${IP}, new code: ${op}, ${arg_num}`);
|
|
||||||
dbgGroupEnd(1, 'Input line');
|
dbgGroupEnd(1, 'Input line');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
exports.printTable = (x) => {
|
exports.logMemory = (x) => {
|
||||||
console.log(`┌────────┬────────┬────────┐`);
|
console.log(`┌────────┬────────┬────────┐`);
|
||||||
console.log(`│ addr │ op │ arg │`);
|
console.log(`│ addr │ op │ arg │`);
|
||||||
console.log(`├────────┼────────┼────────┤`);
|
console.log(`├────────┼────────┼────────┤`);
|
||||||
|
|
@ -11,4 +11,6 @@ exports.printTable = (x) => {
|
||||||
console.log(`└────────┴────────┴────────┘`);
|
console.log(`└────────┴────────┴────────┘`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const num2hex = (num) => num.toString(16).toUpperCase().padStart(2, "0");
|
const num2hex = (num) => num.toString(16).toUpperCase().padStart(2, "0");
|
||||||
|
|
||||||
|
exports.num2hex = num2hex;
|
||||||
Loading…
Reference in New Issue