filename | src/dreamcast.c |
changeset | 167:71c0cc416a64 |
prev | 146:f91fa34ab219 |
next | 180:e6dcf9b65658 |
author | nkeynes |
date | Mon Jun 19 11:00:42 2006 +0000 (17 years ago) |
permissions | -rw-r--r-- |
last change | Add config value retrieval for pathnames Implement default path for disc + save state loaders Dump disc ID when mounting a CD |
view | annotate | diff | log | raw |
1 /**
2 * $Id: dreamcast.c,v 1.17 2006-06-19 11:00:40 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...
6 *
7 * Copyright (c) 2005 Nathan Keynes.
8 *
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.
13 *
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.
18 */
20 #include <errno.h>
21 #include <glib/gstrfuncs.h>
22 #include "dream.h"
23 #include "mem.h"
24 #include "aica/aica.h"
25 #include "asic.h"
26 #include "dreamcast.h"
27 #include "gdrom/ide.h"
28 #include "maple/maple.h"
30 /**
31 * Current state of the DC virtual machine
32 */
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];
45 /**
46 * The unknown module is used for logging files without an actual module
47 * declaration
48 */
49 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
50 NULL, NULL, NULL };
52 /**
53 * This function is responsible for defining how all the pieces of the
54 * dreamcast actually fit together.
55 *
56 * Note currently the locations of the various MMIO pages are hard coded in
57 * the MMIO definitions - they should probably be moved here.
58 */
59 void dreamcast_configure( )
60 {
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( dreamcast_get_config_value(CONFIG_BIOS_PATH),
70 0x00000000, 0x00200000, 0x89f2b1a1 );
71 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
72 mem_load_block( dreamcast_get_config_value(CONFIG_FLASH_PATH),
73 0x00200000, 0x00020000 );
75 /* Load in the rest of the core modules */
76 dreamcast_register_module( &sh4_module );
77 dreamcast_register_module( &asic_module );
78 dreamcast_register_module( &pvr2_module );
79 dreamcast_register_module( &aica_module );
80 dreamcast_register_module( &maple_module );
81 dreamcast_register_module( &ide_module );
82 }
84 /**
85 * Constructs a system configuration for the AICA in standalone mode,
86 * ie sound chip only.
87 */
88 void dreamcast_configure_aica_only( )
89 {
90 dreamcast_register_module( &mem_module );
91 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
92 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
93 dreamcast_register_module( &aica_module );
94 aica_enable();
95 dreamcast_state = STATE_STOPPED;
96 }
98 void dreamcast_register_module( dreamcast_module_t module )
99 {
100 modules[num_modules++] = module;
101 if( module->init != NULL )
102 module->init();
103 }
106 void dreamcast_init( void )
107 {
108 dreamcast_configure();
109 dreamcast_state = STATE_STOPPED;
110 }
112 void dreamcast_reset( void )
113 {
114 int i;
115 for( i=0; i<num_modules; i++ ) {
116 if( modules[i]->reset != NULL )
117 modules[i]->reset();
118 }
119 }
121 void dreamcast_run( void )
122 {
123 int i;
124 if( dreamcast_state != STATE_RUNNING ) {
125 for( i=0; i<num_modules; i++ ) {
126 if( modules[i]->start != NULL )
127 modules[i]->start();
128 }
129 }
130 dreamcast_state = STATE_RUNNING;
131 while( dreamcast_state == STATE_RUNNING ) {
132 int time_to_run = timeslice_length;
133 for( i=0; i<num_modules; i++ ) {
134 if( modules[i]->run_time_slice != NULL )
135 time_to_run = modules[i]->run_time_slice( time_to_run );
136 }
138 }
140 for( i=0; i<num_modules; i++ ) {
141 if( modules[i]->stop != NULL )
142 modules[i]->stop();
143 }
144 dreamcast_state = STATE_STOPPED;
145 }
147 void dreamcast_stop( void )
148 {
149 if( dreamcast_state == STATE_RUNNING )
150 dreamcast_state = STATE_STOPPING;
151 }
153 gboolean dreamcast_is_running( void )
154 {
155 return dreamcast_state == STATE_RUNNING;
156 }
158 /***************************** User Configuration **************************/
161 static struct dreamcast_config_entry global_config[] =
162 {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
163 { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
164 { "default path", CONFIG_TYPE_PATH, "." },
165 { "save path", CONFIG_TYPE_PATH, "save" },
166 { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
167 { NULL, CONFIG_TYPE_NONE }};
169 static struct dreamcast_config_entry serial_config[] =
170 {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
171 { NULL, CONFIG_TYPE_NONE }};
173 struct dreamcast_config_group dreamcast_config_root[] =
174 {{ "global", global_config },
175 { "controllers", NULL },
176 { "serial", serial_config },
177 { NULL, CONFIG_TYPE_NONE }};
179 void dreamcast_set_default_config( )
180 {
181 struct dreamcast_config_group *group = dreamcast_config_root;
182 while( group->key != NULL ) {
183 struct dreamcast_config_entry *param = group->params;
184 if( param != NULL ) {
185 while( param->key != NULL ) {
186 if( param->value != param->default_value ) {
187 if( param->value != NULL )
188 free( param->value );
189 param->value = (gchar *)param->default_value;
190 }
191 param++;
192 }
193 }
194 group++;
195 }
196 maple_detach_all();
197 }
199 const gchar *dreamcast_get_config_value( int key )
200 {
201 return global_config[key].value;
202 }
204 gboolean dreamcast_load_config( const gchar *filename )
205 {
206 FILE *f = fopen(filename, "ro");
207 gboolean result;
209 if( f == NULL ) {
210 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
211 return FALSE;
212 }
214 result = dreamcast_load_config_stream( f );
215 fclose(f);
216 return result;
217 }
219 gboolean dreamcast_load_config_stream( FILE *f )
220 {
222 char buf[512], *p;
223 int maple_device = -1, maple_subdevice = -1;
224 struct dreamcast_config_group devgroup;
225 struct dreamcast_config_group *group = NULL;
226 maple_device_t device = NULL;
227 dreamcast_set_default_config();
229 while( fgets( buf, sizeof(buf), f ) != NULL ) {
230 g_strstrip(buf);
231 if( buf[0] == '#' )
232 continue;
233 if( *buf == '[' ) {
234 char *p = strchr(buf, ']');
235 if( p != NULL ) {
236 struct dreamcast_config_group *tmp_group;
237 maple_device = maple_subdevice = -1;
238 *p = '\0';
239 g_strstrip(buf+1);
240 tmp_group = &dreamcast_config_root[0];
241 while( tmp_group->key != NULL ) {
242 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
243 group = tmp_group;
244 break;
245 }
246 tmp_group++;
247 }
248 }
249 } else if( group != NULL ) {
250 char *value = strchr( buf, '=' );
251 if( value != NULL ) {
252 struct dreamcast_config_entry *param = group->params;
253 *value = '\0';
254 value++;
255 g_strstrip(buf);
256 g_strstrip(value);
257 if( strcmp(group->key,"controllers") == 0 ) {
258 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
259 maple_device = strtoul( buf+7, NULL, 0 );
260 if( maple_device < 0 || maple_device > 3 ) {
261 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
262 continue;
263 }
264 maple_subdevice = 0;
265 device = maple_new_device( value );
266 if( device == NULL ) {
267 ERROR( "Unrecognized device '%s'", value );
268 } else {
269 devgroup.key = "controllers";
270 devgroup.params = maple_get_device_config(device);
271 maple_attach_device( device, maple_device, maple_subdevice );
272 group = &devgroup;
273 }
274 continue;
275 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
276 maple_subdevice = strtoul( buf+10, NULL, 0 );
277 if( maple_device == -1 ) {
278 ERROR( "Subdevice not allowed without primary device" );
279 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
280 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
281 } else if( (device = maple_new_device(value)) == NULL ) {
282 ERROR( "Unrecognized subdevice '%s'", value );
283 } else {
284 devgroup.key = "controllers";
285 devgroup.params = maple_get_device_config(device);
286 maple_attach_device( device, maple_device, maple_subdevice );
287 group = &devgroup;
288 }
289 continue;
290 }
291 }
292 while( param->key != NULL ) {
293 if( strcasecmp( param->key, buf ) == 0 ) {
294 param->value = g_strdup(value);
295 break;
296 }
297 param++;
298 }
299 }
300 }
301 }
302 return TRUE;
303 }
305 gboolean dreamcast_save_config( const gchar *filename )
306 {
307 FILE *f = fopen(filename, "wo");
308 gboolean result;
309 if( f == NULL ) {
310 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
311 return FALSE;
312 }
313 result = dreamcast_save_config_stream(f);
314 fclose(f);
315 }
317 gboolean dreamcast_save_config_stream( FILE *f )
318 {
319 struct dreamcast_config_group *group = &dreamcast_config_root[0];
321 while( group->key != NULL ) {
322 struct dreamcast_config_entry *entry = group->params;
323 fprintf( f, "[%s]\n", group->key );
325 if( entry != NULL ) {
326 while( entry->key != NULL ) {
327 fprintf( f, "%s = %s\n", entry->key, entry->value );
328 entry++;
329 }
330 } else if( strcmp(group->key, "controllers") == 0 ) {
331 int i,j;
332 for( i=0; i<4; i++ ) {
333 for( j=0; j<6; j++ ) {
334 maple_device_t dev = maple_get_device( i, j );
335 if( dev != NULL ) {
336 if( j == 0 )
337 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
338 else
339 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
340 entry = dev->get_config(dev);
341 while( entry->key != NULL ) {
342 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
343 entry++;
344 }
345 }
346 }
347 }
348 }
349 fprintf( f, "\n" );
350 group++;
351 }
352 return TRUE;
353 }
355 /********************************* Save States *****************************/
357 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
358 #define DREAMCAST_SAVE_VERSION 0x00010000
360 struct save_state_header {
361 char magic[16];
362 uint32_t version;
363 uint32_t module_count;
364 };
366 int dreamcast_load_state( const gchar *filename )
367 {
368 int i,j;
369 uint32_t count, len;
370 int have_read[MAX_MODULES];
371 char tmp[64];
372 struct save_state_header header;
373 FILE *f;
375 f = fopen( filename, "r" );
376 if( f == NULL ) return errno;
378 fread( &header, sizeof(header), 1, f );
379 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
380 ERROR( "Not a DreamOn save state file" );
381 return 1;
382 }
383 if( header.version != DREAMCAST_SAVE_VERSION ) {
384 ERROR( "DreamOn save state version not supported" );
385 return 1;
386 }
387 if( header.module_count > MAX_MODULES ) {
388 ERROR( "DreamOn save state is corrupted (bad module count)" );
389 return 1;
390 }
391 for( i=0; i<MAX_MODULES; i++ ) {
392 have_read[i] = 0;
393 }
395 for( i=0; i<header.module_count; i++ ) {
396 fread(tmp, 4, 1, f );
397 if( strncmp(tmp, "BLCK", 4) != 0 ) {
398 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
399 return 2;
400 }
401 len = fread_string(tmp, sizeof(tmp), f );
402 if( len > 64 || len < 1 ) {
403 ERROR( "DreamOn save state is corrupted (bad string)" );
404 return 2;
405 }
407 /* Find the matching module by name */
408 for( j=0; j<num_modules; j++ ) {
409 if( strcmp(modules[j]->name,tmp) == 0 ) {
410 have_read[j] = 1;
411 if( modules[j]->load == NULL ) {
412 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
413 return 2;
414 } else if( modules[j]->load(f) != 0 ) {
415 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
416 return 2;
417 }
418 break;
419 }
420 }
421 if( j == num_modules ) {
422 ERROR( "DreamOn save state contains unrecognized section" );
423 return 2;
424 }
425 }
427 /* Any modules that we didn't load - reset to the default state.
428 * (ie it's not an error to skip a module if you don't actually
429 * care about its state).
430 */
431 for( j=0; j<num_modules; j++ ) {
432 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
433 modules[j]->reset();
434 }
435 }
436 fclose(f);
437 INFO( "Save state read from %s", filename );
438 }
440 int dreamcast_save_state( const gchar *filename )
441 {
442 int i;
443 FILE *f;
444 struct save_state_header header;
446 f = fopen( filename, "w" );
447 if( f == NULL )
448 return errno;
449 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
450 header.version = DREAMCAST_SAVE_VERSION;
451 header.module_count = 0;
453 for( i=0; i<num_modules; i++ ) {
454 if( modules[i]->save != NULL )
455 header.module_count++;
456 }
457 fwrite( &header, sizeof(header), 1, f );
458 for( i=0; i<num_modules; i++ ) {
459 if( modules[i]->save != NULL ) {
460 fwrite( "BLCK", 4, 1, f );
461 fwrite_string( modules[i]->name, f );
462 modules[i]->save(f);
463 }
464 }
465 fclose( f );
466 INFO( "Save state written to %s", filename );
467 }
.