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