Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 277:fcc1274776cb
prev265:5daf59b7f31b
next295:6637664291a8
author nkeynes
date Mon Jan 15 12:17:02 2007 +0000 (17 years ago)
permissions -rw-r--r--
last change Fix output of yuv_decode() (yes, again...)
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.20 2007-01-12 10:16:02 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_stop( void )
   158 {
   159     if( dreamcast_state == STATE_RUNNING )
   160 	dreamcast_state = STATE_STOPPING;
   161 }
   163 void dreamcast_shutdown()
   164 {
   165     dreamcast_stop();
   166     sh4_stop();
   167     dreamcast_save_flash();
   168 }
   170 gboolean dreamcast_is_running( void )
   171 {
   172     return dreamcast_state == STATE_RUNNING;
   173 }
   175 /***************************** User Configuration **************************/
   178 static struct dreamcast_config_entry global_config[] =
   179     {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
   180      { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
   181      { "default path", CONFIG_TYPE_PATH, "." },
   182      { "save path", CONFIG_TYPE_PATH, "save" },
   183      { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
   184      { NULL, CONFIG_TYPE_NONE }};
   186 static struct dreamcast_config_entry serial_config[] =
   187     {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
   188      { NULL, CONFIG_TYPE_NONE }};
   190 struct dreamcast_config_group dreamcast_config_root[] = 
   191     {{ "global", global_config },
   192      { "controllers", NULL },
   193      { "serial", serial_config },
   194      { NULL, CONFIG_TYPE_NONE }};
   196 void dreamcast_set_default_config( )
   197 {
   198     struct dreamcast_config_group *group = dreamcast_config_root;
   199     while( group->key != NULL ) {
   200 	struct dreamcast_config_entry *param = group->params;
   201 	if( param != NULL ) {
   202 	    while( param->key != NULL ) {
   203 		if( param->value != param->default_value ) {
   204 		    if( param->value != NULL )
   205 			free( param->value );
   206 		    param->value = (gchar *)param->default_value;
   207 		}
   208 		param++;
   209 	    }
   210 	}
   211 	group++;
   212     }
   213     maple_detach_all();
   214 }
   216 const gchar *dreamcast_get_config_value( int key )
   217 {
   218     return global_config[key].value;
   219 }
   221 gboolean dreamcast_load_config( const gchar *filename )
   222 {
   223     FILE *f = fopen(filename, "ro");
   224     gboolean result;
   226     if( f == NULL ) {
   227 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   228 	return FALSE;
   229     }
   231     result = dreamcast_load_config_stream( f );
   232     fclose(f);
   233     return result;
   234 }
   236 gboolean dreamcast_load_config_stream( FILE *f )
   237 {
   239     char buf[512], *p;
   240     int maple_device = -1, maple_subdevice = -1;
   241     struct dreamcast_config_group devgroup;
   242     struct dreamcast_config_group *group = NULL;
   243     maple_device_t device = NULL;
   244     dreamcast_set_default_config();
   246     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   247 	g_strstrip(buf);
   248 	if( buf[0] == '#' )
   249 	    continue;
   250 	if( *buf == '[' ) {
   251 	    char *p = strchr(buf, ']');
   252 	    if( p != NULL ) {
   253 		struct dreamcast_config_group *tmp_group;
   254 		maple_device = maple_subdevice = -1;
   255 		*p = '\0';
   256 		g_strstrip(buf+1);
   257 		tmp_group = &dreamcast_config_root[0];
   258 		while( tmp_group->key != NULL ) {
   259 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   260 			group = tmp_group;
   261 			break;
   262 		    }
   263 		    tmp_group++;
   264 		}
   265 	    }
   266 	} else if( group != NULL ) {
   267 	    char *value = strchr( buf, '=' );
   268 	    if( value != NULL ) {
   269 		struct dreamcast_config_entry *param = group->params;
   270 		*value = '\0';
   271 		value++;
   272 		g_strstrip(buf);
   273 		g_strstrip(value);
   274 		if( strcmp(group->key,"controllers") == 0  ) {
   275 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   276 			maple_device = strtoul( buf+7, NULL, 0 );
   277 			if( maple_device < 0 || maple_device > 3 ) {
   278 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   279 			    continue;
   280 			}
   281 			maple_subdevice = 0;
   282 			device = maple_new_device( value );
   283 			if( device == NULL ) {
   284 			    ERROR( "Unrecognized device '%s'", value );
   285 			} else {
   286 			    devgroup.key = "controllers";
   287 			    devgroup.params = maple_get_device_config(device);
   288 			    maple_attach_device( device, maple_device, maple_subdevice );
   289 			    group = &devgroup;
   290 			}
   291 			continue;
   292 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   293 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   294 			if( maple_device == -1 ) {
   295 			    ERROR( "Subdevice not allowed without primary device" );
   296 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   297 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   298 			} else if( (device = maple_new_device(value)) == NULL ) {
   299 			    ERROR( "Unrecognized subdevice '%s'", value );
   300 			} else {
   301 			    devgroup.key = "controllers";
   302 			    devgroup.params = maple_get_device_config(device);
   303 			    maple_attach_device( device, maple_device, maple_subdevice );
   304 			    group = &devgroup;
   305 			}
   306 			continue;
   307 		    }
   308 		}
   309 		while( param->key != NULL ) {
   310 		    if( strcasecmp( param->key, buf ) == 0 ) {
   311 			param->value = g_strdup(value);
   312 			break;
   313 		    }
   314 		    param++;
   315 		}
   316 	    }
   317 	}
   318     }
   319     return TRUE;
   320 }
   322 gboolean dreamcast_save_config( const gchar *filename )
   323 {
   324     FILE *f = fopen(filename, "wo");
   325     gboolean result;
   326     if( f == NULL ) {
   327 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   328 	return FALSE;
   329     }
   330     result = dreamcast_save_config_stream(f);
   331     fclose(f);
   332 }    
   334 gboolean dreamcast_save_config_stream( FILE *f )
   335 {
   336     struct dreamcast_config_group *group = &dreamcast_config_root[0];
   338     while( group->key != NULL ) {
   339 	struct dreamcast_config_entry *entry = group->params;
   340 	fprintf( f, "[%s]\n", group->key );
   342 	if( entry != NULL ) {
   343 	    while( entry->key != NULL ) {
   344 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   345 		entry++;
   346 	    }
   347 	} else if( strcmp(group->key, "controllers") == 0 ) {
   348 	    int i,j;
   349 	    for( i=0; i<4; i++ ) {
   350 		for( j=0; j<6; j++ ) {
   351 		    maple_device_t dev = maple_get_device( i, j );
   352 		    if( dev != NULL ) {
   353 			if( j == 0 )
   354 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   355 			else 
   356 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   357 			entry = dev->get_config(dev);
   358 			while( entry->key != NULL ) {
   359 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   360 			    entry++;
   361 			}
   362 		    }
   363 		}
   364 	    }
   365 	}
   366 	fprintf( f, "\n" );
   367 	group++;
   368     }
   369     return TRUE;
   370 }
   372 /********************************* Save States *****************************/
   374 #define DREAMCAST_SAVE_MAGIC "%!-lxDream!Save\0"
   375 #define DREAMCAST_SAVE_VERSION 0x00010000
   377 struct save_state_header {
   378     char magic[16];
   379     uint32_t version;
   380     uint32_t module_count;
   381 };
   383 int dreamcast_load_state( const gchar *filename )
   384 {
   385     int i,j;
   386     uint32_t count, len;
   387     int have_read[MAX_MODULES];
   388     char tmp[64];
   389     struct save_state_header header;
   390     FILE *f;
   392     f = fopen( filename, "r" );
   393     if( f == NULL ) return errno;
   395     fread( &header, sizeof(header), 1, f );
   396     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   397 	ERROR( "Not a %s save state file", APP_NAME );
   398 	return 1;
   399     }
   400     if( header.version != DREAMCAST_SAVE_VERSION ) {
   401 	ERROR( "%s save state version not supported", APP_NAME );
   402 	return 1;
   403     }
   404     if( header.module_count > MAX_MODULES ) {
   405 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   406 	return 1;
   407     }
   408     for( i=0; i<MAX_MODULES; i++ ) {
   409 	have_read[i] = 0;
   410     }
   412     for( i=0; i<header.module_count; i++ ) {
   413 	fread(tmp, 4, 1, f );
   414 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   415 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   416 	    return 2;
   417 	}
   418 	len = fread_string(tmp, sizeof(tmp), f );
   419 	if( len > 64 || len < 1 ) {
   420 	    ERROR( "%s save state is corrupted (bad string)", APP_NAME );
   421 	    return 2;
   422 	}
   424 	/* Find the matching module by name */
   425 	for( j=0; j<num_modules; j++ ) {
   426 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   427 		have_read[j] = 1;
   428 		if( modules[j]->load == NULL ) {
   429 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   430 		    return 2;
   431 		} else if( modules[j]->load(f) != 0 ) {
   432 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   433 		    return 2;
   434 		}
   435 		break;
   436 	    }
   437 	}
   438 	if( j == num_modules ) {
   439 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   440 	    return 2;
   441 	}
   442     }
   444     /* Any modules that we didn't load - reset to the default state.
   445      * (ie it's not an error to skip a module if you don't actually
   446      * care about its state).
   447      */
   448     for( j=0; j<num_modules; j++ ) {
   449 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   450 	    modules[j]->reset();
   451 	}
   452     }
   453     fclose(f);
   454     INFO( "Save state read from %s", filename );
   455 }
   457 int dreamcast_save_state( const gchar *filename )
   458 {
   459     int i;
   460     FILE *f;
   461     struct save_state_header header;
   463     f = fopen( filename, "w" );
   464     if( f == NULL )
   465 	return errno;
   466     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   467     header.version = DREAMCAST_SAVE_VERSION;
   468     header.module_count = 0;
   470     for( i=0; i<num_modules; i++ ) {
   471 	if( modules[i]->save != NULL )
   472 	    header.module_count++;
   473     }
   474     fwrite( &header, sizeof(header), 1, f );
   475     for( i=0; i<num_modules; i++ ) {
   476 	if( modules[i]->save != NULL ) {
   477 	    fwrite( "BLCK", 4, 1, f );
   478 	    fwrite_string( modules[i]->name, f );
   479 	    modules[i]->save(f);
   480 	}
   481     }
   482     fclose( f );
   483     INFO( "Save state written to %s", filename );
   484 }
.