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