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