use getpot for parsing args

This commit is contained in:
Gwenhael Le Moine 2024-04-17 14:45:30 +02:00
parent 7c9ab6de42
commit 7f4d5bebf4
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
4 changed files with 68 additions and 18 deletions

View file

@ -1,3 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <getopt.h>
#include "config.h" #include "config.h"
Config config = { Config config = {
@ -7,23 +12,67 @@ Config config = {
.ui_font2 = "/usr/share/fonts/TTF/unifont.ttf", .ui_font2 = "/usr/share/fonts/TTF/unifont.ttf",
.ui_font_size1 = 6, .ui_font_size1 = 6,
.ui_font_size2 = 4, .ui_font_size2 = 4,
.real_speed = true, .throttle = false,
.verbose = true, .verbose = false,
.allow_shutdn = false,
}; };
void parse_args( int argc, char* argv[] ) void parse_args( int argc, char* argv[] )
{ {
while ( --argc ) { int option_index;
argv++; int c = '?';
if ( argv[ 0 ][ 0 ] == '-' ) { char* optstring = "hVs:";
switch ( argv[ 0 ][ 1 ] ) { struct option long_options[] = {
case 't': {"help", no_argument, NULL, 'h' },
config.real_speed = true; {"verbose", no_argument, NULL, 'V' },
break; {"scale", required_argument, NULL, 's' },
case 'r': {"throttle", no_argument, NULL, 1800},
config.real_speed = false; {"allow-shutdown", no_argument, NULL, 1900},
break;
} {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=<i> 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" );
}
} }

View file

@ -10,8 +10,9 @@ typedef struct {
char* ui_font2; char* ui_font2;
int ui_font_size1; int ui_font_size1;
int ui_font_size2; int ui_font_size2;
bool real_speed; bool throttle;
bool verbose; bool verbose;
bool allow_shutdn;
} Config; } Config;
extern Config config; extern Config config;

View file

@ -117,7 +117,7 @@ bool emulator_run( void )
if ( !cpu.shutdown ) { if ( !cpu.shutdown ) {
execute_instruction(); execute_instruction();
throttle( config.real_speed || cpu.keyintp ); throttle( config.throttle || cpu.keyintp );
if ( emulator_state == EMULATOR_STEP ) if ( emulator_state == EMULATOR_STEP )
emulator_set_state( EMULATOR_STOP ); emulator_set_state( EMULATOR_STOP );

View file

@ -1,6 +1,7 @@
#include <string.h> #include <string.h>
#include "types.h" #include "types.h"
#include "config.h"
#include "cpu.h" #include "cpu.h"
#include "bus.h" #include "bus.h"
#include "keyboard.h" #include "keyboard.h"
@ -367,9 +368,8 @@ static void op806( byte* opc ) // C=ID
static void op807( byte* opc ) // SHUTDN static void op807( byte* opc ) // SHUTDN
{ {
// TODO: Fix SHUTDN // TODO: Fix SHUTDN
/* if ( !cpu.in[ 0 ] && !cpu.in[ 1 ] && !cpu.in[ 3 ] ) { */ cpu.shutdown = config.allow_shutdn && ( !cpu.in[ 0 ] && !cpu.in[ 1 ] && !cpu.in[ 3 ] );
/* cpu.shutdown = true; */
/* } */
cpu.pc += 3; cpu.pc += 3;
cpu.cycles += 5; cpu.cycles += 5;
} }