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 Tue Jan 15 20:50:23 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Merged lxdream-mmu r570:596 to trunk
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@68
    20
#include <unistd.h>
nkeynes@94
    21
#include <getopt.h>
nkeynes@27
    22
#include "dream.h"
nkeynes@450
    23
#include "config.h"
nkeynes@105
    24
#include "syscall.h"
nkeynes@422
    25
#include "mem.h"
nkeynes@27
    26
#include "dreamcast.h"
nkeynes@422
    27
#include "display.h"
nkeynes@422
    28
#include "loader.h"
nkeynes@450
    29
#include "gui.h"
nkeynes@106
    30
#include "aica/audio.h"
nkeynes@422
    31
#include "gdrom/gdrom.h"
nkeynes@144
    32
#include "maple/maple.h"
nkeynes@586
    33
#include "sh4/sh4.h"
nkeynes@11
    34
nkeynes@77
    35
#define S3M_PLAYER "s3mplay.bin"
nkeynes@77
    36
nkeynes@586
    37
char *option_list = "a:m:s:A:V:v:puhbd:c:t:T:xDn";
nkeynes@94
    38
struct option longopts[1] = { { NULL, 0, 0, 0 } };
nkeynes@68
    39
char *aica_program = NULL;
nkeynes@77
    40
char *s3m_file = NULL;
nkeynes@480
    41
const char *disc_file = NULL;
nkeynes@531
    42
char *display_driver_name = NULL;
nkeynes@531
    43
char *audio_driver_name = NULL;
nkeynes@586
    44
char *trace_regions = NULL;
nkeynes@68
    45
gboolean start_immediately = FALSE;
nkeynes@586
    46
gboolean no_start = FALSE;
nkeynes@77
    47
gboolean headless = FALSE;
nkeynes@87
    48
gboolean without_bios = FALSE;
nkeynes@402
    49
gboolean use_xlat = TRUE;
nkeynes@392
    50
gboolean show_debugger = FALSE;
nkeynes@372
    51
uint32_t time_secs = 0;
nkeynes@372
    52
uint32_t time_nanos = 0;
nkeynes@414
    53
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    54
nkeynes@30
    55
int main (int argc, char *argv[])
nkeynes@1
    56
{
nkeynes@106
    57
    int opt, i;
nkeynes@372
    58
    double t;
nkeynes@531
    59
    gboolean display_ok;
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@372
   114
	case 't': /* Time limit */
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@379
   118
	    break;
nkeynes@586
   119
	case 'T': /* trace regions */
nkeynes@586
   120
	    trace_regions = optarg;
nkeynes@586
   121
	    break;
nkeynes@586
   122
	case 'v': /* Log verbosity */
nkeynes@586
   123
	    if( !set_global_log_level(optarg) ) {
nkeynes@586
   124
		ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@586
   125
	    }
nkeynes@586
   126
	    break;
nkeynes@402
   127
	case 'x': /* Disable translator */
nkeynes@402
   128
	    use_xlat = FALSE;
nkeynes@379
   129
	    break;
nkeynes@68
   130
	}
nkeynes@68
   131
    }
nkeynes@30
   132
nkeynes@450
   133
    lxdream_load_config( );
nkeynes@144
   134
nkeynes@68
   135
    if( aica_program == NULL ) {
nkeynes@435
   136
	dreamcast_init();
nkeynes@68
   137
    } else {
nkeynes@68
   138
	dreamcast_configure_aica_only();
nkeynes@68
   139
	mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@77
   140
	if( s3m_file != NULL ) {
nkeynes@77
   141
	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
nkeynes@77
   142
	}
nkeynes@68
   143
    }
nkeynes@586
   144
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   145
nkeynes@87
   146
    if( without_bios ) {
nkeynes@87
   147
    	bios_install();
nkeynes@105
   148
	dcload_install();
nkeynes@87
   149
    }
nkeynes@106
   150
nkeynes@531
   151
    audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
nkeynes@531
   152
    if( audio_driver == NULL ) {
nkeynes@531
   153
	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
nkeynes@531
   154
	exit(2);
nkeynes@531
   155
    } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
nkeynes@531
   156
	ERROR( "Failed to initialize audio driver '%s', using null driver", 
nkeynes@531
   157
	       audio_driver->name );
nkeynes@106
   158
	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
nkeynes@106
   159
    }
nkeynes@106
   160
nkeynes@106
   161
    if( headless ) {
nkeynes@144
   162
	display_set_driver( &display_null_driver );
nkeynes@106
   163
    } else {
nkeynes@435
   164
	gui_init(show_debugger);
nkeynes@435
   165
nkeynes@531
   166
	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@531
   167
	if( display_driver == NULL ) {
nkeynes@531
   168
	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@531
   169
	    exit(2);
nkeynes@531
   170
	} else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@531
   171
	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@531
   172
		   display_driver->name );
nkeynes@531
   173
	    exit(2);
nkeynes@106
   174
	}
nkeynes@106
   175
    }
nkeynes@106
   176
nkeynes@144
   177
    maple_reattach_all();
nkeynes@180
   178
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   179
nkeynes@446
   180
    for( ; optind < argc; optind++ ) {
nkeynes@469
   181
	gboolean ok = gdrom_menu_open_file(argv[optind]);
nkeynes@446
   182
	if( !ok ) {
nkeynes@446
   183
	    ok = file_load_magic( argv[optind] );
nkeynes@446
   184
	}
nkeynes@446
   185
	if( !ok ) {
nkeynes@446
   186
	    ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@446
   187
	}
nkeynes@586
   188
	if( !no_start ) {
nkeynes@586
   189
	    start_immediately = ok;
nkeynes@586
   190
	}
nkeynes@94
   191
    }
nkeynes@94
   192
nkeynes@144
   193
    if( disc_file != NULL ) {
nkeynes@470
   194
	gdrom_menu_open_file( disc_file );
nkeynes@144
   195
    }
nkeynes@144
   196
nkeynes@464
   197
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@464
   198
	disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@464
   199
	if( disc_file != NULL ) {
nkeynes@469
   200
	    gdrom_menu_open_file( disc_file );
nkeynes@464
   201
	}
nkeynes@464
   202
    }
nkeynes@464
   203
nkeynes@379
   204
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   205
nkeynes@372
   206
    if( start_immediately ) {
nkeynes@543
   207
        if( dreamcast_can_run() ) {
nkeynes@543
   208
	    if( time_nanos != 0 || time_secs != 0 ) {
nkeynes@543
   209
	        dreamcast_run_for(time_secs, time_nanos);
nkeynes@543
   210
		return 0;
nkeynes@543
   211
	    } else {
nkeynes@543
   212
	        dreamcast_run();
nkeynes@543
   213
	    }
nkeynes@372
   214
	} else {
nkeynes@543
   215
	    ERROR( "Unable to start dreamcast: no program/bios loaded" );
nkeynes@372
   216
	}
nkeynes@372
   217
    }
nkeynes@77
   218
    if( !headless ) {
nkeynes@435
   219
	gui_main_loop();
nkeynes@77
   220
    }
nkeynes@68
   221
    return 0;
nkeynes@1
   222
}
nkeynes@1
   223
.