Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 700:4650d0c7f6f9
prev698:6d9fd7f54609
next708:0b8b5eaa3b52
author nkeynes
date Wed Jun 25 10:03:28 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Add sh4_dump_region convenience function
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Main program, initializes dreamcast and gui, then passes control off to
     5  * the main loop. 
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdlib.h>
    21 #include <unistd.h>
    22 #include <getopt.h>
    23 #include <glib/gi18n.h>
    24 #include "lxdream.h"
    25 #include "syscall.h"
    26 #include "mem.h"
    27 #include "dreamcast.h"
    28 #include "display.h"
    29 #include "loader.h"
    30 #include "gui.h"
    31 #include "aica/audio.h"
    32 #include "gdrom/gdrom.h"
    33 #include "maple/maple.h"
    34 #include "sh4/sh4.h"
    36 char *option_list = "a:A:c:dhHl:m:npt:T:uvV:x?";
    37 struct option longopts[] = {
    38         { "aica", required_argument, NULL, 'a' },
    39         { "audio", required_argument, NULL, 'A' },
    40         { "config", required_argument, NULL, 'c' },
    41         { "debugger", no_argument, NULL, 'D' },
    42         { "help", no_argument, NULL, 'h' },
    43         { "headless", no_argument, NULL, 'H' },
    44         { "log", required_argument, NULL,'l' }, 
    45         { "multiplier", required_argument, NULL, 'm' },
    46         { "run-time", required_argument, NULL, 't' },
    47         { "trace", required_argument, NULL, 'T' },
    48         { "unsafe", no_argument, NULL, 'u' },
    49         { "video", no_argument, NULL, 'V' },
    50         { "version", no_argument, NULL, 'v' }, 
    51         { NULL, 0, 0, 0 } };
    52 char *aica_program = NULL;
    53 char *display_driver_name = NULL;
    54 char *audio_driver_name = NULL;
    55 char *trace_regions = NULL;
    56 gboolean start_immediately = FALSE;
    57 gboolean no_start = FALSE;
    58 gboolean headless = FALSE;
    59 gboolean use_xlat = TRUE;
    60 gboolean show_debugger = FALSE;
    61 extern uint32_t sh4_cpu_multiplier;
    63 void print_version()
    64 {
    65     printf( "lxdream " APP_VERSION "\n" );
    66 }
    68 void print_usage()
    69 {
    70     print_version();
    71     printf( "Usage: lxdream [options] [disc-file] [program-file]\n\n" );
    73     printf( "Options:\n" );
    74     printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
    75     printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
    76     printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
    77     printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
    78     printf( "   -h, --help             %s\n", _("Display this usage information") );
    79     printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
    80     printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
    81     printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
    82     printf( "   -n                     %s\n", _("Don't start running immediately") );
    83     printf( "   -p                     %s\n", _("Start running immediately on startup") );
    84     printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
    85     printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
    86     printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
    87     printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
    88     printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
    89     printf( "   -x                     %s\n", _("Disable the SH4 translator") );
    90 }
    92 int main (int argc, char *argv[])
    93 {
    94     int opt;
    95     double t;
    96     gboolean display_ok;
    97     uint32_t time_secs, time_nanos;
    99     install_crash_handler();
   100     gdrom_get_native_devices();
   101 #ifdef ENABLE_NLS
   102     bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
   103     textdomain (PACKAGE);
   104 #endif
   105     display_ok = gui_parse_cmdline(&argc, &argv);
   107     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   108 	switch( opt ) {
   109 	case 'a': /* AICA only mode - argument is an AICA program */
   110 	    aica_program = optarg;
   111 	    break;
   112     case 'A': /* Audio driver */
   113         audio_driver_name = optarg;
   114         if( strcmp(audio_driver_name, "?") == 0 ) {
   115             print_version();
   116             print_audio_drivers(stdout);
   117             exit(0);
   118         }
   119         break;
   120 	case 'c': /* Config file */
   121 	    lxdream_set_config_filename(optarg);
   122 	    break;
   123 	case 'd': /* Launch w/ debugger */
   124 	    show_debugger = TRUE;
   125 	    break;
   126     case 'h': /* help */
   127     case '?':
   128         print_usage();
   129         exit(0);
   130         break;
   131     case 'H': /* Headless - shorthand for -V null */
   132         display_driver_name = "null";
   133         break;
   134     case 'l': /* Log verbosity */
   135         if( !set_global_log_level(optarg) ) {
   136             ERROR( "Unrecognized log level '%s'", optarg );
   137         }
   138         break;
   139 	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
   140 	    t = strtod(optarg, NULL);
   141 	    sh4_cpu_multiplier = (int)(1000.0/t);
   142 	    break;
   143 	case 'n': /* Don't start immediately */
   144 	    no_start = TRUE;
   145 	    start_immediately = FALSE;
   146 	    break;
   147 	case 'p': /* Start immediately */
   148 	    start_immediately = TRUE;
   149 	    no_start = FALSE;
   150 	    break;
   151 	case 't': /* Time limit + auto quit */
   152 	    t = strtod(optarg, NULL);
   153 	    time_secs = (uint32_t)t;
   154 	    time_nanos = (int)((t - time_secs) * 1000000000);
   155 	    dreamcast_set_run_time( time_secs, time_nanos );
   156 	    dreamcast_set_exit_on_stop( TRUE );
   157 	    break;
   158 	case 'T': /* trace regions */
   159 	    trace_regions = optarg;
   160 	    set_global_log_level("trace");
   161 	    break;
   162     case 'u': /* Allow unsafe dcload syscalls */
   163         dcload_set_allow_unsafe(TRUE);
   164         break;
   165 	case 'v': 
   166 	    print_version();
   167 	    exit(0);
   168 	    break;
   169     case 'V': /* Video driver */
   170         display_driver_name = optarg;
   171         if( strcmp(display_driver_name,"?") == 0 ) {
   172             print_version();
   173             print_display_drivers(stdout);
   174             exit(0);
   175         }
   176         break;
   177 	case 'x': /* Disable translator */
   178 	    use_xlat = FALSE;
   179 	    break;
   180 	}
   181     }
   183     lxdream_load_config( );
   184     gdrom_list_init();
   186     if( aica_program == NULL ) {
   187         dreamcast_init();
   188     } else {
   189         dreamcast_configure_aica_only();
   190         mem_load_block( aica_program, 0x00800000, 2048*1024 );
   191     }
   192     mem_set_trace( trace_regions, TRUE );
   194     audio_init_driver( audio_driver_name, 44100, AUDIO_FMT_16ST );
   196     headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
   197     if( headless ) {
   198         display_set_driver( &display_null_driver );
   199     } else {
   200         gui_init(show_debugger);
   202         display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   203         if( display_driver == NULL ) {
   204             ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   205             exit(2);
   206         } else if( display_set_driver( display_driver ) == FALSE ) {
   207             ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   208                     display_driver->name );
   209             exit(2);
   210         }
   211     }
   213     maple_reattach_all();
   214     INFO( "%s! ready...", APP_NAME );
   216     for( ; optind < argc; optind++ ) {
   217         gboolean ok = gdrom_mount_image(argv[optind]);
   218         if( !ok ) {
   219             ok = file_load_magic( argv[optind] );
   220         }
   221         if( !ok ) {
   222             ERROR( "Unrecognized file '%s'", argv[optind] );
   223         }
   224         if( !no_start ) {
   225             start_immediately = ok;
   226         }
   227     }
   229     if( gdrom_get_current_disc() == NULL ) {
   230         const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
   231         if( disc_file != NULL ) {
   232             gdrom_mount_image( disc_file );
   233         }
   234     }
   236     sh4_set_use_xlat( use_xlat );
   238     if( headless ) {
   239         dreamcast_run();
   240     } else {
   241         gui_main_loop( start_immediately && dreamcast_can_run() );
   242     }
   243     dreamcast_shutdown();
   244     return 0;
   245 }
.