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