Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 1108:305ef2082079
prev1107:7b279d10f46f
next1109:700c5ab26a63
author nkeynes
date Fri Jun 04 09:13:40 2010 +1000 (11 years ago)
permissions -rw-r--r--
last change Add ability to wrap a binary program up in a virtual cd image (so that we
can boot it normally)
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/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:dfg:G:hHl:m:npt:T:uvV:w:x?";
    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         { "fullscreen", no_argument, NULL, 'f' },
    53         { "gdb-sh4", required_argument, NULL, 'g' },  
    54         { "gdb-arm", required_argument, NULL, 'G' },  
    55         { "help", no_argument, NULL, 'h' },
    56         { "headless", no_argument, NULL, 'H' },
    57         { "log", required_argument, NULL,'l' }, 
    58         { "multiplier", required_argument, NULL, 'm' },
    59         { "run-time", required_argument, NULL, 't' },
    60         { "trace", required_argument, NULL, 'T' },
    61         { "unsafe", no_argument, NULL, 'u' },
    62         { "video", no_argument, NULL, 'V' },
    63         { "version", no_argument, NULL, 'v' }, 
    64         { "wrap", required_argument, NULL, 'w' },
    65         { NULL, 0, 0, 0 } };
    66 char *aica_program = NULL;
    67 char *display_driver_name = NULL;
    68 char *audio_driver_name = NULL;
    69 char *trace_regions = NULL;
    70 char *sh4_gdb_port = NULL;
    71 char *arm_gdb_port = NULL;
    72 gboolean start_immediately = FALSE;
    73 gboolean no_start = FALSE;
    74 gboolean headless = FALSE;
    75 gboolean use_xlat = TRUE;
    76 gboolean show_debugger = FALSE;
    77 gboolean show_fullscreen = FALSE;
    78 gboolean use_bootrom = TRUE;
    79 extern uint32_t sh4_cpu_multiplier;
    81 static void print_version()
    82 {
    83     printf( "lxdream %s\n", lxdream_full_version );
    84 }
    86 static void print_usage()
    87 {
    88     print_version();
    89     printf( "Usage: lxdream %s [options] [disc-file] [program-file]\n\n", lxdream_full_version );
    91     printf( "Options:\n" );
    92     printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
    93     printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
    94     printf( "   -b, --biosless         %s\n", _("Run without the BIOS boot rom even if available") );
    95     printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
    96     printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
    97     printf( "   -f, --fullscreen       %s\n", _("Start in fullscreen mode") );
    98     printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
    99     printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
   100     printf( "   -h, --help             %s\n", _("Display this usage information") );
   101     printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
   102     printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
   103     printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
   104     printf( "   -n                     %s\n", _("Don't start running immediately") );
   105     printf( "   -p                     %s\n", _("Start running immediately on startup") );
   106     printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
   107     printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
   108     printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
   109     printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
   110     printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
   111     printf( "   -w, --wrap=FILENAME    %s\n", _("Wrap the specified binary file in a disc image") );
   112     printf( "   -x                     %s\n", _("Disable the SH4 translator") );
   113 }
   115 static void bind_gettext_domain()
   116 {
   117 #ifdef ENABLE_NLS
   118     bindtextdomain( PACKAGE, get_locale_path() );
   119     textdomain(PACKAGE);
   120 #endif
   121 }
   123 int main (int argc, char *argv[])
   124 {
   125     int opt;
   126     double t;
   127     gboolean display_ok;
   128     uint32_t time_secs, time_nanos;
   129     const char *wrap_name = NULL;
   131     install_crash_handler();
   132     bind_gettext_domain();
   133     display_ok = gui_parse_cmdline(&argc, &argv);
   135     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   136         switch( opt ) {
   137         case 'a': /* AICA only mode - argument is an AICA program */
   138             aica_program = optarg;
   139             break;
   140         case 'A': /* Audio driver */
   141             audio_driver_name = optarg;
   142             break;
   143         case 'b': /* No Boot rom */
   144             use_bootrom = FALSE;
   145         case 'c': /* Config file */
   146             lxdream_set_config_filename(optarg);
   147             break;
   148         case 'd': /* Launch w/ debugger */
   149             show_debugger = TRUE;
   150             break;
   151         case 'f':
   152             show_fullscreen = TRUE;
   153             break;
   154         case 'g':
   155             sh4_gdb_port = optarg;
   156             break;
   157         case 'G':
   158             arm_gdb_port = optarg;
   159             break;
   160         case 'h': /* help */
   161         case '?':
   162             print_usage();
   163             exit(0);
   164             break;
   165         case 'H': /* Headless - shorthand for -V null */
   166             display_driver_name = "null";
   167             break;
   168         case 'l': /* Log verbosity */
   169             if( !set_global_log_level(optarg) ) {
   170                 ERROR( "Unrecognized log level '%s'", optarg );
   171             }
   172             break;
   173         case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
   174             t = strtod(optarg, NULL);
   175             sh4_cpu_multiplier = (int)(1000.0/t);
   176             break;
   177         case 'n': /* Don't start immediately */
   178             no_start = TRUE;
   179             start_immediately = FALSE;
   180             break;
   181         case 'p': /* Start immediately */
   182             start_immediately = TRUE;
   183             no_start = FALSE;
   184             break;
   185         case 't': /* Time limit + auto quit */
   186             t = strtod(optarg, NULL);
   187             time_secs = (uint32_t)t;
   188             time_nanos = (int)((t - time_secs) * 1000000000);
   189             dreamcast_set_run_time( time_secs, time_nanos );
   190             dreamcast_set_exit_on_stop( TRUE );
   191             break;
   192         case 'T': /* trace regions */
   193             trace_regions = optarg;
   194             set_global_log_level("trace");
   195             break;
   196         case 'u': /* Allow unsafe dcload syscalls */
   197             dcload_set_allow_unsafe(TRUE);
   198             break;
   199         case 'v': 
   200             print_version();
   201             exit(0);
   202             break;
   203         case 'V': /* Video driver */
   204             display_driver_name = optarg;
   205             break;
   206         case 'w': /* Wrap image file */
   207             wrap_name = optarg;
   208             break;
   209         case 'x': /* Disable translator */
   210             use_xlat = FALSE;
   211             break;
   212         }
   213     }
   215 #ifdef ENABLE_SHARED
   216     plugin_init();
   217 #endif
   219     lxdream_make_config_dir( );
   220     lxdream_load_config( );
   222     if( audio_driver_name != NULL && strcmp(audio_driver_name, "?") == 0 ) {
   223         print_version();
   224         print_audio_drivers(stdout);
   225         exit(0);
   226     }
   228     if( display_driver_name != NULL && strcmp(display_driver_name,"?") == 0 ) {
   229         print_version();
   230         print_display_drivers(stdout);
   231         exit(0);
   232     }
   234     iso_init();
   235     gdrom_list_init();
   236     vmulist_init();
   238     if( aica_program == NULL ) {
   239         dreamcast_init(use_bootrom);
   240     } else {
   241         dreamcast_configure_aica_only();
   242         mem_load_block( aica_program, 0x00800000, 2048*1024 );
   243     }
   244     mem_set_trace( trace_regions, TRUE );
   246     audio_init_driver( audio_driver_name );
   248     headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
   249     if( headless ) {
   250         display_set_driver( &display_null_driver );
   251     } else {
   252         gui_init(show_debugger, show_fullscreen);
   254         display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   255         if( display_driver == NULL ) {
   256             ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   257             exit(2);
   258         } else if( display_set_driver( display_driver ) == FALSE ) {
   259             ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   260                     display_driver->name );
   261             exit(2);
   262         }
   263     }
   265     hotkeys_init();
   266     serial_init();
   268     maple_reattach_all();
   269     INFO( "%s! ready...", APP_NAME );
   271     if( wrap_name != NULL ) {
   272         ERROR err;
   273         cdrom_disc_t disc = cdrom_wrap_magic( CDROM_DISC_XA, wrap_name, &err );
   274         if( disc == NULL )
   275             ERROR(err.msg);
   276         else {
   277             gdrom_mount_disc(disc);
   278             if( !no_start ) {
   279                 start_immediately = TRUE;
   280             }
   281         }
   282     }
   284     for( ; optind < argc; optind++ ) {
   285         gboolean ok = gdrom_mount_image(argv[optind]);
   286         if( !ok ) {
   287             ok = file_load_magic( argv[optind] );
   288         }
   289         if( !ok ) {
   290             ERROR( "Unrecognized file '%s'", argv[optind] );
   291         }
   292         if( !no_start ) {
   293             start_immediately = ok;
   294         }
   295     }
   297     if( gdrom_get_current_disc() == NULL ) {
   298         gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
   299         if( disc_file != NULL ) {
   300             gdrom_mount_image( disc_file );
   301             g_free(disc_file);
   302         }
   303     }
   305     sh4_translate_set_enabled( use_xlat );
   307     /* If requested, start the gdb server immediately before we go into the main
   308      * loop.
   309      */
   310     if( sh4_gdb_port != NULL ) {
   311         gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
   312     }
   313     if( arm_gdb_port != NULL ) {
   314         gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
   315     }
   317     if( headless ) {
   318         dreamcast_run();
   319     } else {
   320         gui_main_loop( start_immediately && dreamcast_can_run() );
   321     }
   322     dreamcast_shutdown();
   323     return 0;
   324 }
.