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];
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
25 * Note currently the locations of the various MMIO pages are hard coded in
26 * the MMIO definitions - they should probably be moved here.
28 void dreamcast_configure( )
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 );
56 void dreamcast_register_module( dreamcast_module_t module )
58 modules[num_modules++] = module;
59 if( module->init != NULL )
64 void dreamcast_init( void )
66 dreamcast_configure();
69 void dreamcast_reset( void )
72 for( i=0; i<num_modules; i++ ) {
73 if( modules[i]->reset != NULL )
78 void dreamcast_start( void )
81 for( i=0; i<num_modules; i++ ) {
82 if( modules[i]->start != NULL )
86 void dreamcast_stop( void )
89 for( i=0; i<num_modules; i++ ) {
90 if( modules[i]->stop != NULL )
95 struct save_state_header {
98 uint32_t module_count;
102 int dreamcast_load_state( const gchar *filename )
106 int have_read[MAX_MODULES];
108 struct save_state_header header;
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" );
119 if( header.version != DREAMCAST_SAVE_VERSION ) {
120 ERROR( "DreamOn save state version not supported" );
123 if( header.module_count > MAX_MODULES ) {
124 ERROR( "DreamOn save state is corrupted (bad module count)" );
127 for( i=0; i<MAX_MODULES; i++ ) {
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 );
137 len = fread_string(tmp, sizeof(tmp), f );
138 if( len > 64 || len < 1 ) {
139 ERROR( "DreamOn save state is corrupted (bad string)" );
143 /* Find the matching module by name */
144 for( j=0; j<num_modules; j++ ) {
145 if( strcmp(modules[j]->name,tmp) == 0 ) {
147 if( modules[j]->load == NULL ) {
148 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
150 } else if( modules[j]->load(f) != 0 ) {
151 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
157 if( j == num_modules ) {
158 ERROR( "DreamOn save state contains unrecognized section" );
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).
167 for( j=0; j<num_modules; j++ ) {
168 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
173 INFO( "Save state read from %s", filename );
176 int dreamcast_save_state( const gchar *filename )
180 struct save_state_header header;
182 f = fopen( filename, "w" );
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++;
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 );
202 INFO( "Save state written to %s", filename );
.