Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 689:9868667e3525
prev681:1755a126b109
next691:ad3356543392
author nkeynes
date Mon Jun 16 10:58:41 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Unbreak and finish cleaning up the 'run for limited time' feature
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@586
   123
	    break;
nkeynes@586
   124
	case 'v': /* Log verbosity */
nkeynes@586
   125
	    if( !set_global_log_level(optarg) ) {
nkeynes@586
   126
		ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@586
   127
	    }
nkeynes@586
   128
	    break;
nkeynes@402
   129
	case 'x': /* Disable translator */
nkeynes@402
   130
	    use_xlat = FALSE;
nkeynes@379
   131
	    break;
nkeynes@68
   132
	}
nkeynes@68
   133
    }
nkeynes@30
   134
nkeynes@450
   135
    lxdream_load_config( );
nkeynes@144
   136
nkeynes@68
   137
    if( aica_program == NULL ) {
nkeynes@435
   138
	dreamcast_init();
nkeynes@68
   139
    } else {
nkeynes@68
   140
	dreamcast_configure_aica_only();
nkeynes@68
   141
	mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@77
   142
	if( s3m_file != NULL ) {
nkeynes@77
   143
	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
nkeynes@77
   144
	}
nkeynes@68
   145
    }
nkeynes@586
   146
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   147
nkeynes@87
   148
    if( without_bios ) {
nkeynes@87
   149
    	bios_install();
nkeynes@105
   150
	dcload_install();
nkeynes@87
   151
    }
nkeynes@106
   152
nkeynes@531
   153
    audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
nkeynes@531
   154
    if( audio_driver == NULL ) {
nkeynes@531
   155
	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
nkeynes@531
   156
	exit(2);
nkeynes@531
   157
    } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
nkeynes@531
   158
	ERROR( "Failed to initialize audio driver '%s', using null driver", 
nkeynes@531
   159
	       audio_driver->name );
nkeynes@106
   160
	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
nkeynes@106
   161
    }
nkeynes@106
   162
nkeynes@106
   163
    if( headless ) {
nkeynes@144
   164
	display_set_driver( &display_null_driver );
nkeynes@106
   165
    } else {
nkeynes@435
   166
	gui_init(show_debugger);
nkeynes@435
   167
nkeynes@531
   168
	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@531
   169
	if( display_driver == NULL ) {
nkeynes@531
   170
	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@531
   171
	    exit(2);
nkeynes@531
   172
	} else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@531
   173
	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@531
   174
		   display_driver->name );
nkeynes@531
   175
	    exit(2);
nkeynes@106
   176
	}
nkeynes@106
   177
    }
nkeynes@106
   178
nkeynes@144
   179
    maple_reattach_all();
nkeynes@180
   180
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   181
nkeynes@446
   182
    for( ; optind < argc; optind++ ) {
nkeynes@678
   183
	gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@446
   184
	if( !ok ) {
nkeynes@446
   185
	    ok = file_load_magic( argv[optind] );
nkeynes@446
   186
	}
nkeynes@446
   187
	if( !ok ) {
nkeynes@446
   188
	    ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@446
   189
	}
nkeynes@586
   190
	if( !no_start ) {
nkeynes@586
   191
	    start_immediately = ok;
nkeynes@586
   192
	}
nkeynes@94
   193
    }
nkeynes@94
   194
nkeynes@144
   195
    if( disc_file != NULL ) {
nkeynes@678
   196
        gdrom_mount_image( disc_file );
nkeynes@144
   197
    }
nkeynes@144
   198
nkeynes@464
   199
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@678
   200
        disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@678
   201
        if( disc_file != NULL ) {
nkeynes@678
   202
            gdrom_mount_image( disc_file );
nkeynes@678
   203
        }
nkeynes@464
   204
    }
nkeynes@464
   205
nkeynes@379
   206
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   207
nkeynes@689
   208
    if( headless ) {
nkeynes@689
   209
        dreamcast_run();
nkeynes@689
   210
    } else {
nkeynes@681
   211
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   212
    }
nkeynes@671
   213
    dreamcast_shutdown();
nkeynes@68
   214
    return 0;
nkeynes@1
   215
}
nkeynes@1
   216
.