2 * $Id: dreamcast.c,v 1.16 2006-05-20 02:38:58 nkeynes Exp $
3 * Central switchboard for the system. This pulls all the individual modules
4 * together into some kind of coherent structure. This is also where you'd
5 * add Naomi support, if I ever get a board to play with...
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <glib/gstrfuncs.h>
24 #include "aica/aica.h"
26 #include "dreamcast.h"
27 #include "gdrom/ide.h"
28 #include "maple/maple.h"
31 * Current state of the DC virtual machine
33 #define STATE_UNINIT 0
34 #define STATE_RUNNING 1
35 #define STATE_STOPPING 2
36 #define STATE_STOPPED 3
37 static volatile int dreamcast_state = STATE_UNINIT;
38 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
39 static char *dreamcast_config = "DEFAULT";
41 #define MAX_MODULES 32
42 static int num_modules = 0;
43 dreamcast_module_t modules[MAX_MODULES];
46 * The unknown module is used for logging files without an actual module
49 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
53 * This function is responsible for defining how all the pieces of the
54 * dreamcast actually fit together.
56 * Note currently the locations of the various MMIO pages are hard coded in
57 * the MMIO definitions - they should probably be moved here.
59 void dreamcast_configure( )
61 /* Register the memory framework */
62 dreamcast_register_module( &mem_module );
64 /* Setup standard memory map */
65 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
66 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
67 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
68 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
69 mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
70 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
71 mem_load_block( "../bios/dcflash.rom", 0x00200000, 0x00020000 );
72 // mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
74 /* Load in the rest of the core modules */
75 dreamcast_register_module( &sh4_module );
76 dreamcast_register_module( &asic_module );
77 dreamcast_register_module( &pvr2_module );
78 dreamcast_register_module( &aica_module );
79 dreamcast_register_module( &maple_module );
80 dreamcast_register_module( &ide_module );
82 /* Attach any default maple devices, ie a pair of controllers */
84 maple_device_t controller1 = maple_new_device("Sega Controller");
85 maple_device_t controller2 = maple_new_device("Sega Controller");
86 maple_attach_device( controller1, 0, 0 );
87 maple_attach_device( controller2, 1, 0 );
92 * Constructs a system configuration for the AICA in standalone mode,
95 void dreamcast_configure_aica_only( )
97 dreamcast_register_module( &mem_module );
98 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
99 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
100 dreamcast_register_module( &aica_module );
102 dreamcast_state = STATE_STOPPED;
105 void dreamcast_register_module( dreamcast_module_t module )
107 modules[num_modules++] = module;
108 if( module->init != NULL )
113 void dreamcast_init( void )
115 dreamcast_configure();
116 dreamcast_state = STATE_STOPPED;
119 void dreamcast_reset( void )
122 for( i=0; i<num_modules; i++ ) {
123 if( modules[i]->reset != NULL )
128 void dreamcast_run( void )
131 if( dreamcast_state != STATE_RUNNING ) {
132 for( i=0; i<num_modules; i++ ) {
133 if( modules[i]->start != NULL )
137 dreamcast_state = STATE_RUNNING;
138 while( dreamcast_state == STATE_RUNNING ) {
139 int time_to_run = timeslice_length;
140 for( i=0; i<num_modules; i++ ) {
141 if( modules[i]->run_time_slice != NULL )
142 time_to_run = modules[i]->run_time_slice( time_to_run );
147 for( i=0; i<num_modules; i++ ) {
148 if( modules[i]->stop != NULL )
151 dreamcast_state = STATE_STOPPED;
154 void dreamcast_stop( void )
156 if( dreamcast_state == STATE_RUNNING )
157 dreamcast_state = STATE_STOPPING;
160 gboolean dreamcast_is_running( void )
162 return dreamcast_state == STATE_RUNNING;
165 /***************************** User Configuration **************************/
167 static struct dreamcast_config_entry global_config[] =
168 {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
169 { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
170 { "default path", CONFIG_TYPE_PATH, "." },
171 { "save path", CONFIG_TYPE_PATH, "save" },
172 { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
173 { NULL, CONFIG_TYPE_NONE }};
175 static struct dreamcast_config_entry serial_config[] =
176 {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
177 { NULL, CONFIG_TYPE_NONE }};
179 struct dreamcast_config_group dreamcast_config_root[] =
180 {{ "global", global_config },
181 { "controllers", NULL },
182 { "serial", serial_config },
183 { NULL, CONFIG_TYPE_NONE }};
185 void dreamcast_set_default_config( )
187 struct dreamcast_config_group *group = dreamcast_config_root;
188 while( group->key != NULL ) {
189 struct dreamcast_config_entry *param = group->params;
190 if( param != NULL ) {
191 while( param->key != NULL ) {
192 if( param->value != param->default_value ) {
193 if( param->value != NULL )
194 free( param->value );
195 param->value = (gchar *)param->default_value;
205 gboolean dreamcast_load_config( const gchar *filename )
207 FILE *f = fopen(filename, "ro");
211 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
215 result = dreamcast_load_config_stream( f );
220 gboolean dreamcast_load_config_stream( FILE *f )
224 int maple_device = -1, maple_subdevice = -1;
225 struct dreamcast_config_group devgroup;
226 struct dreamcast_config_group *group = NULL;
227 maple_device_t device = NULL;
228 dreamcast_set_default_config();
230 while( fgets( buf, sizeof(buf), f ) != NULL ) {
235 char *p = strchr(buf, ']');
237 struct dreamcast_config_group *tmp_group;
238 maple_device = maple_subdevice = -1;
241 tmp_group = &dreamcast_config_root[0];
242 while( tmp_group->key != NULL ) {
243 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
250 } else if( group != NULL ) {
251 char *value = strchr( buf, '=' );
252 if( value != NULL ) {
253 struct dreamcast_config_entry *param = group->params;
258 if( strcmp(group->key,"controllers") == 0 ) {
259 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
260 maple_device = strtoul( buf+7, NULL, 0 );
261 if( maple_device < 0 || maple_device > 3 ) {
262 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
266 device = maple_new_device( value );
267 if( device == NULL ) {
268 ERROR( "Unrecognized device '%s'", value );
270 devgroup.key = "controllers";
271 devgroup.params = maple_get_device_config(device);
272 maple_attach_device( device, maple_device, maple_subdevice );
276 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
277 maple_subdevice = strtoul( buf+10, NULL, 0 );
278 if( maple_device == -1 ) {
279 ERROR( "Subdevice not allowed without primary device" );
280 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
281 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
282 } else if( (device = maple_new_device(value)) == NULL ) {
283 ERROR( "Unrecognized subdevice '%s'", value );
285 devgroup.key = "controllers";
286 devgroup.params = maple_get_device_config(device);
287 maple_attach_device( device, maple_device, maple_subdevice );
293 while( param->key != NULL ) {
294 if( strcasecmp( param->key, buf ) == 0 ) {
295 param->value = g_strdup(value);
306 gboolean dreamcast_save_config( const gchar *filename )
308 FILE *f = fopen(filename, "wo");
311 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
314 result = dreamcast_save_config_stream(f);
318 gboolean dreamcast_save_config_stream( FILE *f )
320 struct dreamcast_config_group *group = &dreamcast_config_root[0];
322 while( group->key != NULL ) {
323 struct dreamcast_config_entry *entry = group->params;
324 fprintf( f, "[%s]\n", group->key );
326 if( entry != NULL ) {
327 while( entry->key != NULL ) {
328 fprintf( f, "%s = %s\n", entry->key, entry->value );
331 } else if( strcmp(group->key, "controllers") == 0 ) {
333 for( i=0; i<4; i++ ) {
334 for( j=0; j<6; j++ ) {
335 maple_device_t dev = maple_get_device( i, j );
338 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
340 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
341 entry = dev->get_config(dev);
342 while( entry->key != NULL ) {
343 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
356 /********************************* Save States *****************************/
358 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
359 #define DREAMCAST_SAVE_VERSION 0x00010000
361 struct save_state_header {
364 uint32_t module_count;
367 int dreamcast_load_state( const gchar *filename )
371 int have_read[MAX_MODULES];
373 struct save_state_header header;
376 f = fopen( filename, "r" );
377 if( f == NULL ) return errno;
379 fread( &header, sizeof(header), 1, f );
380 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
381 ERROR( "Not a DreamOn save state file" );
384 if( header.version != DREAMCAST_SAVE_VERSION ) {
385 ERROR( "DreamOn save state version not supported" );
388 if( header.module_count > MAX_MODULES ) {
389 ERROR( "DreamOn save state is corrupted (bad module count)" );
392 for( i=0; i<MAX_MODULES; i++ ) {
396 for( i=0; i<header.module_count; i++ ) {
397 fread(tmp, 4, 1, f );
398 if( strncmp(tmp, "BLCK", 4) != 0 ) {
399 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
402 len = fread_string(tmp, sizeof(tmp), f );
403 if( len > 64 || len < 1 ) {
404 ERROR( "DreamOn save state is corrupted (bad string)" );
408 /* Find the matching module by name */
409 for( j=0; j<num_modules; j++ ) {
410 if( strcmp(modules[j]->name,tmp) == 0 ) {
412 if( modules[j]->load == NULL ) {
413 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
415 } else if( modules[j]->load(f) != 0 ) {
416 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
422 if( j == num_modules ) {
423 ERROR( "DreamOn save state contains unrecognized section" );
428 /* Any modules that we didn't load - reset to the default state.
429 * (ie it's not an error to skip a module if you don't actually
430 * care about its state).
432 for( j=0; j<num_modules; j++ ) {
433 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
438 INFO( "Save state read from %s", filename );
441 int dreamcast_save_state( const gchar *filename )
445 struct save_state_header header;
447 f = fopen( filename, "w" );
450 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
451 header.version = DREAMCAST_SAVE_VERSION;
452 header.module_count = 0;
454 for( i=0; i<num_modules; i++ ) {
455 if( modules[i]->save != NULL )
456 header.module_count++;
458 fwrite( &header, sizeof(header), 1, f );
459 for( i=0; i<num_modules; i++ ) {
460 if( modules[i]->save != NULL ) {
461 fwrite( "BLCK", 4, 1, f );
462 fwrite_string( modules[i]->name, f );
467 INFO( "Save state written to %s", filename );
.