cardiograph - Change to read command line options using Opter

This commit is contained in:
n loewen 2023-09-23 19:33:58 -07:00
parent 98bfa50269
commit 91cba57aa1
1 changed files with 18 additions and 6 deletions

View File

@ -1,6 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
const fs = require('fs');
const DBG = require('./dbg.js'); const DBG = require('./dbg.js');
const Opter = require('./opter/opter.js');
const { num2hex, bool2bit } = require('./conversions.js'); const { num2hex, bool2bit } = require('./conversions.js');
const CFG = require('./machine.config.js'); const CFG = require('./machine.config.js');
@ -15,16 +18,25 @@ let cpu = new CPU(CFG.initialIP, CFG.defaultCycleLimit);
main(); main();
async function main() { async function main() {
const input = await readPipedStdin(); const opter = new Opter();
opter.addOption('-d', '--debug');
opter.addOption('-i', '--in', false, true, 1);
const opts = opter.parse(process.argv);
const debuggable = true; // FIXME - Get this as a command line flag instead const sourcecodeHasAnnotations = 'debug' in opts;
// if debuggable === true, the input is JSON {sourceInfo, machineCode} // if true, then the input is JSON {sourceInfo, machineCode}
// otherwise, the input is a string of space-separated numbers // otherwise, the input is a string of space-separated numbers
let input = null;
if ('in' in opts) { // Read from file
input = fs.readFileSync(opts.in[0], 'utf8');
} else { // Read from stdin
input = await readPipedStdin();
}
let code = null; let code = null;
let sourceInfo = null; let sourceInfo = null;
if (!sourcecodeHasAnnotations) {
if (!debuggable) {
code = new Uint8Array(input.split(' ')); code = new Uint8Array(input.split(' '));
} else { } else {
const parsedInput = JSON.parse(input); const parsedInput = JSON.parse(input);
@ -33,7 +45,7 @@ async function main() {
} }
cpu.loadMemory(code); cpu.loadMemory(code);
if (debuggable) { cpu.loadSourceInfo(sourceInfo); } if (sourcecodeHasAnnotations) { cpu.loadSourceInfo(sourceInfo); }
cpu.onCycleEnd(tick); cpu.onCycleEnd(tick);
cpu.onCycleEnd(logCPUState); cpu.onCycleEnd(logCPUState);