logging - Fix memory printing function so that it works for arrays that start or end on odd address numbers

This commit is contained in:
n loewen 2023-08-26 11:07:38 +01:00
parent 902b218547
commit dea1a445f6
3 changed files with 17 additions and 13 deletions

View File

@ -41,6 +41,7 @@ For extended commentary, see [issues](issues.md).
## Closed ## Closed
- 2023-08-26 - [fix] (logging) - 'undefined operand' situation is caused by assembling to an initial IP of $1C, which is an odd number
- (assembler) Pass asm line thru to cpu to print when debugging - (assembler) Pass asm line thru to cpu to print when debugging

View File

@ -6,26 +6,28 @@
* @param {number} [end] - An end-index, in decimal * @param {number} [end] - An end-index, in decimal
**/ **/
const logMemory = (mem, start=0, end=mem.length) => { const logMemory = (mem, start=0, end=mem.length) => {
mem = mem.slice(start, end); let top1 = `┌─────────┬────────┬─────────┐`;
console.log(`┌────────┬────────┬─────────┐`); let top2 = `│ addrs │ opcode │ operand │`;
console.log(`│ addr │ opcode │ operand │`); let top3 = `├─────────┼────────┼─────────┤`;
console.log(`├────────┼────────┼─────────┤`); let blnk = `│ │ │ │`;
let bot1 = `└─────────┴────────┴─────────┘`;
console.log(`${top1}\n${top2}\n${top3}`);
for (let i = start; i < mem.length; i +=2) { for (let i = start; i < mem.length; i +=2) {
let operand = mem[i+1]; let operand = mem[i+1];
if (typeof operand === 'undefined') { if (typeof operand === 'undefined') {
console.warn(' operand undefined'); console.log(` ${num2hex(i)} ${num2hex(i+1)}${num2hex(mem[i])} │ │`);
operand = -1; } else {
console.log(`${num2hex(i)} ${num2hex(i+1)}${num2hex(mem[i])}${num2hex(operand)}`);
} }
console.log(`${num2hex(i)}${num2hex(mem[i])}${num2hex(operand)}`);
// Add a blank row every 4 lines: // Add a blank row every 4 lines:
if (((i + 2) % 8) === 0) { let rowNum = i - start + 2; // Not actually the row number...
if ((i < (mem.length - 2))) { if ((rowNum % 8 === 0)
console.log(`│ │ │ │`); && (i < (mem.length - 2))) {
} console.log(blnk);
} }
} }
console.log(`└────────┴────────┴─────────┘`); console.log(bot1);
} }
const logRunningHeader = () => { const logRunningHeader = () => {

View File

@ -8,6 +8,7 @@ const fs = require('fs');
const assembler = require('./assembler.js'); const assembler = require('./assembler.js');
const { logMemory, num2hex, num2bin } = require('./logging.js'); const { logMemory, num2hex, num2bin } = require('./logging.js');
const { machine } = require('os'); const { machine } = require('os');
const machineConfig = require('./machine.config.js');
const mode = process.argv[2]; const mode = process.argv[2];
const filename = process.argv[3]; const filename = process.argv[3];
@ -19,7 +20,7 @@ if (mode === "debug") {
assembler_output = assembler.assemble(inputFile_str, true); assembler_output = assembler.assemble(inputFile_str, true);
console.log(''); console.log('');
console.group("Machine code output"); console.group("Machine code output");
logMemory(assembler_output.machineCode); logMemory(assembler_output.machineCode, machineConfig.INITIAL_IP_ADDRESS);
console.groupEnd(); console.groupEnd();
} else { } else {
assembler_output = assembler.assemble(inputFile_str); assembler_output = assembler.assemble(inputFile_str);