Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 671:a530ea88eebd
prev669:ab344e42bca9
next689:9868667e3525
author nkeynes
date Thu May 15 10:22:39 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Permanently add SH4 instruction statistics tracking (enabled with --enable-sh4stats)
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@586
     2
 * $Id$
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@543
    21
#include <glib.h>
nkeynes@669
    22
#include "lxdream.h"
nkeynes@2
    23
#include "mem.h"
nkeynes@669
    24
#include "dreamcast.h"
nkeynes@669
    25
#include "asic.h"
nkeynes@669
    26
#include "syscall.h"
nkeynes@669
    27
#include "gui.h"
nkeynes@11
    28
#include "aica/aica.h"
nkeynes@25
    29
#include "gdrom/ide.h"
nkeynes@25
    30
#include "maple/maple.h"
nkeynes@669
    31
#include "sh4/sh4.h"
nkeynes@586
    32
#include "sh4/sh4trans.h"
nkeynes@15
    33
nkeynes@26
    34
/**
nkeynes@26
    35
 * Current state of the DC virtual machine
nkeynes@26
    36
 */
nkeynes@543
    37
typedef enum { STATE_UNINIT=0, STATE_RUNNING, 
nkeynes@543
    38
	       STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
nkeynes@543
    39
nkeynes@543
    40
static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
nkeynes@543
    41
static gboolean dreamcast_has_bios = FALSE;
nkeynes@543
    42
static gchar *dreamcast_program_name = NULL;
nkeynes@543
    43
static sh4addr_t dreamcast_entry_point = 0xA0000000;
nkeynes@26
    44
static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
nkeynes@2
    45
nkeynes@15
    46
#define MAX_MODULES 32
nkeynes@15
    47
static int num_modules = 0;
nkeynes@16
    48
dreamcast_module_t modules[MAX_MODULES];
nkeynes@15
    49
nkeynes@16
    50
/**
nkeynes@35
    51
 * The unknown module is used for logging files without an actual module
nkeynes@35
    52
 * declaration
nkeynes@35
    53
 */
nkeynes@35
    54
struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
nkeynes@35
    55
					   NULL, NULL, NULL };
nkeynes@35
    56
nkeynes@35
    57
/**
nkeynes@16
    58
 * This function is responsible for defining how all the pieces of the
nkeynes@26
    59
 * dreamcast actually fit together. 
nkeynes@16
    60
 *
nkeynes@16
    61
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    62
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    63
 */
nkeynes@15
    64
void dreamcast_configure( )
nkeynes@15
    65
{
nkeynes@265
    66
    dreamcast_register_module( &eventq_module );
nkeynes@16
    67
    /* Register the memory framework */
nkeynes@15
    68
    dreamcast_register_module( &mem_module );
nkeynes@16
    69
nkeynes@16
    70
    /* Setup standard memory map */
nkeynes@146
    71
    mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
nkeynes@16
    72
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@16
    73
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@16
    74
    mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
nkeynes@543
    75
    dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
nkeynes@543
    76
				       0x00000000, 0x00200000, 0x89f2b1a1,
nkeynes@543
    77
				       MEM_REGION_BIOS );
nkeynes@146
    78
    mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
nkeynes@450
    79
    mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
nkeynes@167
    80
		    0x00200000, 0x00020000 );
nkeynes@16
    81
nkeynes@16
    82
    /* Load in the rest of the core modules */
nkeynes@15
    83
    dreamcast_register_module( &sh4_module );
nkeynes@15
    84
    dreamcast_register_module( &asic_module );
nkeynes@15
    85
    dreamcast_register_module( &pvr2_module );
nkeynes@15
    86
    dreamcast_register_module( &aica_module );
nkeynes@15
    87
    dreamcast_register_module( &maple_module );
nkeynes@15
    88
    dreamcast_register_module( &ide_module );
nkeynes@15
    89
}
nkeynes@15
    90
nkeynes@461
    91
void dreamcast_config_changed(void)
nkeynes@461
    92
{
nkeynes@543
    93
    dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
nkeynes@543
    94
				       0x00000000, 0x00200000, 0x89f2b1a1, 
nkeynes@543
    95
				       MEM_REGION_BIOS );
nkeynes@461
    96
    mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
nkeynes@461
    97
		    0x00200000, 0x00020000 );
nkeynes@461
    98
}
nkeynes@461
    99
nkeynes@277
   100
void dreamcast_save_flash()
nkeynes@277
   101
{
nkeynes@450
   102
    const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
nkeynes@277
   103
    mem_save_block( file, 0x00200000, 0x00020000 );
nkeynes@277
   104
}
nkeynes@277
   105
nkeynes@71
   106
/**
nkeynes@71
   107
 * Constructs a system configuration for the AICA in standalone mode,
nkeynes@71
   108
 * ie sound chip only.
nkeynes@71
   109
 */
nkeynes@71
   110
void dreamcast_configure_aica_only( )
nkeynes@71
   111
{
nkeynes@71
   112
    dreamcast_register_module( &mem_module );
nkeynes@71
   113
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@71
   114
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@71
   115
    dreamcast_register_module( &aica_module );
nkeynes@89
   116
    aica_enable();
nkeynes@71
   117
    dreamcast_state = STATE_STOPPED;
nkeynes@71
   118
}
nkeynes@71
   119
nkeynes@15
   120
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
   121
{
nkeynes@15
   122
    modules[num_modules++] = module;
nkeynes@16
   123
    if( module->init != NULL )
nkeynes@16
   124
	module->init();
nkeynes@15
   125
}
nkeynes@15
   126
nkeynes@15
   127
nkeynes@2
   128
void dreamcast_init( void )
nkeynes@2
   129
{
nkeynes@15
   130
    dreamcast_configure();
nkeynes@23
   131
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   132
}
nkeynes@2
   133
nkeynes@2
   134
void dreamcast_reset( void )
nkeynes@2
   135
{
nkeynes@15
   136
    int i;
nkeynes@586
   137
    if( sh4_xlat_is_running() ) {
nkeynes@586
   138
	sh4_translate_exit( XLAT_EXIT_SYSRESET );
nkeynes@586
   139
    }
nkeynes@15
   140
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   141
	if( modules[i]->reset != NULL )
nkeynes@15
   142
	    modules[i]->reset();
nkeynes@15
   143
    }
nkeynes@2
   144
}
nkeynes@2
   145
nkeynes@23
   146
void dreamcast_run( void )
nkeynes@15
   147
{
nkeynes@15
   148
    int i;
nkeynes@23
   149
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@23
   150
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   151
	    if( modules[i]->start != NULL )
nkeynes@23
   152
		modules[i]->start();
nkeynes@23
   153
	}
nkeynes@15
   154
    }
nkeynes@23
   155
    dreamcast_state = STATE_RUNNING;
nkeynes@23
   156
    while( dreamcast_state == STATE_RUNNING ) {
nkeynes@27
   157
	int time_to_run = timeslice_length;
nkeynes@23
   158
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   159
	    if( modules[i]->run_time_slice != NULL )
nkeynes@27
   160
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@23
   161
	}
nkeynes@23
   162
nkeynes@23
   163
    }
nkeynes@23
   164
nkeynes@15
   165
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   166
	if( modules[i]->stop != NULL )
nkeynes@15
   167
	    modules[i]->stop();
nkeynes@15
   168
    }
nkeynes@23
   169
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   170
}
nkeynes@15
   171
nkeynes@372
   172
void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
nkeynes@372
   173
{
nkeynes@372
   174
    
nkeynes@372
   175
    int i;
nkeynes@372
   176
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@372
   177
	for( i=0; i<num_modules; i++ ) {
nkeynes@372
   178
	    if( modules[i]->start != NULL )
nkeynes@372
   179
		modules[i]->start();
nkeynes@372
   180
	}
nkeynes@372
   181
    }
nkeynes@372
   182
    dreamcast_state = STATE_RUNNING;
nkeynes@372
   183
    uint32_t nanos = 0;
nkeynes@372
   184
    if( nanosecs != 0 ) {
nkeynes@372
   185
        nanos = 1000000000 - nanosecs;
nkeynes@372
   186
	seconds++;
nkeynes@372
   187
    }
nkeynes@372
   188
    while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
nkeynes@372
   189
	int time_to_run = timeslice_length;
nkeynes@372
   190
	for( i=0; i<num_modules; i++ ) {
nkeynes@372
   191
	    if( modules[i]->run_time_slice != NULL )
nkeynes@372
   192
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@372
   193
	}
nkeynes@372
   194
	nanos += time_to_run;
nkeynes@372
   195
	if( nanos >= 1000000000 ) {
nkeynes@372
   196
	    nanos -= 1000000000;
nkeynes@372
   197
	    seconds--;
nkeynes@372
   198
	}
nkeynes@372
   199
    }
nkeynes@372
   200
nkeynes@372
   201
    for( i=0; i<num_modules; i++ ) {
nkeynes@372
   202
	if( modules[i]->stop != NULL )
nkeynes@372
   203
	    modules[i]->stop();
nkeynes@372
   204
    }
nkeynes@372
   205
    dreamcast_state = STATE_STOPPED;
nkeynes@372
   206
}
nkeynes@372
   207
nkeynes@23
   208
void dreamcast_stop( void )
nkeynes@23
   209
{
nkeynes@586
   210
    if( sh4_xlat_is_running() ) {
nkeynes@586
   211
	sh4_translate_exit( XLAT_EXIT_HALT );
nkeynes@586
   212
    }
nkeynes@23
   213
    if( dreamcast_state == STATE_RUNNING )
nkeynes@23
   214
	dreamcast_state = STATE_STOPPING;
nkeynes@23
   215
}
nkeynes@17
   216
nkeynes@277
   217
void dreamcast_shutdown()
nkeynes@277
   218
{
nkeynes@277
   219
    dreamcast_stop();
nkeynes@277
   220
    dreamcast_save_flash();
nkeynes@671
   221
#ifdef ENABLE_SH4STATS
nkeynes@671
   222
    sh4_stats_print(stdout);
nkeynes@671
   223
#endif
nkeynes@277
   224
}
nkeynes@277
   225
nkeynes@543
   226
void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
nkeynes@543
   227
{
nkeynes@543
   228
    if( dreamcast_program_name != NULL ) {
nkeynes@543
   229
        g_free(dreamcast_program_name);
nkeynes@543
   230
    }
nkeynes@543
   231
    dreamcast_program_name = g_strdup(name);
nkeynes@543
   232
    dreamcast_entry_point = entry_point;
nkeynes@543
   233
    sh4_set_pc(entry_point);
nkeynes@586
   234
    bios_install();
nkeynes@543
   235
    dcload_install();
nkeynes@543
   236
    gui_update_state();
nkeynes@543
   237
}
nkeynes@543
   238
nkeynes@26
   239
gboolean dreamcast_is_running( void )
nkeynes@26
   240
{
nkeynes@26
   241
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   242
}
nkeynes@26
   243
nkeynes@543
   244
gboolean dreamcast_can_run(void)
nkeynes@543
   245
{
nkeynes@543
   246
    return dreamcast_state != STATE_UNINIT &&
nkeynes@543
   247
      (dreamcast_has_bios || dreamcast_program_name != NULL);
nkeynes@543
   248
}
nkeynes@543
   249
nkeynes@26
   250
/********************************* Save States *****************************/
nkeynes@26
   251
nkeynes@26
   252
struct save_state_header {
nkeynes@26
   253
    char magic[16];
nkeynes@26
   254
    uint32_t version;
nkeynes@26
   255
    uint32_t module_count;
nkeynes@26
   256
};
nkeynes@26
   257
nkeynes@477
   258
struct chunk_header {
nkeynes@477
   259
    char marker[4]; /* Always BLCK */
nkeynes@477
   260
    char name[8]; /* Block (module) name */
nkeynes@477
   261
    uint32_t block_length;
nkeynes@477
   262
};
nkeynes@477
   263
nkeynes@477
   264
/**
nkeynes@477
   265
 * Check the save state header to ensure that it is a valid, supported
nkeynes@477
   266
 * file. 
nkeynes@477
   267
 * @return the number of blocks following, or 0 if the file is invalid.
nkeynes@477
   268
 */
nkeynes@477
   269
int dreamcast_read_save_state_header( FILE *f )
nkeynes@477
   270
{
nkeynes@477
   271
    struct save_state_header header;
nkeynes@477
   272
    if( fread( &header, sizeof(header), 1, f ) != 1 ) {
nkeynes@477
   273
	return 0;
nkeynes@477
   274
    }
nkeynes@477
   275
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@477
   276
	ERROR( "Not a %s save state file", APP_NAME );
nkeynes@477
   277
	return 0;
nkeynes@477
   278
    }
nkeynes@477
   279
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@477
   280
	ERROR( "%s save state version not supported", APP_NAME );
nkeynes@477
   281
	return 0;
nkeynes@477
   282
    }
nkeynes@477
   283
    if( header.module_count > MAX_MODULES ) {
nkeynes@477
   284
	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
nkeynes@477
   285
	return 0;
nkeynes@477
   286
    }
nkeynes@477
   287
    return header.module_count;
nkeynes@477
   288
}
nkeynes@477
   289
nkeynes@477
   290
int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
nkeynes@477
   291
{
nkeynes@477
   292
    struct chunk_header head;
nkeynes@477
   293
nkeynes@477
   294
    memcpy( head.marker, "BLCK", 4 );
nkeynes@477
   295
    memset( head.name, 0, 8 );
nkeynes@477
   296
    memcpy( head.name, name, strlen(name) );
nkeynes@477
   297
    head.block_length = length;
nkeynes@477
   298
    return fwrite( &head, sizeof(head), 1, f );
nkeynes@477
   299
}
nkeynes@477
   300
nkeynes@477
   301
nkeynes@477
   302
frame_buffer_t dreamcast_load_preview( const gchar *filename )
nkeynes@477
   303
{
nkeynes@477
   304
    int i;
nkeynes@477
   305
    FILE *f = fopen( filename, "r" );
nkeynes@477
   306
    if( f == NULL ) return NULL;
nkeynes@477
   307
    
nkeynes@477
   308
    int module_count = dreamcast_read_save_state_header(f);
nkeynes@477
   309
    if( module_count <= 0 ) {
nkeynes@477
   310
	fclose(f);
nkeynes@477
   311
	return NULL;
nkeynes@477
   312
    }
nkeynes@477
   313
    for( i=0; i<module_count; i++ ) {
nkeynes@477
   314
	struct chunk_header head;
nkeynes@477
   315
	if( fread( &head, sizeof(head), 1, f ) != 1 ) {
nkeynes@477
   316
	    fclose(f);
nkeynes@477
   317
	    return NULL;
nkeynes@477
   318
	}
nkeynes@477
   319
	if( memcmp("BLCK", head.marker, 4) != 0 ) {
nkeynes@477
   320
	    fclose(f);
nkeynes@477
   321
	    return NULL;
nkeynes@477
   322
	}
nkeynes@477
   323
nkeynes@477
   324
	if( strcmp("PVR2", head.name) == 0 ) {
nkeynes@477
   325
	    uint32_t buf_count;
nkeynes@477
   326
	    int has_front;
nkeynes@477
   327
	    fread( &buf_count, sizeof(buf_count), 1, f );
nkeynes@477
   328
	    fread( &has_front, sizeof(has_front), 1, f );
nkeynes@477
   329
	    if( buf_count != 0 && has_front ) {
nkeynes@477
   330
		frame_buffer_t result = read_png_from_stream(f);
nkeynes@477
   331
		fclose(f);
nkeynes@477
   332
		return result;
nkeynes@477
   333
	    }
nkeynes@477
   334
	    break;
nkeynes@477
   335
	} else {
nkeynes@477
   336
	    fseek( f, head.block_length, SEEK_CUR );
nkeynes@477
   337
	}
nkeynes@477
   338
    }
nkeynes@477
   339
    return NULL;
nkeynes@477
   340
}
nkeynes@477
   341
nkeynes@18
   342
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   343
{
nkeynes@17
   344
    int i,j;
nkeynes@477
   345
    int module_count;
nkeynes@17
   346
    int have_read[MAX_MODULES];
nkeynes@18
   347
nkeynes@477
   348
    FILE *f = fopen( filename, "r" );
nkeynes@18
   349
    if( f == NULL ) return errno;
nkeynes@15
   350
nkeynes@477
   351
    module_count = dreamcast_read_save_state_header(f);
nkeynes@477
   352
    if( module_count <= 0 ) {
nkeynes@477
   353
	fclose(f);
nkeynes@17
   354
	return 1;
nkeynes@15
   355
    }
nkeynes@477
   356
nkeynes@17
   357
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@17
   358
	have_read[i] = 0;
nkeynes@15
   359
    }
nkeynes@15
   360
nkeynes@477
   361
    for( i=0; i<module_count; i++ ) {
nkeynes@477
   362
	struct chunk_header chunk;
nkeynes@477
   363
	fread( &chunk, sizeof(chunk), 1, f );
nkeynes@477
   364
	if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
nkeynes@180
   365
	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
nkeynes@477
   366
	    fclose(f);
nkeynes@17
   367
	    return 2;
nkeynes@17
   368
	}
nkeynes@17
   369
	
nkeynes@17
   370
	/* Find the matching module by name */
nkeynes@17
   371
	for( j=0; j<num_modules; j++ ) {
nkeynes@477
   372
	    if( strcmp(modules[j]->name,chunk.name) == 0 ) {
nkeynes@17
   373
		have_read[j] = 1;
nkeynes@17
   374
		if( modules[j]->load == NULL ) {
nkeynes@180
   375
		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
nkeynes@477
   376
		    fclose(f);
nkeynes@17
   377
		    return 2;
nkeynes@17
   378
		} else if( modules[j]->load(f) != 0 ) {
nkeynes@180
   379
		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
nkeynes@477
   380
		    fclose(f);
nkeynes@17
   381
		    return 2;
nkeynes@17
   382
		}
nkeynes@17
   383
		break;
nkeynes@17
   384
	    }
nkeynes@17
   385
	}
nkeynes@17
   386
	if( j == num_modules ) {
nkeynes@477
   387
	    fclose(f);
nkeynes@180
   388
	    ERROR( "%s save state contains unrecognized section", APP_NAME );
nkeynes@17
   389
	    return 2;
nkeynes@17
   390
	}
nkeynes@17
   391
    }
nkeynes@17
   392
    
nkeynes@17
   393
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   394
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   395
     * care about its state).
nkeynes@17
   396
     */
nkeynes@17
   397
    for( j=0; j<num_modules; j++ ) {
nkeynes@17
   398
	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@17
   399
	    modules[j]->reset();
nkeynes@17
   400
	}
nkeynes@15
   401
    }
nkeynes@18
   402
    fclose(f);
nkeynes@18
   403
    INFO( "Save state read from %s", filename );
nkeynes@422
   404
    return 0;
nkeynes@15
   405
}
nkeynes@15
   406
nkeynes@18
   407
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   408
{
nkeynes@15
   409
    int i;
nkeynes@18
   410
    FILE *f;
nkeynes@15
   411
    struct save_state_header header;
nkeynes@15
   412
    
nkeynes@18
   413
    f = fopen( filename, "w" );
nkeynes@18
   414
    if( f == NULL )
nkeynes@18
   415
	return errno;
nkeynes@15
   416
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   417
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   418
    header.module_count = 0;
nkeynes@18
   419
nkeynes@18
   420
    for( i=0; i<num_modules; i++ ) {
nkeynes@18
   421
	if( modules[i]->save != NULL )
nkeynes@18
   422
	    header.module_count++;
nkeynes@18
   423
    }
nkeynes@15
   424
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   425
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   426
	if( modules[i]->save != NULL ) {
nkeynes@477
   427
	    uint32_t blocklen, posn1, posn2;
nkeynes@477
   428
	    dreamcast_write_chunk_header( modules[i]->name, 0, f );
nkeynes@477
   429
	    posn1 = ftell(f);
nkeynes@15
   430
	    modules[i]->save(f);
nkeynes@477
   431
	    posn2 = ftell(f);
nkeynes@477
   432
	    blocklen = posn2 - posn1;
nkeynes@477
   433
	    fseek( f, posn1-4, SEEK_SET );
nkeynes@477
   434
	    fwrite( &blocklen, sizeof(blocklen), 1, f );
nkeynes@477
   435
	    fseek( f, posn2, SEEK_SET );
nkeynes@15
   436
	}
nkeynes@15
   437
    }
nkeynes@18
   438
    fclose( f );
nkeynes@18
   439
    INFO( "Save state written to %s", filename );
nkeynes@422
   440
    return 0;
nkeynes@15
   441
}
nkeynes@15
   442
.