diff --git a/cpu.js b/cpu.js index 38b7c90..abe7cb3 100644 --- a/cpu.js +++ b/cpu.js @@ -404,17 +404,16 @@ exports.singleStepProgram = (code, debugInfo, debug = false, prettyPrintDisplay * @param {Boolean} [debug] - Enable/disable debugging printouts **/ function logCPUState(debugInfo, debug = false, prettyPrintDisplay = false) { - console.group(`Line ${debugInfo[CPU.previousIP].lineNumber} - Step ${CPU.cycleCounter}`); + debugInfo = debugInfo[CPU.previousIP] !== 'undefined' ? debugInfo[CPU.previousIP] : false; + console.group(`Step ${CPU.cycleCounter}`); console.log(); if (!debug) console.clear(); - if (debug && !prettyPrintDisplay) { - display.printDisplay(CPU.memory); - } else { - display.prettyPrintDisplay(CPU.memory); + display.show(CPU.memory, prettyPrintDisplay); + console.log(); + if (debugInfo) { + console.log(`Line ${debugInfo.lineNumber}: ${debugInfo.source}`); + console.log(); } - console.log(); - console.log(`Line ${debugInfo[CPU.previousIP].lineNumber}: ${debugInfo[CPU.previousIP].source}`); - console.log(); console.log('Mnemonic:', CPU.currentInstruction.mnemonic); console.log(`Machine: $${num2hex(CPU.currentInstruction.opcode)} $${num2hex(CPU.currentInstruction.operand)}`); console.log(); diff --git a/display.js b/display.js index 94eae3b..efa4c34 100644 --- a/display.js +++ b/display.js @@ -2,29 +2,21 @@ const { POINTER_TO_DISPLAY } = require('./machine.config'); const { num2hex } = require('./logging.js'); /** - * Print the contents of display memory as hex number + * Print the contents of display memory + * by default, each pixel is shown as a hex number * @param {Uint8Array} mem - CPU memory + * @param {Boolean} pretty - Display pixels using black and white emoji circles **/ -const printDisplay = (mem) => { - const disp = mem[POINTER_TO_DISPLAY]; - for (let i = disp; i < disp + 25; i += 5) { - console.log(`${num2hex(mem[i])} ${num2hex(mem[i+1])} ${num2hex(mem[i+2])} ${num2hex(mem[i+3])} ${num2hex(mem[i+4])}`); - } -} - -/** - * Print the contents of display memory using black and white emoji circles - * @param {Uint8Array} mem - CPU memory - **/ -const prettyPrintDisplay = (mem) => { +const printDisplay = (mem, pretty=false) => { const disp = mem[POINTER_TO_DISPLAY]; const num2pic = (n) => n > 0 ? '⚫' : '⚪'; + let fmt = (n) => num2hex(n); + if (pretty) fmt = (n) => num2pic(n); for (let i = disp; i < disp + 25; i += 5) { - console.log(`${num2pic(mem[i])} ${num2pic(mem[i+1])} ${num2pic(mem[i+2])} ${num2pic(mem[i+3])} ${num2pic(mem[i+4])}`); + console.log(`${fmt(mem[i])} ${fmt(mem[i+1])} ${fmt(mem[i+2])} ${fmt(mem[i+3])} ${fmt(mem[i+4])}`); } } module.exports = { - "printDisplay": printDisplay, - "prettyPrintDisplay": prettyPrintDisplay, + "show": printDisplay, } \ No newline at end of file