Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 678:35eb00945316
prev671:a530ea88eebd
next681:1755a126b109
author nkeynes
date Thu May 29 11:00:26 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Split gdrom.h into public and private gddriver.h
Reorganize gdrom mount to use a disc change hook
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 <glib/gi18n.h>
    24 #include "lxdream.h"
    25 #include "syscall.h"
    26 #include "mem.h"
    27 #include "dreamcast.h"
    28 #include "display.h"
    29 #include "loader.h"
    30 #include "gui.h"
    31 #include "aica/audio.h"
    32 #include "gdrom/gdrom.h"
    33 #include "maple/maple.h"
    34 #include "sh4/sh4.h"
    36 #define S3M_PLAYER "s3mplay.bin"
    38 char *option_list = "a:m:s:A:V:v:puhbd:c:t:T:xDn";
    39 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    40 char *aica_program = NULL;
    41 char *s3m_file = NULL;
    42 const char *disc_file = NULL;
    43 char *display_driver_name = NULL;
    44 char *audio_driver_name = NULL;
    45 char *trace_regions = NULL;
    46 gboolean start_immediately = FALSE;
    47 gboolean no_start = FALSE;
    48 gboolean headless = FALSE;
    49 gboolean without_bios = FALSE;
    50 gboolean use_xlat = TRUE;
    51 gboolean show_debugger = FALSE;
    52 uint32_t time_secs = 0;
    53 uint32_t time_nanos = 0;
    54 extern uint32_t sh4_cpu_multiplier;
    56 int main (int argc, char *argv[])
    57 {
    58     int opt;
    59     double t;
    60     gboolean display_ok;
    62     install_crash_handler();
    63     gdrom_get_native_devices();
    64 #ifdef ENABLE_NLS
    65     bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
    66     textdomain (PACKAGE);
    67 #endif
    68     display_ok = gui_parse_cmdline(&argc, &argv);
    70     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
    71 	switch( opt ) {
    72 	case 'a': /* AICA only mode - argument is an AICA program */
    73 	    aica_program = optarg;
    74 	    break;
    75 	case 'c': /* Config file */
    76 	    lxdream_set_config_filename(optarg);
    77 	    break;
    78 	case 'd': /* Mount disc */
    79 	    disc_file = optarg;
    80 	    break;
    81 	case 'D': /* Launch w/ debugger */
    82 	    show_debugger = TRUE;
    83 	    break;
    84 	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
    85 	    t = strtod(optarg, NULL);
    86 	    sh4_cpu_multiplier = (int)(1000.0/t);
    87 	    break;
    88 	case 'n': /* Don't start immediately */
    89 	    no_start = TRUE;
    90 	    start_immediately = FALSE;
    91 	    break;
    92 	case 's': /* AICA-only w/ S3M player */
    93 	    aica_program = S3M_PLAYER;
    94 	    s3m_file = optarg;
    95 	    break;
    96 	case 'A': /* Audio driver */
    97 	    audio_driver_name = optarg;
    98 	    break;
    99 	case 'V': /* Video driver */
   100 	    display_driver_name = optarg;
   101 	    break;
   102 	case 'p': /* Start immediately */
   103 	    start_immediately = TRUE;
   104 	    no_start = FALSE;
   105     	    break;
   106 	case 'u': /* Allow unsafe dcload syscalls */
   107 	    dcload_set_allow_unsafe(TRUE);
   108 	    break;
   109     	case 'b': /* No BIOS */
   110     	    without_bios = TRUE;
   111     	    break;
   112         case 'h': /* Headless */
   113             headless = TRUE;
   114             break;
   115 	case 't': /* Time limit */
   116 	    t = strtod(optarg, NULL);
   117 	    time_secs = (uint32_t)t;
   118 	    time_nanos = (int)((t - time_secs) * 1000000000);
   119 	    break;
   120 	case 'T': /* trace regions */
   121 	    trace_regions = optarg;
   122 	    break;
   123 	case 'v': /* Log verbosity */
   124 	    if( !set_global_log_level(optarg) ) {
   125 		ERROR( "Unrecognized log level '%s'", optarg );
   126 	    }
   127 	    break;
   128 	case 'x': /* Disable translator */
   129 	    use_xlat = FALSE;
   130 	    break;
   131 	}
   132     }
   134     lxdream_load_config( );
   136     if( aica_program == NULL ) {
   137 	dreamcast_init();
   138     } else {
   139 	dreamcast_configure_aica_only();
   140 	mem_load_block( aica_program, 0x00800000, 2048*1024 );
   141 	if( s3m_file != NULL ) {
   142 	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
   143 	}
   144     }
   145     mem_set_trace( trace_regions, TRUE );
   147     if( without_bios ) {
   148     	bios_install();
   149 	dcload_install();
   150     }
   152     audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
   153     if( audio_driver == NULL ) {
   154 	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
   155 	exit(2);
   156     } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
   157 	ERROR( "Failed to initialize audio driver '%s', using null driver", 
   158 	       audio_driver->name );
   159 	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
   160     }
   162     if( headless ) {
   163 	display_set_driver( &display_null_driver );
   164     } else {
   165 	gui_init(show_debugger);
   167 	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   168 	if( display_driver == NULL ) {
   169 	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   170 	    exit(2);
   171 	} else if( display_set_driver( display_driver ) == FALSE ) {
   172 	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   173 		   display_driver->name );
   174 	    exit(2);
   175 	}
   176     }
   178     maple_reattach_all();
   179     INFO( "%s! ready...", APP_NAME );
   181     for( ; optind < argc; optind++ ) {
   182 	gboolean ok = gdrom_mount_image(argv[optind]);
   183 	if( !ok ) {
   184 	    ok = file_load_magic( argv[optind] );
   185 	}
   186 	if( !ok ) {
   187 	    ERROR( "Unrecognized file '%s'", argv[optind] );
   188 	}
   189 	if( !no_start ) {
   190 	    start_immediately = ok;
   191 	}
   192     }
   194     if( disc_file != NULL ) {
   195         gdrom_mount_image( disc_file );
   196     }
   198     if( gdrom_get_current_disc() == NULL ) {
   199         disc_file = lxdream_get_config_value( CONFIG_GDROM );
   200         if( disc_file != NULL ) {
   201             gdrom_mount_image( disc_file );
   202         }
   203     }
   205     sh4_set_use_xlat( use_xlat );
   207     if( start_immediately ) {
   208         if( dreamcast_can_run() ) {
   209 	    if( time_nanos != 0 || time_secs != 0 ) {
   210 	        dreamcast_run_for(time_secs, time_nanos);
   211 		dreamcast_shutdown();
   212 		return 0;
   213 	    } else {
   214 	        dreamcast_run();
   215 	    }
   216 	} else {
   217 	    ERROR( "Unable to start dreamcast: no program/bios loaded" );
   218 	}
   219     }
   220     if( !headless ) {
   221 	gui_main_loop();
   222     }
   223     dreamcast_shutdown();
   224     return 0;
   225 }
.