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