Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 265:5daf59b7f31b
prev180:e6dcf9b65658
next277:fcc1274776cb
author nkeynes
date Sat Jan 06 04:06:36 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Implement event queue.
Fix pvr2 timing (yes, again).
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.19 2007-01-06 04:06:36 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 /**
    89  * Constructs a system configuration for the AICA in standalone mode,
    90  * ie sound chip only.
    91  */
    92 void dreamcast_configure_aica_only( )
    93 {
    94     dreamcast_register_module( &mem_module );
    95     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    96     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    97     dreamcast_register_module( &aica_module );
    98     aica_enable();
    99     dreamcast_state = STATE_STOPPED;
   100 }
   102 void dreamcast_register_module( dreamcast_module_t module ) 
   103 {
   104     modules[num_modules++] = module;
   105     if( module->init != NULL )
   106 	module->init();
   107 }
   110 void dreamcast_init( void )
   111 {
   112     dreamcast_configure();
   113     dreamcast_state = STATE_STOPPED;
   114 }
   116 void dreamcast_reset( void )
   117 {
   118     int i;
   119     for( i=0; i<num_modules; i++ ) {
   120 	if( modules[i]->reset != NULL )
   121 	    modules[i]->reset();
   122     }
   123 }
   125 void dreamcast_run( void )
   126 {
   127     int i;
   128     if( dreamcast_state != STATE_RUNNING ) {
   129 	for( i=0; i<num_modules; i++ ) {
   130 	    if( modules[i]->start != NULL )
   131 		modules[i]->start();
   132 	}
   133     }
   134     dreamcast_state = STATE_RUNNING;
   135     while( dreamcast_state == STATE_RUNNING ) {
   136 	int time_to_run = timeslice_length;
   137 	for( i=0; i<num_modules; i++ ) {
   138 	    if( modules[i]->run_time_slice != NULL )
   139 		time_to_run = modules[i]->run_time_slice( time_to_run );
   140 	}
   142     }
   144     for( i=0; i<num_modules; i++ ) {
   145 	if( modules[i]->stop != NULL )
   146 	    modules[i]->stop();
   147     }
   148     dreamcast_state = STATE_STOPPED;
   149 }
   151 void dreamcast_stop( void )
   152 {
   153     if( dreamcast_state == STATE_RUNNING )
   154 	dreamcast_state = STATE_STOPPING;
   155 }
   157 gboolean dreamcast_is_running( void )
   158 {
   159     return dreamcast_state == STATE_RUNNING;
   160 }
   162 /***************************** User Configuration **************************/
   165 static struct dreamcast_config_entry global_config[] =
   166     {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
   167      { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
   168      { "default path", CONFIG_TYPE_PATH, "." },
   169      { "save path", CONFIG_TYPE_PATH, "save" },
   170      { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
   171      { NULL, CONFIG_TYPE_NONE }};
   173 static struct dreamcast_config_entry serial_config[] =
   174     {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
   175      { NULL, CONFIG_TYPE_NONE }};
   177 struct dreamcast_config_group dreamcast_config_root[] = 
   178     {{ "global", global_config },
   179      { "controllers", NULL },
   180      { "serial", serial_config },
   181      { NULL, CONFIG_TYPE_NONE }};
   183 void dreamcast_set_default_config( )
   184 {
   185     struct dreamcast_config_group *group = dreamcast_config_root;
   186     while( group->key != NULL ) {
   187 	struct dreamcast_config_entry *param = group->params;
   188 	if( param != NULL ) {
   189 	    while( param->key != NULL ) {
   190 		if( param->value != param->default_value ) {
   191 		    if( param->value != NULL )
   192 			free( param->value );
   193 		    param->value = (gchar *)param->default_value;
   194 		}
   195 		param++;
   196 	    }
   197 	}
   198 	group++;
   199     }
   200     maple_detach_all();
   201 }
   203 const gchar *dreamcast_get_config_value( int key )
   204 {
   205     return global_config[key].value;
   206 }
   208 gboolean dreamcast_load_config( const gchar *filename )
   209 {
   210     FILE *f = fopen(filename, "ro");
   211     gboolean result;
   213     if( f == NULL ) {
   214 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   215 	return FALSE;
   216     }
   218     result = dreamcast_load_config_stream( f );
   219     fclose(f);
   220     return result;
   221 }
   223 gboolean dreamcast_load_config_stream( FILE *f )
   224 {
   226     char buf[512], *p;
   227     int maple_device = -1, maple_subdevice = -1;
   228     struct dreamcast_config_group devgroup;
   229     struct dreamcast_config_group *group = NULL;
   230     maple_device_t device = NULL;
   231     dreamcast_set_default_config();
   233     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   234 	g_strstrip(buf);
   235 	if( buf[0] == '#' )
   236 	    continue;
   237 	if( *buf == '[' ) {
   238 	    char *p = strchr(buf, ']');
   239 	    if( p != NULL ) {
   240 		struct dreamcast_config_group *tmp_group;
   241 		maple_device = maple_subdevice = -1;
   242 		*p = '\0';
   243 		g_strstrip(buf+1);
   244 		tmp_group = &dreamcast_config_root[0];
   245 		while( tmp_group->key != NULL ) {
   246 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   247 			group = tmp_group;
   248 			break;
   249 		    }
   250 		    tmp_group++;
   251 		}
   252 	    }
   253 	} else if( group != NULL ) {
   254 	    char *value = strchr( buf, '=' );
   255 	    if( value != NULL ) {
   256 		struct dreamcast_config_entry *param = group->params;
   257 		*value = '\0';
   258 		value++;
   259 		g_strstrip(buf);
   260 		g_strstrip(value);
   261 		if( strcmp(group->key,"controllers") == 0  ) {
   262 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   263 			maple_device = strtoul( buf+7, NULL, 0 );
   264 			if( maple_device < 0 || maple_device > 3 ) {
   265 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   266 			    continue;
   267 			}
   268 			maple_subdevice = 0;
   269 			device = maple_new_device( value );
   270 			if( device == NULL ) {
   271 			    ERROR( "Unrecognized device '%s'", value );
   272 			} else {
   273 			    devgroup.key = "controllers";
   274 			    devgroup.params = maple_get_device_config(device);
   275 			    maple_attach_device( device, maple_device, maple_subdevice );
   276 			    group = &devgroup;
   277 			}
   278 			continue;
   279 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   280 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   281 			if( maple_device == -1 ) {
   282 			    ERROR( "Subdevice not allowed without primary device" );
   283 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   284 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   285 			} else if( (device = maple_new_device(value)) == NULL ) {
   286 			    ERROR( "Unrecognized subdevice '%s'", value );
   287 			} else {
   288 			    devgroup.key = "controllers";
   289 			    devgroup.params = maple_get_device_config(device);
   290 			    maple_attach_device( device, maple_device, maple_subdevice );
   291 			    group = &devgroup;
   292 			}
   293 			continue;
   294 		    }
   295 		}
   296 		while( param->key != NULL ) {
   297 		    if( strcasecmp( param->key, buf ) == 0 ) {
   298 			param->value = g_strdup(value);
   299 			break;
   300 		    }
   301 		    param++;
   302 		}
   303 	    }
   304 	}
   305     }
   306     return TRUE;
   307 }
   309 gboolean dreamcast_save_config( const gchar *filename )
   310 {
   311     FILE *f = fopen(filename, "wo");
   312     gboolean result;
   313     if( f == NULL ) {
   314 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   315 	return FALSE;
   316     }
   317     result = dreamcast_save_config_stream(f);
   318     fclose(f);
   319 }    
   321 gboolean dreamcast_save_config_stream( FILE *f )
   322 {
   323     struct dreamcast_config_group *group = &dreamcast_config_root[0];
   325     while( group->key != NULL ) {
   326 	struct dreamcast_config_entry *entry = group->params;
   327 	fprintf( f, "[%s]\n", group->key );
   329 	if( entry != NULL ) {
   330 	    while( entry->key != NULL ) {
   331 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   332 		entry++;
   333 	    }
   334 	} else if( strcmp(group->key, "controllers") == 0 ) {
   335 	    int i,j;
   336 	    for( i=0; i<4; i++ ) {
   337 		for( j=0; j<6; j++ ) {
   338 		    maple_device_t dev = maple_get_device( i, j );
   339 		    if( dev != NULL ) {
   340 			if( j == 0 )
   341 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   342 			else 
   343 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   344 			entry = dev->get_config(dev);
   345 			while( entry->key != NULL ) {
   346 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   347 			    entry++;
   348 			}
   349 		    }
   350 		}
   351 	    }
   352 	}
   353 	fprintf( f, "\n" );
   354 	group++;
   355     }
   356     return TRUE;
   357 }
   359 /********************************* Save States *****************************/
   361 #define DREAMCAST_SAVE_MAGIC "%!-lxDream!Save\0"
   362 #define DREAMCAST_SAVE_VERSION 0x00010000
   364 struct save_state_header {
   365     char magic[16];
   366     uint32_t version;
   367     uint32_t module_count;
   368 };
   370 int dreamcast_load_state( const gchar *filename )
   371 {
   372     int i,j;
   373     uint32_t count, len;
   374     int have_read[MAX_MODULES];
   375     char tmp[64];
   376     struct save_state_header header;
   377     FILE *f;
   379     f = fopen( filename, "r" );
   380     if( f == NULL ) return errno;
   382     fread( &header, sizeof(header), 1, f );
   383     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   384 	ERROR( "Not a %s save state file", APP_NAME );
   385 	return 1;
   386     }
   387     if( header.version != DREAMCAST_SAVE_VERSION ) {
   388 	ERROR( "%s save state version not supported", APP_NAME );
   389 	return 1;
   390     }
   391     if( header.module_count > MAX_MODULES ) {
   392 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   393 	return 1;
   394     }
   395     for( i=0; i<MAX_MODULES; i++ ) {
   396 	have_read[i] = 0;
   397     }
   399     for( i=0; i<header.module_count; i++ ) {
   400 	fread(tmp, 4, 1, f );
   401 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   402 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   403 	    return 2;
   404 	}
   405 	len = fread_string(tmp, sizeof(tmp), f );
   406 	if( len > 64 || len < 1 ) {
   407 	    ERROR( "%s save state is corrupted (bad string)", APP_NAME );
   408 	    return 2;
   409 	}
   411 	/* Find the matching module by name */
   412 	for( j=0; j<num_modules; j++ ) {
   413 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   414 		have_read[j] = 1;
   415 		if( modules[j]->load == NULL ) {
   416 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   417 		    return 2;
   418 		} else if( modules[j]->load(f) != 0 ) {
   419 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   420 		    return 2;
   421 		}
   422 		break;
   423 	    }
   424 	}
   425 	if( j == num_modules ) {
   426 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   427 	    return 2;
   428 	}
   429     }
   431     /* Any modules that we didn't load - reset to the default state.
   432      * (ie it's not an error to skip a module if you don't actually
   433      * care about its state).
   434      */
   435     for( j=0; j<num_modules; j++ ) {
   436 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   437 	    modules[j]->reset();
   438 	}
   439     }
   440     fclose(f);
   441     INFO( "Save state read from %s", filename );
   442 }
   444 int dreamcast_save_state( const gchar *filename )
   445 {
   446     int i;
   447     FILE *f;
   448     struct save_state_header header;
   450     f = fopen( filename, "w" );
   451     if( f == NULL )
   452 	return errno;
   453     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   454     header.version = DREAMCAST_SAVE_VERSION;
   455     header.module_count = 0;
   457     for( i=0; i<num_modules; i++ ) {
   458 	if( modules[i]->save != NULL )
   459 	    header.module_count++;
   460     }
   461     fwrite( &header, sizeof(header), 1, f );
   462     for( i=0; i<num_modules; i++ ) {
   463 	if( modules[i]->save != NULL ) {
   464 	    fwrite( "BLCK", 4, 1, f );
   465 	    fwrite_string( modules[i]->name, f );
   466 	    modules[i]->save(f);
   467 	}
   468     }
   469     fclose( f );
   470     INFO( "Save state written to %s", filename );
   471 }
.