Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 556:72dbf70344db
prev543:361ec0a70cf2
next561:533f6b478071
author nkeynes
date Tue Jan 01 04:49:57 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Add command line option to control default log level
view annotate diff log raw
     1 /**
     2  * $Id: main.c,v 1.35 2007-11-07 11:45:53 nkeynes Exp $
     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 <unistd.h>
    21 #include <getopt.h>
    22 #include "dream.h"
    23 #include "config.h"
    24 #include "syscall.h"
    25 #include "mem.h"
    26 #include "dreamcast.h"
    27 #include "display.h"
    28 #include "loader.h"
    29 #include "gui.h"
    30 #include "aica/audio.h"
    31 #include "gdrom/gdrom.h"
    32 #include "maple/maple.h"
    33 #include "sh4/sh4core.h"
    35 #define S3M_PLAYER "s3mplay.bin"
    37 char *option_list = "a:m:s:A:V:v:puhbd:c:t:xD";
    38 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    39 char *aica_program = NULL;
    40 char *s3m_file = NULL;
    41 const char *disc_file = NULL;
    42 char *display_driver_name = NULL;
    43 char *audio_driver_name = NULL;
    44 gboolean start_immediately = FALSE;
    45 gboolean headless = FALSE;
    46 gboolean without_bios = FALSE;
    47 gboolean use_xlat = TRUE;
    48 gboolean show_debugger = FALSE;
    49 uint32_t time_secs = 0;
    50 uint32_t time_nanos = 0;
    51 extern uint32_t sh4_cpu_multiplier;
    53 int main (int argc, char *argv[])
    54 {
    55     int opt, i;
    56     double t;
    57     gboolean display_ok;
    59     install_crash_handler();
    60     gdrom_get_native_devices();
    61 #ifdef ENABLE_NLS
    62     bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
    63     textdomain (PACKAGE);
    64 #endif
    65     display_ok = gui_parse_cmdline(&argc, &argv);
    67     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
    68 	switch( opt ) {
    69 	case 'a': /* AICA only mode - argument is an AICA program */
    70 	    aica_program = optarg;
    71 	    break;
    72 	case 'c': /* Config file */
    73 	    lxdream_set_config_filename(optarg);
    74 	    break;
    75 	case 'd': /* Mount disc */
    76 	    disc_file = optarg;
    77 	    break;
    78 	case 'D': /* Launch w/ debugger */
    79 	    show_debugger = TRUE;
    80 	    break;
    81 	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
    82 	    t = strtod(optarg, NULL);
    83 	    sh4_cpu_multiplier = (int)(1000.0/t);
    84 	    break;
    85 	case 's': /* AICA-only w/ S3M player */
    86 	    aica_program = S3M_PLAYER;
    87 	    s3m_file = optarg;
    88 	    break;
    89 	case 'A': /* Audio driver */
    90 	    audio_driver_name = optarg;
    91 	    break;
    92 	case 'V': /* Video driver */
    93 	    display_driver_name = optarg;
    94 	    break;
    95 	case 'p': /* Start immediately */
    96 	    start_immediately = TRUE;
    97     	    break;
    98 	case 'u': /* Allow unsafe dcload syscalls */
    99 	    dcload_set_allow_unsafe(TRUE);
   100 	    break;
   101     	case 'b': /* No BIOS */
   102     	    without_bios = TRUE;
   103     	    break;
   104         case 'h': /* Headless */
   105             headless = TRUE;
   106             break;
   107 	case 't': /* Time limit */
   108 	    t = strtod(optarg, NULL);
   109 	    time_secs = (uint32_t)t;
   110 	    time_nanos = (int)((t - time_secs) * 1000000000);
   111 	    break;
   112 	case 'v': /* Log verbosity */
   113 	    if( !set_global_log_level(optarg) ) {
   114 		ERROR( "Unrecognized log level '%s'", optarg );
   115 	    }
   116 	    break;
   117 	case 'x': /* Disable translator */
   118 	    use_xlat = FALSE;
   119 	    break;
   120 	}
   121     }
   123     lxdream_load_config( );
   125     if( aica_program == NULL ) {
   126 	dreamcast_init();
   127     } else {
   128 	dreamcast_configure_aica_only();
   129 	mem_load_block( aica_program, 0x00800000, 2048*1024 );
   130 	if( s3m_file != NULL ) {
   131 	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
   132 	}
   133     }
   135     if( without_bios ) {
   136     	bios_install();
   137 	dcload_install();
   138     }
   140     audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
   141     if( audio_driver == NULL ) {
   142 	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
   143 	exit(2);
   144     } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
   145 	ERROR( "Failed to initialize audio driver '%s', using null driver", 
   146 	       audio_driver->name );
   147 	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
   148     }
   150     if( headless ) {
   151 	display_set_driver( &display_null_driver );
   152     } else {
   153 	gui_init(show_debugger);
   155 	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   156 	if( display_driver == NULL ) {
   157 	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   158 	    exit(2);
   159 	} else if( display_set_driver( display_driver ) == FALSE ) {
   160 	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   161 		   display_driver->name );
   162 	    exit(2);
   163 	}
   164     }
   166     maple_reattach_all();
   167     INFO( "%s! ready...", APP_NAME );
   169     for( ; optind < argc; optind++ ) {
   170 	gboolean ok = gdrom_menu_open_file(argv[optind]);
   171 	if( !ok ) {
   172 	    ok = file_load_magic( argv[optind] );
   173 	}
   174 	if( !ok ) {
   175 	    ERROR( "Unrecognized file '%s'", argv[optind] );
   176 	}
   177 	start_immediately = ok;
   178     }
   180     if( disc_file != NULL ) {
   181 	gdrom_menu_open_file( disc_file );
   182     }
   184     if( gdrom_get_current_disc() == NULL ) {
   185 	disc_file = lxdream_get_config_value( CONFIG_GDROM );
   186 	if( disc_file != NULL ) {
   187 	    gdrom_menu_open_file( disc_file );
   188 	}
   189     }
   191     sh4_set_use_xlat( use_xlat );
   193     if( start_immediately ) {
   194         if( dreamcast_can_run() ) {
   195 	    if( time_nanos != 0 || time_secs != 0 ) {
   196 	        dreamcast_run_for(time_secs, time_nanos);
   197 		return 0;
   198 	    } else {
   199 	        dreamcast_run();
   200 	    }
   201 	} else {
   202 	    ERROR( "Unable to start dreamcast: no program/bios loaded" );
   203 	}
   204     }
   205     if( !headless ) {
   206 	gui_main_loop();
   207     }
   208     return 0;
   209 }
.