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