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