Search
lxdream.org :: lxdream/src/dreamcast.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 18:9a1b5d75703f
prev17:944f75eea496
next23:1ec3acd0594d
author nkeynes
date Thu Dec 15 13:33:14 2005 +0000 (18 years ago)
permissions -rw-r--r--
last change Implement save states
file annotate diff log raw
1.1 --- a/src/dreamcast.c Tue Dec 13 14:47:59 2005 +0000
1.2 +++ b/src/dreamcast.c Thu Dec 15 13:33:14 2005 +0000
1.3 @@ -1,3 +1,4 @@
1.4 +#include <errno.h>
1.5 #include "dream.h"
1.6 #include "mem.h"
1.7 #include "aica/aica.h"
1.8 @@ -98,13 +99,17 @@
1.9 };
1.10
1.11
1.12 -int dreamcast_load_state( FILE *f )
1.13 +int dreamcast_load_state( const gchar *filename )
1.14 {
1.15 int i,j;
1.16 uint32_t count, len;
1.17 int have_read[MAX_MODULES];
1.18 char tmp[64];
1.19 struct save_state_header header;
1.20 + FILE *f;
1.21 +
1.22 + f = fopen( filename, "r" );
1.23 + if( f == NULL ) return errno;
1.24
1.25 fread( &header, sizeof(header), 1, f );
1.26 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
1.27 @@ -115,24 +120,23 @@
1.28 ERROR( "DreamOn save state version not supported" );
1.29 return 1;
1.30 }
1.31 - fread( &count, sizeof(count), 1, f );
1.32 - if( count > MAX_MODULES ) {
1.33 - ERROR( "DreamOn save state is corrupted" );
1.34 + if( header.module_count > MAX_MODULES ) {
1.35 + ERROR( "DreamOn save state is corrupted (bad module count)" );
1.36 return 1;
1.37 }
1.38 for( i=0; i<MAX_MODULES; i++ ) {
1.39 have_read[i] = 0;
1.40 }
1.41
1.42 - for( i=0; i<count; i++ ) {
1.43 + for( i=0; i<header.module_count; i++ ) {
1.44 fread(tmp, 4, 1, f );
1.45 - if( strcmp(tmp, "BLCK") != 0 ) {
1.46 - ERROR( "DreamOn save state is corrupted" );
1.47 + if( strncmp(tmp, "BLCK", 4) != 0 ) {
1.48 + ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
1.49 return 2;
1.50 }
1.51 len = fread_string(tmp, sizeof(tmp), f );
1.52 if( len > 64 || len < 1 ) {
1.53 - ERROR( "DreamOn save state is corrupted" );
1.54 + ERROR( "DreamOn save state is corrupted (bad string)" );
1.55 return 2;
1.56 }
1.57
1.58 @@ -141,10 +145,10 @@
1.59 if( strcmp(modules[j]->name,tmp) == 0 ) {
1.60 have_read[j] = 1;
1.61 if( modules[j]->load == NULL ) {
1.62 - ERROR( "DreamOn save state is corrupted" );
1.63 + ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
1.64 return 2;
1.65 } else if( modules[j]->load(f) != 0 ) {
1.66 - ERROR( "DreamOn save state is corrupted" );
1.67 + ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
1.68 return 2;
1.69 }
1.70 break;
1.71 @@ -165,18 +169,28 @@
1.72 modules[j]->reset();
1.73 }
1.74 }
1.75 + fclose(f);
1.76 + INFO( "Save state read from %s", filename );
1.77 }
1.78
1.79 -void dreamcast_save_state( FILE *f )
1.80 +int dreamcast_save_state( const gchar *filename )
1.81 {
1.82 int i;
1.83 + FILE *f;
1.84 struct save_state_header header;
1.85
1.86 + f = fopen( filename, "w" );
1.87 + if( f == NULL )
1.88 + return errno;
1.89 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
1.90 header.version = DREAMCAST_SAVE_VERSION;
1.91 - header.module_count = num_modules;
1.92 + header.module_count = 0;
1.93 +
1.94 + for( i=0; i<num_modules; i++ ) {
1.95 + if( modules[i]->save != NULL )
1.96 + header.module_count++;
1.97 + }
1.98 fwrite( &header, sizeof(header), 1, f );
1.99 - fwrite_string( dreamcast_config, f );
1.100 for( i=0; i<num_modules; i++ ) {
1.101 if( modules[i]->save != NULL ) {
1.102 fwrite( "BLCK", 4, 1, f );
1.103 @@ -184,5 +198,7 @@
1.104 modules[i]->save(f);
1.105 }
1.106 }
1.107 + fclose( f );
1.108 + INFO( "Save state written to %s", filename );
1.109 }
1.110
.