From 049d9367ac4b289ac47ececd180637a5a04c80b1 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 29 Aug 2023 12:54:32 -0400 Subject: [PATCH] cardiograph - Change to take input from stdin! --- src/cardiograph.js | 64 +++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/cardiograph.js b/src/cardiograph.js index 4694460..931c8d2 100644 --- a/src/cardiograph.js +++ b/src/cardiograph.js @@ -1,3 +1,4 @@ +const DBG = require('./dbg.js'); const { num2hex, bool2bit } = require('./logging.js'); const CFG = require('./machine.config.js'); @@ -5,25 +6,45 @@ const CPU = require('./cpu.js'); const io = require('./io.js'); -// TODO - replace with reading STDIN: - const assembler = require('./assembler.js'); - const fs = require('fs'); - - const filename = process.argv[2]; - const inputFile_str = fs.readFileSync(filename, 'utf8'); - let assemblerOutput = assembler.assemble(inputFile_str); - +/** SETUP **/ +const dbg = new DBG('nitpick'); let cpu = new CPU(CFG.initialIP, CFG.defaultCycleLimit); -cpu.loadMemory(assemblerOutput.machineCode); -cpu.loadSourceInfo(assemblerOutput.debugInfo); +main(); -cpu.onCycleEnd(tick); -cpu.onCycleEnd(logCPUState); +async function main() { + const input = await readPipedStdin(); + dbg.nit('input: \n', input); + + const debuggable = true; // FIXME - Get this as a command line flag instead + // if debuggable === true, the input is JSON {sourceInfo, machineCode} + // otherwise, the input is a string of space-separated numbers + + let code = null; + let sourceInfo = null; + + if (!debuggable) { + code = new Uint8Array(input.split(' ')); + dbg.nit(code); + } else { + const parsedInput = JSON.parse(input); + code = new Uint8Array(parsedInput.machineCode); + sourceInfo = parsedInput.sourceInfo; + dbg.nit(code); + dbg.nit(sourceInfo); + } + + cpu.loadMemory(code); + if (debuggable) { cpu.loadSourceInfo(sourceInfo); } + + cpu.onCycleEnd(tick); + cpu.onCycleEnd(logCPUState); + + cpu.start(); + io.getKeypadInput(cpu); + cpu.step(); +} -cpu.start(); -io.getKeypadInput(cpu); -cpu.step(); async function tick() { const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) @@ -36,7 +57,10 @@ async function tick() { } function logCPUState() { - let lineInfo = cpu.dbg.sourceInfo[cpu.dbg.previousIP]; + let lineInfo = null; + if (cpu.dbg.sourceInfo) { + lineInfo = cpu.dbg.sourceInfo[cpu.dbg.previousIP]; + } console.group(`Step ${cpu.dbg.cycleCounter}`); console.log(); io.showDisplay(cpu.memory, true); // FIXME - display - allow printing hex as well as pretty-printing @@ -54,14 +78,6 @@ function logCPUState() { console.groupEnd(); }; - -/* -(async function() { - let input = await readPipedStdin(); - console.log(input); -})() -*/ - async function readPipedStdin() { // https://wellingguzman.com/notes/node-pipe-input return new Promise(function (resolve, reject) {