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