Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 562:e598411b640b
prev561:533f6b478071
next564:dc7b5ffb0535
author nkeynes
date Tue Jan 01 05:48:06 2008 +0000 (13 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Add configure and command-line parameters to perform IO tracing
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@561
     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@422
    33
#include "sh4/sh4core.h"
nkeynes@11
    34
nkeynes@77
    35
#define S3M_PLAYER "s3mplay.bin"
nkeynes@77
    36
nkeynes@562
    37
char *option_list = "a:m:s:A:V:v:puhbd:c:t:T:xD";
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@562
    44
char *trace_regions = NULL;
nkeynes@68
    45
gboolean start_immediately = FALSE;
nkeynes@77
    46
gboolean headless = FALSE;
nkeynes@87
    47
gboolean without_bios = FALSE;
nkeynes@402
    48
gboolean use_xlat = TRUE;
nkeynes@392
    49
gboolean show_debugger = FALSE;
nkeynes@372
    50
uint32_t time_secs = 0;
nkeynes@372
    51
uint32_t time_nanos = 0;
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@106
    56
    int opt, i;
nkeynes@372
    57
    double t;
nkeynes@531
    58
    gboolean display_ok;
nkeynes@495
    59
nkeynes@495
    60
    install_crash_handler();
nkeynes@543
    61
    gdrom_get_native_devices();
nkeynes@1
    62
#ifdef ENABLE_NLS
nkeynes@68
    63
    bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
nkeynes@68
    64
    textdomain (PACKAGE);
nkeynes@1
    65
#endif
nkeynes@531
    66
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
    67
nkeynes@94
    68
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@68
    69
	switch( opt ) {
nkeynes@68
    70
	case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@68
    71
	    aica_program = optarg;
nkeynes@68
    72
	    break;
nkeynes@144
    73
	case 'c': /* Config file */
nkeynes@450
    74
	    lxdream_set_config_filename(optarg);
nkeynes@144
    75
	    break;
nkeynes@144
    76
	case 'd': /* Mount disc */
nkeynes@144
    77
	    disc_file = optarg;
nkeynes@144
    78
	    break;
nkeynes@392
    79
	case 'D': /* Launch w/ debugger */
nkeynes@392
    80
	    show_debugger = TRUE;
nkeynes@392
    81
	    break;
nkeynes@414
    82
	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@414
    83
	    t = strtod(optarg, NULL);
nkeynes@414
    84
	    sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@414
    85
	    break;
nkeynes@77
    86
	case 's': /* AICA-only w/ S3M player */
nkeynes@77
    87
	    aica_program = S3M_PLAYER;
nkeynes@77
    88
	    s3m_file = optarg;
nkeynes@77
    89
	    break;
nkeynes@68
    90
	case 'A': /* Audio driver */
nkeynes@106
    91
	    audio_driver_name = optarg;
nkeynes@68
    92
	    break;
nkeynes@68
    93
	case 'V': /* Video driver */
nkeynes@144
    94
	    display_driver_name = optarg;
nkeynes@68
    95
	    break;
nkeynes@68
    96
	case 'p': /* Start immediately */
nkeynes@68
    97
	    start_immediately = TRUE;
nkeynes@77
    98
    	    break;
nkeynes@209
    99
	case 'u': /* Allow unsafe dcload syscalls */
nkeynes@209
   100
	    dcload_set_allow_unsafe(TRUE);
nkeynes@182
   101
	    break;
nkeynes@87
   102
    	case 'b': /* No BIOS */
nkeynes@87
   103
    	    without_bios = TRUE;
nkeynes@87
   104
    	    break;
nkeynes@77
   105
        case 'h': /* Headless */
nkeynes@77
   106
            headless = TRUE;
nkeynes@77
   107
            break;
nkeynes@372
   108
	case 't': /* Time limit */
nkeynes@372
   109
	    t = strtod(optarg, NULL);
nkeynes@372
   110
	    time_secs = (uint32_t)t;
nkeynes@372
   111
	    time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@379
   112
	    break;
nkeynes@562
   113
	case 'T': /* trace regions */
nkeynes@562
   114
	    trace_regions = optarg;
nkeynes@562
   115
	    break;
nkeynes@556
   116
	case 'v': /* Log verbosity */
nkeynes@556
   117
	    if( !set_global_log_level(optarg) ) {
nkeynes@556
   118
		ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@556
   119
	    }
nkeynes@556
   120
	    break;
nkeynes@402
   121
	case 'x': /* Disable translator */
nkeynes@402
   122
	    use_xlat = FALSE;
nkeynes@379
   123
	    break;
nkeynes@68
   124
	}
nkeynes@68
   125
    }
nkeynes@30
   126
nkeynes@450
   127
    lxdream_load_config( );
nkeynes@144
   128
nkeynes@68
   129
    if( aica_program == NULL ) {
nkeynes@435
   130
	dreamcast_init();
nkeynes@68
   131
    } else {
nkeynes@68
   132
	dreamcast_configure_aica_only();
nkeynes@68
   133
	mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@77
   134
	if( s3m_file != NULL ) {
nkeynes@77
   135
	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
nkeynes@77
   136
	}
nkeynes@68
   137
    }
nkeynes@562
   138
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   139
nkeynes@87
   140
    if( without_bios ) {
nkeynes@87
   141
    	bios_install();
nkeynes@105
   142
	dcload_install();
nkeynes@87
   143
    }
nkeynes@106
   144
nkeynes@531
   145
    audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
nkeynes@531
   146
    if( audio_driver == NULL ) {
nkeynes@531
   147
	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
nkeynes@531
   148
	exit(2);
nkeynes@531
   149
    } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
nkeynes@531
   150
	ERROR( "Failed to initialize audio driver '%s', using null driver", 
nkeynes@531
   151
	       audio_driver->name );
nkeynes@106
   152
	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
nkeynes@106
   153
    }
nkeynes@106
   154
nkeynes@106
   155
    if( headless ) {
nkeynes@144
   156
	display_set_driver( &display_null_driver );
nkeynes@106
   157
    } else {
nkeynes@435
   158
	gui_init(show_debugger);
nkeynes@435
   159
nkeynes@531
   160
	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@531
   161
	if( display_driver == NULL ) {
nkeynes@531
   162
	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@531
   163
	    exit(2);
nkeynes@531
   164
	} else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@531
   165
	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@531
   166
		   display_driver->name );
nkeynes@531
   167
	    exit(2);
nkeynes@106
   168
	}
nkeynes@106
   169
    }
nkeynes@106
   170
nkeynes@144
   171
    maple_reattach_all();
nkeynes@180
   172
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   173
nkeynes@446
   174
    for( ; optind < argc; optind++ ) {
nkeynes@469
   175
	gboolean ok = gdrom_menu_open_file(argv[optind]);
nkeynes@446
   176
	if( !ok ) {
nkeynes@446
   177
	    ok = file_load_magic( argv[optind] );
nkeynes@446
   178
	}
nkeynes@446
   179
	if( !ok ) {
nkeynes@446
   180
	    ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@446
   181
	}
nkeynes@446
   182
	start_immediately = ok;
nkeynes@94
   183
    }
nkeynes@94
   184
nkeynes@144
   185
    if( disc_file != NULL ) {
nkeynes@470
   186
	gdrom_menu_open_file( disc_file );
nkeynes@144
   187
    }
nkeynes@144
   188
nkeynes@464
   189
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@464
   190
	disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@464
   191
	if( disc_file != NULL ) {
nkeynes@469
   192
	    gdrom_menu_open_file( disc_file );
nkeynes@464
   193
	}
nkeynes@464
   194
    }
nkeynes@464
   195
nkeynes@379
   196
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   197
nkeynes@372
   198
    if( start_immediately ) {
nkeynes@543
   199
        if( dreamcast_can_run() ) {
nkeynes@543
   200
	    if( time_nanos != 0 || time_secs != 0 ) {
nkeynes@543
   201
	        dreamcast_run_for(time_secs, time_nanos);
nkeynes@543
   202
		return 0;
nkeynes@543
   203
	    } else {
nkeynes@543
   204
	        dreamcast_run();
nkeynes@543
   205
	    }
nkeynes@372
   206
	} else {
nkeynes@543
   207
	    ERROR( "Unable to start dreamcast: no program/bios loaded" );
nkeynes@372
   208
	}
nkeynes@372
   209
    }
nkeynes@77
   210
    if( !headless ) {
nkeynes@435
   211
	gui_main_loop();
nkeynes@77
   212
    }
nkeynes@68
   213
    return 0;
nkeynes@1
   214
}
nkeynes@1
   215
.