Search
lxdream.org :: lxdream/src/dreamcast.c
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 (15 years ago)
permissions -rw-r--r--
last change Implement save states
file annotate diff log raw
nkeynes@18
     1
#include <errno.h>
nkeynes@2
     2
#include "dream.h"
nkeynes@2
     3
#include "mem.h"
nkeynes@11
     4
#include "aica/aica.h"
nkeynes@2
     5
#include "asic.h"
nkeynes@2
     6
#include "ide.h"
nkeynes@2
     7
#include "dreamcast.h"
nkeynes@16
     8
#include "maple.h"
nkeynes@15
     9
#include "modules.h"
nkeynes@15
    10
nkeynes@2
    11
/* Central switchboard for the system */
nkeynes@2
    12
nkeynes@15
    13
#define MAX_MODULES 32
nkeynes@15
    14
static int num_modules = 0;
nkeynes@15
    15
static int dreamcast_state = 0;
nkeynes@17
    16
static char *dreamcast_config = "DEFAULT";
nkeynes@16
    17
dreamcast_module_t modules[MAX_MODULES];
nkeynes@15
    18
nkeynes@16
    19
/**
nkeynes@16
    20
 * This function is responsible for defining how all the pieces of the
nkeynes@16
    21
 * dreamcast actually fit together. Among other things, this lets us
nkeynes@16
    22
 * (reasonably) easily redefine the structure for eg various versions of the
nkeynes@16
    23
 * Naomi.
nkeynes@16
    24
 *
nkeynes@16
    25
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    26
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    27
 */
nkeynes@15
    28
void dreamcast_configure( )
nkeynes@15
    29
{
nkeynes@16
    30
    /* Register the memory framework */
nkeynes@15
    31
    dreamcast_register_module( &mem_module );
nkeynes@16
    32
nkeynes@16
    33
    /* Setup standard memory map */
nkeynes@16
    34
    mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
nkeynes@16
    35
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@16
    36
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@16
    37
    mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
nkeynes@16
    38
    mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
nkeynes@16
    39
    mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
nkeynes@16
    40
nkeynes@16
    41
    /* Load in the rest of the core modules */
nkeynes@15
    42
    dreamcast_register_module( &sh4_module );
nkeynes@15
    43
    dreamcast_register_module( &asic_module );
nkeynes@15
    44
    dreamcast_register_module( &pvr2_module );
nkeynes@15
    45
    dreamcast_register_module( &aica_module );
nkeynes@15
    46
    dreamcast_register_module( &maple_module );
nkeynes@15
    47
    dreamcast_register_module( &ide_module );
nkeynes@16
    48
nkeynes@16
    49
    /* Attach any default maple devices, ie a pair of controllers */
nkeynes@16
    50
    maple_device_t controller1 = controller_new();
nkeynes@16
    51
    maple_device_t controller2 = controller_new();
nkeynes@16
    52
    maple_attach_device( controller1, 0, 0 );
nkeynes@16
    53
    maple_attach_device( controller2, 1, 0 );
nkeynes@15
    54
}
nkeynes@15
    55
nkeynes@15
    56
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
    57
{
nkeynes@15
    58
    modules[num_modules++] = module;
nkeynes@16
    59
    if( module->init != NULL )
nkeynes@16
    60
	module->init();
nkeynes@15
    61
}
nkeynes@15
    62
nkeynes@15
    63
nkeynes@2
    64
void dreamcast_init( void )
nkeynes@2
    65
{
nkeynes@15
    66
    dreamcast_configure();
nkeynes@2
    67
}
nkeynes@2
    68
nkeynes@2
    69
void dreamcast_reset( void )
nkeynes@2
    70
{
nkeynes@15
    71
    int i;
nkeynes@15
    72
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
    73
	if( modules[i]->reset != NULL )
nkeynes@15
    74
	    modules[i]->reset();
nkeynes@15
    75
    }
nkeynes@2
    76
}
nkeynes@2
    77
nkeynes@15
    78
void dreamcast_start( void )
nkeynes@15
    79
{
nkeynes@15
    80
    int i;
nkeynes@15
    81
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
    82
	if( modules[i]->start != NULL )
nkeynes@15
    83
	    modules[i]->start();
nkeynes@15
    84
    }
nkeynes@15
    85
}
nkeynes@2
    86
void dreamcast_stop( void )
nkeynes@2
    87
{
nkeynes@15
    88
    int i;
nkeynes@15
    89
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
    90
	if( modules[i]->stop != NULL )
nkeynes@15
    91
	    modules[i]->stop();
nkeynes@15
    92
    }
nkeynes@2
    93
}
nkeynes@15
    94
nkeynes@15
    95
struct save_state_header {
nkeynes@15
    96
    char magic[16];
nkeynes@15
    97
    uint32_t version;
nkeynes@17
    98
    uint32_t module_count;
nkeynes@15
    99
};
nkeynes@15
   100
nkeynes@17
   101
nkeynes@18
   102
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   103
{
nkeynes@17
   104
    int i,j;
nkeynes@17
   105
    uint32_t count, len;
nkeynes@17
   106
    int have_read[MAX_MODULES];
nkeynes@17
   107
    char tmp[64];
nkeynes@15
   108
    struct save_state_header header;
nkeynes@18
   109
    FILE *f;
nkeynes@18
   110
nkeynes@18
   111
    f = fopen( filename, "r" );
nkeynes@18
   112
    if( f == NULL ) return errno;
nkeynes@15
   113
nkeynes@15
   114
    fread( &header, sizeof(header), 1, f );
nkeynes@15
   115
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@15
   116
	ERROR( "Not a DreamOn save state file" );
nkeynes@17
   117
	return 1;
nkeynes@15
   118
    }
nkeynes@15
   119
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@15
   120
	ERROR( "DreamOn save state version not supported" );
nkeynes@17
   121
	return 1;
nkeynes@17
   122
    }
nkeynes@18
   123
    if( header.module_count > MAX_MODULES ) {
nkeynes@18
   124
	ERROR( "DreamOn save state is corrupted (bad module count)" );
nkeynes@17
   125
	return 1;
nkeynes@17
   126
    }
nkeynes@17
   127
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@17
   128
	have_read[i] = 0;
nkeynes@15
   129
    }
nkeynes@15
   130
nkeynes@18
   131
    for( i=0; i<header.module_count; i++ ) {
nkeynes@17
   132
	fread(tmp, 4, 1, f );
nkeynes@18
   133
	if( strncmp(tmp, "BLCK", 4) != 0 ) {
nkeynes@18
   134
	    ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
nkeynes@17
   135
	    return 2;
nkeynes@17
   136
	}
nkeynes@17
   137
	len = fread_string(tmp, sizeof(tmp), f );
nkeynes@17
   138
	if( len > 64 || len < 1 ) {
nkeynes@18
   139
	    ERROR( "DreamOn save state is corrupted (bad string)" );
nkeynes@17
   140
	    return 2;
nkeynes@17
   141
	}
nkeynes@17
   142
	
nkeynes@17
   143
	/* Find the matching module by name */
nkeynes@17
   144
	for( j=0; j<num_modules; j++ ) {
nkeynes@17
   145
	    if( strcmp(modules[j]->name,tmp) == 0 ) {
nkeynes@17
   146
		have_read[j] = 1;
nkeynes@17
   147
		if( modules[j]->load == NULL ) {
nkeynes@18
   148
		    ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
nkeynes@17
   149
		    return 2;
nkeynes@17
   150
		} else if( modules[j]->load(f) != 0 ) {
nkeynes@18
   151
		    ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
nkeynes@17
   152
		    return 2;
nkeynes@17
   153
		}
nkeynes@17
   154
		break;
nkeynes@17
   155
	    }
nkeynes@17
   156
	}
nkeynes@17
   157
	if( j == num_modules ) {
nkeynes@17
   158
	    ERROR( "DreamOn save state contains unrecognized section" );
nkeynes@17
   159
	    return 2;
nkeynes@17
   160
	}
nkeynes@17
   161
    }
nkeynes@17
   162
    
nkeynes@17
   163
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   164
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   165
     * care about its state).
nkeynes@17
   166
     */
nkeynes@17
   167
    for( j=0; j<num_modules; j++ ) {
nkeynes@17
   168
	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@17
   169
	    modules[j]->reset();
nkeynes@17
   170
	}
nkeynes@15
   171
    }
nkeynes@18
   172
    fclose(f);
nkeynes@18
   173
    INFO( "Save state read from %s", filename );
nkeynes@15
   174
}
nkeynes@15
   175
nkeynes@18
   176
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   177
{
nkeynes@15
   178
    int i;
nkeynes@18
   179
    FILE *f;
nkeynes@15
   180
    struct save_state_header header;
nkeynes@15
   181
    
nkeynes@18
   182
    f = fopen( filename, "w" );
nkeynes@18
   183
    if( f == NULL )
nkeynes@18
   184
	return errno;
nkeynes@15
   185
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   186
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   187
    header.module_count = 0;
nkeynes@18
   188
nkeynes@18
   189
    for( i=0; i<num_modules; i++ ) {
nkeynes@18
   190
	if( modules[i]->save != NULL )
nkeynes@18
   191
	    header.module_count++;
nkeynes@18
   192
    }
nkeynes@15
   193
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   194
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   195
	if( modules[i]->save != NULL ) {
nkeynes@17
   196
	    fwrite( "BLCK", 4, 1, f );
nkeynes@17
   197
	    fwrite_string( modules[i]->name, f );
nkeynes@15
   198
	    modules[i]->save(f);
nkeynes@15
   199
	}
nkeynes@15
   200
    }
nkeynes@18
   201
    fclose( f );
nkeynes@18
   202
    INFO( "Save state written to %s", filename );
nkeynes@15
   203
}
nkeynes@15
   204
.