From 6545df9cfdc5241ec4fb5e846cf89ca5c248433d Mon Sep 17 00:00:00 2001 From: n loewen Date: Mon, 28 Aug 2023 09:10:21 -0400 Subject: [PATCH] cpu - WIP - Start re-configuring CLI interface, in order to allow passing clock speed as a CLI argument (while also keeping a defualt value in machine.config.js) --- cpu.js | 6 +++--- issues/todo.md | 4 ++++ run-cpu.js | 40 ++++++++++++++++++++++++++++------------ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/cpu.js b/cpu.js index dbfa40c..fa1d567 100644 --- a/cpu.js +++ b/cpu.js @@ -3,7 +3,7 @@ const readlineSync = require('readline-sync'); const { INITIAL_IP_ADDRESS, - CYCLE_LIMIT, + DEFAULT_CYCLE_LIMIT, KEYPAD_ADDR, KEY_MAP, } = require('./machine.config'); @@ -340,8 +340,8 @@ async function stepCPU(debugInfo, debug = false, prettyPrintDisplay = false) { CPU.cycleCounter += 1; } logCPUState(debugInfo, debug, prettyPrintDisplay); - if (CYCLE_LIMIT) { // Temporary limit as a lazy way to halt infinite loops - if (CPU.cycleCounter >= CYCLE_LIMIT) { + if (DEFAULT_CYCLE_LIMIT) { // Temporary limit as a lazy way to halt infinite loops + if (CPU.cycleCounter >= DEFAULT_CYCLE_LIMIT) { console.warn(' HALTING - reached cycle limit'); CPU.running = false; } diff --git a/issues/todo.md b/issues/todo.md index 77380a2..e29a352 100644 --- a/issues/todo.md +++ b/issues/todo.md @@ -8,6 +8,9 @@ For extended commentary, see [issues](issues.md). ### Todo +- Finish WIP on run-cli arg parsing +- Pass CYCLE_COUNT as a cli arg + - (cpu) !! Fix overflow flag - Add a flag for bank-switching to the ~zero-page - Remove run-scripts and add the ability to run `./cpu.js` and `./assembler.js` directly -- cf. [#1](issues.md#1---improve-cli-interface) @@ -15,6 +18,7 @@ For extended commentary, see [issues](issues.md). ### Features +- (cpu) allow arrow keys, too - [fix] (cpu) Execute `JMP $FF` on startup / Implement ROM — see [#2](issues.md#2---startup-execute-jmp-ff) - (assembler) Validate labels - (assembler) Extract debugging to its own module diff --git a/run-cpu.js b/run-cpu.js index a9cd7d1..face7d4 100755 --- a/run-cpu.js +++ b/run-cpu.js @@ -7,19 +7,13 @@ const computer = require('./cpu.js'); const assembler = require('./assembler.js'); const { logRunningHeader } = require('./logging.js'); - // Load file... -let filename = null; -process.argv.forEach((arg, index) => { - if (arg === '-f' && process.argv.length > (index -1)) { - filename = process.argv[index + 1]; - } -}); - -if (!filename) { - console.error('Filename required'); - console.error('eg: run-cpu.js -f code.asm'); +let filename; +try { + filename = getArgumentValue('-f', `Missing filename`); +} catch (error) { + console.error(error.message); process.exit() } @@ -52,4 +46,26 @@ computer.runProgram( singleStep, prettyPrint, speed -); \ No newline at end of file +); + + +// CLI args TODO +// - check if value is the name of another arg +// - usage info +// - catch nonexistant flags + +/** + * @param {string} flag - The command line flag, eg. '-f' + * @param {string} errorMessage - The error to throw if a value isn't found + * @returns {string} + **/ +function getArgumentValue(flag, errorMessage) { + let value = null; + process.argv.forEach((arg, index) => { + if (arg === flag && process.argv.length > (index -1)) { + value = process.argv[index + 1]; + } + }); + if (!value) throw new Error(errorMessage); + return value; +}