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