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