Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 929:fd8cb0c82f5f
prev892:126aa7db6162
next930:07e5b11419db
author nkeynes
date Sat Dec 20 03:01:40 2008 +0000 (12 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change First pass experiment using cached decoding.
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@929
    61
extern struct mem_region_fn mem_region_sdram;
nkeynes@929
    62
extern struct mem_region_fn mem_region_vram32;
nkeynes@929
    63
extern struct mem_region_fn mem_region_vram64;
nkeynes@929
    64
extern struct mem_region_fn mem_region_audioram;
nkeynes@929
    65
extern struct mem_region_fn mem_region_flashram;
nkeynes@929
    66
extern struct mem_region_fn mem_region_bootrom;
nkeynes@929
    67
nkeynes@35
    68
/**
nkeynes@16
    69
 * This function is responsible for defining how all the pieces of the
nkeynes@26
    70
 * dreamcast actually fit together. 
nkeynes@16
    71
 *
nkeynes@16
    72
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    73
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    74
 */
nkeynes@15
    75
void dreamcast_configure( )
nkeynes@15
    76
{
nkeynes@825
    77
    const char *bios_path = lxdream_get_config_value(CONFIG_BIOS_PATH);
nkeynes@825
    78
    const char *flash_path = lxdream_get_config_value(CONFIG_FLASH_PATH);
nkeynes@825
    79
    
nkeynes@265
    80
    dreamcast_register_module( &eventq_module );
nkeynes@16
    81
    /* Register the memory framework */
nkeynes@15
    82
    dreamcast_register_module( &mem_module );
nkeynes@16
    83
nkeynes@16
    84
    /* Setup standard memory map */
nkeynes@929
    85
    mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, &mem_region_sdram, 0x01000000, 0x0F000000 );
nkeynes@929
    86
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO, &mem_region_audioram );
nkeynes@929
    87
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH, NULL );
nkeynes@929
    88
    mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO, &mem_region_vram32 );
nkeynes@929
    89
    dreamcast_has_bios = mem_load_rom( bios_path, 0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS, &mem_region_bootrom );
nkeynes@929
    90
    mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH, &mem_region_flashram );
nkeynes@825
    91
    if( flash_path != NULL && flash_path[0] != '\0' ) {
nkeynes@825
    92
        mem_load_block( flash_path, 0x00200000, 0x00020000 );
nkeynes@825
    93
    }
nkeynes@742
    94
    dreamcast_has_flash = TRUE;
nkeynes@16
    95
nkeynes@16
    96
    /* Load in the rest of the core modules */
nkeynes@15
    97
    dreamcast_register_module( &sh4_module );
nkeynes@15
    98
    dreamcast_register_module( &asic_module );
nkeynes@15
    99
    dreamcast_register_module( &pvr2_module );
nkeynes@15
   100
    dreamcast_register_module( &aica_module );
nkeynes@15
   101
    dreamcast_register_module( &maple_module );
nkeynes@15
   102
    dreamcast_register_module( &ide_module );
nkeynes@15
   103
}
nkeynes@15
   104
nkeynes@461
   105
void dreamcast_config_changed(void)
nkeynes@461
   106
{
nkeynes@825
   107
    const char *bios_path = lxdream_get_config_value(CONFIG_BIOS_PATH);
nkeynes@825
   108
    const char *flash_path = lxdream_get_config_value(CONFIG_FLASH_PATH);
nkeynes@929
   109
    dreamcast_has_bios = mem_load_rom( bios_path, 0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS, &mem_region_bootrom );
nkeynes@825
   110
    if( flash_path != NULL && flash_path[0] != '\0' ) {
nkeynes@825
   111
        mem_load_block( flash_path, 0x00200000, 0x00020000 );
nkeynes@825
   112
    }
nkeynes@461
   113
}
nkeynes@461
   114
nkeynes@277
   115
void dreamcast_save_flash()
nkeynes@277
   116
{
nkeynes@742
   117
    if( dreamcast_has_flash ) {
nkeynes@742
   118
        const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
nkeynes@742
   119
        mem_save_block( file, 0x00200000, 0x00020000 );
nkeynes@742
   120
    }
nkeynes@277
   121
}
nkeynes@277
   122
nkeynes@71
   123
/**
nkeynes@71
   124
 * Constructs a system configuration for the AICA in standalone mode,
nkeynes@71
   125
 * ie sound chip only.
nkeynes@71
   126
 */
nkeynes@71
   127
void dreamcast_configure_aica_only( )
nkeynes@71
   128
{
nkeynes@71
   129
    dreamcast_register_module( &mem_module );
nkeynes@929
   130
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO, &mem_region_audioram );
nkeynes@929
   131
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH, NULL );
nkeynes@71
   132
    dreamcast_register_module( &aica_module );
nkeynes@89
   133
    aica_enable();
nkeynes@71
   134
    dreamcast_state = STATE_STOPPED;
nkeynes@71
   135
}
nkeynes@71
   136
nkeynes@15
   137
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
   138
{
nkeynes@15
   139
    modules[num_modules++] = module;
nkeynes@16
   140
    if( module->init != NULL )
nkeynes@736
   141
        module->init();
nkeynes@15
   142
}
nkeynes@15
   143
nkeynes@689
   144
void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
nkeynes@689
   145
{
nkeynes@689
   146
    run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
nkeynes@689
   147
}
nkeynes@689
   148
nkeynes@689
   149
void dreamcast_set_exit_on_stop( gboolean flag )
nkeynes@689
   150
{
nkeynes@689
   151
    dreamcast_exit_on_stop = flag;
nkeynes@689
   152
}
nkeynes@15
   153
nkeynes@2
   154
void dreamcast_init( void )
nkeynes@2
   155
{
nkeynes@15
   156
    dreamcast_configure();
nkeynes@23
   157
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   158
}
nkeynes@2
   159
nkeynes@2
   160
void dreamcast_reset( void )
nkeynes@2
   161
{
nkeynes@766
   162
    sh4_core_exit(CORE_EXIT_SYSRESET);
nkeynes@15
   163
    int i;
nkeynes@15
   164
    for( i=0; i<num_modules; i++ ) {
nkeynes@736
   165
        if( modules[i]->reset != NULL )
nkeynes@736
   166
            modules[i]->reset();
nkeynes@15
   167
    }
nkeynes@2
   168
}
nkeynes@2
   169
nkeynes@23
   170
void dreamcast_run( void )
nkeynes@15
   171
{
nkeynes@15
   172
    int i;
nkeynes@889
   173
    
nkeynes@889
   174
    if( !dreamcast_can_run() ) {
nkeynes@889
   175
        ERROR(_("No program is loaded, and no BIOS is configured (required to boot a CD image). To continue, either load a binary program, or set the path to your BIOS file in the Path Preferences"));
nkeynes@889
   176
        return;
nkeynes@889
   177
    }
nkeynes@889
   178
    
nkeynes@23
   179
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@689
   180
        for( i=0; i<num_modules; i++ ) {
nkeynes@689
   181
            if( modules[i]->start != NULL )
nkeynes@689
   182
                modules[i]->start();
nkeynes@689
   183
        }
nkeynes@15
   184
    }
nkeynes@839
   185
    
nkeynes@839
   186
    if( maple_should_grab() ) {
nkeynes@839
   187
        gui_set_use_grab(TRUE);
nkeynes@839
   188
    }
nkeynes@736
   189
nkeynes@23
   190
    dreamcast_state = STATE_RUNNING;
nkeynes@736
   191
nkeynes@689
   192
    if( run_time_nanosecs != 0 ) {
nkeynes@689
   193
        while( dreamcast_state == STATE_RUNNING ) {
nkeynes@689
   194
            uint32_t time_to_run = timeslice_length;
nkeynes@689
   195
            if( run_time_nanosecs < time_to_run ) {
nkeynes@689
   196
                time_to_run = (uint32_t)run_time_nanosecs;
nkeynes@689
   197
            }
nkeynes@736
   198
nkeynes@689
   199
            for( i=0; i<num_modules; i++ ) {
nkeynes@689
   200
                if( modules[i]->run_time_slice != NULL )
nkeynes@689
   201
                    time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@689
   202
            }
nkeynes@736
   203
nkeynes@689
   204
            if( run_time_nanosecs > time_to_run ) {
nkeynes@689
   205
                run_time_nanosecs -= time_to_run;
nkeynes@689
   206
            } else {
nkeynes@689
   207
                run_time_nanosecs = 0; // Finished
nkeynes@689
   208
                break;
nkeynes@689
   209
            }
nkeynes@689
   210
        }
nkeynes@689
   211
    } else {
nkeynes@689
   212
        while( dreamcast_state == STATE_RUNNING ) {
nkeynes@689
   213
            int time_to_run = timeslice_length;
nkeynes@689
   214
            for( i=0; i<num_modules; i++ ) {
nkeynes@689
   215
                if( modules[i]->run_time_slice != NULL )
nkeynes@689
   216
                    time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@689
   217
            }
nkeynes@23
   218
nkeynes@689
   219
        }
nkeynes@23
   220
    }
nkeynes@23
   221
nkeynes@839
   222
    gui_set_use_grab(FALSE);
nkeynes@839
   223
    
nkeynes@15
   224
    for( i=0; i<num_modules; i++ ) {
nkeynes@689
   225
        if( modules[i]->stop != NULL )
nkeynes@689
   226
            modules[i]->stop();
nkeynes@15
   227
    }
nkeynes@23
   228
    dreamcast_state = STATE_STOPPED;
nkeynes@736
   229
nkeynes@689
   230
    if( dreamcast_exit_on_stop ) {
nkeynes@689
   231
        dreamcast_shutdown();
nkeynes@689
   232
        exit(0);
nkeynes@372
   233
    }
nkeynes@372
   234
}
nkeynes@372
   235
nkeynes@23
   236
void dreamcast_stop( void )
nkeynes@23
   237
{
nkeynes@740
   238
    sh4_core_exit(CORE_EXIT_HALT); // returns only if not inside SH4 core
nkeynes@23
   239
    if( dreamcast_state == STATE_RUNNING )
nkeynes@736
   240
        dreamcast_state = STATE_STOPPING;
nkeynes@23
   241
}
nkeynes@17
   242
nkeynes@277
   243
void dreamcast_shutdown()
nkeynes@277
   244
{
nkeynes@727
   245
    // Don't do a dreamcast_stop - if we're calling this out of SH4 code,
nkeynes@727
   246
    // it's a shutdown-and-quit event
nkeynes@727
   247
    if( dreamcast_state == STATE_RUNNING )
nkeynes@727
   248
        dreamcast_state = STATE_STOPPING;
nkeynes@277
   249
    dreamcast_save_flash();
nkeynes@671
   250
#ifdef ENABLE_SH4STATS
nkeynes@671
   251
    sh4_stats_print(stdout);
nkeynes@671
   252
#endif
nkeynes@929
   253
    print_sh4mem_stats();
nkeynes@277
   254
}
nkeynes@277
   255
nkeynes@543
   256
void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
nkeynes@543
   257
{
nkeynes@543
   258
    if( dreamcast_program_name != NULL ) {
nkeynes@543
   259
        g_free(dreamcast_program_name);
nkeynes@543
   260
    }
nkeynes@543
   261
    dreamcast_program_name = g_strdup(name);
nkeynes@543
   262
    dreamcast_entry_point = entry_point;
nkeynes@543
   263
    sh4_set_pc(entry_point);
nkeynes@586
   264
    bios_install();
nkeynes@543
   265
    dcload_install();
nkeynes@543
   266
    gui_update_state();
nkeynes@543
   267
}
nkeynes@543
   268
nkeynes@26
   269
gboolean dreamcast_is_running( void )
nkeynes@26
   270
{
nkeynes@26
   271
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   272
}
nkeynes@26
   273
nkeynes@543
   274
gboolean dreamcast_can_run(void)
nkeynes@543
   275
{
nkeynes@543
   276
    return dreamcast_state != STATE_UNINIT &&
nkeynes@736
   277
    (dreamcast_has_bios || dreamcast_program_name != NULL);
nkeynes@543
   278
}
nkeynes@543
   279
nkeynes@26
   280
/********************************* Save States *****************************/
nkeynes@26
   281
nkeynes@26
   282
struct save_state_header {
nkeynes@26
   283
    char magic[16];
nkeynes@26
   284
    uint32_t version;
nkeynes@26
   285
    uint32_t module_count;
nkeynes@26
   286
};
nkeynes@26
   287
nkeynes@477
   288
struct chunk_header {
nkeynes@477
   289
    char marker[4]; /* Always BLCK */
nkeynes@477
   290
    char name[8]; /* Block (module) name */
nkeynes@477
   291
    uint32_t block_length;
nkeynes@477
   292
};
nkeynes@477
   293
nkeynes@477
   294
/**
nkeynes@477
   295
 * Check the save state header to ensure that it is a valid, supported
nkeynes@477
   296
 * file. 
nkeynes@477
   297
 * @return the number of blocks following, or 0 if the file is invalid.
nkeynes@477
   298
 */
nkeynes@891
   299
int dreamcast_read_save_state_header( FILE *f, char *error, int errorlen )
nkeynes@477
   300
{
nkeynes@477
   301
    struct save_state_header header;
nkeynes@477
   302
    if( fread( &header, sizeof(header), 1, f ) != 1 ) {
nkeynes@736
   303
        return 0;
nkeynes@477
   304
    }
nkeynes@477
   305
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@891
   306
    	if( error != NULL )
nkeynes@891
   307
    		snprintf( error, errorlen, _("File is not a %s save state"), APP_NAME );
nkeynes@736
   308
        return 0;
nkeynes@477
   309
    }
nkeynes@477
   310
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@891
   311
    	if( error != NULL )
nkeynes@892
   312
    		snprintf( error, errorlen, _("Unsupported %s save state version"), APP_NAME );
nkeynes@736
   313
        return 0;
nkeynes@477
   314
    }
nkeynes@477
   315
    if( header.module_count > MAX_MODULES ) {
nkeynes@891
   316
    	if( error != NULL )
nkeynes@891
   317
    		snprintf( error, errorlen, _("%s save state is corrupted (bad module count)"), APP_NAME );
nkeynes@736
   318
        return 0;
nkeynes@477
   319
    }
nkeynes@477
   320
    return header.module_count;
nkeynes@477
   321
}
nkeynes@477
   322
nkeynes@477
   323
int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
nkeynes@477
   324
{
nkeynes@477
   325
    struct chunk_header head;
nkeynes@477
   326
nkeynes@477
   327
    memcpy( head.marker, "BLCK", 4 );
nkeynes@477
   328
    memset( head.name, 0, 8 );
nkeynes@477
   329
    memcpy( head.name, name, strlen(name) );
nkeynes@477
   330
    head.block_length = length;
nkeynes@477
   331
    return fwrite( &head, sizeof(head), 1, f );
nkeynes@477
   332
}
nkeynes@477
   333
nkeynes@477
   334
nkeynes@477
   335
frame_buffer_t dreamcast_load_preview( const gchar *filename )
nkeynes@477
   336
{
nkeynes@477
   337
    int i;
nkeynes@477
   338
    FILE *f = fopen( filename, "r" );
nkeynes@477
   339
    if( f == NULL ) return NULL;
nkeynes@736
   340
nkeynes@891
   341
    int module_count = dreamcast_read_save_state_header(f, NULL, 0);
nkeynes@477
   342
    if( module_count <= 0 ) {
nkeynes@736
   343
        fclose(f);
nkeynes@736
   344
        return NULL;
nkeynes@477
   345
    }
nkeynes@477
   346
    for( i=0; i<module_count; i++ ) {
nkeynes@736
   347
        struct chunk_header head;
nkeynes@736
   348
        if( fread( &head, sizeof(head), 1, f ) != 1 ) {
nkeynes@736
   349
            fclose(f);
nkeynes@736
   350
            return NULL;
nkeynes@736
   351
        }
nkeynes@736
   352
        if( memcmp("BLCK", head.marker, 4) != 0 ) {
nkeynes@736
   353
            fclose(f);
nkeynes@736
   354
            return NULL;
nkeynes@736
   355
        }
nkeynes@477
   356
nkeynes@736
   357
        if( strcmp("PVR2", head.name) == 0 ) {
nkeynes@736
   358
            uint32_t buf_count;
nkeynes@736
   359
            int has_front;
nkeynes@736
   360
            fread( &buf_count, sizeof(buf_count), 1, f );
nkeynes@736
   361
            fread( &has_front, sizeof(has_front), 1, f );
nkeynes@736
   362
            if( buf_count != 0 && has_front ) {
nkeynes@736
   363
                frame_buffer_t result = read_png_from_stream(f);
nkeynes@736
   364
                fclose(f);
nkeynes@736
   365
                return result;
nkeynes@736
   366
            }
nkeynes@736
   367
            break;
nkeynes@736
   368
        } else {
nkeynes@736
   369
            fseek( f, head.block_length, SEEK_CUR );
nkeynes@736
   370
        }
nkeynes@477
   371
    }
nkeynes@477
   372
    return NULL;
nkeynes@477
   373
}
nkeynes@477
   374
nkeynes@18
   375
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   376
{
nkeynes@17
   377
    int i,j;
nkeynes@477
   378
    int module_count;
nkeynes@891
   379
    char error[128];
nkeynes@17
   380
    int have_read[MAX_MODULES];
nkeynes@18
   381
nkeynes@477
   382
    FILE *f = fopen( filename, "r" );
nkeynes@18
   383
    if( f == NULL ) return errno;
nkeynes@15
   384
nkeynes@891
   385
    module_count = dreamcast_read_save_state_header(f, error, sizeof(error));
nkeynes@477
   386
    if( module_count <= 0 ) {
nkeynes@891
   387
    	ERROR( error );
nkeynes@736
   388
        fclose(f);
nkeynes@736
   389
        return 1;
nkeynes@15
   390
    }
nkeynes@477
   391
nkeynes@17
   392
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@736
   393
        have_read[i] = 0;
nkeynes@15
   394
    }
nkeynes@15
   395
nkeynes@477
   396
    for( i=0; i<module_count; i++ ) {
nkeynes@736
   397
        struct chunk_header chunk;
nkeynes@736
   398
        fread( &chunk, sizeof(chunk), 1, f );
nkeynes@736
   399
        if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
nkeynes@736
   400
            ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
nkeynes@736
   401
            fclose(f);
nkeynes@736
   402
            return 2;
nkeynes@736
   403
        }
nkeynes@736
   404
nkeynes@736
   405
        /* Find the matching module by name */
nkeynes@736
   406
        for( j=0; j<num_modules; j++ ) {
nkeynes@736
   407
            if( strcmp(modules[j]->name,chunk.name) == 0 ) {
nkeynes@736
   408
                have_read[j] = 1;
nkeynes@736
   409
                if( modules[j]->load == NULL ) {
nkeynes@736
   410
                    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
nkeynes@736
   411
                    fclose(f);
nkeynes@736
   412
                    return 2;
nkeynes@736
   413
                } else if( modules[j]->load(f) != 0 ) {
nkeynes@736
   414
                    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
nkeynes@736
   415
                    fclose(f);
nkeynes@736
   416
                    return 2;
nkeynes@736
   417
                }
nkeynes@736
   418
                break;
nkeynes@736
   419
            }
nkeynes@736
   420
        }
nkeynes@736
   421
        if( j == num_modules ) {
nkeynes@736
   422
            fclose(f);
nkeynes@736
   423
            ERROR( "%s save state contains unrecognized section", APP_NAME );
nkeynes@736
   424
            return 2;
nkeynes@736
   425
        }
nkeynes@17
   426
    }
nkeynes@736
   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@736
   433
        if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@736
   434
            modules[j]->reset();
nkeynes@736
   435
        }
nkeynes@15
   436
    }
nkeynes@18
   437
    fclose(f);
nkeynes@18
   438
    INFO( "Save state read from %s", filename );
nkeynes@422
   439
    return 0;
nkeynes@15
   440
}
nkeynes@15
   441
nkeynes@18
   442
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   443
{
nkeynes@15
   444
    int i;
nkeynes@18
   445
    FILE *f;
nkeynes@15
   446
    struct save_state_header header;
nkeynes@736
   447
nkeynes@18
   448
    f = fopen( filename, "w" );
nkeynes@18
   449
    if( f == NULL )
nkeynes@736
   450
        return errno;
nkeynes@15
   451
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   452
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   453
    header.module_count = 0;
nkeynes@18
   454
nkeynes@18
   455
    for( i=0; i<num_modules; i++ ) {
nkeynes@736
   456
        if( modules[i]->save != NULL )
nkeynes@736
   457
            header.module_count++;
nkeynes@18
   458
    }
nkeynes@15
   459
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   460
    for( i=0; i<num_modules; i++ ) {
nkeynes@736
   461
        if( modules[i]->save != NULL ) {
nkeynes@736
   462
            uint32_t blocklen, posn1, posn2;
nkeynes@736
   463
            dreamcast_write_chunk_header( modules[i]->name, 0, f );
nkeynes@736
   464
            posn1 = ftell(f);
nkeynes@736
   465
            modules[i]->save(f);
nkeynes@736
   466
            posn2 = ftell(f);
nkeynes@736
   467
            blocklen = posn2 - posn1;
nkeynes@736
   468
            fseek( f, posn1-4, SEEK_SET );
nkeynes@736
   469
            fwrite( &blocklen, sizeof(blocklen), 1, f );
nkeynes@736
   470
            fseek( f, posn2, SEEK_SET );
nkeynes@736
   471
        }
nkeynes@15
   472
    }
nkeynes@18
   473
    fclose( f );
nkeynes@18
   474
    INFO( "Save state written to %s", filename );
nkeynes@422
   475
    return 0;
nkeynes@15
   476
}
nkeynes@15
   477
.