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