Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 372:86aa060ddcec
prev295:6637664291a8
next422:61a0598e07ff
author nkeynes
date Sat Sep 08 04:38:38 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Add time-limited run option (for time trials)
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.22 2007-09-08 04:38:38 nkeynes Exp $
     3  * Central switchboard for the system. This pulls all the individual modules
     4  * together into some kind of coherent structure. This is also where you'd
     5  * add Naomi support, if I ever get a board to play with...
     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 <errno.h>
    21 #include <glib/gstrfuncs.h>
    22 #include "dream.h"
    23 #include "mem.h"
    24 #include "aica/aica.h"
    25 #include "asic.h"
    26 #include "dreamcast.h"
    27 #include "gdrom/ide.h"
    28 #include "maple/maple.h"
    30 /**
    31  * Current state of the DC virtual machine
    32  */
    33 #define STATE_UNINIT 0
    34 #define STATE_RUNNING 1
    35 #define STATE_STOPPING 2
    36 #define STATE_STOPPED 3 
    37 static volatile int dreamcast_state = STATE_UNINIT;
    38 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
    39 static char *dreamcast_config = "DEFAULT";
    41 #define MAX_MODULES 32
    42 static int num_modules = 0;
    43 dreamcast_module_t modules[MAX_MODULES];
    45 /**
    46  * The unknown module is used for logging files without an actual module
    47  * declaration
    48  */
    49 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
    50 					   NULL, NULL, NULL };
    52 /**
    53  * This function is responsible for defining how all the pieces of the
    54  * dreamcast actually fit together. 
    55  *
    56  * Note currently the locations of the various MMIO pages are hard coded in
    57  * the MMIO definitions - they should probably be moved here.
    58  */
    59 void dreamcast_configure( )
    60 {
    61     dreamcast_register_module( &eventq_module );
    62     /* Register the memory framework */
    63     dreamcast_register_module( &mem_module );
    65     /* Setup standard memory map */
    66     mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
    67     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    68     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    69     mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
    70     if( mem_load_rom( dreamcast_get_config_value(CONFIG_BIOS_PATH),
    71 		      0x00000000, 0x00200000, 0x89f2b1a1 ) == NULL ) {
    72 	/* Bios wasn't found. Dump an empty ram region in there for something to do */
    73 	mem_create_ram_region( 0x00000000, 0x00200000, MEM_REGION_BIOS );
    74     }
    75     mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
    76     mem_load_block( dreamcast_get_config_value(CONFIG_FLASH_PATH),
    77 		    0x00200000, 0x00020000 );
    79     /* Load in the rest of the core modules */
    80     dreamcast_register_module( &sh4_module );
    81     dreamcast_register_module( &asic_module );
    82     dreamcast_register_module( &pvr2_module );
    83     dreamcast_register_module( &aica_module );
    84     dreamcast_register_module( &maple_module );
    85     dreamcast_register_module( &ide_module );
    86 }
    88 void dreamcast_save_flash()
    89 {
    90     char *file = dreamcast_get_config_value(CONFIG_FLASH_PATH);
    91     mem_save_block( file, 0x00200000, 0x00020000 );
    92 }
    94 /**
    95  * Constructs a system configuration for the AICA in standalone mode,
    96  * ie sound chip only.
    97  */
    98 void dreamcast_configure_aica_only( )
    99 {
   100     dreamcast_register_module( &mem_module );
   101     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
   102     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
   103     dreamcast_register_module( &aica_module );
   104     aica_enable();
   105     dreamcast_state = STATE_STOPPED;
   106 }
   108 void dreamcast_register_module( dreamcast_module_t module ) 
   109 {
   110     modules[num_modules++] = module;
   111     if( module->init != NULL )
   112 	module->init();
   113 }
   116 void dreamcast_init( void )
   117 {
   118     dreamcast_configure();
   119     dreamcast_state = STATE_STOPPED;
   120 }
   122 void dreamcast_reset( void )
   123 {
   124     int i;
   125     for( i=0; i<num_modules; i++ ) {
   126 	if( modules[i]->reset != NULL )
   127 	    modules[i]->reset();
   128     }
   129 }
   131 void dreamcast_run( void )
   132 {
   133     int i;
   134     if( dreamcast_state != STATE_RUNNING ) {
   135 	for( i=0; i<num_modules; i++ ) {
   136 	    if( modules[i]->start != NULL )
   137 		modules[i]->start();
   138 	}
   139     }
   140     dreamcast_state = STATE_RUNNING;
   141     while( dreamcast_state == STATE_RUNNING ) {
   142 	int time_to_run = timeslice_length;
   143 	for( i=0; i<num_modules; i++ ) {
   144 	    if( modules[i]->run_time_slice != NULL )
   145 		time_to_run = modules[i]->run_time_slice( time_to_run );
   146 	}
   148     }
   150     for( i=0; i<num_modules; i++ ) {
   151 	if( modules[i]->stop != NULL )
   152 	    modules[i]->stop();
   153     }
   154     dreamcast_state = STATE_STOPPED;
   155 }
   157 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
   158 {
   160     int i;
   161     if( dreamcast_state != STATE_RUNNING ) {
   162 	for( i=0; i<num_modules; i++ ) {
   163 	    if( modules[i]->start != NULL )
   164 		modules[i]->start();
   165 	}
   166     }
   167     dreamcast_state = STATE_RUNNING;
   168     uint32_t nanos = 0;
   169     if( nanosecs != 0 ) {
   170         nanos = 1000000000 - nanosecs;
   171 	seconds++;
   172     }
   173     while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
   174 	int time_to_run = timeslice_length;
   175 	for( i=0; i<num_modules; i++ ) {
   176 	    if( modules[i]->run_time_slice != NULL )
   177 		time_to_run = modules[i]->run_time_slice( time_to_run );
   178 	}
   179 	nanos += time_to_run;
   180 	if( nanos >= 1000000000 ) {
   181 	    nanos -= 1000000000;
   182 	    seconds--;
   183 	}
   184     }
   186     for( i=0; i<num_modules; i++ ) {
   187 	if( modules[i]->stop != NULL )
   188 	    modules[i]->stop();
   189     }
   190     dreamcast_state = STATE_STOPPED;
   191 }
   193 void dreamcast_stop( void )
   194 {
   195     if( dreamcast_state == STATE_RUNNING )
   196 	dreamcast_state = STATE_STOPPING;
   197 }
   199 void dreamcast_shutdown()
   200 {
   201     dreamcast_stop();
   202     sh4_stop();
   203     dreamcast_save_flash();
   204 }
   206 gboolean dreamcast_is_running( void )
   207 {
   208     return dreamcast_state == STATE_RUNNING;
   209 }
   211 /***************************** User Configuration **************************/
   214 static struct dreamcast_config_entry global_config[] =
   215     {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
   216      { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
   217      { "default path", CONFIG_TYPE_PATH, "." },
   218      { "save path", CONFIG_TYPE_PATH, "save" },
   219      { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
   220      { NULL, CONFIG_TYPE_NONE }};
   222 static struct dreamcast_config_entry serial_config[] =
   223     {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
   224      { NULL, CONFIG_TYPE_NONE }};
   226 struct dreamcast_config_group dreamcast_config_root[] = 
   227     {{ "global", global_config },
   228      { "controllers", NULL },
   229      { "serial", serial_config },
   230      { NULL, CONFIG_TYPE_NONE }};
   232 void dreamcast_set_default_config( )
   233 {
   234     struct dreamcast_config_group *group = dreamcast_config_root;
   235     while( group->key != NULL ) {
   236 	struct dreamcast_config_entry *param = group->params;
   237 	if( param != NULL ) {
   238 	    while( param->key != NULL ) {
   239 		if( param->value != param->default_value ) {
   240 		    if( param->value != NULL )
   241 			free( param->value );
   242 		    param->value = (gchar *)param->default_value;
   243 		}
   244 		param++;
   245 	    }
   246 	}
   247 	group++;
   248     }
   249     maple_detach_all();
   250 }
   252 const gchar *dreamcast_get_config_value( int key )
   253 {
   254     return global_config[key].value;
   255 }
   257 gboolean dreamcast_load_config( const gchar *filename )
   258 {
   259     FILE *f = fopen(filename, "ro");
   260     gboolean result;
   262     if( f == NULL ) {
   263 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   264 	return FALSE;
   265     }
   267     result = dreamcast_load_config_stream( f );
   268     fclose(f);
   269     return result;
   270 }
   272 gboolean dreamcast_load_config_stream( FILE *f )
   273 {
   275     char buf[512], *p;
   276     int maple_device = -1, maple_subdevice = -1;
   277     struct dreamcast_config_group devgroup;
   278     struct dreamcast_config_group *group = NULL;
   279     maple_device_t device = NULL;
   280     dreamcast_set_default_config();
   282     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   283 	g_strstrip(buf);
   284 	if( buf[0] == '#' )
   285 	    continue;
   286 	if( *buf == '[' ) {
   287 	    char *p = strchr(buf, ']');
   288 	    if( p != NULL ) {
   289 		struct dreamcast_config_group *tmp_group;
   290 		maple_device = maple_subdevice = -1;
   291 		*p = '\0';
   292 		g_strstrip(buf+1);
   293 		tmp_group = &dreamcast_config_root[0];
   294 		while( tmp_group->key != NULL ) {
   295 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   296 			group = tmp_group;
   297 			break;
   298 		    }
   299 		    tmp_group++;
   300 		}
   301 	    }
   302 	} else if( group != NULL ) {
   303 	    char *value = strchr( buf, '=' );
   304 	    if( value != NULL ) {
   305 		struct dreamcast_config_entry *param = group->params;
   306 		*value = '\0';
   307 		value++;
   308 		g_strstrip(buf);
   309 		g_strstrip(value);
   310 		if( strcmp(group->key,"controllers") == 0  ) {
   311 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   312 			maple_device = strtoul( buf+7, NULL, 0 );
   313 			if( maple_device < 0 || maple_device > 3 ) {
   314 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   315 			    continue;
   316 			}
   317 			maple_subdevice = 0;
   318 			device = maple_new_device( value );
   319 			if( device == NULL ) {
   320 			    ERROR( "Unrecognized device '%s'", value );
   321 			} else {
   322 			    devgroup.key = "controllers";
   323 			    devgroup.params = maple_get_device_config(device);
   324 			    maple_attach_device( device, maple_device, maple_subdevice );
   325 			    group = &devgroup;
   326 			}
   327 			continue;
   328 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   329 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   330 			if( maple_device == -1 ) {
   331 			    ERROR( "Subdevice not allowed without primary device" );
   332 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   333 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   334 			} else if( (device = maple_new_device(value)) == NULL ) {
   335 			    ERROR( "Unrecognized subdevice '%s'", value );
   336 			} else {
   337 			    devgroup.key = "controllers";
   338 			    devgroup.params = maple_get_device_config(device);
   339 			    maple_attach_device( device, maple_device, maple_subdevice );
   340 			    group = &devgroup;
   341 			}
   342 			continue;
   343 		    }
   344 		}
   345 		while( param->key != NULL ) {
   346 		    if( strcasecmp( param->key, buf ) == 0 ) {
   347 			param->value = g_strdup(value);
   348 			break;
   349 		    }
   350 		    param++;
   351 		}
   352 	    }
   353 	}
   354     }
   355     return TRUE;
   356 }
   358 gboolean dreamcast_save_config( const gchar *filename )
   359 {
   360     FILE *f = fopen(filename, "wo");
   361     gboolean result;
   362     if( f == NULL ) {
   363 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   364 	return FALSE;
   365     }
   366     result = dreamcast_save_config_stream(f);
   367     fclose(f);
   368 }    
   370 gboolean dreamcast_save_config_stream( FILE *f )
   371 {
   372     struct dreamcast_config_group *group = &dreamcast_config_root[0];
   374     while( group->key != NULL ) {
   375 	struct dreamcast_config_entry *entry = group->params;
   376 	fprintf( f, "[%s]\n", group->key );
   378 	if( entry != NULL ) {
   379 	    while( entry->key != NULL ) {
   380 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   381 		entry++;
   382 	    }
   383 	} else if( strcmp(group->key, "controllers") == 0 ) {
   384 	    int i,j;
   385 	    for( i=0; i<4; i++ ) {
   386 		for( j=0; j<6; j++ ) {
   387 		    maple_device_t dev = maple_get_device( i, j );
   388 		    if( dev != NULL ) {
   389 			if( j == 0 )
   390 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   391 			else 
   392 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   393 			entry = dev->get_config(dev);
   394 			while( entry->key != NULL ) {
   395 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   396 			    entry++;
   397 			}
   398 		    }
   399 		}
   400 	    }
   401 	}
   402 	fprintf( f, "\n" );
   403 	group++;
   404     }
   405     return TRUE;
   406 }
   408 /********************************* Save States *****************************/
   410 struct save_state_header {
   411     char magic[16];
   412     uint32_t version;
   413     uint32_t module_count;
   414 };
   416 int dreamcast_load_state( const gchar *filename )
   417 {
   418     int i,j;
   419     uint32_t count, len;
   420     int have_read[MAX_MODULES];
   421     char tmp[64];
   422     struct save_state_header header;
   423     FILE *f;
   425     f = fopen( filename, "r" );
   426     if( f == NULL ) return errno;
   428     fread( &header, sizeof(header), 1, f );
   429     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   430 	ERROR( "Not a %s save state file", APP_NAME );
   431 	return 1;
   432     }
   433     if( header.version != DREAMCAST_SAVE_VERSION ) {
   434 	ERROR( "%s save state version not supported", APP_NAME );
   435 	return 1;
   436     }
   437     if( header.module_count > MAX_MODULES ) {
   438 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   439 	return 1;
   440     }
   441     for( i=0; i<MAX_MODULES; i++ ) {
   442 	have_read[i] = 0;
   443     }
   445     for( i=0; i<header.module_count; i++ ) {
   446 	fread(tmp, 4, 1, f );
   447 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   448 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   449 	    return 2;
   450 	}
   451 	len = fread_string(tmp, sizeof(tmp), f );
   452 	if( len > 64 || len < 1 ) {
   453 	    ERROR( "%s save state is corrupted (bad string)", APP_NAME );
   454 	    return 2;
   455 	}
   457 	/* Find the matching module by name */
   458 	for( j=0; j<num_modules; j++ ) {
   459 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   460 		have_read[j] = 1;
   461 		if( modules[j]->load == NULL ) {
   462 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   463 		    return 2;
   464 		} else if( modules[j]->load(f) != 0 ) {
   465 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   466 		    return 2;
   467 		}
   468 		break;
   469 	    }
   470 	}
   471 	if( j == num_modules ) {
   472 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   473 	    return 2;
   474 	}
   475     }
   477     /* Any modules that we didn't load - reset to the default state.
   478      * (ie it's not an error to skip a module if you don't actually
   479      * care about its state).
   480      */
   481     for( j=0; j<num_modules; j++ ) {
   482 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   483 	    modules[j]->reset();
   484 	}
   485     }
   486     fclose(f);
   487     INFO( "Save state read from %s", filename );
   488 }
   490 int dreamcast_save_state( const gchar *filename )
   491 {
   492     int i;
   493     FILE *f;
   494     struct save_state_header header;
   496     f = fopen( filename, "w" );
   497     if( f == NULL )
   498 	return errno;
   499     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   500     header.version = DREAMCAST_SAVE_VERSION;
   501     header.module_count = 0;
   503     for( i=0; i<num_modules; i++ ) {
   504 	if( modules[i]->save != NULL )
   505 	    header.module_count++;
   506     }
   507     fwrite( &header, sizeof(header), 1, f );
   508     for( i=0; i<num_modules; i++ ) {
   509 	if( modules[i]->save != NULL ) {
   510 	    fwrite( "BLCK", 4, 1, f );
   511 	    fwrite_string( modules[i]->name, f );
   512 	    modules[i]->save(f);
   513 	}
   514     }
   515     fclose( f );
   516     INFO( "Save state written to %s", filename );
   517 }
.