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