filename | src/dreamcast.c |
changeset | 144:7f0714e89aaa |
prev | 89:939afb9f0f98 |
next | 146:f91fa34ab219 |
author | nkeynes |
date | Mon May 15 08:28:52 2006 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Rename video_driver to display_driver Add input source to display Implement configuration file support Hook controllers up to configuration |
view | annotate | diff | log | raw |
1 /**
2 * $Id: dreamcast.c,v 1.15 2006-05-15 08:28:48 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_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
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_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
72 /* Load in the rest of the core modules */
73 dreamcast_register_module( &sh4_module );
74 dreamcast_register_module( &asic_module );
75 dreamcast_register_module( &pvr2_module );
76 dreamcast_register_module( &aica_module );
77 dreamcast_register_module( &maple_module );
78 dreamcast_register_module( &ide_module );
80 /* Attach any default maple devices, ie a pair of controllers */
81 /*
82 maple_device_t controller1 = maple_new_device("Sega Controller");
83 maple_device_t controller2 = maple_new_device("Sega Controller");
84 maple_attach_device( controller1, 0, 0 );
85 maple_attach_device( controller2, 1, 0 );
86 */
87 }
89 /**
90 * Constructs a system configuration for the AICA in standalone mode,
91 * ie sound chip only.
92 */
93 void dreamcast_configure_aica_only( )
94 {
95 dreamcast_register_module( &mem_module );
96 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
97 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
98 dreamcast_register_module( &aica_module );
99 aica_enable();
100 dreamcast_state = STATE_STOPPED;
101 }
103 void dreamcast_register_module( dreamcast_module_t module )
104 {
105 modules[num_modules++] = module;
106 if( module->init != NULL )
107 module->init();
108 }
111 void dreamcast_init( void )
112 {
113 dreamcast_configure();
114 dreamcast_state = STATE_STOPPED;
115 }
117 void dreamcast_reset( void )
118 {
119 int i;
120 for( i=0; i<num_modules; i++ ) {
121 if( modules[i]->reset != NULL )
122 modules[i]->reset();
123 }
124 }
126 void dreamcast_run( void )
127 {
128 int i;
129 if( dreamcast_state != STATE_RUNNING ) {
130 for( i=0; i<num_modules; i++ ) {
131 if( modules[i]->start != NULL )
132 modules[i]->start();
133 }
134 }
135 dreamcast_state = STATE_RUNNING;
136 while( dreamcast_state == STATE_RUNNING ) {
137 int time_to_run = timeslice_length;
138 for( i=0; i<num_modules; i++ ) {
139 if( modules[i]->run_time_slice != NULL )
140 time_to_run = modules[i]->run_time_slice( time_to_run );
141 }
143 }
145 for( i=0; i<num_modules; i++ ) {
146 if( modules[i]->stop != NULL )
147 modules[i]->stop();
148 }
149 dreamcast_state = STATE_STOPPED;
150 }
152 void dreamcast_stop( void )
153 {
154 if( dreamcast_state == STATE_RUNNING )
155 dreamcast_state = STATE_STOPPING;
156 }
158 gboolean dreamcast_is_running( void )
159 {
160 return dreamcast_state == STATE_RUNNING;
161 }
163 /***************************** User Configuration **************************/
165 static struct dreamcast_config_entry global_config[] =
166 {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
167 { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
168 { "default path", CONFIG_TYPE_PATH, "." },
169 { "save path", CONFIG_TYPE_PATH, "save" },
170 { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
171 { NULL, CONFIG_TYPE_NONE }};
173 static struct dreamcast_config_entry serial_config[] =
174 {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
175 { NULL, CONFIG_TYPE_NONE }};
177 struct dreamcast_config_group dreamcast_config_root[] =
178 {{ "global", global_config },
179 { "controllers", NULL },
180 { "serial", serial_config },
181 { NULL, CONFIG_TYPE_NONE }};
183 void dreamcast_set_default_config( )
184 {
185 struct dreamcast_config_group *group = dreamcast_config_root;
186 while( group->key != NULL ) {
187 struct dreamcast_config_entry *param = group->params;
188 if( param != NULL ) {
189 while( param->key != NULL ) {
190 if( param->value != param->default_value ) {
191 if( param->value != NULL )
192 free( param->value );
193 param->value = (gchar *)param->default_value;
194 }
195 param++;
196 }
197 }
198 group++;
199 }
200 maple_detach_all();
201 }
203 gboolean dreamcast_load_config( const gchar *filename )
204 {
205 FILE *f = fopen(filename, "ro");
206 gboolean result;
208 if( f == NULL ) {
209 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
210 return FALSE;
211 }
213 result = dreamcast_load_config_stream( f );
214 fclose(f);
215 return result;
216 }
218 gboolean dreamcast_load_config_stream( FILE *f )
219 {
221 char buf[512], *p;
222 int maple_device = -1, maple_subdevice = -1;
223 struct dreamcast_config_group devgroup;
224 struct dreamcast_config_group *group = NULL;
225 maple_device_t device = NULL;
226 dreamcast_set_default_config();
228 while( fgets( buf, sizeof(buf), f ) != NULL ) {
229 g_strstrip(buf);
230 if( buf[0] == '#' )
231 continue;
232 if( *buf == '[' ) {
233 char *p = strchr(buf, ']');
234 if( p != NULL ) {
235 struct dreamcast_config_group *tmp_group;
236 maple_device = maple_subdevice = -1;
237 *p = '\0';
238 g_strstrip(buf+1);
239 tmp_group = &dreamcast_config_root[0];
240 while( tmp_group->key != NULL ) {
241 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
242 group = tmp_group;
243 break;
244 }
245 tmp_group++;
246 }
247 }
248 } else if( group != NULL ) {
249 char *value = strchr( buf, '=' );
250 if( value != NULL ) {
251 struct dreamcast_config_entry *param = group->params;
252 *value = '\0';
253 value++;
254 g_strstrip(buf);
255 g_strstrip(value);
256 if( strcmp(group->key,"controllers") == 0 ) {
257 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
258 maple_device = strtoul( buf+7, NULL, 0 );
259 if( maple_device < 0 || maple_device > 3 ) {
260 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
261 continue;
262 }
263 maple_subdevice = 0;
264 device = maple_new_device( value );
265 if( device == NULL ) {
266 ERROR( "Unrecognized device '%s'", value );
267 } else {
268 devgroup.key = "controllers";
269 devgroup.params = maple_get_device_config(device);
270 maple_attach_device( device, maple_device, maple_subdevice );
271 group = &devgroup;
272 }
273 continue;
274 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
275 maple_subdevice = strtoul( buf+10, NULL, 0 );
276 if( maple_device == -1 ) {
277 ERROR( "Subdevice not allowed without primary device" );
278 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
279 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
280 } else if( (device = maple_new_device(value)) == NULL ) {
281 ERROR( "Unrecognized subdevice '%s'", value );
282 } else {
283 devgroup.key = "controllers";
284 devgroup.params = maple_get_device_config(device);
285 maple_attach_device( device, maple_device, maple_subdevice );
286 group = &devgroup;
287 }
288 continue;
289 }
290 }
291 while( param->key != NULL ) {
292 if( strcasecmp( param->key, buf ) == 0 ) {
293 param->value = g_strdup(value);
294 break;
295 }
296 param++;
297 }
298 }
299 }
300 }
301 return TRUE;
302 }
304 gboolean dreamcast_save_config( const gchar *filename )
305 {
306 FILE *f = fopen(filename, "wo");
307 gboolean result;
308 if( f == NULL ) {
309 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
310 return FALSE;
311 }
312 result = dreamcast_save_config_stream(f);
313 fclose(f);
314 }
316 gboolean dreamcast_save_config_stream( FILE *f )
317 {
318 struct dreamcast_config_group *group = &dreamcast_config_root[0];
320 while( group->key != NULL ) {
321 struct dreamcast_config_entry *entry = group->params;
322 fprintf( f, "[%s]\n", group->key );
324 if( entry != NULL ) {
325 while( entry->key != NULL ) {
326 fprintf( f, "%s = %s\n", entry->key, entry->value );
327 entry++;
328 }
329 } else if( strcmp(group->key, "controllers") == 0 ) {
330 int i,j;
331 for( i=0; i<4; i++ ) {
332 for( j=0; j<6; j++ ) {
333 maple_device_t dev = maple_get_device( i, j );
334 if( dev != NULL ) {
335 if( j == 0 )
336 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
337 else
338 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
339 entry = dev->get_config(dev);
340 while( entry->key != NULL ) {
341 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
342 entry++;
343 }
344 }
345 }
346 }
347 }
348 fprintf( f, "\n" );
349 group++;
350 }
351 return TRUE;
352 }
354 /********************************* Save States *****************************/
356 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
357 #define DREAMCAST_SAVE_VERSION 0x00010000
359 struct save_state_header {
360 char magic[16];
361 uint32_t version;
362 uint32_t module_count;
363 };
365 int dreamcast_load_state( const gchar *filename )
366 {
367 int i,j;
368 uint32_t count, len;
369 int have_read[MAX_MODULES];
370 char tmp[64];
371 struct save_state_header header;
372 FILE *f;
374 f = fopen( filename, "r" );
375 if( f == NULL ) return errno;
377 fread( &header, sizeof(header), 1, f );
378 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
379 ERROR( "Not a DreamOn save state file" );
380 return 1;
381 }
382 if( header.version != DREAMCAST_SAVE_VERSION ) {
383 ERROR( "DreamOn save state version not supported" );
384 return 1;
385 }
386 if( header.module_count > MAX_MODULES ) {
387 ERROR( "DreamOn save state is corrupted (bad module count)" );
388 return 1;
389 }
390 for( i=0; i<MAX_MODULES; i++ ) {
391 have_read[i] = 0;
392 }
394 for( i=0; i<header.module_count; i++ ) {
395 fread(tmp, 4, 1, f );
396 if( strncmp(tmp, "BLCK", 4) != 0 ) {
397 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
398 return 2;
399 }
400 len = fread_string(tmp, sizeof(tmp), f );
401 if( len > 64 || len < 1 ) {
402 ERROR( "DreamOn save state is corrupted (bad string)" );
403 return 2;
404 }
406 /* Find the matching module by name */
407 for( j=0; j<num_modules; j++ ) {
408 if( strcmp(modules[j]->name,tmp) == 0 ) {
409 have_read[j] = 1;
410 if( modules[j]->load == NULL ) {
411 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
412 return 2;
413 } else if( modules[j]->load(f) != 0 ) {
414 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
415 return 2;
416 }
417 break;
418 }
419 }
420 if( j == num_modules ) {
421 ERROR( "DreamOn save state contains unrecognized section" );
422 return 2;
423 }
424 }
426 /* Any modules that we didn't load - reset to the default state.
427 * (ie it's not an error to skip a module if you don't actually
428 * care about its state).
429 */
430 for( j=0; j<num_modules; j++ ) {
431 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
432 modules[j]->reset();
433 }
434 }
435 fclose(f);
436 INFO( "Save state read from %s", filename );
437 }
439 int dreamcast_save_state( const gchar *filename )
440 {
441 int i;
442 FILE *f;
443 struct save_state_header header;
445 f = fopen( filename, "w" );
446 if( f == NULL )
447 return errno;
448 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
449 header.version = DREAMCAST_SAVE_VERSION;
450 header.module_count = 0;
452 for( i=0; i<num_modules; i++ ) {
453 if( modules[i]->save != NULL )
454 header.module_count++;
455 }
456 fwrite( &header, sizeof(header), 1, f );
457 for( i=0; i<num_modules; i++ ) {
458 if( modules[i]->save != NULL ) {
459 fwrite( "BLCK", 4, 1, f );
460 fwrite_string( modules[i]->name, f );
461 modules[i]->save(f);
462 }
463 }
464 fclose( f );
465 INFO( "Save state written to %s", filename );
466 }
.