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
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@30
     3
 *
nkeynes@30
     4
 * Main program, initializes dreamcast and gui, then passes control off to
nkeynes@537
     5
 * the main loop. 
nkeynes@30
     6
 *
nkeynes@30
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@30
     8
 *
nkeynes@30
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@30
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@30
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@30
    12
 * (at your option) any later version.
nkeynes@30
    13
 *
nkeynes@30
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@30
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@30
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@30
    17
 * GNU General Public License for more details.
nkeynes@1
    18
 */
nkeynes@1
    19
nkeynes@678
    20
#include <stdlib.h>
nkeynes@68
    21
#include <unistd.h>
nkeynes@94
    22
#include <getopt.h>
nkeynes@669
    23
#include <glib/gi18n.h>
nkeynes@669
    24
#include "lxdream.h"
nkeynes@105
    25
#include "syscall.h"
nkeynes@422
    26
#include "mem.h"
nkeynes@27
    27
#include "dreamcast.h"
nkeynes@422
    28
#include "display.h"
nkeynes@422
    29
#include "loader.h"
nkeynes@450
    30
#include "gui.h"
nkeynes@106
    31
#include "aica/audio.h"
nkeynes@422
    32
#include "gdrom/gdrom.h"
nkeynes@144
    33
#include "maple/maple.h"
nkeynes@586
    34
#include "sh4/sh4.h"
nkeynes@11
    35
nkeynes@77
    36
#define S3M_PLAYER "s3mplay.bin"
nkeynes@77
    37
nkeynes@586
    38
char *option_list = "a:m:s:A:V:v:puhbd:c:t:T:xDn";
nkeynes@94
    39
struct option longopts[1] = { { NULL, 0, 0, 0 } };
nkeynes@68
    40
char *aica_program = NULL;
nkeynes@77
    41
char *s3m_file = NULL;
nkeynes@480
    42
const char *disc_file = NULL;
nkeynes@531
    43
char *display_driver_name = NULL;
nkeynes@531
    44
char *audio_driver_name = NULL;
nkeynes@586
    45
char *trace_regions = NULL;
nkeynes@68
    46
gboolean start_immediately = FALSE;
nkeynes@586
    47
gboolean no_start = FALSE;
nkeynes@77
    48
gboolean headless = FALSE;
nkeynes@87
    49
gboolean without_bios = FALSE;
nkeynes@402
    50
gboolean use_xlat = TRUE;
nkeynes@392
    51
gboolean show_debugger = FALSE;
nkeynes@414
    52
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    53
nkeynes@30
    54
int main (int argc, char *argv[])
nkeynes@1
    55
{
nkeynes@669
    56
    int opt;
nkeynes@372
    57
    double t;
nkeynes@531
    58
    gboolean display_ok;
nkeynes@689
    59
    uint32_t time_secs, time_nanos;
nkeynes@495
    60
nkeynes@495
    61
    install_crash_handler();
nkeynes@543
    62
    gdrom_get_native_devices();
nkeynes@1
    63
#ifdef ENABLE_NLS
nkeynes@68
    64
    bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
nkeynes@68
    65
    textdomain (PACKAGE);
nkeynes@1
    66
#endif
nkeynes@531
    67
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
    68
nkeynes@94
    69
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@68
    70
	switch( opt ) {
nkeynes@68
    71
	case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@68
    72
	    aica_program = optarg;
nkeynes@68
    73
	    break;
nkeynes@144
    74
	case 'c': /* Config file */
nkeynes@450
    75
	    lxdream_set_config_filename(optarg);
nkeynes@144
    76
	    break;
nkeynes@144
    77
	case 'd': /* Mount disc */
nkeynes@144
    78
	    disc_file = optarg;
nkeynes@144
    79
	    break;
nkeynes@392
    80
	case 'D': /* Launch w/ debugger */
nkeynes@392
    81
	    show_debugger = TRUE;
nkeynes@392
    82
	    break;
nkeynes@414
    83
	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@414
    84
	    t = strtod(optarg, NULL);
nkeynes@414
    85
	    sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@414
    86
	    break;
nkeynes@586
    87
	case 'n': /* Don't start immediately */
nkeynes@586
    88
	    no_start = TRUE;
nkeynes@586
    89
	    start_immediately = FALSE;
nkeynes@586
    90
	    break;
nkeynes@77
    91
	case 's': /* AICA-only w/ S3M player */
nkeynes@77
    92
	    aica_program = S3M_PLAYER;
nkeynes@77
    93
	    s3m_file = optarg;
nkeynes@77
    94
	    break;
nkeynes@68
    95
	case 'A': /* Audio driver */
nkeynes@106
    96
	    audio_driver_name = optarg;
nkeynes@68
    97
	    break;
nkeynes@68
    98
	case 'V': /* Video driver */
nkeynes@144
    99
	    display_driver_name = optarg;
nkeynes@68
   100
	    break;
nkeynes@68
   101
	case 'p': /* Start immediately */
nkeynes@68
   102
	    start_immediately = TRUE;
nkeynes@586
   103
	    no_start = FALSE;
nkeynes@77
   104
    	    break;
nkeynes@209
   105
	case 'u': /* Allow unsafe dcload syscalls */
nkeynes@209
   106
	    dcload_set_allow_unsafe(TRUE);
nkeynes@182
   107
	    break;
nkeynes@87
   108
    	case 'b': /* No BIOS */
nkeynes@87
   109
    	    without_bios = TRUE;
nkeynes@87
   110
    	    break;
nkeynes@77
   111
        case 'h': /* Headless */
nkeynes@77
   112
            headless = TRUE;
nkeynes@77
   113
            break;
nkeynes@689
   114
	case 't': /* Time limit + auto quit */
nkeynes@372
   115
	    t = strtod(optarg, NULL);
nkeynes@372
   116
	    time_secs = (uint32_t)t;
nkeynes@372
   117
	    time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@689
   118
	    dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@689
   119
	    dreamcast_set_exit_on_stop( TRUE );
nkeynes@379
   120
	    break;
nkeynes@586
   121
	case 'T': /* trace regions */
nkeynes@586
   122
	    trace_regions = optarg;
nkeynes@698
   123
	    set_global_log_level("trace");
nkeynes@586
   124
	    break;
nkeynes@586
   125
	case 'v': /* Log verbosity */
nkeynes@586
   126
	    if( !set_global_log_level(optarg) ) {
nkeynes@586
   127
		ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@586
   128
	    }
nkeynes@586
   129
	    break;
nkeynes@402
   130
	case 'x': /* Disable translator */
nkeynes@402
   131
	    use_xlat = FALSE;
nkeynes@379
   132
	    break;
nkeynes@68
   133
	}
nkeynes@68
   134
    }
nkeynes@30
   135
nkeynes@450
   136
    lxdream_load_config( );
nkeynes@691
   137
    gdrom_list_init();
nkeynes@691
   138
    
nkeynes@68
   139
    if( aica_program == NULL ) {
nkeynes@435
   140
	dreamcast_init();
nkeynes@68
   141
    } else {
nkeynes@68
   142
	dreamcast_configure_aica_only();
nkeynes@68
   143
	mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@77
   144
	if( s3m_file != NULL ) {
nkeynes@77
   145
	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
nkeynes@77
   146
	}
nkeynes@68
   147
    }
nkeynes@586
   148
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   149
nkeynes@87
   150
    if( without_bios ) {
nkeynes@87
   151
    	bios_install();
nkeynes@697
   152
    	dcload_install();
nkeynes@87
   153
    }
nkeynes@106
   154
nkeynes@697
   155
    audio_init_driver( audio_driver_name, 44100, AUDIO_FMT_16ST );
nkeynes@697
   156
    
nkeynes@106
   157
    if( headless ) {
nkeynes@144
   158
	display_set_driver( &display_null_driver );
nkeynes@106
   159
    } else {
nkeynes@435
   160
	gui_init(show_debugger);
nkeynes@435
   161
nkeynes@531
   162
	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@531
   163
	if( display_driver == NULL ) {
nkeynes@531
   164
	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@531
   165
	    exit(2);
nkeynes@531
   166
	} else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@531
   167
	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@531
   168
		   display_driver->name );
nkeynes@531
   169
	    exit(2);
nkeynes@106
   170
	}
nkeynes@106
   171
    }
nkeynes@106
   172
nkeynes@144
   173
    maple_reattach_all();
nkeynes@180
   174
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   175
nkeynes@446
   176
    for( ; optind < argc; optind++ ) {
nkeynes@678
   177
	gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@446
   178
	if( !ok ) {
nkeynes@446
   179
	    ok = file_load_magic( argv[optind] );
nkeynes@446
   180
	}
nkeynes@446
   181
	if( !ok ) {
nkeynes@446
   182
	    ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@446
   183
	}
nkeynes@586
   184
	if( !no_start ) {
nkeynes@586
   185
	    start_immediately = ok;
nkeynes@586
   186
	}
nkeynes@94
   187
    }
nkeynes@94
   188
nkeynes@144
   189
    if( disc_file != NULL ) {
nkeynes@678
   190
        gdrom_mount_image( disc_file );
nkeynes@144
   191
    }
nkeynes@144
   192
nkeynes@464
   193
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@678
   194
        disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@678
   195
        if( disc_file != NULL ) {
nkeynes@678
   196
            gdrom_mount_image( disc_file );
nkeynes@678
   197
        }
nkeynes@464
   198
    }
nkeynes@464
   199
nkeynes@379
   200
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   201
nkeynes@689
   202
    if( headless ) {
nkeynes@689
   203
        dreamcast_run();
nkeynes@689
   204
    } else {
nkeynes@681
   205
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   206
    }
nkeynes@671
   207
    dreamcast_shutdown();
nkeynes@68
   208
    return 0;
nkeynes@1
   209
}
nkeynes@1
   210
.