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