Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 968:6fb1481859a4
prev866:86cd01c2b2d3
next998:1754a8c6a9cf
author nkeynes
date Thu Jan 15 11:23:20 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix various compile-time warnings
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@772
    23
#include "lxdream.h"
nkeynes@755
    24
#include "gettext.h"
nkeynes@422
    25
#include "mem.h"
nkeynes@27
    26
#include "dreamcast.h"
nkeynes@759
    27
#include "dream.h"
nkeynes@422
    28
#include "display.h"
nkeynes@759
    29
#include "gui.h"
nkeynes@759
    30
#include "gdlist.h"
nkeynes@759
    31
#include "syscall.h"
nkeynes@422
    32
#include "loader.h"
nkeynes@106
    33
#include "aica/audio.h"
nkeynes@422
    34
#include "gdrom/gdrom.h"
nkeynes@144
    35
#include "maple/maple.h"
nkeynes@586
    36
#include "sh4/sh4.h"
nkeynes@11
    37
nkeynes@723
    38
nkeynes@700
    39
char *option_list = "a:A:c:dhHl:m:npt:T:uvV:x?";
nkeynes@700
    40
struct option longopts[] = {
nkeynes@700
    41
        { "aica", required_argument, NULL, 'a' },
nkeynes@700
    42
        { "audio", required_argument, NULL, 'A' },
nkeynes@700
    43
        { "config", required_argument, NULL, 'c' },
nkeynes@700
    44
        { "debugger", no_argument, NULL, 'D' },
nkeynes@700
    45
        { "help", no_argument, NULL, 'h' },
nkeynes@700
    46
        { "headless", no_argument, NULL, 'H' },
nkeynes@700
    47
        { "log", required_argument, NULL,'l' }, 
nkeynes@700
    48
        { "multiplier", required_argument, NULL, 'm' },
nkeynes@700
    49
        { "run-time", required_argument, NULL, 't' },
nkeynes@700
    50
        { "trace", required_argument, NULL, 'T' },
nkeynes@700
    51
        { "unsafe", no_argument, NULL, 'u' },
nkeynes@700
    52
        { "video", no_argument, NULL, 'V' },
nkeynes@700
    53
        { "version", no_argument, NULL, 'v' }, 
nkeynes@700
    54
        { NULL, 0, 0, 0 } };
nkeynes@68
    55
char *aica_program = NULL;
nkeynes@531
    56
char *display_driver_name = NULL;
nkeynes@531
    57
char *audio_driver_name = NULL;
nkeynes@586
    58
char *trace_regions = NULL;
nkeynes@68
    59
gboolean start_immediately = FALSE;
nkeynes@586
    60
gboolean no_start = FALSE;
nkeynes@77
    61
gboolean headless = FALSE;
nkeynes@402
    62
gboolean use_xlat = TRUE;
nkeynes@392
    63
gboolean show_debugger = FALSE;
nkeynes@414
    64
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    65
nkeynes@968
    66
static void print_version()
nkeynes@700
    67
{
nkeynes@738
    68
    printf( "lxdream %s\n", lxdream_full_version );
nkeynes@700
    69
}
nkeynes@700
    70
nkeynes@968
    71
static void print_usage()
nkeynes@700
    72
{
nkeynes@700
    73
    print_version();
nkeynes@738
    74
    printf( "Usage: lxdream %s [options] [disc-file] [program-file]\n\n", lxdream_full_version );
nkeynes@736
    75
nkeynes@700
    76
    printf( "Options:\n" );
nkeynes@700
    77
    printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
nkeynes@700
    78
    printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
nkeynes@700
    79
    printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
nkeynes@700
    80
    printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
nkeynes@700
    81
    printf( "   -h, --help             %s\n", _("Display this usage information") );
nkeynes@700
    82
    printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
nkeynes@700
    83
    printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
nkeynes@700
    84
    printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
nkeynes@700
    85
    printf( "   -n                     %s\n", _("Don't start running immediately") );
nkeynes@700
    86
    printf( "   -p                     %s\n", _("Start running immediately on startup") );
nkeynes@700
    87
    printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
nkeynes@700
    88
    printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
nkeynes@700
    89
    printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
nkeynes@700
    90
    printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
nkeynes@700
    91
    printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
nkeynes@700
    92
    printf( "   -x                     %s\n", _("Disable the SH4 translator") );
nkeynes@700
    93
}
nkeynes@700
    94
nkeynes@968
    95
static void bind_gettext_domain()
nkeynes@723
    96
{
nkeynes@723
    97
#ifdef ENABLE_NLS
nkeynes@866
    98
    bindtextdomain( PACKAGE, get_locale_path() );
nkeynes@723
    99
    textdomain(PACKAGE);
nkeynes@723
   100
#endif
nkeynes@723
   101
}
nkeynes@723
   102
nkeynes@30
   103
int main (int argc, char *argv[])
nkeynes@1
   104
{
nkeynes@669
   105
    int opt;
nkeynes@372
   106
    double t;
nkeynes@531
   107
    gboolean display_ok;
nkeynes@689
   108
    uint32_t time_secs, time_nanos;
nkeynes@495
   109
nkeynes@495
   110
    install_crash_handler();
nkeynes@723
   111
    bind_gettext_domain();
nkeynes@531
   112
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
   113
nkeynes@94
   114
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@736
   115
        switch( opt ) {
nkeynes@736
   116
        case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@736
   117
            aica_program = optarg;
nkeynes@736
   118
            break;
nkeynes@736
   119
        case 'A': /* Audio driver */
nkeynes@736
   120
            audio_driver_name = optarg;
nkeynes@736
   121
            if( strcmp(audio_driver_name, "?") == 0 ) {
nkeynes@736
   122
                print_version();
nkeynes@736
   123
                print_audio_drivers(stdout);
nkeynes@736
   124
                exit(0);
nkeynes@736
   125
            }
nkeynes@736
   126
            break;
nkeynes@736
   127
        case 'c': /* Config file */
nkeynes@736
   128
            lxdream_set_config_filename(optarg);
nkeynes@736
   129
            break;
nkeynes@736
   130
        case 'd': /* Launch w/ debugger */
nkeynes@736
   131
            show_debugger = TRUE;
nkeynes@736
   132
            break;
nkeynes@736
   133
        case 'h': /* help */
nkeynes@736
   134
        case '?':
nkeynes@736
   135
            print_usage();
nkeynes@736
   136
            exit(0);
nkeynes@736
   137
            break;
nkeynes@736
   138
        case 'H': /* Headless - shorthand for -V null */
nkeynes@736
   139
            display_driver_name = "null";
nkeynes@736
   140
            break;
nkeynes@736
   141
        case 'l': /* Log verbosity */
nkeynes@736
   142
            if( !set_global_log_level(optarg) ) {
nkeynes@736
   143
                ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@736
   144
            }
nkeynes@736
   145
            break;
nkeynes@736
   146
        case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@736
   147
            t = strtod(optarg, NULL);
nkeynes@736
   148
            sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@736
   149
            break;
nkeynes@736
   150
        case 'n': /* Don't start immediately */
nkeynes@736
   151
            no_start = TRUE;
nkeynes@736
   152
            start_immediately = FALSE;
nkeynes@736
   153
            break;
nkeynes@736
   154
        case 'p': /* Start immediately */
nkeynes@736
   155
            start_immediately = TRUE;
nkeynes@736
   156
            no_start = FALSE;
nkeynes@736
   157
            break;
nkeynes@736
   158
        case 't': /* Time limit + auto quit */
nkeynes@736
   159
            t = strtod(optarg, NULL);
nkeynes@736
   160
            time_secs = (uint32_t)t;
nkeynes@736
   161
            time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@736
   162
            dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@736
   163
            dreamcast_set_exit_on_stop( TRUE );
nkeynes@736
   164
            break;
nkeynes@736
   165
        case 'T': /* trace regions */
nkeynes@736
   166
            trace_regions = optarg;
nkeynes@736
   167
            set_global_log_level("trace");
nkeynes@736
   168
            break;
nkeynes@736
   169
        case 'u': /* Allow unsafe dcload syscalls */
nkeynes@736
   170
            dcload_set_allow_unsafe(TRUE);
nkeynes@736
   171
            break;
nkeynes@736
   172
        case 'v': 
nkeynes@700
   173
            print_version();
nkeynes@700
   174
            exit(0);
nkeynes@736
   175
            break;
nkeynes@736
   176
        case 'V': /* Video driver */
nkeynes@736
   177
            display_driver_name = optarg;
nkeynes@736
   178
            if( strcmp(display_driver_name,"?") == 0 ) {
nkeynes@736
   179
                print_version();
nkeynes@736
   180
                print_display_drivers(stdout);
nkeynes@736
   181
                exit(0);
nkeynes@736
   182
            }
nkeynes@736
   183
            break;
nkeynes@736
   184
        case 'x': /* Disable translator */
nkeynes@736
   185
            use_xlat = FALSE;
nkeynes@736
   186
            break;
nkeynes@700
   187
        }
nkeynes@68
   188
    }
nkeynes@30
   189
nkeynes@450
   190
    lxdream_load_config( );
nkeynes@691
   191
    gdrom_list_init();
nkeynes@736
   192
nkeynes@68
   193
    if( aica_program == NULL ) {
nkeynes@700
   194
        dreamcast_init();
nkeynes@68
   195
    } else {
nkeynes@700
   196
        dreamcast_configure_aica_only();
nkeynes@700
   197
        mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@68
   198
    }
nkeynes@586
   199
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   200
nkeynes@759
   201
    audio_init_driver( audio_driver_name );
nkeynes@736
   202
nkeynes@700
   203
    headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
nkeynes@106
   204
    if( headless ) {
nkeynes@700
   205
        display_set_driver( &display_null_driver );
nkeynes@106
   206
    } else {
nkeynes@700
   207
        gui_init(show_debugger);
nkeynes@435
   208
nkeynes@700
   209
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@700
   210
        if( display_driver == NULL ) {
nkeynes@700
   211
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@700
   212
            exit(2);
nkeynes@700
   213
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@700
   214
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@700
   215
                    display_driver->name );
nkeynes@700
   216
            exit(2);
nkeynes@700
   217
        }
nkeynes@106
   218
    }
nkeynes@106
   219
nkeynes@144
   220
    maple_reattach_all();
nkeynes@180
   221
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   222
nkeynes@446
   223
    for( ; optind < argc; optind++ ) {
nkeynes@700
   224
        gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@700
   225
        if( !ok ) {
nkeynes@700
   226
            ok = file_load_magic( argv[optind] );
nkeynes@700
   227
        }
nkeynes@700
   228
        if( !ok ) {
nkeynes@700
   229
            ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@700
   230
        }
nkeynes@700
   231
        if( !no_start ) {
nkeynes@700
   232
            start_immediately = ok;
nkeynes@700
   233
        }
nkeynes@144
   234
    }
nkeynes@144
   235
nkeynes@464
   236
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@700
   237
        const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@678
   238
        if( disc_file != NULL ) {
nkeynes@678
   239
            gdrom_mount_image( disc_file );
nkeynes@678
   240
        }
nkeynes@464
   241
    }
nkeynes@464
   242
nkeynes@740
   243
    sh4_translate_set_enabled( use_xlat );
nkeynes@379
   244
nkeynes@689
   245
    if( headless ) {
nkeynes@689
   246
        dreamcast_run();
nkeynes@689
   247
    } else {
nkeynes@681
   248
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   249
    }
nkeynes@671
   250
    dreamcast_shutdown();
nkeynes@68
   251
    return 0;
nkeynes@1
   252
}
nkeynes@1
   253
.