Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 1108:305ef2082079
prev1107:7b279d10f46f
next1109:700c5ab26a63
author nkeynes
date Fri Jun 04 09:13:40 2010 +1000 (11 years ago)
permissions -rw-r--r--
last change Add ability to wrap a binary program up in a virtual cd image (so that we
can boot it normally)
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@1107
    23
#include <libisofs/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@1108
    45
char *option_list = "a:A:bc:dfg:G:hHl:m:npt:T:uvV:w:x?";
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@1015
    52
        { "fullscreen", no_argument, NULL, 'f' },
nkeynes@998
    53
        { "gdb-sh4", required_argument, NULL, 'g' },  
nkeynes@998
    54
        { "gdb-arm", required_argument, NULL, 'G' },  
nkeynes@700
    55
        { "help", no_argument, NULL, 'h' },
nkeynes@700
    56
        { "headless", no_argument, NULL, 'H' },
nkeynes@700
    57
        { "log", required_argument, NULL,'l' }, 
nkeynes@700
    58
        { "multiplier", required_argument, NULL, 'm' },
nkeynes@700
    59
        { "run-time", required_argument, NULL, 't' },
nkeynes@700
    60
        { "trace", required_argument, NULL, 'T' },
nkeynes@700
    61
        { "unsafe", no_argument, NULL, 'u' },
nkeynes@700
    62
        { "video", no_argument, NULL, 'V' },
nkeynes@700
    63
        { "version", no_argument, NULL, 'v' }, 
nkeynes@1108
    64
        { "wrap", required_argument, NULL, 'w' },
nkeynes@700
    65
        { NULL, 0, 0, 0 } };
nkeynes@68
    66
char *aica_program = NULL;
nkeynes@531
    67
char *display_driver_name = NULL;
nkeynes@531
    68
char *audio_driver_name = NULL;
nkeynes@562
    69
char *trace_regions = NULL;
nkeynes@998
    70
char *sh4_gdb_port = NULL;
nkeynes@998
    71
char *arm_gdb_port = NULL;
nkeynes@68
    72
gboolean start_immediately = FALSE;
nkeynes@575
    73
gboolean no_start = FALSE;
nkeynes@77
    74
gboolean headless = FALSE;
nkeynes@402
    75
gboolean use_xlat = TRUE;
nkeynes@392
    76
gboolean show_debugger = FALSE;
nkeynes@1015
    77
gboolean show_fullscreen = FALSE;
nkeynes@1100
    78
gboolean use_bootrom = TRUE;
nkeynes@414
    79
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    80
nkeynes@968
    81
static void print_version()
nkeynes@700
    82
{
nkeynes@738
    83
    printf( "lxdream %s\n", lxdream_full_version );
nkeynes@700
    84
}
nkeynes@700
    85
nkeynes@968
    86
static void print_usage()
nkeynes@700
    87
{
nkeynes@700
    88
    print_version();
nkeynes@738
    89
    printf( "Usage: lxdream %s [options] [disc-file] [program-file]\n\n", lxdream_full_version );
nkeynes@736
    90
nkeynes@700
    91
    printf( "Options:\n" );
nkeynes@700
    92
    printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
nkeynes@700
    93
    printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
nkeynes@1100
    94
    printf( "   -b, --biosless         %s\n", _("Run without the BIOS boot rom even if available") );
nkeynes@700
    95
    printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
nkeynes@700
    96
    printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
nkeynes@1015
    97
    printf( "   -f, --fullscreen       %s\n", _("Start in fullscreen mode") );
nkeynes@998
    98
    printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
nkeynes@998
    99
    printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
nkeynes@700
   100
    printf( "   -h, --help             %s\n", _("Display this usage information") );
nkeynes@700
   101
    printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
nkeynes@700
   102
    printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
nkeynes@700
   103
    printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
nkeynes@700
   104
    printf( "   -n                     %s\n", _("Don't start running immediately") );
nkeynes@700
   105
    printf( "   -p                     %s\n", _("Start running immediately on startup") );
nkeynes@700
   106
    printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
nkeynes@700
   107
    printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
nkeynes@700
   108
    printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
nkeynes@700
   109
    printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
nkeynes@700
   110
    printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
nkeynes@1108
   111
    printf( "   -w, --wrap=FILENAME    %s\n", _("Wrap the specified binary file in a disc image") );
nkeynes@700
   112
    printf( "   -x                     %s\n", _("Disable the SH4 translator") );
nkeynes@700
   113
}
nkeynes@700
   114
nkeynes@968
   115
static void bind_gettext_domain()
nkeynes@723
   116
{
nkeynes@723
   117
#ifdef ENABLE_NLS
nkeynes@866
   118
    bindtextdomain( PACKAGE, get_locale_path() );
nkeynes@723
   119
    textdomain(PACKAGE);
nkeynes@723
   120
#endif
nkeynes@723
   121
}
nkeynes@723
   122
nkeynes@30
   123
int main (int argc, char *argv[])
nkeynes@1
   124
{
nkeynes@669
   125
    int opt;
nkeynes@372
   126
    double t;
nkeynes@531
   127
    gboolean display_ok;
nkeynes@689
   128
    uint32_t time_secs, time_nanos;
nkeynes@1108
   129
    const char *wrap_name = NULL;
nkeynes@495
   130
nkeynes@495
   131
    install_crash_handler();
nkeynes@723
   132
    bind_gettext_domain();
nkeynes@531
   133
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
   134
nkeynes@94
   135
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@736
   136
        switch( opt ) {
nkeynes@736
   137
        case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@736
   138
            aica_program = optarg;
nkeynes@77
   139
            break;
nkeynes@736
   140
        case 'A': /* Audio driver */
nkeynes@736
   141
            audio_driver_name = optarg;
nkeynes@736
   142
            break;
nkeynes@1100
   143
        case 'b': /* No Boot rom */
nkeynes@1100
   144
            use_bootrom = FALSE;
nkeynes@736
   145
        case 'c': /* Config file */
nkeynes@736
   146
            lxdream_set_config_filename(optarg);
nkeynes@736
   147
            break;
nkeynes@736
   148
        case 'd': /* Launch w/ debugger */
nkeynes@736
   149
            show_debugger = TRUE;
nkeynes@736
   150
            break;
nkeynes@1015
   151
        case 'f':
nkeynes@1015
   152
            show_fullscreen = TRUE;
nkeynes@1015
   153
            break;
nkeynes@998
   154
        case 'g':
nkeynes@998
   155
            sh4_gdb_port = optarg;
nkeynes@998
   156
            break;
nkeynes@998
   157
        case 'G':
nkeynes@998
   158
            arm_gdb_port = optarg;
nkeynes@998
   159
            break;
nkeynes@736
   160
        case 'h': /* help */
nkeynes@736
   161
        case '?':
nkeynes@736
   162
            print_usage();
nkeynes@736
   163
            exit(0);
nkeynes@736
   164
            break;
nkeynes@736
   165
        case 'H': /* Headless - shorthand for -V null */
nkeynes@736
   166
            display_driver_name = "null";
nkeynes@736
   167
            break;
nkeynes@736
   168
        case 'l': /* Log verbosity */
nkeynes@736
   169
            if( !set_global_log_level(optarg) ) {
nkeynes@736
   170
                ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@736
   171
            }
nkeynes@736
   172
            break;
nkeynes@736
   173
        case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@736
   174
            t = strtod(optarg, NULL);
nkeynes@736
   175
            sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@736
   176
            break;
nkeynes@736
   177
        case 'n': /* Don't start immediately */
nkeynes@736
   178
            no_start = TRUE;
nkeynes@736
   179
            start_immediately = FALSE;
nkeynes@736
   180
            break;
nkeynes@736
   181
        case 'p': /* Start immediately */
nkeynes@736
   182
            start_immediately = TRUE;
nkeynes@736
   183
            no_start = FALSE;
nkeynes@736
   184
            break;
nkeynes@736
   185
        case 't': /* Time limit + auto quit */
nkeynes@736
   186
            t = strtod(optarg, NULL);
nkeynes@736
   187
            time_secs = (uint32_t)t;
nkeynes@736
   188
            time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@736
   189
            dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@736
   190
            dreamcast_set_exit_on_stop( TRUE );
nkeynes@736
   191
            break;
nkeynes@736
   192
        case 'T': /* trace regions */
nkeynes@736
   193
            trace_regions = optarg;
nkeynes@736
   194
            set_global_log_level("trace");
nkeynes@736
   195
            break;
nkeynes@736
   196
        case 'u': /* Allow unsafe dcload syscalls */
nkeynes@736
   197
            dcload_set_allow_unsafe(TRUE);
nkeynes@736
   198
            break;
nkeynes@736
   199
        case 'v': 
nkeynes@700
   200
            print_version();
nkeynes@700
   201
            exit(0);
nkeynes@736
   202
            break;
nkeynes@736
   203
        case 'V': /* Video driver */
nkeynes@736
   204
            display_driver_name = optarg;
nkeynes@736
   205
            break;
nkeynes@1108
   206
        case 'w': /* Wrap image file */
nkeynes@1108
   207
            wrap_name = optarg;
nkeynes@1108
   208
            break;
nkeynes@736
   209
        case 'x': /* Disable translator */
nkeynes@736
   210
            use_xlat = FALSE;
nkeynes@736
   211
            break;
nkeynes@700
   212
        }
nkeynes@68
   213
    }
nkeynes@30
   214
nkeynes@1024
   215
#ifdef ENABLE_SHARED
nkeynes@1027
   216
    plugin_init();
nkeynes@1024
   217
#endif
nkeynes@1024
   218
nkeynes@1038
   219
    lxdream_make_config_dir( );
nkeynes@450
   220
    lxdream_load_config( );
nkeynes@144
   221
nkeynes@1024
   222
    if( audio_driver_name != NULL && strcmp(audio_driver_name, "?") == 0 ) {
nkeynes@1024
   223
        print_version();
nkeynes@1024
   224
        print_audio_drivers(stdout);
nkeynes@1024
   225
        exit(0);
nkeynes@1024
   226
    }
nkeynes@1024
   227
nkeynes@1024
   228
    if( display_driver_name != NULL && strcmp(display_driver_name,"?") == 0 ) {
nkeynes@1024
   229
        print_version();
nkeynes@1024
   230
        print_display_drivers(stdout);
nkeynes@1024
   231
        exit(0);
nkeynes@1024
   232
    }
nkeynes@1024
   233
nkeynes@1107
   234
    iso_init();
nkeynes@691
   235
    gdrom_list_init();
nkeynes@1034
   236
    vmulist_init();
nkeynes@736
   237
nkeynes@68
   238
    if( aica_program == NULL ) {
nkeynes@1100
   239
        dreamcast_init(use_bootrom);
nkeynes@68
   240
    } else {
nkeynes@700
   241
        dreamcast_configure_aica_only();
nkeynes@700
   242
        mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@68
   243
    }
nkeynes@562
   244
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   245
nkeynes@759
   246
    audio_init_driver( audio_driver_name );
nkeynes@736
   247
nkeynes@700
   248
    headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
nkeynes@106
   249
    if( headless ) {
nkeynes@700
   250
        display_set_driver( &display_null_driver );
nkeynes@106
   251
    } else {
nkeynes@1015
   252
        gui_init(show_debugger, show_fullscreen);
nkeynes@435
   253
nkeynes@700
   254
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@700
   255
        if( display_driver == NULL ) {
nkeynes@700
   256
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@700
   257
            exit(2);
nkeynes@700
   258
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@700
   259
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@700
   260
                    display_driver->name );
nkeynes@700
   261
            exit(2);
nkeynes@700
   262
        }
nkeynes@87
   263
    }
nkeynes@1015
   264
    
nkeynes@1015
   265
    hotkeys_init();
nkeynes@1077
   266
    serial_init();
nkeynes@106
   267
nkeynes@144
   268
    maple_reattach_all();
nkeynes@180
   269
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   270
nkeynes@1108
   271
    if( wrap_name != NULL ) {
nkeynes@1108
   272
        ERROR err;
nkeynes@1108
   273
        cdrom_disc_t disc = cdrom_wrap_magic( CDROM_DISC_XA, wrap_name, &err );
nkeynes@1108
   274
        if( disc == NULL )
nkeynes@1108
   275
            ERROR(err.msg);
nkeynes@1108
   276
        else {
nkeynes@1108
   277
            gdrom_mount_disc(disc);
nkeynes@1108
   278
            if( !no_start ) {
nkeynes@1108
   279
                start_immediately = TRUE;
nkeynes@1108
   280
            }
nkeynes@1108
   281
        }
nkeynes@1108
   282
    }
nkeynes@1108
   283
nkeynes@446
   284
    for( ; optind < argc; optind++ ) {
nkeynes@700
   285
        gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@700
   286
        if( !ok ) {
nkeynes@700
   287
            ok = file_load_magic( argv[optind] );
nkeynes@700
   288
        }
nkeynes@700
   289
        if( !ok ) {
nkeynes@700
   290
            ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@700
   291
        }
nkeynes@700
   292
        if( !no_start ) {
nkeynes@700
   293
            start_immediately = ok;
nkeynes@700
   294
        }
nkeynes@144
   295
    }
nkeynes@144
   296
nkeynes@464
   297
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@1036
   298
        gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
nkeynes@678
   299
        if( disc_file != NULL ) {
nkeynes@678
   300
            gdrom_mount_image( disc_file );
nkeynes@1036
   301
            g_free(disc_file);
nkeynes@678
   302
        }
nkeynes@464
   303
    }
nkeynes@464
   304
nkeynes@740
   305
    sh4_translate_set_enabled( use_xlat );
nkeynes@379
   306
nkeynes@998
   307
    /* If requested, start the gdb server immediately before we go into the main
nkeynes@998
   308
     * loop.
nkeynes@998
   309
     */
nkeynes@998
   310
    if( sh4_gdb_port != NULL ) {
nkeynes@998
   311
        gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
nkeynes@372
   312
    }
nkeynes@998
   313
    if( arm_gdb_port != NULL ) {
nkeynes@998
   314
        gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
nkeynes@77
   315
    }
nkeynes@998
   316
    
nkeynes@689
   317
    if( headless ) {
nkeynes@689
   318
        dreamcast_run();
nkeynes@689
   319
    } else {
nkeynes@681
   320
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   321
    }
nkeynes@671
   322
    dreamcast_shutdown();
nkeynes@68
   323
    return 0;
nkeynes@1
   324
}
nkeynes@1
   325
.