Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 586:2a3ba82cf243
prev543:361ec0a70cf2
next669:ab344e42bca9
author nkeynes
date Wed Jan 30 09:38:24 2008 +0000 (16 years ago)
permissions -rw-r--r--
last change Deliver TMU interrupts precisely rather than only at end of time slice
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/sh4.h"
    35 #define S3M_PLAYER "s3mplay.bin"
    37 char *option_list = "a:m:s:A:V:v:puhbd:c:t:T:xDn";
    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 no_start = FALSE;
    47 gboolean headless = FALSE;
    48 gboolean without_bios = FALSE;
    49 gboolean use_xlat = TRUE;
    50 gboolean show_debugger = FALSE;
    51 uint32_t time_secs = 0;
    52 uint32_t time_nanos = 0;
    53 extern uint32_t sh4_cpu_multiplier;
    55 int main (int argc, char *argv[])
    56 {
    57     int opt, i;
    58     double t;
    59     gboolean display_ok;
    61     install_crash_handler();
    62     gdrom_get_native_devices();
    63 #ifdef ENABLE_NLS
    64     bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
    65     textdomain (PACKAGE);
    66 #endif
    67     display_ok = gui_parse_cmdline(&argc, &argv);
    69     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
    70 	switch( opt ) {
    71 	case 'a': /* AICA only mode - argument is an AICA program */
    72 	    aica_program = optarg;
    73 	    break;
    74 	case 'c': /* Config file */
    75 	    lxdream_set_config_filename(optarg);
    76 	    break;
    77 	case 'd': /* Mount disc */
    78 	    disc_file = optarg;
    79 	    break;
    80 	case 'D': /* Launch w/ debugger */
    81 	    show_debugger = TRUE;
    82 	    break;
    83 	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
    84 	    t = strtod(optarg, NULL);
    85 	    sh4_cpu_multiplier = (int)(1000.0/t);
    86 	    break;
    87 	case 'n': /* Don't start immediately */
    88 	    no_start = TRUE;
    89 	    start_immediately = FALSE;
    90 	    break;
    91 	case 's': /* AICA-only w/ S3M player */
    92 	    aica_program = S3M_PLAYER;
    93 	    s3m_file = optarg;
    94 	    break;
    95 	case 'A': /* Audio driver */
    96 	    audio_driver_name = optarg;
    97 	    break;
    98 	case 'V': /* Video driver */
    99 	    display_driver_name = optarg;
   100 	    break;
   101 	case 'p': /* Start immediately */
   102 	    start_immediately = TRUE;
   103 	    no_start = FALSE;
   104     	    break;
   105 	case 'u': /* Allow unsafe dcload syscalls */
   106 	    dcload_set_allow_unsafe(TRUE);
   107 	    break;
   108     	case 'b': /* No BIOS */
   109     	    without_bios = TRUE;
   110     	    break;
   111         case 'h': /* Headless */
   112             headless = TRUE;
   113             break;
   114 	case 't': /* Time limit */
   115 	    t = strtod(optarg, NULL);
   116 	    time_secs = (uint32_t)t;
   117 	    time_nanos = (int)((t - time_secs) * 1000000000);
   118 	    break;
   119 	case 'T': /* trace regions */
   120 	    trace_regions = optarg;
   121 	    break;
   122 	case 'v': /* Log verbosity */
   123 	    if( !set_global_log_level(optarg) ) {
   124 		ERROR( "Unrecognized log level '%s'", optarg );
   125 	    }
   126 	    break;
   127 	case 'x': /* Disable translator */
   128 	    use_xlat = FALSE;
   129 	    break;
   130 	}
   131     }
   133     lxdream_load_config( );
   135     if( aica_program == NULL ) {
   136 	dreamcast_init();
   137     } else {
   138 	dreamcast_configure_aica_only();
   139 	mem_load_block( aica_program, 0x00800000, 2048*1024 );
   140 	if( s3m_file != NULL ) {
   141 	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
   142 	}
   143     }
   144     mem_set_trace( trace_regions, TRUE );
   146     if( without_bios ) {
   147     	bios_install();
   148 	dcload_install();
   149     }
   151     audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
   152     if( audio_driver == NULL ) {
   153 	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
   154 	exit(2);
   155     } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
   156 	ERROR( "Failed to initialize audio driver '%s', using null driver", 
   157 	       audio_driver->name );
   158 	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
   159     }
   161     if( headless ) {
   162 	display_set_driver( &display_null_driver );
   163     } else {
   164 	gui_init(show_debugger);
   166 	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   167 	if( display_driver == NULL ) {
   168 	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   169 	    exit(2);
   170 	} else if( display_set_driver( display_driver ) == FALSE ) {
   171 	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   172 		   display_driver->name );
   173 	    exit(2);
   174 	}
   175     }
   177     maple_reattach_all();
   178     INFO( "%s! ready...", APP_NAME );
   180     for( ; optind < argc; optind++ ) {
   181 	gboolean ok = gdrom_menu_open_file(argv[optind]);
   182 	if( !ok ) {
   183 	    ok = file_load_magic( argv[optind] );
   184 	}
   185 	if( !ok ) {
   186 	    ERROR( "Unrecognized file '%s'", argv[optind] );
   187 	}
   188 	if( !no_start ) {
   189 	    start_immediately = ok;
   190 	}
   191     }
   193     if( disc_file != NULL ) {
   194 	gdrom_menu_open_file( disc_file );
   195     }
   197     if( gdrom_get_current_disc() == NULL ) {
   198 	disc_file = lxdream_get_config_value( CONFIG_GDROM );
   199 	if( disc_file != NULL ) {
   200 	    gdrom_menu_open_file( disc_file );
   201 	}
   202     }
   204     sh4_set_use_xlat( use_xlat );
   206     if( start_immediately ) {
   207         if( dreamcast_can_run() ) {
   208 	    if( time_nanos != 0 || time_secs != 0 ) {
   209 	        dreamcast_run_for(time_secs, time_nanos);
   210 		return 0;
   211 	    } else {
   212 	        dreamcast_run();
   213 	    }
   214 	} else {
   215 	    ERROR( "Unable to start dreamcast: no program/bios loaded" );
   216 	}
   217     }
   218     if( !headless ) {
   219 	gui_main_loop();
   220     }
   221     return 0;
   222 }
.