Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 698:6d9fd7f54609
prev697:479b8c213f61
next700:4650d0c7f6f9
author nkeynes
date Sun Jun 22 04:30:38 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Set log-level to trace automatically if we've asked for a trace on the command line
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 extern uint32_t sh4_cpu_multiplier;
    54 int main (int argc, char *argv[])
    55 {
    56     int opt;
    57     double t;
    58     gboolean display_ok;
    59     uint32_t time_secs, time_nanos;
    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 + auto quit */
   115 	    t = strtod(optarg, NULL);
   116 	    time_secs = (uint32_t)t;
   117 	    time_nanos = (int)((t - time_secs) * 1000000000);
   118 	    dreamcast_set_run_time( time_secs, time_nanos );
   119 	    dreamcast_set_exit_on_stop( TRUE );
   120 	    break;
   121 	case 'T': /* trace regions */
   122 	    trace_regions = optarg;
   123 	    set_global_log_level("trace");
   124 	    break;
   125 	case 'v': /* Log verbosity */
   126 	    if( !set_global_log_level(optarg) ) {
   127 		ERROR( "Unrecognized log level '%s'", optarg );
   128 	    }
   129 	    break;
   130 	case 'x': /* Disable translator */
   131 	    use_xlat = FALSE;
   132 	    break;
   133 	}
   134     }
   136     lxdream_load_config( );
   137     gdrom_list_init();
   139     if( aica_program == NULL ) {
   140 	dreamcast_init();
   141     } else {
   142 	dreamcast_configure_aica_only();
   143 	mem_load_block( aica_program, 0x00800000, 2048*1024 );
   144 	if( s3m_file != NULL ) {
   145 	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
   146 	}
   147     }
   148     mem_set_trace( trace_regions, TRUE );
   150     if( without_bios ) {
   151     	bios_install();
   152     	dcload_install();
   153     }
   155     audio_init_driver( audio_driver_name, 44100, AUDIO_FMT_16ST );
   157     if( headless ) {
   158 	display_set_driver( &display_null_driver );
   159     } else {
   160 	gui_init(show_debugger);
   162 	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   163 	if( display_driver == NULL ) {
   164 	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   165 	    exit(2);
   166 	} else if( display_set_driver( display_driver ) == FALSE ) {
   167 	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   168 		   display_driver->name );
   169 	    exit(2);
   170 	}
   171     }
   173     maple_reattach_all();
   174     INFO( "%s! ready...", APP_NAME );
   176     for( ; optind < argc; optind++ ) {
   177 	gboolean ok = gdrom_mount_image(argv[optind]);
   178 	if( !ok ) {
   179 	    ok = file_load_magic( argv[optind] );
   180 	}
   181 	if( !ok ) {
   182 	    ERROR( "Unrecognized file '%s'", argv[optind] );
   183 	}
   184 	if( !no_start ) {
   185 	    start_immediately = ok;
   186 	}
   187     }
   189     if( disc_file != NULL ) {
   190         gdrom_mount_image( disc_file );
   191     }
   193     if( gdrom_get_current_disc() == NULL ) {
   194         disc_file = lxdream_get_config_value( CONFIG_GDROM );
   195         if( disc_file != NULL ) {
   196             gdrom_mount_image( disc_file );
   197         }
   198     }
   200     sh4_set_use_xlat( use_xlat );
   202     if( headless ) {
   203         dreamcast_run();
   204     } else {
   205         gui_main_loop( start_immediately && dreamcast_can_run() );
   206     }
   207     dreamcast_shutdown();
   208     return 0;
   209 }
.