Search
lxdream.org :: lxdream/src/dreamcast.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 17:944f75eea496
prev16:f383e7640da4
next18:9a1b5d75703f
author nkeynes
date Tue Dec 13 14:47:59 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change More work on load/save state - save state a little more structured now
Memory save now in
file annotate diff log raw
1.1 --- a/src/dreamcast.c Tue Dec 13 12:17:26 2005 +0000
1.2 +++ b/src/dreamcast.c Tue Dec 13 14:47:59 2005 +0000
1.3 @@ -12,6 +12,7 @@
1.4 #define MAX_MODULES 32
1.5 static int num_modules = 0;
1.6 static int dreamcast_state = 0;
1.7 +static char *dreamcast_config = "DEFAULT";
1.8 dreamcast_module_t modules[MAX_MODULES];
1.9
1.10 /**
1.11 @@ -93,28 +94,76 @@
1.12 struct save_state_header {
1.13 char magic[16];
1.14 uint32_t version;
1.15 + uint32_t module_count;
1.16 };
1.17
1.18 -void dreamcast_load_state( FILE *f )
1.19 +
1.20 +int dreamcast_load_state( FILE *f )
1.21 {
1.22 - int i;
1.23 + int i,j;
1.24 + uint32_t count, len;
1.25 + int have_read[MAX_MODULES];
1.26 + char tmp[64];
1.27 struct save_state_header header;
1.28
1.29 fread( &header, sizeof(header), 1, f );
1.30 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
1.31 ERROR( "Not a DreamOn save state file" );
1.32 - return;
1.33 + return 1;
1.34 }
1.35 if( header.version != DREAMCAST_SAVE_VERSION ) {
1.36 ERROR( "DreamOn save state version not supported" );
1.37 - return;
1.38 + return 1;
1.39 + }
1.40 + fread( &count, sizeof(count), 1, f );
1.41 + if( count > MAX_MODULES ) {
1.42 + ERROR( "DreamOn save state is corrupted" );
1.43 + return 1;
1.44 + }
1.45 + for( i=0; i<MAX_MODULES; i++ ) {
1.46 + have_read[i] = 0;
1.47 }
1.48
1.49 - for( i=0; i<num_modules; i++ ) {
1.50 - if( modules[i]->load != NULL )
1.51 - modules[i]->load(f);
1.52 - else if( modules[i]->reset != NULL )
1.53 - modules[i]->reset();
1.54 + for( i=0; i<count; i++ ) {
1.55 + fread(tmp, 4, 1, f );
1.56 + if( strcmp(tmp, "BLCK") != 0 ) {
1.57 + ERROR( "DreamOn save state is corrupted" );
1.58 + return 2;
1.59 + }
1.60 + len = fread_string(tmp, sizeof(tmp), f );
1.61 + if( len > 64 || len < 1 ) {
1.62 + ERROR( "DreamOn save state is corrupted" );
1.63 + return 2;
1.64 + }
1.65 +
1.66 + /* Find the matching module by name */
1.67 + for( j=0; j<num_modules; j++ ) {
1.68 + if( strcmp(modules[j]->name,tmp) == 0 ) {
1.69 + have_read[j] = 1;
1.70 + if( modules[j]->load == NULL ) {
1.71 + ERROR( "DreamOn save state is corrupted" );
1.72 + return 2;
1.73 + } else if( modules[j]->load(f) != 0 ) {
1.74 + ERROR( "DreamOn save state is corrupted" );
1.75 + return 2;
1.76 + }
1.77 + break;
1.78 + }
1.79 + }
1.80 + if( j == num_modules ) {
1.81 + ERROR( "DreamOn save state contains unrecognized section" );
1.82 + return 2;
1.83 + }
1.84 + }
1.85 +
1.86 + /* Any modules that we didn't load - reset to the default state.
1.87 + * (ie it's not an error to skip a module if you don't actually
1.88 + * care about its state).
1.89 + */
1.90 + for( j=0; j<num_modules; j++ ) {
1.91 + if( have_read[j] == 0 && modules[j]->reset != NULL ) {
1.92 + modules[j]->reset();
1.93 + }
1.94 }
1.95 }
1.96
1.97 @@ -125,9 +174,13 @@
1.98
1.99 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
1.100 header.version = DREAMCAST_SAVE_VERSION;
1.101 + header.module_count = num_modules;
1.102 fwrite( &header, sizeof(header), 1, f );
1.103 + fwrite_string( dreamcast_config, f );
1.104 for( i=0; i<num_modules; i++ ) {
1.105 if( modules[i]->save != NULL ) {
1.106 + fwrite( "BLCK", 4, 1, f );
1.107 + fwrite_string( modules[i]->name, f );
1.108 modules[i]->save(f);
1.109 }
1.110 }
.