From 7f4d5bebf469bcb30693a9e85d6d39bd77cd02bc Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 17 Apr 2024 14:45:30 +0200 Subject: [PATCH] use getpot for parsing args --- src/config.c | 75 +++++++++++++++++++++++++++++++++++++++++--------- src/config.h | 3 +- src/emulator.c | 2 +- src/opcodes.c | 6 ++-- 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/config.c b/src/config.c index dcb6b9d..f320f33 100644 --- a/src/config.c +++ b/src/config.c @@ -1,3 +1,8 @@ +#include +#include +#include +#include + #include "config.h" Config config = { @@ -7,23 +12,67 @@ Config config = { .ui_font2 = "/usr/share/fonts/TTF/unifont.ttf", .ui_font_size1 = 6, .ui_font_size2 = 4, - .real_speed = true, - .verbose = true, + .throttle = false, + .verbose = false, + .allow_shutdn = false, }; void parse_args( int argc, char* argv[] ) { - while ( --argc ) { - argv++; - if ( argv[ 0 ][ 0 ] == '-' ) { - switch ( argv[ 0 ][ 1 ] ) { - case 't': - config.real_speed = true; - break; - case 'r': - config.real_speed = false; - break; - } + int option_index; + int c = '?'; + char* optstring = "hVs:"; + struct option long_options[] = { + {"help", no_argument, NULL, 'h' }, + {"verbose", no_argument, NULL, 'V' }, + {"scale", required_argument, NULL, 's' }, + {"throttle", no_argument, NULL, 1800}, + {"allow-shutdown", no_argument, NULL, 1900}, + + {0, 0, 0, 0 } + }; + + char* help_text = "usage: %s [options]\n" + "options:\n" + " -h --help what you are reading\n" + " -V --verbose be verbose (default: false)\n" + " -s --scale= scale GUI (default: 3)\n" + " --throttle Throttle speed (default: false)\n" + " --allow-shutdown Enable SHUTDN instruction (default: false)\n"; + while ( c != EOF ) { + c = getopt_long( argc, argv, optstring, long_options, &option_index ); + + switch ( c ) { + case 'h': + fprintf( stderr, help_text, config.progname ); + exit( 0 ); + break; + case 's': + config.ui_scale = atoi( optarg ); + break; + case 'V': + config.verbose = true; + break; + case 1800: + config.throttle = true; + break; + case 1900: + config.allow_shutdn = true; + break; + + case '?': + case ':': + exit( 0 ); + break; + default: + break; } } + + if ( optind < argc ) { + fprintf( stderr, "Invalid arguments : " ); + while ( optind < argc ) + fprintf( stderr, "%s\n", argv[ optind++ ] ); + fprintf( stderr, "\n" ); + } } diff --git a/src/config.h b/src/config.h index efd3d27..b24ba05 100644 --- a/src/config.h +++ b/src/config.h @@ -10,8 +10,9 @@ typedef struct { char* ui_font2; int ui_font_size1; int ui_font_size2; - bool real_speed; + bool throttle; bool verbose; + bool allow_shutdn; } Config; extern Config config; diff --git a/src/emulator.c b/src/emulator.c index 8d1e8cf..9928be0 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -117,7 +117,7 @@ bool emulator_run( void ) if ( !cpu.shutdown ) { execute_instruction(); - throttle( config.real_speed || cpu.keyintp ); + throttle( config.throttle || cpu.keyintp ); if ( emulator_state == EMULATOR_STEP ) emulator_set_state( EMULATOR_STOP ); diff --git a/src/opcodes.c b/src/opcodes.c index fa6054c..e91c9c0 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -1,6 +1,7 @@ #include #include "types.h" +#include "config.h" #include "cpu.h" #include "bus.h" #include "keyboard.h" @@ -367,9 +368,8 @@ static void op806( byte* opc ) // C=ID static void op807( byte* opc ) // SHUTDN { // TODO: Fix SHUTDN - /* if ( !cpu.in[ 0 ] && !cpu.in[ 1 ] && !cpu.in[ 3 ] ) { */ - /* cpu.shutdown = true; */ - /* } */ + cpu.shutdown = config.allow_shutdn && ( !cpu.in[ 0 ] && !cpu.in[ 1 ] && !cpu.in[ 3 ] ); + cpu.pc += 3; cpu.cycles += 5; }