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