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