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