Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 1134:f502f3d32f90
prev1129:7b16bbd6209c
next1179:af1c5d7d5a5a
author nkeynes
date Wed Jan 19 17:50:09 2011 +1000 (13 years ago)
permissions -rw-r--r--
last change Implement vertex array range support, and move buffer operations to gl_vbo.c
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.h>
    24 #include "lxdream.h"
    25 #include "lxpaths.h"
    26 #include "gettext.h"
    27 #include "dream.h"
    28 #include "dreamcast.h"
    29 #include "display.h"
    30 #include "gui.h"
    31 #include "gdlist.h"
    32 #include "hotkeys.h"
    33 #include "loader.h"
    34 #include "mem.h"
    35 #include "plugin.h"
    36 #include "serial.h"
    37 #include "syscall.h"
    38 #include "aica/audio.h"
    39 #include "aica/armdasm.h"
    40 #include "gdrom/gdrom.h"
    41 #include "maple/maple.h"
    42 #include "pvr2/glutil.h"
    43 #include "sh4/sh4.h"
    44 #include "vmu/vmulist.h"
    46 #define GL_INFO_OPT 1
    48 char *option_list = "a:A:bc:e:dfg:G:hHl:m:npt:T:uvV:xX?";
    49 struct option longopts[] = {
    50         { "aica", required_argument, NULL, 'a' },
    51         { "audio", required_argument, NULL, 'A' },
    52         { "biosless", no_argument, NULL, 'b' },
    53         { "config", required_argument, NULL, 'c' },
    54         { "debugger", no_argument, NULL, 'd' },
    55         { "execute", required_argument, NULL, 'e' },
    56         { "fullscreen", no_argument, NULL, 'f' },
    57         { "gdb-sh4", required_argument, NULL, 'g' },  
    58         { "gdb-arm", required_argument, NULL, 'G' },
    59         { "gl-info", no_argument, NULL, GL_INFO_OPT },
    60         { "help", no_argument, NULL, 'h' },
    61         { "headless", no_argument, NULL, 'H' },
    62         { "log", required_argument, NULL,'l' }, 
    63         { "multiplier", required_argument, NULL, 'm' },
    64         { "run-time", required_argument, NULL, 't' },
    65         { "shadow", no_argument, NULL, 'X' },
    66         { "trace", required_argument, NULL, 'T' },
    67         { "unsafe", no_argument, NULL, 'u' },
    68         { "video", no_argument, NULL, 'V' },
    69         { "version", no_argument, NULL, 'v' }, 
    70         { NULL, 0, 0, 0 } };
    71 char *aica_program = NULL;
    72 char *display_driver_name = NULL;
    73 char *audio_driver_name = NULL;
    74 char *trace_regions = NULL;
    75 char *sh4_gdb_port = NULL;
    76 char *arm_gdb_port = NULL;
    77 gboolean start_immediately = FALSE;
    78 gboolean no_start = FALSE;
    79 gboolean headless = FALSE;
    80 sh4core_t sh4_core = SH4_TRANSLATE;
    81 gboolean show_debugger = FALSE;
    82 gboolean show_fullscreen = FALSE;
    83 gboolean use_bootrom = TRUE;
    84 extern uint32_t sh4_cpu_multiplier;
    86 static void print_version()
    87 {
    88     printf( "lxdream %s\n", lxdream_full_version );
    89 }
    91 static void print_usage()
    92 {
    93     print_version();
    94     printf( "Usage: lxdream %s [options] [disc-file] [save-state]\n\n", lxdream_full_version );
    96     printf( "Options:\n" );
    97     printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
    98     printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
    99     printf( "   -b, --biosless         %s\n", _("Run without the BIOS boot rom even if available") );
   100     printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
   101     printf( "   -e, --execute=PROGRAM  %s\n", _("Load and execute the given SH4 program") );
   102     printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
   103     printf( "   -f, --fullscreen       %s\n", _("Start in fullscreen mode") );
   104     printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
   105     printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
   106     printf( "   -h, --help             %s\n", _("Display this usage information") );
   107     printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
   108     printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
   109     printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
   110     printf( "   -n                     %s\n", _("Don't start running immediately") );
   111     printf( "   -p                     %s\n", _("Start running immediately on startup") );
   112     printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
   113     printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
   114     printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
   115     printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
   116     printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
   117     printf( "   -x                     %s\n", _("Disable the SH4 translator") );
   118     printf( "   -X                     %s\n", _("Run both SH4 interpreter and translator") );
   119 }
   121 static void bind_gettext_domain()
   122 {
   123 #ifdef ENABLE_NLS
   124     bindtextdomain( PACKAGE, get_locale_path() );
   125     textdomain(PACKAGE);
   126 #endif
   127 }
   129 int main (int argc, char *argv[])
   130 {
   131     int opt;
   132     double t;
   133     gboolean display_ok, have_disc = FALSE, have_save = FALSE, have_exec = FALSE;
   134     gboolean print_glinfo = FALSE;
   135     uint32_t time_secs, time_nanos;
   136     const char *exec_name = NULL;
   138     install_crash_handler();
   139     bind_gettext_domain();
   140     display_ok = gui_parse_cmdline(&argc, &argv);
   142     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   143         switch( opt ) {
   144         case 'a': /* AICA only mode - argument is an AICA program */
   145             aica_program = optarg;
   146             break;
   147         case 'A': /* Audio driver */
   148             audio_driver_name = optarg;
   149             break;
   150         case 'b': /* No Boot rom */
   151             use_bootrom = FALSE;
   152         case 'c': /* Config file */
   153             lxdream_set_config_filename(optarg);
   154             break;
   155         case 'd': /* Launch w/ debugger */
   156             show_debugger = TRUE;
   157             break;
   158         case 'e':
   159             exec_name = optarg;
   160             break;
   161         case 'f':
   162             show_fullscreen = TRUE;
   163             break;
   164         case 'g':
   165             sh4_gdb_port = optarg;
   166             break;
   167         case 'G':
   168             arm_gdb_port = optarg;
   169             break;
   170         case 'h': /* help */
   171         case '?':
   172             print_usage();
   173             exit(0);
   174             break;
   175         case 'H': /* Headless - shorthand for -V null */
   176             display_driver_name = "null";
   177             break;
   178         case 'l': /* Log verbosity */
   179             if( !set_global_log_level(optarg) ) {
   180                 ERROR( "Unrecognized log level '%s'", optarg );
   181             }
   182             break;
   183         case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
   184             t = strtod(optarg, NULL);
   185             sh4_cpu_multiplier = (int)(1000.0/t);
   186             break;
   187         case 'n': /* Don't start immediately */
   188             no_start = TRUE;
   189             start_immediately = FALSE;
   190             break;
   191         case 'p': /* Start immediately */
   192             start_immediately = TRUE;
   193             no_start = FALSE;
   194             break;
   195         case 't': /* Time limit + auto quit */
   196             t = strtod(optarg, NULL);
   197             time_secs = (uint32_t)t;
   198             time_nanos = (int)((t - time_secs) * 1000000000);
   199             dreamcast_set_run_time( time_secs, time_nanos );
   200             dreamcast_set_exit_on_stop( TRUE );
   201             break;
   202         case 'T': /* trace regions */
   203             trace_regions = optarg;
   204             set_global_log_level("trace");
   205             break;
   206         case 'u': /* Allow unsafe dcload syscalls */
   207             dcload_set_allow_unsafe(TRUE);
   208             break;
   209         case 'v': 
   210             print_version();
   211             exit(0);
   212             break;
   213         case 'V': /* Video driver */
   214             display_driver_name = optarg;
   215             break;
   216         case 'x': /* Disable translator */
   217             sh4_core = SH4_INTERPRET;
   218             break;
   219         case 'X': /* Shadow translator */
   220             sh4_core = SH4_SHADOW;
   221             break;
   222         case GL_INFO_OPT:
   223             print_glinfo = TRUE;
   224             break;
   225         }
   226     }
   228 #ifdef ENABLE_SHARED
   229     plugin_init();
   230 #endif
   232     lxdream_make_config_dir( );
   233     lxdream_load_config( );
   235     if( audio_driver_name != NULL && strcmp(audio_driver_name, "?") == 0 ) {
   236         print_version();
   237         print_audio_drivers(stdout);
   238         exit(0);
   239     }
   241     if( display_driver_name != NULL && strcmp(display_driver_name,"?") == 0 ) {
   242         print_version();
   243         print_display_drivers(stdout);
   244         exit(0);
   245     }
   247     if( print_glinfo ) {
   248         gui_init(FALSE, FALSE);
   249         display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   250         if( display_driver == NULL ) {
   251             ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   252             exit(2);
   253         } else if( display_set_driver( display_driver ) == FALSE ) {
   254             ERROR( "Video driver '%s' failed to initialize (could not connect to display?)",
   255                     display_driver->name );
   256             exit(2);
   257         } else if( display_driver->capabilities.has_gl == FALSE ) {
   258             ERROR( "Video driver '%s' has no GL capabilities.", display_driver_name );
   259             exit(2);
   260         }
   261         glPrintInfo(stdout);
   262         exit(0);
   264     }
   267     iso_init();
   268     gdrom_list_init();
   269     vmulist_init();
   271     if( aica_program == NULL ) {
   272         dreamcast_init(use_bootrom);
   273     } else {
   274         dreamcast_configure_aica_only();
   275         mem_load_block( aica_program, 0x00800000, 2048*1024 );
   276     }
   277     mem_set_trace( trace_regions, TRUE );
   279     audio_init_driver( audio_driver_name );
   281     headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
   282     if( headless ) {
   283         display_set_driver( &display_null_driver );
   284     } else {
   285         gui_init(show_debugger, show_fullscreen);
   287         display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   288         if( display_driver == NULL ) {
   289             ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   290             exit(2);
   291         } else if( display_set_driver( display_driver ) == FALSE ) {
   292             ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   293                     display_driver->name );
   294             exit(2);
   295         }
   296     }
   298     hotkeys_init();
   299     serial_init();
   301     maple_reattach_all();
   302     INFO( "%s! ready...", APP_NAME );
   304     for( ; optind < argc; optind++ ) {
   305         ERROR err;
   306         lxdream_file_type_t type = file_identify(argv[optind], -1, &err);
   307         if( type == FILE_SAVE_STATE ) {
   308             if( have_save ) {
   309                 ERROR( "Multiple save states given on command-line, ignoring %s", argv[optind] );
   310             } else {
   311                 have_save = dreamcast_load_state(argv[optind]);
   312                 if( !have_save )
   313                     no_start = TRUE;
   314             }
   315         } else {
   316             if( have_disc ) {
   317                 ERROR( "Multiple GD-ROM discs given on command-line, ignoring %s", argv[optind] );
   318             } else {
   319                 have_disc = gdrom_mount_image(argv[optind], &err);
   320                 if( !have_disc ) {
   321                     ERROR( err.msg );
   322                     no_start = TRUE;
   323                 }
   324             }
   325         }
   326     }
   328     if( exec_name != NULL ) {
   329         ERROR err;
   330         if( have_save ) {
   331             ERROR( "Both a save state and an executable were specified, ignoring %s", exec_name );
   332         } else {
   333             have_exec = file_load_exec( exec_name, &err );
   334             if( !have_exec ) {
   335                 ERROR( err.msg );
   336                 no_start = TRUE;
   337             }
   338         }
   339     }
   341     if( !no_start && (have_exec || have_disc || have_save) ) {
   342         start_immediately = TRUE;
   343     }
   345     if( gdrom_get_current_disc() == NULL ) {
   346         ERROR err;
   347         gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
   348         if( disc_file != NULL ) {
   349             gboolean ok = gdrom_mount_image( disc_file, &err );
   350             g_free(disc_file);
   351             if( !ok ) {
   352                 WARN( err.msg );
   353             }
   354         }
   355     }
   357     sh4_set_core( sh4_core );
   359     /* If requested, start the gdb server immediately before we go into the main
   360      * loop.
   361      */
   362     if( sh4_gdb_port != NULL ) {
   363         gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
   364     }
   365     if( arm_gdb_port != NULL ) {
   366         gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
   367     }
   369     if( headless ) {
   370         dreamcast_run();
   371     } else {
   372         gui_main_loop( start_immediately && dreamcast_can_run() );
   373     }
   374     dreamcast_shutdown();
   375     return 0;
   376 }
.