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