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 (15 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
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Main program, initializes dreamcast and gui, then passes control off to
     5  * the main loop. 
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdlib.h>
    21 #include <unistd.h>
    22 #include <getopt.h>
    23 #include "lxdream.h"
    24 #include "gettext.h"
    25 #include "mem.h"
    26 #include "dreamcast.h"
    27 #include "dream.h"
    28 #include "display.h"
    29 #include "gui.h"
    30 #include "gdlist.h"
    31 #include "syscall.h"
    32 #include "loader.h"
    33 #include "aica/audio.h"
    34 #include "gdrom/gdrom.h"
    35 #include "maple/maple.h"
    36 #include "sh4/sh4.h"
    37 #include "aica/armdasm.h"
    39 char *option_list = "a:A:c:dg:G:hHl:m:npt:T:uvV:x?";
    40 struct option longopts[] = {
    41         { "aica", required_argument, NULL, 'a' },
    42         { "audio", required_argument, NULL, 'A' },
    43         { "config", required_argument, NULL, 'c' },
    44         { "debugger", no_argument, NULL, 'D' },
    45         { "gdb-sh4", required_argument, NULL, 'g' },  
    46         { "gdb-arm", required_argument, NULL, 'G' },  
    47         { "help", no_argument, NULL, 'h' },
    48         { "headless", no_argument, NULL, 'H' },
    49         { "log", required_argument, NULL,'l' }, 
    50         { "multiplier", required_argument, NULL, 'm' },
    51         { "run-time", required_argument, NULL, 't' },
    52         { "trace", required_argument, NULL, 'T' },
    53         { "unsafe", no_argument, NULL, 'u' },
    54         { "video", no_argument, NULL, 'V' },
    55         { "version", no_argument, NULL, 'v' }, 
    56         { NULL, 0, 0, 0 } };
    57 char *aica_program = NULL;
    58 char *display_driver_name = NULL;
    59 char *audio_driver_name = NULL;
    60 char *trace_regions = NULL;
    61 char *sh4_gdb_port = NULL;
    62 char *arm_gdb_port = NULL;
    63 gboolean start_immediately = FALSE;
    64 gboolean no_start = FALSE;
    65 gboolean headless = FALSE;
    66 gboolean use_xlat = TRUE;
    67 gboolean show_debugger = FALSE;
    68 extern uint32_t sh4_cpu_multiplier;
    70 static void print_version()
    71 {
    72     printf( "lxdream %s\n", lxdream_full_version );
    73 }
    75 static void print_usage()
    76 {
    77     print_version();
    78     printf( "Usage: lxdream %s [options] [disc-file] [program-file]\n\n", lxdream_full_version );
    80     printf( "Options:\n" );
    81     printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
    82     printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
    83     printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
    84     printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
    85     printf( "   -g, --gdb-sh4=PORT     %s\n", _("Start GDB remote server on PORT for SH4") );
    86     printf( "   -G, --gdb-arm=PORT     %s\n", _("Start GDB remote server on PORT for ARM") );
    87     printf( "   -h, --help             %s\n", _("Display this usage information") );
    88     printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
    89     printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
    90     printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
    91     printf( "   -n                     %s\n", _("Don't start running immediately") );
    92     printf( "   -p                     %s\n", _("Start running immediately on startup") );
    93     printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
    94     printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
    95     printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
    96     printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
    97     printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
    98     printf( "   -x                     %s\n", _("Disable the SH4 translator") );
    99 }
   101 static void bind_gettext_domain()
   102 {
   103 #ifdef ENABLE_NLS
   104     bindtextdomain( PACKAGE, get_locale_path() );
   105     textdomain(PACKAGE);
   106 #endif
   107 }
   109 int main (int argc, char *argv[])
   110 {
   111     int opt;
   112     double t;
   113     gboolean display_ok;
   114     uint32_t time_secs, time_nanos;
   116     install_crash_handler();
   117     bind_gettext_domain();
   118     display_ok = gui_parse_cmdline(&argc, &argv);
   120     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   121         switch( opt ) {
   122         case 'a': /* AICA only mode - argument is an AICA program */
   123             aica_program = optarg;
   124             break;
   125         case 'A': /* Audio driver */
   126             audio_driver_name = optarg;
   127             if( strcmp(audio_driver_name, "?") == 0 ) {
   128                 print_version();
   129                 print_audio_drivers(stdout);
   130                 exit(0);
   131             }
   132             break;
   133         case 'c': /* Config file */
   134             lxdream_set_config_filename(optarg);
   135             break;
   136         case 'd': /* Launch w/ debugger */
   137             show_debugger = TRUE;
   138             break;
   139         case 'g':
   140             sh4_gdb_port = optarg;
   141             break;
   142         case 'G':
   143             arm_gdb_port = optarg;
   144             break;
   145         case 'h': /* help */
   146         case '?':
   147             print_usage();
   148             exit(0);
   149             break;
   150         case 'H': /* Headless - shorthand for -V null */
   151             display_driver_name = "null";
   152             break;
   153         case 'l': /* Log verbosity */
   154             if( !set_global_log_level(optarg) ) {
   155                 ERROR( "Unrecognized log level '%s'", optarg );
   156             }
   157             break;
   158         case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
   159             t = strtod(optarg, NULL);
   160             sh4_cpu_multiplier = (int)(1000.0/t);
   161             break;
   162         case 'n': /* Don't start immediately */
   163             no_start = TRUE;
   164             start_immediately = FALSE;
   165             break;
   166         case 'p': /* Start immediately */
   167             start_immediately = TRUE;
   168             no_start = FALSE;
   169             break;
   170         case 't': /* Time limit + auto quit */
   171             t = strtod(optarg, NULL);
   172             time_secs = (uint32_t)t;
   173             time_nanos = (int)((t - time_secs) * 1000000000);
   174             dreamcast_set_run_time( time_secs, time_nanos );
   175             dreamcast_set_exit_on_stop( TRUE );
   176             break;
   177         case 'T': /* trace regions */
   178             trace_regions = optarg;
   179             set_global_log_level("trace");
   180             break;
   181         case 'u': /* Allow unsafe dcload syscalls */
   182             dcload_set_allow_unsafe(TRUE);
   183             break;
   184         case 'v': 
   185             print_version();
   186             exit(0);
   187             break;
   188         case 'V': /* Video driver */
   189             display_driver_name = optarg;
   190             if( strcmp(display_driver_name,"?") == 0 ) {
   191                 print_version();
   192                 print_display_drivers(stdout);
   193                 exit(0);
   194             }
   195             break;
   196         case 'x': /* Disable translator */
   197             use_xlat = FALSE;
   198             break;
   199         }
   200     }
   202     lxdream_load_config( );
   203     gdrom_list_init();
   205     if( aica_program == NULL ) {
   206         dreamcast_init();
   207     } else {
   208         dreamcast_configure_aica_only();
   209         mem_load_block( aica_program, 0x00800000, 2048*1024 );
   210     }
   211     mem_set_trace( trace_regions, TRUE );
   213     audio_init_driver( audio_driver_name );
   215     headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
   216     if( headless ) {
   217         display_set_driver( &display_null_driver );
   218     } else {
   219         gui_init(show_debugger);
   221         display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
   222         if( display_driver == NULL ) {
   223             ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
   224             exit(2);
   225         } else if( display_set_driver( display_driver ) == FALSE ) {
   226             ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
   227                     display_driver->name );
   228             exit(2);
   229         }
   230     }
   232     maple_reattach_all();
   233     INFO( "%s! ready...", APP_NAME );
   235     for( ; optind < argc; optind++ ) {
   236         gboolean ok = gdrom_mount_image(argv[optind]);
   237         if( !ok ) {
   238             ok = file_load_magic( argv[optind] );
   239         }
   240         if( !ok ) {
   241             ERROR( "Unrecognized file '%s'", argv[optind] );
   242         }
   243         if( !no_start ) {
   244             start_immediately = ok;
   245         }
   246     }
   248     if( gdrom_get_current_disc() == NULL ) {
   249         const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
   250         if( disc_file != NULL ) {
   251             gdrom_mount_image( disc_file );
   252         }
   253     }
   255     sh4_translate_set_enabled( use_xlat );
   257     /* If requested, start the gdb server immediately before we go into the main
   258      * loop.
   259      */
   260     if( sh4_gdb_port != NULL ) {
   261         gdb_init_server( NULL, strtol(sh4_gdb_port,NULL,0), &sh4_cpu_desc, TRUE );
   262     }
   263     if( arm_gdb_port != NULL ) {
   264         gdb_init_server( NULL, strtol(arm_gdb_port,NULL,0), &arm_cpu_desc, TRUE );
   265     }
   267     if( headless ) {
   268         dreamcast_run();
   269     } else {
   270         gui_main_loop( start_immediately && dreamcast_can_run() );
   271     }
   272     dreamcast_shutdown();
   273     return 0;
   274 }
.