44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const readline = require('readline');
|
|
|
|
const CFG = require('./machine.config.js');
|
|
const { num2hex } = require('./logging.js');
|
|
|
|
function readKeyMem(mem) {
|
|
return mem[CFG.keypadAddr];
|
|
}
|
|
|
|
function getKeypadInput(cpu) {
|
|
readline.emitKeypressEvents(process.stdin);
|
|
if (process.stdin.setRawMode != null) {
|
|
process.stdin.setRawMode(true);
|
|
}
|
|
process.stdin.on('keypress', (str, key) => {
|
|
if (key.sequence === '\x03') process.exit();
|
|
let name = key.name.toUpperCase();
|
|
if (name in CFG.keyMap) {
|
|
cpu.memory[CFG.keypadAddr] = CFG.keyMap[name];
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
**/
|
|
function showDisplay(mem, pretty=false) {
|
|
const disp = mem[CFG.pointerToDisplay];
|
|
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(`${fmt(mem[i])} ${fmt(mem[i+1])} ${fmt(mem[i+2])} ${fmt(mem[i+3])} ${fmt(mem[i+4])}`);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
"showDisplay": showDisplay,
|
|
"getKeypadInput": getKeypadInput,
|
|
"readKeyMem": readKeyMem,
|
|
} |