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"
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=<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;
int ui_font_size1;
int ui_font_size2;
bool real_speed;
bool throttle;
bool verbose;
bool allow_shutdn;
} Config;
extern Config config;

View file

@ -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 );

View file

@ -1,6 +1,7 @@
#include <string.h>
#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;
}