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 assembler = require('./assembler.js');
|
||||
const printMemory = require('./print-memory.js');
|
||||
const { logMemory } = require('./print-memory.js');
|
||||
|
||||
const mode = process.argv[2];
|
||||
const filename = process.argv[3];
|
||||
|
|
@ -14,7 +14,7 @@ let machineCode;
|
|||
if (mode === "debug") {
|
||||
machineCode = assembler.assemble(inputFile_str, true);
|
||||
console.group("Machine code output");
|
||||
printMemory.printTable(machineCode);
|
||||
logMemory(machineCode);
|
||||
console.groupEnd('Machine code output');
|
||||
} else {
|
||||
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');
|
||||
|
||||
// 1 = verbose
|
||||
|
|
@ -36,13 +36,13 @@ function decodeInstructions(str) {
|
|||
let IP = INITIAL_IP_ADDRESS;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
dbg(1, '');
|
||||
dbgGroup(1, `Input line ${i}, IP ${IP}`);
|
||||
dbgGroup(1, `Input line ${i}, IP ${num2hex(IP)}`);
|
||||
dbg(3, `> ${lines[i]}`);
|
||||
let line = stripWhitespaceFromEnds(stripComments(lines[i]));
|
||||
|
||||
// Handle blank lines
|
||||
if (line.length === 0) {
|
||||
dbg(3, `IP: ${IP}, new code: none`);
|
||||
dbg(3, `IP: $${num2hex(IP)}, new code: none`);
|
||||
dbg(1, 'blank');
|
||||
dbgGroupEnd(1, 'Input line');
|
||||
continue;
|
||||
|
|
@ -64,7 +64,7 @@ function decodeInstructions(str) {
|
|||
}
|
||||
dbg(2, `pointsToByte: ${labels[label].pointsToByte}`);
|
||||
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');
|
||||
continue;
|
||||
}
|
||||
|
|
@ -123,8 +123,8 @@ function decodeInstructions(str) {
|
|||
|
||||
machineCode.push(op);
|
||||
machineCode.push(arg_num);
|
||||
dbg(3, `IP: $${num2hex(IP)}, new code: $${num2hex(op)} $${num2hex(arg_num)}`);
|
||||
IP += 2;
|
||||
dbg(3, `IP: ${IP}, new code: ${op}, ${arg_num}`);
|
||||
dbgGroupEnd(1, 'Input line');
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
exports.printTable = (x) => {
|
||||
exports.logMemory = (x) => {
|
||||
console.log(`┌────────┬────────┬────────┐`);
|
||||
console.log(`│ addr │ op │ arg │`);
|
||||
console.log(`├────────┼────────┼────────┤`);
|
||||
|
|
@ -11,4 +11,6 @@ exports.printTable = (x) => {
|
|||
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