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