Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 562:e598411b640b
prev561:533f6b478071
next564:dc7b5ffb0535
author nkeynes
date Tue Jan 01 05:48:06 2008 +0000 (11 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Add configure and command-line parameters to perform IO tracing
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 <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: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 char *trace_regions = NULL;
    45 gboolean start_immediately = FALSE;
    46 gboolean headless = FALSE;
    47 gboolean without_bios = FALSE;
    48 gboolean use_xlat = TRUE;
    49 gboolean show_debugger = FALSE;
    50 uint32_t time_secs = 0;
    51 uint32_t time_nanos = 0;
    52 extern uint32_t sh4_cpu_multiplier;
    54 int main (int argc, char *argv[])
    55 {
    56     int opt, i;
    57     double t;
    58     gboolean display_ok;
    60     install_crash_handler();
    61     gdrom_get_native_devices();
    62 #ifdef ENABLE_NLS
    63     bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
    64     textdomain (PACKAGE);
    65 #endif
    66     display_ok = gui_parse_cmdline(&argc, &argv);
    68     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
    69 	switch( opt ) {
    70 	case 'a': /* AICA only mode - argument is an AICA program */
    71 	    aica_program = optarg;
    72 	    break;
    73 	case 'c': /* Config file */
    74 	    lxdream_set_config_filename(optarg);
    75 	    break;
    76 	case 'd': /* Mount disc */
    77 	    disc_file = optarg;
    78 	    break;
    79 	case 'D': /* Launch w/ debugger */
    80 	    show_debugger = TRUE;
    81 	    break;
    82 	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
    83 	    t = strtod(optarg, NULL);
    84 	    sh4_cpu_multiplier = (int)(1000.0/t);
    85 	    break;
    86 	case 's': /* AICA-only w/ S3M player */
    87 	    aica_program = S3M_PLAYER;
    88 	    s3m_file = optarg;
    89 	    break;
    90 	case 'A': /* Audio driver */
    91 	    audio_driver_name = optarg;
    92 	    break;
    93 	case 'V': /* Video driver */
    94 	    display_driver_name = optarg;
    95 	    break;
    96 	case 'p': /* Start immediately */
    97 	    start_immediately = TRUE;
    98     	    break;
    99 	case 'u': /* Allow unsafe dcload syscalls */
   100 	    dcload_set_allow_unsafe(TRUE);
   101 	    break;
   102     	case 'b': /* No BIOS */
   103     	    without_bios = TRUE;
   104     	    break;
   105         case 'h': /* Headless */
   106             headless = TRUE;
   107             break;
   108 	case 't': /* Time limit */
   109 	    t = strtod(optarg, NULL);
   110 	    time_secs = (uint32_t)t;
   111 	    time_nanos = (int)((t - time_secs) * 1000000000);
   112 	    break;
   113 	case 'T': /* trace regions */
   114 	    trace_regions = optarg;
   115 	    break;
   116 	case 'v': /* Log verbosity */
   117 	    if( !set_global_log_level(optarg) ) {
   118 		ERROR( "Unrecognized log level '%s'", optarg );
   119 	    }
   120 	    break;
   121 	case 'x': /* Disable translator */
   122 	    use_xlat = FALSE;
   123 	    break;
   124 	}
   125     }
   127     lxdream_load_config( );
   129     if( aica_program == NULL ) {
   130 	dreamcast_init();
   131     } else {
   132 	dreamcast_configure_aica_only();
   133 	mem_load_block( aica_program, 0x00800000, 2048*1024 );
   134 	if( s3m_file != NULL ) {
   135 	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
   136 	}
   137     }
   138     mem_set_trace( trace_regions, TRUE );
   140     if( without_bios ) {
   141     	bios_install();
   142 	dcload_install();
   143     }
   145     audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
   146     if( audio_driver == NULL ) {
   147 	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
   148 	exit(2);
   149     } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
   150 	ERROR( "Failed to initialize audio driver '%s', using null driver", 
   151 	       audio_driver->name );
   152 	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
   153     }
   155     if( headless ) {
   156 	display_set_driver( &display_null_driver );
   157     } else {
   158 	gui_init(show_debugger);
   160 	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   161 	if( display_driver == NULL ) {
   162 	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   163 	    exit(2);
   164 	} else if( display_set_driver( display_driver ) == FALSE ) {
   165 	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   166 		   display_driver->name );
   167 	    exit(2);
   168 	}
   169     }
   171     maple_reattach_all();
   172     INFO( "%s! ready...", APP_NAME );
   174     for( ; optind < argc; optind++ ) {
   175 	gboolean ok = gdrom_menu_open_file(argv[optind]);
   176 	if( !ok ) {
   177 	    ok = file_load_magic( argv[optind] );
   178 	}
   179 	if( !ok ) {
   180 	    ERROR( "Unrecognized file '%s'", argv[optind] );
   181 	}
   182 	start_immediately = ok;
   183     }
   185     if( disc_file != NULL ) {
   186 	gdrom_menu_open_file( disc_file );
   187     }
   189     if( gdrom_get_current_disc() == NULL ) {
   190 	disc_file = lxdream_get_config_value( CONFIG_GDROM );
   191 	if( disc_file != NULL ) {
   192 	    gdrom_menu_open_file( disc_file );
   193 	}
   194     }
   196     sh4_set_use_xlat( use_xlat );
   198     if( start_immediately ) {
   199         if( dreamcast_can_run() ) {
   200 	    if( time_nanos != 0 || time_secs != 0 ) {
   201 	        dreamcast_run_for(time_secs, time_nanos);
   202 		return 0;
   203 	    } else {
   204 	        dreamcast_run();
   205 	    }
   206 	} else {
   207 	    ERROR( "Unable to start dreamcast: no program/bios loaded" );
   208 	}
   209     }
   210     if( !headless ) {
   211 	gui_main_loop();
   212     }
   213     return 0;
   214 }
.