Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 998:1754a8c6a9cf
prev968:6fb1481859a4
next1015:ad448bedc48a
author nkeynes
date Tue Mar 24 11:15:57 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Add preliminary implementation of the GDB remote debugging server - attaches to
either or both the SH4 and ARM
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@772
    23
#include "lxdream.h"
nkeynes@755
    24
#include "gettext.h"
nkeynes@422
    25
#include "mem.h"
nkeynes@27
    26
#include "dreamcast.h"
nkeynes@759
    27
#include "dream.h"
nkeynes@422
    28
#include "display.h"
nkeynes@759
    29
#include "gui.h"
nkeynes@759
    30
#include "gdlist.h"
nkeynes@759
    31
#include "syscall.h"
nkeynes@422
    32
#include "loader.h"
nkeynes@106
    33
#include "aica/audio.h"
nkeynes@422
    34
#include "gdrom/gdrom.h"
nkeynes@144
    35
#include "maple/maple.h"
nkeynes@586
    36
#include "sh4/sh4.h"
nkeynes@998
    37
#include "aica/armdasm.h"
nkeynes@11
    38
nkeynes@998
    39
char *option_list = "a:A:c:dg:G:hHl:m:npt:T:uvV:x?";
nkeynes@700
    40
struct option longopts[] = {
nkeynes@700
    41
        { "aica", required_argument, NULL, 'a' },
nkeynes@700
    42
        { "audio", required_argument, NULL, 'A' },
nkeynes@700
    43
        { "config", required_argument, NULL, 'c' },
nkeynes@700
    44
        { "debugger", no_argument, NULL, 'D' },
nkeynes@998
    45
        { "gdb-sh4", required_argument, NULL, 'g' },  
nkeynes@998
    46
        { "gdb-arm", required_argument, NULL, 'G' },  
nkeynes@700
    47
        { "help", no_argument, NULL, 'h' },
nkeynes@700
    48
        { "headless", no_argument, NULL, 'H' },
nkeynes@700
    49
        { "log", required_argument, NULL,'l' }, 
nkeynes@700
    50
        { "multiplier", required_argument, NULL, 'm' },
nkeynes@700
    51
        { "run-time", required_argument, NULL, 't' },
nkeynes@700
    52
        { "trace", required_argument, NULL, 'T' },
nkeynes@700
    53
        { "unsafe", no_argument, NULL, 'u' },
nkeynes@700
    54
        { "video", no_argument, NULL, 'V' },
nkeynes@700
    55
        { "version", no_argument, NULL, 'v' }, 
nkeynes@700
    56
        { NULL, 0, 0, 0 } };
nkeynes@68
    57
char *aica_program = NULL;
nkeynes@531
    58
char *display_driver_name = NULL;
nkeynes@531
    59
char *audio_driver_name = NULL;
nkeynes@586
    60
char *trace_regions = NULL;
nkeynes@998
    61
char *sh4_gdb_port = NULL;
nkeynes@998
    62
char *arm_gdb_port = NULL;
nkeynes@68
    63
gboolean start_immediately = FALSE;
nkeynes@586
    64
gboolean no_start = FALSE;
nkeynes@77
    65
gboolean headless = FALSE;
nkeynes@402
    66
gboolean use_xlat = TRUE;
nkeynes@392
    67
gboolean show_debugger = FALSE;
nkeynes@414
    68
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    69
nkeynes@968
    70
static void print_version()
nkeynes@700
    71
{
nkeynes@738
    72
    printf( "lxdream %s\n", lxdream_full_version );
nkeynes@700
    73
}
nkeynes@700
    74
nkeynes@968
    75
static void print_usage()
nkeynes@700
    76
{
nkeynes@700
    77
    print_version();
nkeynes@738
    78
    printf( "Usage: lxdream %s [options] [disc-file] [program-file]\n\n", lxdream_full_version );
nkeynes@736
    79
nkeynes@700
    80
    printf( "Options:\n" );
nkeynes@700
    81
    printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
nkeynes@700
    82
    printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
nkeynes@700
    83
    printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
nkeynes@700
    84
    printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
nkeynes@998
    85
    printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
nkeynes@998
    86
    printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
nkeynes@700
    87
    printf( "   -h, --help             %s\n", _("Display this usage information") );
nkeynes@700
    88
    printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
nkeynes@700
    89
    printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
nkeynes@700
    90
    printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
nkeynes@700
    91
    printf( "   -n                     %s\n", _("Don't start running immediately") );
nkeynes@700
    92
    printf( "   -p                     %s\n", _("Start running immediately on startup") );
nkeynes@700
    93
    printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
nkeynes@700
    94
    printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
nkeynes@700
    95
    printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
nkeynes@700
    96
    printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
nkeynes@700
    97
    printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
nkeynes@700
    98
    printf( "   -x                     %s\n", _("Disable the SH4 translator") );
nkeynes@700
    99
}
nkeynes@700
   100
nkeynes@968
   101
static void bind_gettext_domain()
nkeynes@723
   102
{
nkeynes@723
   103
#ifdef ENABLE_NLS
nkeynes@866
   104
    bindtextdomain( PACKAGE, get_locale_path() );
nkeynes@723
   105
    textdomain(PACKAGE);
nkeynes@723
   106
#endif
nkeynes@723
   107
}
nkeynes@723
   108
nkeynes@30
   109
int main (int argc, char *argv[])
nkeynes@1
   110
{
nkeynes@669
   111
    int opt;
nkeynes@372
   112
    double t;
nkeynes@531
   113
    gboolean display_ok;
nkeynes@689
   114
    uint32_t time_secs, time_nanos;
nkeynes@495
   115
nkeynes@495
   116
    install_crash_handler();
nkeynes@723
   117
    bind_gettext_domain();
nkeynes@531
   118
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
   119
nkeynes@94
   120
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@736
   121
        switch( opt ) {
nkeynes@736
   122
        case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@736
   123
            aica_program = optarg;
nkeynes@736
   124
            break;
nkeynes@736
   125
        case 'A': /* Audio driver */
nkeynes@736
   126
            audio_driver_name = optarg;
nkeynes@736
   127
            if( strcmp(audio_driver_name, "?") == 0 ) {
nkeynes@736
   128
                print_version();
nkeynes@736
   129
                print_audio_drivers(stdout);
nkeynes@736
   130
                exit(0);
nkeynes@736
   131
            }
nkeynes@736
   132
            break;
nkeynes@736
   133
        case 'c': /* Config file */
nkeynes@736
   134
            lxdream_set_config_filename(optarg);
nkeynes@736
   135
            break;
nkeynes@736
   136
        case 'd': /* Launch w/ debugger */
nkeynes@736
   137
            show_debugger = TRUE;
nkeynes@736
   138
            break;
nkeynes@998
   139
        case 'g':
nkeynes@998
   140
            sh4_gdb_port = optarg;
nkeynes@998
   141
            break;
nkeynes@998
   142
        case 'G':
nkeynes@998
   143
            arm_gdb_port = optarg;
nkeynes@998
   144
            break;
nkeynes@736
   145
        case 'h': /* help */
nkeynes@736
   146
        case '?':
nkeynes@736
   147
            print_usage();
nkeynes@736
   148
            exit(0);
nkeynes@736
   149
            break;
nkeynes@736
   150
        case 'H': /* Headless - shorthand for -V null */
nkeynes@736
   151
            display_driver_name = "null";
nkeynes@736
   152
            break;
nkeynes@736
   153
        case 'l': /* Log verbosity */
nkeynes@736
   154
            if( !set_global_log_level(optarg) ) {
nkeynes@736
   155
                ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@736
   156
            }
nkeynes@736
   157
            break;
nkeynes@736
   158
        case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@736
   159
            t = strtod(optarg, NULL);
nkeynes@736
   160
            sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@736
   161
            break;
nkeynes@736
   162
        case 'n': /* Don't start immediately */
nkeynes@736
   163
            no_start = TRUE;
nkeynes@736
   164
            start_immediately = FALSE;
nkeynes@736
   165
            break;
nkeynes@736
   166
        case 'p': /* Start immediately */
nkeynes@736
   167
            start_immediately = TRUE;
nkeynes@736
   168
            no_start = FALSE;
nkeynes@736
   169
            break;
nkeynes@736
   170
        case 't': /* Time limit + auto quit */
nkeynes@736
   171
            t = strtod(optarg, NULL);
nkeynes@736
   172
            time_secs = (uint32_t)t;
nkeynes@736
   173
            time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@736
   174
            dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@736
   175
            dreamcast_set_exit_on_stop( TRUE );
nkeynes@736
   176
            break;
nkeynes@736
   177
        case 'T': /* trace regions */
nkeynes@736
   178
            trace_regions = optarg;
nkeynes@736
   179
            set_global_log_level("trace");
nkeynes@736
   180
            break;
nkeynes@736
   181
        case 'u': /* Allow unsafe dcload syscalls */
nkeynes@736
   182
            dcload_set_allow_unsafe(TRUE);
nkeynes@736
   183
            break;
nkeynes@736
   184
        case 'v': 
nkeynes@700
   185
            print_version();
nkeynes@700
   186
            exit(0);
nkeynes@736
   187
            break;
nkeynes@736
   188
        case 'V': /* Video driver */
nkeynes@736
   189
            display_driver_name = optarg;
nkeynes@736
   190
            if( strcmp(display_driver_name,"?") == 0 ) {
nkeynes@736
   191
                print_version();
nkeynes@736
   192
                print_display_drivers(stdout);
nkeynes@736
   193
                exit(0);
nkeynes@736
   194
            }
nkeynes@736
   195
            break;
nkeynes@736
   196
        case 'x': /* Disable translator */
nkeynes@736
   197
            use_xlat = FALSE;
nkeynes@736
   198
            break;
nkeynes@700
   199
        }
nkeynes@68
   200
    }
nkeynes@30
   201
nkeynes@450
   202
    lxdream_load_config( );
nkeynes@691
   203
    gdrom_list_init();
nkeynes@736
   204
nkeynes@68
   205
    if( aica_program == NULL ) {
nkeynes@700
   206
        dreamcast_init();
nkeynes@68
   207
    } else {
nkeynes@700
   208
        dreamcast_configure_aica_only();
nkeynes@700
   209
        mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@68
   210
    }
nkeynes@586
   211
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   212
nkeynes@759
   213
    audio_init_driver( audio_driver_name );
nkeynes@736
   214
nkeynes@700
   215
    headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
nkeynes@106
   216
    if( headless ) {
nkeynes@700
   217
        display_set_driver( &display_null_driver );
nkeynes@106
   218
    } else {
nkeynes@700
   219
        gui_init(show_debugger);
nkeynes@435
   220
nkeynes@700
   221
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@700
   222
        if( display_driver == NULL ) {
nkeynes@700
   223
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@700
   224
            exit(2);
nkeynes@700
   225
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@700
   226
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@700
   227
                    display_driver->name );
nkeynes@700
   228
            exit(2);
nkeynes@700
   229
        }
nkeynes@106
   230
    }
nkeynes@106
   231
nkeynes@144
   232
    maple_reattach_all();
nkeynes@180
   233
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   234
nkeynes@446
   235
    for( ; optind < argc; optind++ ) {
nkeynes@700
   236
        gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@700
   237
        if( !ok ) {
nkeynes@700
   238
            ok = file_load_magic( argv[optind] );
nkeynes@700
   239
        }
nkeynes@700
   240
        if( !ok ) {
nkeynes@700
   241
            ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@700
   242
        }
nkeynes@700
   243
        if( !no_start ) {
nkeynes@700
   244
            start_immediately = ok;
nkeynes@700
   245
        }
nkeynes@144
   246
    }
nkeynes@144
   247
nkeynes@464
   248
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@700
   249
        const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@678
   250
        if( disc_file != NULL ) {
nkeynes@678
   251
            gdrom_mount_image( disc_file );
nkeynes@678
   252
        }
nkeynes@464
   253
    }
nkeynes@464
   254
nkeynes@740
   255
    sh4_translate_set_enabled( use_xlat );
nkeynes@379
   256
nkeynes@998
   257
    /* If requested, start the gdb server immediately before we go into the main
nkeynes@998
   258
     * loop.
nkeynes@998
   259
     */
nkeynes@998
   260
    if( sh4_gdb_port != NULL ) {
nkeynes@998
   261
        gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
nkeynes@998
   262
    }
nkeynes@998
   263
    if( arm_gdb_port != NULL ) {
nkeynes@998
   264
        gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
nkeynes@998
   265
    }
nkeynes@998
   266
    
nkeynes@689
   267
    if( headless ) {
nkeynes@689
   268
        dreamcast_run();
nkeynes@689
   269
    } else {
nkeynes@681
   270
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   271
    }
nkeynes@671
   272
    dreamcast_shutdown();
nkeynes@68
   273
    return 0;
nkeynes@1
   274
}
nkeynes@1
   275
.