Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 1225:73dd41cb37a8
prev1218:be02e87f9f87
author nkeynes
date Thu Feb 23 08:41:07 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change rename BUILD_SHARED conditional flag to BUILD_PLUGINS, for clarity
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@561
     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@1179
    23
#include "lxdream.h"
nkeynes@1111
    24
#include <libisofs.h>
nkeynes@1041
    25
#include "lxpaths.h"
nkeynes@755
    26
#include "gettext.h"
nkeynes@1129
    27
#include "dream.h"
nkeynes@27
    28
#include "dreamcast.h"
nkeynes@422
    29
#include "display.h"
nkeynes@759
    30
#include "gui.h"
nkeynes@759
    31
#include "gdlist.h"
nkeynes@1129
    32
#include "hotkeys.h"
nkeynes@1129
    33
#include "loader.h"
nkeynes@1129
    34
#include "mem.h"
nkeynes@1129
    35
#include "plugin.h"
nkeynes@1129
    36
#include "serial.h"
nkeynes@759
    37
#include "syscall.h"
nkeynes@106
    38
#include "aica/audio.h"
nkeynes@1129
    39
#include "aica/armdasm.h"
nkeynes@422
    40
#include "gdrom/gdrom.h"
nkeynes@144
    41
#include "maple/maple.h"
nkeynes@1129
    42
#include "pvr2/glutil.h"
nkeynes@564
    43
#include "sh4/sh4.h"
nkeynes@1034
    44
#include "vmu/vmulist.h"
nkeynes@1129
    45
nkeynes@1129
    46
#define GL_INFO_OPT 1
nkeynes@11
    47
nkeynes@1184
    48
char *option_list = "a:A:bc:e:dfg:G:hHl:m:npPt:T:uvV:xX?";
nkeynes@700
    49
struct option longopts[] = {
nkeynes@700
    50
        { "aica", required_argument, NULL, 'a' },
nkeynes@700
    51
        { "audio", required_argument, NULL, 'A' },
nkeynes@1100
    52
        { "biosless", no_argument, NULL, 'b' },
nkeynes@700
    53
        { "config", required_argument, NULL, 'c' },
nkeynes@1108
    54
        { "debugger", no_argument, NULL, 'd' },
nkeynes@1109
    55
        { "execute", required_argument, NULL, 'e' },
nkeynes@1015
    56
        { "fullscreen", no_argument, NULL, 'f' },
nkeynes@998
    57
        { "gdb-sh4", required_argument, NULL, 'g' },  
nkeynes@1129
    58
        { "gdb-arm", required_argument, NULL, 'G' },
nkeynes@1129
    59
        { "gl-info", no_argument, NULL, GL_INFO_OPT },
nkeynes@700
    60
        { "help", no_argument, NULL, 'h' },
nkeynes@700
    61
        { "headless", no_argument, NULL, 'H' },
nkeynes@700
    62
        { "log", required_argument, NULL,'l' }, 
nkeynes@700
    63
        { "multiplier", required_argument, NULL, 'm' },
nkeynes@700
    64
        { "run-time", required_argument, NULL, 't' },
nkeynes@1125
    65
        { "shadow", no_argument, NULL, 'X' },
nkeynes@700
    66
        { "trace", required_argument, NULL, 'T' },
nkeynes@700
    67
        { "unsafe", no_argument, NULL, 'u' },
nkeynes@700
    68
        { "video", no_argument, NULL, 'V' },
nkeynes@700
    69
        { "version", no_argument, NULL, 'v' }, 
nkeynes@1184
    70
        { "sh4-profile-blocks", no_argument, NULL, 'P' },
nkeynes@700
    71
        { NULL, 0, 0, 0 } };
nkeynes@68
    72
char *aica_program = NULL;
nkeynes@531
    73
char *display_driver_name = NULL;
nkeynes@531
    74
char *audio_driver_name = NULL;
nkeynes@562
    75
char *trace_regions = NULL;
nkeynes@998
    76
char *sh4_gdb_port = NULL;
nkeynes@998
    77
char *arm_gdb_port = NULL;
nkeynes@68
    78
gboolean start_immediately = FALSE;
nkeynes@575
    79
gboolean no_start = FALSE;
nkeynes@77
    80
gboolean headless = FALSE;
nkeynes@1125
    81
sh4core_t sh4_core = SH4_TRANSLATE;
nkeynes@392
    82
gboolean show_debugger = FALSE;
nkeynes@1015
    83
gboolean show_fullscreen = FALSE;
nkeynes@1100
    84
gboolean use_bootrom = TRUE;
nkeynes@414
    85
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    86
nkeynes@968
    87
static void print_version()
nkeynes@700
    88
{
nkeynes@738
    89
    printf( "lxdream %s\n", lxdream_full_version );
nkeynes@700
    90
}
nkeynes@700
    91
nkeynes@968
    92
static void print_usage()
nkeynes@700
    93
{
nkeynes@700
    94
    print_version();
nkeynes@1109
    95
    printf( "Usage: lxdream %s [options] [disc-file] [save-state]\n\n", lxdream_full_version );
nkeynes@736
    96
nkeynes@700
    97
    printf( "Options:\n" );
nkeynes@700
    98
    printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
nkeynes@700
    99
    printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
nkeynes@1100
   100
    printf( "   -b, --biosless         %s\n", _("Run without the BIOS boot rom even if available") );
nkeynes@700
   101
    printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
nkeynes@1109
   102
    printf( "   -e, --execute=PROGRAM  %s\n", _("Load and execute the given SH4 program") );
nkeynes@700
   103
    printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
nkeynes@1015
   104
    printf( "   -f, --fullscreen       %s\n", _("Start in fullscreen mode") );
nkeynes@998
   105
    printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
nkeynes@998
   106
    printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
nkeynes@700
   107
    printf( "   -h, --help             %s\n", _("Display this usage information") );
nkeynes@700
   108
    printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
nkeynes@700
   109
    printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
nkeynes@700
   110
    printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
nkeynes@700
   111
    printf( "   -n                     %s\n", _("Don't start running immediately") );
nkeynes@700
   112
    printf( "   -p                     %s\n", _("Start running immediately on startup") );
nkeynes@700
   113
    printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
nkeynes@700
   114
    printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
nkeynes@700
   115
    printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
nkeynes@700
   116
    printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
nkeynes@700
   117
    printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
nkeynes@700
   118
    printf( "   -x                     %s\n", _("Disable the SH4 translator") );
nkeynes@1125
   119
    printf( "   -X                     %s\n", _("Run both SH4 interpreter and translator") );
nkeynes@700
   120
}
nkeynes@700
   121
nkeynes@968
   122
static void bind_gettext_domain()
nkeynes@723
   123
{
nkeynes@723
   124
#ifdef ENABLE_NLS
nkeynes@866
   125
    bindtextdomain( PACKAGE, get_locale_path() );
nkeynes@723
   126
    textdomain(PACKAGE);
nkeynes@723
   127
#endif
nkeynes@723
   128
}
nkeynes@723
   129
nkeynes@30
   130
int main (int argc, char *argv[])
nkeynes@1
   131
{
nkeynes@669
   132
    int opt;
nkeynes@372
   133
    double t;
nkeynes@1109
   134
    gboolean display_ok, have_disc = FALSE, have_save = FALSE, have_exec = FALSE;
nkeynes@1184
   135
    gboolean print_glinfo = FALSE, sh4_profile_blocks = FALSE;
nkeynes@689
   136
    uint32_t time_secs, time_nanos;
nkeynes@1109
   137
    const char *exec_name = NULL;
nkeynes@495
   138
nkeynes@495
   139
    install_crash_handler();
nkeynes@723
   140
    bind_gettext_domain();
nkeynes@531
   141
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
   142
nkeynes@94
   143
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@736
   144
        switch( opt ) {
nkeynes@736
   145
        case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@736
   146
            aica_program = optarg;
nkeynes@77
   147
            break;
nkeynes@736
   148
        case 'A': /* Audio driver */
nkeynes@736
   149
            audio_driver_name = optarg;
nkeynes@736
   150
            break;
nkeynes@1100
   151
        case 'b': /* No Boot rom */
nkeynes@1100
   152
            use_bootrom = FALSE;
nkeynes@736
   153
        case 'c': /* Config file */
nkeynes@736
   154
            lxdream_set_config_filename(optarg);
nkeynes@736
   155
            break;
nkeynes@736
   156
        case 'd': /* Launch w/ debugger */
nkeynes@736
   157
            show_debugger = TRUE;
nkeynes@736
   158
            break;
nkeynes@1109
   159
        case 'e':
nkeynes@1109
   160
            exec_name = optarg;
nkeynes@1109
   161
            break;
nkeynes@1015
   162
        case 'f':
nkeynes@1015
   163
            show_fullscreen = TRUE;
nkeynes@1015
   164
            break;
nkeynes@998
   165
        case 'g':
nkeynes@998
   166
            sh4_gdb_port = optarg;
nkeynes@998
   167
            break;
nkeynes@998
   168
        case 'G':
nkeynes@998
   169
            arm_gdb_port = optarg;
nkeynes@998
   170
            break;
nkeynes@736
   171
        case 'h': /* help */
nkeynes@736
   172
        case '?':
nkeynes@736
   173
            print_usage();
nkeynes@736
   174
            exit(0);
nkeynes@736
   175
            break;
nkeynes@736
   176
        case 'H': /* Headless - shorthand for -V null */
nkeynes@736
   177
            display_driver_name = "null";
nkeynes@736
   178
            break;
nkeynes@736
   179
        case 'l': /* Log verbosity */
nkeynes@736
   180
            if( !set_global_log_level(optarg) ) {
nkeynes@736
   181
                ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@736
   182
            }
nkeynes@736
   183
            break;
nkeynes@736
   184
        case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@736
   185
            t = strtod(optarg, NULL);
nkeynes@736
   186
            sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@736
   187
            break;
nkeynes@736
   188
        case 'n': /* Don't start immediately */
nkeynes@736
   189
            no_start = TRUE;
nkeynes@736
   190
            start_immediately = FALSE;
nkeynes@736
   191
            break;
nkeynes@736
   192
        case 'p': /* Start immediately */
nkeynes@736
   193
            start_immediately = TRUE;
nkeynes@736
   194
            no_start = FALSE;
nkeynes@736
   195
            break;
nkeynes@1184
   196
        case 'P':
nkeynes@1184
   197
            sh4_profile_blocks = TRUE;
nkeynes@1184
   198
            break;
nkeynes@736
   199
        case 't': /* Time limit + auto quit */
nkeynes@736
   200
            t = strtod(optarg, NULL);
nkeynes@736
   201
            time_secs = (uint32_t)t;
nkeynes@736
   202
            time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@736
   203
            dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@736
   204
            dreamcast_set_exit_on_stop( TRUE );
nkeynes@736
   205
            break;
nkeynes@736
   206
        case 'T': /* trace regions */
nkeynes@736
   207
            trace_regions = optarg;
nkeynes@736
   208
            set_global_log_level("trace");
nkeynes@736
   209
            break;
nkeynes@736
   210
        case 'u': /* Allow unsafe dcload syscalls */
nkeynes@736
   211
            dcload_set_allow_unsafe(TRUE);
nkeynes@736
   212
            break;
nkeynes@736
   213
        case 'v': 
nkeynes@700
   214
            print_version();
nkeynes@700
   215
            exit(0);
nkeynes@736
   216
            break;
nkeynes@736
   217
        case 'V': /* Video driver */
nkeynes@736
   218
            display_driver_name = optarg;
nkeynes@736
   219
            break;
nkeynes@736
   220
        case 'x': /* Disable translator */
nkeynes@1125
   221
            sh4_core = SH4_INTERPRET;
nkeynes@1125
   222
            break;
nkeynes@1125
   223
        case 'X': /* Shadow translator */
nkeynes@1125
   224
            sh4_core = SH4_SHADOW;
nkeynes@736
   225
            break;
nkeynes@1129
   226
        case GL_INFO_OPT:
nkeynes@1129
   227
            print_glinfo = TRUE;
nkeynes@1129
   228
            break;
nkeynes@700
   229
        }
nkeynes@68
   230
    }
nkeynes@30
   231
nkeynes@1225
   232
#ifdef BUILD_PLUGINS
nkeynes@1027
   233
    plugin_init();
nkeynes@1024
   234
#endif
nkeynes@1024
   235
nkeynes@1038
   236
    lxdream_make_config_dir( );
nkeynes@450
   237
    lxdream_load_config( );
nkeynes@144
   238
nkeynes@1024
   239
    if( audio_driver_name != NULL && strcmp(audio_driver_name, "?") == 0 ) {
nkeynes@1024
   240
        print_version();
nkeynes@1024
   241
        print_audio_drivers(stdout);
nkeynes@1024
   242
        exit(0);
nkeynes@1024
   243
    }
nkeynes@1024
   244
nkeynes@1024
   245
    if( display_driver_name != NULL && strcmp(display_driver_name,"?") == 0 ) {
nkeynes@1024
   246
        print_version();
nkeynes@1024
   247
        print_display_drivers(stdout);
nkeynes@1024
   248
        exit(0);
nkeynes@1024
   249
    }
nkeynes@1024
   250
nkeynes@1129
   251
    if( print_glinfo ) {
nkeynes@1129
   252
        gui_init(FALSE, FALSE);
nkeynes@1129
   253
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@1129
   254
        if( display_driver == NULL ) {
nkeynes@1129
   255
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@1129
   256
            exit(2);
nkeynes@1129
   257
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@1129
   258
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)",
nkeynes@1129
   259
                    display_driver->name );
nkeynes@1129
   260
            exit(2);
nkeynes@1134
   261
        } else if( display_driver->capabilities.has_gl == FALSE ) {
nkeynes@1134
   262
            ERROR( "Video driver '%s' has no GL capabilities.", display_driver_name );
nkeynes@1134
   263
            exit(2);
nkeynes@1129
   264
        }
nkeynes@1129
   265
        glPrintInfo(stdout);
nkeynes@1129
   266
        exit(0);
nkeynes@1129
   267
nkeynes@1129
   268
    }
nkeynes@1129
   269
nkeynes@1129
   270
nkeynes@1107
   271
    iso_init();
nkeynes@691
   272
    gdrom_list_init();
nkeynes@1034
   273
    vmulist_init();
nkeynes@736
   274
nkeynes@68
   275
    if( aica_program == NULL ) {
nkeynes@1100
   276
        dreamcast_init(use_bootrom);
nkeynes@68
   277
    } else {
nkeynes@700
   278
        dreamcast_configure_aica_only();
nkeynes@700
   279
        mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@68
   280
    }
nkeynes@562
   281
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   282
nkeynes@759
   283
    audio_init_driver( audio_driver_name );
nkeynes@736
   284
nkeynes@700
   285
    headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
nkeynes@106
   286
    if( headless ) {
nkeynes@700
   287
        display_set_driver( &display_null_driver );
nkeynes@106
   288
    } else {
nkeynes@1015
   289
        gui_init(show_debugger, show_fullscreen);
nkeynes@435
   290
nkeynes@700
   291
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@700
   292
        if( display_driver == NULL ) {
nkeynes@700
   293
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@700
   294
            exit(2);
nkeynes@700
   295
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@700
   296
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@700
   297
                    display_driver->name );
nkeynes@700
   298
            exit(2);
nkeynes@700
   299
        }
nkeynes@87
   300
    }
nkeynes@1015
   301
    
nkeynes@1015
   302
    hotkeys_init();
nkeynes@1077
   303
    serial_init();
nkeynes@106
   304
nkeynes@144
   305
    maple_reattach_all();
nkeynes@180
   306
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   307
nkeynes@1109
   308
    for( ; optind < argc; optind++ ) {
nkeynes@1108
   309
        ERROR err;
nkeynes@1109
   310
        lxdream_file_type_t type = file_identify(argv[optind], -1, &err);
nkeynes@1109
   311
        if( type == FILE_SAVE_STATE ) {
nkeynes@1109
   312
            if( have_save ) {
nkeynes@1109
   313
                ERROR( "Multiple save states given on command-line, ignoring %s", argv[optind] );
nkeynes@1109
   314
            } else {
nkeynes@1109
   315
                have_save = dreamcast_load_state(argv[optind]);
nkeynes@1109
   316
                if( !have_save )
nkeynes@1109
   317
                    no_start = TRUE;
nkeynes@1109
   318
            }
nkeynes@1109
   319
        } else {
nkeynes@1109
   320
            if( have_disc ) {
nkeynes@1109
   321
                ERROR( "Multiple GD-ROM discs given on command-line, ignoring %s", argv[optind] );
nkeynes@1109
   322
            } else {
nkeynes@1109
   323
                have_disc = gdrom_mount_image(argv[optind], &err);
nkeynes@1116
   324
                if( !have_disc ) {
nkeynes@1116
   325
                    ERROR( err.msg );
nkeynes@1109
   326
                    no_start = TRUE;
nkeynes@1116
   327
                }
nkeynes@1108
   328
            }
nkeynes@1108
   329
        }
nkeynes@1108
   330
    }
nkeynes@1108
   331
nkeynes@1109
   332
    if( exec_name != NULL ) {
nkeynes@1109
   333
        ERROR err;
nkeynes@1109
   334
        if( have_save ) {
nkeynes@1109
   335
            ERROR( "Both a save state and an executable were specified, ignoring %s", exec_name );
nkeynes@1109
   336
        } else {
nkeynes@1109
   337
            have_exec = file_load_exec( exec_name, &err );
nkeynes@1116
   338
            if( !have_exec ) {
nkeynes@1116
   339
                ERROR( err.msg );
nkeynes@1109
   340
                no_start = TRUE;
nkeynes@1116
   341
            }
nkeynes@700
   342
        }
nkeynes@144
   343
    }
nkeynes@144
   344
nkeynes@1109
   345
    if( !no_start && (have_exec || have_disc || have_save) ) {
nkeynes@1109
   346
        start_immediately = TRUE;
nkeynes@1109
   347
    }
nkeynes@1109
   348
nkeynes@464
   349
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@1109
   350
        ERROR err;
nkeynes@1036
   351
        gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
nkeynes@678
   352
        if( disc_file != NULL ) {
nkeynes@1109
   353
            gboolean ok = gdrom_mount_image( disc_file, &err );
nkeynes@1036
   354
            g_free(disc_file);
nkeynes@1109
   355
            if( !ok ) {
nkeynes@1109
   356
                WARN( err.msg );
nkeynes@1109
   357
            }
nkeynes@678
   358
        }
nkeynes@464
   359
    }
nkeynes@464
   360
nkeynes@1125
   361
    sh4_set_core( sh4_core );
nkeynes@1218
   362
    sh4_set_profile_blocks( sh4_profile_blocks );
nkeynes@379
   363
nkeynes@998
   364
    /* If requested, start the gdb server immediately before we go into the main
nkeynes@998
   365
     * loop.
nkeynes@998
   366
     */
nkeynes@998
   367
    if( sh4_gdb_port != NULL ) {
nkeynes@998
   368
        gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
nkeynes@372
   369
    }
nkeynes@998
   370
    if( arm_gdb_port != NULL ) {
nkeynes@998
   371
        gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
nkeynes@77
   372
    }
nkeynes@998
   373
    
nkeynes@689
   374
    if( headless ) {
nkeynes@689
   375
        dreamcast_run();
nkeynes@689
   376
    } else {
nkeynes@681
   377
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   378
    }
nkeynes@671
   379
    dreamcast_shutdown();
nkeynes@68
   380
    return 0;
nkeynes@1
   381
}
nkeynes@1
   382
.