Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 708:0b8b5eaa3b52
prev700:4650d0c7f6f9
next723:a13e5529c52a
author nkeynes
date Wed Jun 25 10:39:05 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Remove superfluous call to gdrom_get_native_devices()
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@30
     3
 *
nkeynes@30
     4
 * Main program, initializes dreamcast and gui, then passes control off to
nkeynes@537
     5
 * the main loop. 
nkeynes@30
     6
 *
nkeynes@30
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@30
     8
 *
nkeynes@30
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@30
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@30
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@30
    12
 * (at your option) any later version.
nkeynes@30
    13
 *
nkeynes@30
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@30
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@30
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@30
    17
 * GNU General Public License for more details.
nkeynes@1
    18
 */
nkeynes@1
    19
nkeynes@678
    20
#include <stdlib.h>
nkeynes@68
    21
#include <unistd.h>
nkeynes@94
    22
#include <getopt.h>
nkeynes@669
    23
#include <glib/gi18n.h>
nkeynes@669
    24
#include "lxdream.h"
nkeynes@105
    25
#include "syscall.h"
nkeynes@422
    26
#include "mem.h"
nkeynes@27
    27
#include "dreamcast.h"
nkeynes@422
    28
#include "display.h"
nkeynes@422
    29
#include "loader.h"
nkeynes@450
    30
#include "gui.h"
nkeynes@106
    31
#include "aica/audio.h"
nkeynes@422
    32
#include "gdrom/gdrom.h"
nkeynes@144
    33
#include "maple/maple.h"
nkeynes@586
    34
#include "sh4/sh4.h"
nkeynes@11
    35
nkeynes@700
    36
char *option_list = "a:A:c:dhHl:m:npt:T:uvV:x?";
nkeynes@700
    37
struct option longopts[] = {
nkeynes@700
    38
        { "aica", required_argument, NULL, 'a' },
nkeynes@700
    39
        { "audio", required_argument, NULL, 'A' },
nkeynes@700
    40
        { "config", required_argument, NULL, 'c' },
nkeynes@700
    41
        { "debugger", no_argument, NULL, 'D' },
nkeynes@700
    42
        { "help", no_argument, NULL, 'h' },
nkeynes@700
    43
        { "headless", no_argument, NULL, 'H' },
nkeynes@700
    44
        { "log", required_argument, NULL,'l' }, 
nkeynes@700
    45
        { "multiplier", required_argument, NULL, 'm' },
nkeynes@700
    46
        { "run-time", required_argument, NULL, 't' },
nkeynes@700
    47
        { "trace", required_argument, NULL, 'T' },
nkeynes@700
    48
        { "unsafe", no_argument, NULL, 'u' },
nkeynes@700
    49
        { "video", no_argument, NULL, 'V' },
nkeynes@700
    50
        { "version", no_argument, NULL, 'v' }, 
nkeynes@700
    51
        { NULL, 0, 0, 0 } };
nkeynes@68
    52
char *aica_program = NULL;
nkeynes@531
    53
char *display_driver_name = NULL;
nkeynes@531
    54
char *audio_driver_name = NULL;
nkeynes@586
    55
char *trace_regions = NULL;
nkeynes@68
    56
gboolean start_immediately = FALSE;
nkeynes@586
    57
gboolean no_start = FALSE;
nkeynes@77
    58
gboolean headless = FALSE;
nkeynes@402
    59
gboolean use_xlat = TRUE;
nkeynes@392
    60
gboolean show_debugger = FALSE;
nkeynes@414
    61
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    62
nkeynes@700
    63
void print_version()
nkeynes@700
    64
{
nkeynes@700
    65
    printf( "lxdream " APP_VERSION "\n" );
nkeynes@700
    66
}
nkeynes@700
    67
nkeynes@700
    68
void print_usage()
nkeynes@700
    69
{
nkeynes@700
    70
    print_version();
nkeynes@700
    71
    printf( "Usage: lxdream [options] [disc-file] [program-file]\n\n" );
nkeynes@700
    72
    
nkeynes@700
    73
    printf( "Options:\n" );
nkeynes@700
    74
    printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
nkeynes@700
    75
    printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
nkeynes@700
    76
    printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
nkeynes@700
    77
    printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
nkeynes@700
    78
    printf( "   -h, --help             %s\n", _("Display this usage information") );
nkeynes@700
    79
    printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
nkeynes@700
    80
    printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
nkeynes@700
    81
    printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
nkeynes@700
    82
    printf( "   -n                     %s\n", _("Don't start running immediately") );
nkeynes@700
    83
    printf( "   -p                     %s\n", _("Start running immediately on startup") );
nkeynes@700
    84
    printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
nkeynes@700
    85
    printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
nkeynes@700
    86
    printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
nkeynes@700
    87
    printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
nkeynes@700
    88
    printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
nkeynes@700
    89
    printf( "   -x                     %s\n", _("Disable the SH4 translator") );
nkeynes@700
    90
}
nkeynes@700
    91
nkeynes@30
    92
int main (int argc, char *argv[])
nkeynes@1
    93
{
nkeynes@669
    94
    int opt;
nkeynes@372
    95
    double t;
nkeynes@531
    96
    gboolean display_ok;
nkeynes@689
    97
    uint32_t time_secs, time_nanos;
nkeynes@495
    98
nkeynes@495
    99
    install_crash_handler();
nkeynes@1
   100
#ifdef ENABLE_NLS
nkeynes@68
   101
    bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
nkeynes@68
   102
    textdomain (PACKAGE);
nkeynes@1
   103
#endif
nkeynes@531
   104
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
   105
nkeynes@94
   106
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@68
   107
	switch( opt ) {
nkeynes@68
   108
	case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@68
   109
	    aica_program = optarg;
nkeynes@68
   110
	    break;
nkeynes@700
   111
    case 'A': /* Audio driver */
nkeynes@700
   112
        audio_driver_name = optarg;
nkeynes@700
   113
        if( strcmp(audio_driver_name, "?") == 0 ) {
nkeynes@700
   114
            print_version();
nkeynes@700
   115
            print_audio_drivers(stdout);
nkeynes@700
   116
            exit(0);
nkeynes@700
   117
        }
nkeynes@700
   118
        break;
nkeynes@144
   119
	case 'c': /* Config file */
nkeynes@450
   120
	    lxdream_set_config_filename(optarg);
nkeynes@144
   121
	    break;
nkeynes@700
   122
	case 'd': /* Launch w/ debugger */
nkeynes@392
   123
	    show_debugger = TRUE;
nkeynes@392
   124
	    break;
nkeynes@700
   125
    case 'h': /* help */
nkeynes@700
   126
    case '?':
nkeynes@700
   127
        print_usage();
nkeynes@700
   128
        exit(0);
nkeynes@700
   129
        break;
nkeynes@700
   130
    case 'H': /* Headless - shorthand for -V null */
nkeynes@700
   131
        display_driver_name = "null";
nkeynes@700
   132
        break;
nkeynes@700
   133
    case 'l': /* Log verbosity */
nkeynes@700
   134
        if( !set_global_log_level(optarg) ) {
nkeynes@700
   135
            ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@700
   136
        }
nkeynes@700
   137
        break;
nkeynes@414
   138
	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@414
   139
	    t = strtod(optarg, NULL);
nkeynes@414
   140
	    sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@414
   141
	    break;
nkeynes@586
   142
	case 'n': /* Don't start immediately */
nkeynes@586
   143
	    no_start = TRUE;
nkeynes@586
   144
	    start_immediately = FALSE;
nkeynes@586
   145
	    break;
nkeynes@68
   146
	case 'p': /* Start immediately */
nkeynes@68
   147
	    start_immediately = TRUE;
nkeynes@586
   148
	    no_start = FALSE;
nkeynes@182
   149
	    break;
nkeynes@689
   150
	case 't': /* Time limit + auto quit */
nkeynes@372
   151
	    t = strtod(optarg, NULL);
nkeynes@372
   152
	    time_secs = (uint32_t)t;
nkeynes@372
   153
	    time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@689
   154
	    dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@689
   155
	    dreamcast_set_exit_on_stop( TRUE );
nkeynes@379
   156
	    break;
nkeynes@586
   157
	case 'T': /* trace regions */
nkeynes@586
   158
	    trace_regions = optarg;
nkeynes@698
   159
	    set_global_log_level("trace");
nkeynes@586
   160
	    break;
nkeynes@700
   161
    case 'u': /* Allow unsafe dcload syscalls */
nkeynes@700
   162
        dcload_set_allow_unsafe(TRUE);
nkeynes@700
   163
        break;
nkeynes@700
   164
	case 'v': 
nkeynes@700
   165
	    print_version();
nkeynes@700
   166
	    exit(0);
nkeynes@586
   167
	    break;
nkeynes@700
   168
    case 'V': /* Video driver */
nkeynes@700
   169
        display_driver_name = optarg;
nkeynes@700
   170
        if( strcmp(display_driver_name,"?") == 0 ) {
nkeynes@700
   171
            print_version();
nkeynes@700
   172
            print_display_drivers(stdout);
nkeynes@700
   173
            exit(0);
nkeynes@700
   174
        }
nkeynes@700
   175
        break;
nkeynes@402
   176
	case 'x': /* Disable translator */
nkeynes@402
   177
	    use_xlat = FALSE;
nkeynes@379
   178
	    break;
nkeynes@68
   179
	}
nkeynes@68
   180
    }
nkeynes@30
   181
nkeynes@450
   182
    lxdream_load_config( );
nkeynes@691
   183
    gdrom_list_init();
nkeynes@691
   184
    
nkeynes@68
   185
    if( aica_program == NULL ) {
nkeynes@700
   186
        dreamcast_init();
nkeynes@68
   187
    } else {
nkeynes@700
   188
        dreamcast_configure_aica_only();
nkeynes@700
   189
        mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@68
   190
    }
nkeynes@586
   191
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   192
nkeynes@697
   193
    audio_init_driver( audio_driver_name, 44100, AUDIO_FMT_16ST );
nkeynes@697
   194
    
nkeynes@700
   195
    headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
nkeynes@106
   196
    if( headless ) {
nkeynes@700
   197
        display_set_driver( &display_null_driver );
nkeynes@106
   198
    } else {
nkeynes@700
   199
        gui_init(show_debugger);
nkeynes@435
   200
nkeynes@700
   201
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@700
   202
        if( display_driver == NULL ) {
nkeynes@700
   203
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@700
   204
            exit(2);
nkeynes@700
   205
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@700
   206
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@700
   207
                    display_driver->name );
nkeynes@700
   208
            exit(2);
nkeynes@700
   209
        }
nkeynes@106
   210
    }
nkeynes@106
   211
nkeynes@144
   212
    maple_reattach_all();
nkeynes@180
   213
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   214
nkeynes@446
   215
    for( ; optind < argc; optind++ ) {
nkeynes@700
   216
        gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@700
   217
        if( !ok ) {
nkeynes@700
   218
            ok = file_load_magic( argv[optind] );
nkeynes@700
   219
        }
nkeynes@700
   220
        if( !ok ) {
nkeynes@700
   221
            ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@700
   222
        }
nkeynes@700
   223
        if( !no_start ) {
nkeynes@700
   224
            start_immediately = ok;
nkeynes@700
   225
        }
nkeynes@144
   226
    }
nkeynes@144
   227
nkeynes@464
   228
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@700
   229
        const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@678
   230
        if( disc_file != NULL ) {
nkeynes@678
   231
            gdrom_mount_image( disc_file );
nkeynes@678
   232
        }
nkeynes@464
   233
    }
nkeynes@464
   234
nkeynes@379
   235
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   236
nkeynes@689
   237
    if( headless ) {
nkeynes@689
   238
        dreamcast_run();
nkeynes@689
   239
    } else {
nkeynes@681
   240
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   241
    }
nkeynes@671
   242
    dreamcast_shutdown();
nkeynes@68
   243
    return 0;
nkeynes@1
   244
}
nkeynes@1
   245
.