Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 1015:ad448bedc48a
prev998:1754a8c6a9cf
next1024:c67f2d61ab97
author nkeynes
date Wed Jun 03 11:37:10 2009 +0000 (14 years ago)
permissions -rw-r--r--
last change Add missing svn:keywords property
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 "gettext.h"
    25 #include "mem.h"
    26 #include "dreamcast.h"
    27 #include "dream.h"
    28 #include "display.h"
    29 #include "gui.h"
    30 #include "gdlist.h"
    31 #include "syscall.h"
    32 #include "loader.h"
    33 #include "aica/audio.h"
    34 #include "gdrom/gdrom.h"
    35 #include "maple/maple.h"
    36 #include "sh4/sh4.h"
    37 #include "aica/armdasm.h"
    38 #include "hotkeys.h"
    40 char *option_list = "a:A:c:dfg:G:hHl:m:npt:T:uvV:x?";
    41 struct option longopts[] = {
    42         { "aica", required_argument, NULL, 'a' },
    43         { "audio", required_argument, NULL, 'A' },
    44         { "config", required_argument, NULL, 'c' },
    45         { "debugger", no_argument, NULL, 'D' },
    46         { "fullscreen", no_argument, NULL, 'f' },
    47         { "gdb-sh4", required_argument, NULL, 'g' },  
    48         { "gdb-arm", required_argument, NULL, 'G' },  
    49         { "help", no_argument, NULL, 'h' },
    50         { "headless", no_argument, NULL, 'H' },
    51         { "log", required_argument, NULL,'l' }, 
    52         { "multiplier", required_argument, NULL, 'm' },
    53         { "run-time", required_argument, NULL, 't' },
    54         { "trace", required_argument, NULL, 'T' },
    55         { "unsafe", no_argument, NULL, 'u' },
    56         { "video", no_argument, NULL, 'V' },
    57         { "version", no_argument, NULL, 'v' }, 
    58         { NULL, 0, 0, 0 } };
    59 char *aica_program = NULL;
    60 char *display_driver_name = NULL;
    61 char *audio_driver_name = NULL;
    62 char *trace_regions = NULL;
    63 char *sh4_gdb_port = NULL;
    64 char *arm_gdb_port = NULL;
    65 gboolean start_immediately = FALSE;
    66 gboolean no_start = FALSE;
    67 gboolean headless = FALSE;
    68 gboolean use_xlat = TRUE;
    69 gboolean show_debugger = FALSE;
    70 gboolean show_fullscreen = FALSE;
    71 extern uint32_t sh4_cpu_multiplier;
    73 static void print_version()
    74 {
    75     printf( "lxdream %s\n", lxdream_full_version );
    76 }
    78 static void print_usage()
    79 {
    80     print_version();
    81     printf( "Usage: lxdream %s [options] [disc-file] [program-file]\n\n", lxdream_full_version );
    83     printf( "Options:\n" );
    84     printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
    85     printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
    86     printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
    87     printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
    88     printf( "   -f, --fullscreen       %s\n", _("Start in fullscreen mode") );
    89     printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
    90     printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
    91     printf( "   -h, --help             %s\n", _("Display this usage information") );
    92     printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
    93     printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
    94     printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
    95     printf( "   -n                     %s\n", _("Don't start running immediately") );
    96     printf( "   -p                     %s\n", _("Start running immediately on startup") );
    97     printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
    98     printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
    99     printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
   100     printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
   101     printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
   102     printf( "   -x                     %s\n", _("Disable the SH4 translator") );
   103 }
   105 static void bind_gettext_domain()
   106 {
   107 #ifdef ENABLE_NLS
   108     bindtextdomain( PACKAGE, get_locale_path() );
   109     textdomain(PACKAGE);
   110 #endif
   111 }
   113 int main (int argc, char *argv[])
   114 {
   115     int opt;
   116     double t;
   117     gboolean display_ok;
   118     uint32_t time_secs, time_nanos;
   120     install_crash_handler();
   121     bind_gettext_domain();
   122     display_ok = gui_parse_cmdline(&argc, &argv);
   124     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   125         switch( opt ) {
   126         case 'a': /* AICA only mode - argument is an AICA program */
   127             aica_program = optarg;
   128             break;
   129         case 'A': /* Audio driver */
   130             audio_driver_name = optarg;
   131             if( strcmp(audio_driver_name, "?") == 0 ) {
   132                 print_version();
   133                 print_audio_drivers(stdout);
   134                 exit(0);
   135             }
   136             break;
   137         case 'c': /* Config file */
   138             lxdream_set_config_filename(optarg);
   139             break;
   140         case 'd': /* Launch w/ debugger */
   141             show_debugger = TRUE;
   142             break;
   143         case 'f':
   144             show_fullscreen = TRUE;
   145             break;
   146         case 'g':
   147             sh4_gdb_port = optarg;
   148             break;
   149         case 'G':
   150             arm_gdb_port = optarg;
   151             break;
   152         case 'h': /* help */
   153         case '?':
   154             print_usage();
   155             exit(0);
   156             break;
   157         case 'H': /* Headless - shorthand for -V null */
   158             display_driver_name = "null";
   159             break;
   160         case 'l': /* Log verbosity */
   161             if( !set_global_log_level(optarg) ) {
   162                 ERROR( "Unrecognized log level '%s'", optarg );
   163             }
   164             break;
   165         case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
   166             t = strtod(optarg, NULL);
   167             sh4_cpu_multiplier = (int)(1000.0/t);
   168             break;
   169         case 'n': /* Don't start immediately */
   170             no_start = TRUE;
   171             start_immediately = FALSE;
   172             break;
   173         case 'p': /* Start immediately */
   174             start_immediately = TRUE;
   175             no_start = FALSE;
   176             break;
   177         case 't': /* Time limit + auto quit */
   178             t = strtod(optarg, NULL);
   179             time_secs = (uint32_t)t;
   180             time_nanos = (int)((t - time_secs) * 1000000000);
   181             dreamcast_set_run_time( time_secs, time_nanos );
   182             dreamcast_set_exit_on_stop( TRUE );
   183             break;
   184         case 'T': /* trace regions */
   185             trace_regions = optarg;
   186             set_global_log_level("trace");
   187             break;
   188         case 'u': /* Allow unsafe dcload syscalls */
   189             dcload_set_allow_unsafe(TRUE);
   190             break;
   191         case 'v': 
   192             print_version();
   193             exit(0);
   194             break;
   195         case 'V': /* Video driver */
   196             display_driver_name = optarg;
   197             if( strcmp(display_driver_name,"?") == 0 ) {
   198                 print_version();
   199                 print_display_drivers(stdout);
   200                 exit(0);
   201             }
   202             break;
   203         case 'x': /* Disable translator */
   204             use_xlat = FALSE;
   205             break;
   206         }
   207     }
   209     lxdream_load_config( );
   210     gdrom_list_init();
   212     if( aica_program == NULL ) {
   213         dreamcast_init();
   214     } else {
   215         dreamcast_configure_aica_only();
   216         mem_load_block( aica_program, 0x00800000, 2048*1024 );
   217     }
   218     mem_set_trace( trace_regions, TRUE );
   220     audio_init_driver( audio_driver_name );
   222     headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
   223     if( headless ) {
   224         display_set_driver( &display_null_driver );
   225     } else {
   226         gui_init(show_debugger, show_fullscreen);
   228         display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   229         if( display_driver == NULL ) {
   230             ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   231             exit(2);
   232         } else if( display_set_driver( display_driver ) == FALSE ) {
   233             ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   234                     display_driver->name );
   235             exit(2);
   236         }
   237     }
   239     hotkeys_init();
   241     maple_reattach_all();
   242     INFO( "%s! ready...", APP_NAME );
   244     for( ; optind < argc; optind++ ) {
   245         gboolean ok = gdrom_mount_image(argv[optind]);
   246         if( !ok ) {
   247             ok = file_load_magic( argv[optind] );
   248         }
   249         if( !ok ) {
   250             ERROR( "Unrecognized file '%s'", argv[optind] );
   251         }
   252         if( !no_start ) {
   253             start_immediately = ok;
   254         }
   255     }
   257     if( gdrom_get_current_disc() == NULL ) {
   258         const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
   259         if( disc_file != NULL ) {
   260             gdrom_mount_image( disc_file );
   261         }
   262     }
   264     sh4_translate_set_enabled( use_xlat );
   266     /* If requested, start the gdb server immediately before we go into the main
   267      * loop.
   268      */
   269     if( sh4_gdb_port != NULL ) {
   270         gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
   271     }
   272     if( arm_gdb_port != NULL ) {
   273         gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
   274     }
   276     if( headless ) {
   277         dreamcast_run();
   278     } else {
   279         gui_main_loop( start_immediately && dreamcast_can_run() );
   280     }
   281     dreamcast_shutdown();
   282     return 0;
   283 }
.