filename | src/dreamcast.c |
changeset | 146:f91fa34ab219 |
prev | 144:7f0714e89aaa |
next | 167:71c0cc416a64 |
author | nkeynes |
date | Thu Jun 15 10:25:45 2006 +0000 (17 years ago) |
permissions | -rw-r--r-- |
last change | Add P4 I/O tracing Add ability to set I/O trace by region address |
view | annotate | diff | log | raw |
1 /**
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...
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( "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 */
83 /*
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 );
88 */
89 }
91 /**
92 * Constructs a system configuration for the AICA in standalone mode,
93 * ie sound chip only.
94 */
95 void dreamcast_configure_aica_only( )
96 {
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 );
101 aica_enable();
102 dreamcast_state = STATE_STOPPED;
103 }
105 void dreamcast_register_module( dreamcast_module_t module )
106 {
107 modules[num_modules++] = module;
108 if( module->init != NULL )
109 module->init();
110 }
113 void dreamcast_init( void )
114 {
115 dreamcast_configure();
116 dreamcast_state = STATE_STOPPED;
117 }
119 void dreamcast_reset( void )
120 {
121 int i;
122 for( i=0; i<num_modules; i++ ) {
123 if( modules[i]->reset != NULL )
124 modules[i]->reset();
125 }
126 }
128 void dreamcast_run( void )
129 {
130 int i;
131 if( dreamcast_state != STATE_RUNNING ) {
132 for( i=0; i<num_modules; i++ ) {
133 if( modules[i]->start != NULL )
134 modules[i]->start();
135 }
136 }
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 );
143 }
145 }
147 for( i=0; i<num_modules; i++ ) {
148 if( modules[i]->stop != NULL )
149 modules[i]->stop();
150 }
151 dreamcast_state = STATE_STOPPED;
152 }
154 void dreamcast_stop( void )
155 {
156 if( dreamcast_state == STATE_RUNNING )
157 dreamcast_state = STATE_STOPPING;
158 }
160 gboolean dreamcast_is_running( void )
161 {
162 return dreamcast_state == STATE_RUNNING;
163 }
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( )
186 {
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;
196 }
197 param++;
198 }
199 }
200 group++;
201 }
202 maple_detach_all();
203 }
205 gboolean dreamcast_load_config( const gchar *filename )
206 {
207 FILE *f = fopen(filename, "ro");
208 gboolean result;
210 if( f == NULL ) {
211 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
212 return FALSE;
213 }
215 result = dreamcast_load_config_stream( f );
216 fclose(f);
217 return result;
218 }
220 gboolean dreamcast_load_config_stream( FILE *f )
221 {
223 char buf[512], *p;
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 ) {
231 g_strstrip(buf);
232 if( buf[0] == '#' )
233 continue;
234 if( *buf == '[' ) {
235 char *p = strchr(buf, ']');
236 if( p != NULL ) {
237 struct dreamcast_config_group *tmp_group;
238 maple_device = maple_subdevice = -1;
239 *p = '\0';
240 g_strstrip(buf+1);
241 tmp_group = &dreamcast_config_root[0];
242 while( tmp_group->key != NULL ) {
243 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
244 group = tmp_group;
245 break;
246 }
247 tmp_group++;
248 }
249 }
250 } else if( group != NULL ) {
251 char *value = strchr( buf, '=' );
252 if( value != NULL ) {
253 struct dreamcast_config_entry *param = group->params;
254 *value = '\0';
255 value++;
256 g_strstrip(buf);
257 g_strstrip(value);
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);
263 continue;
264 }
265 maple_subdevice = 0;
266 device = maple_new_device( value );
267 if( device == NULL ) {
268 ERROR( "Unrecognized device '%s'", value );
269 } else {
270 devgroup.key = "controllers";
271 devgroup.params = maple_get_device_config(device);
272 maple_attach_device( device, maple_device, maple_subdevice );
273 group = &devgroup;
274 }
275 continue;
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 );
284 } else {
285 devgroup.key = "controllers";
286 devgroup.params = maple_get_device_config(device);
287 maple_attach_device( device, maple_device, maple_subdevice );
288 group = &devgroup;
289 }
290 continue;
291 }
292 }
293 while( param->key != NULL ) {
294 if( strcasecmp( param->key, buf ) == 0 ) {
295 param->value = g_strdup(value);
296 break;
297 }
298 param++;
299 }
300 }
301 }
302 }
303 return TRUE;
304 }
306 gboolean dreamcast_save_config( const gchar *filename )
307 {
308 FILE *f = fopen(filename, "wo");
309 gboolean result;
310 if( f == NULL ) {
311 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
312 return FALSE;
313 }
314 result = dreamcast_save_config_stream(f);
315 fclose(f);
316 }
318 gboolean dreamcast_save_config_stream( FILE *f )
319 {
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 );
329 entry++;
330 }
331 } else if( strcmp(group->key, "controllers") == 0 ) {
332 int i,j;
333 for( i=0; i<4; i++ ) {
334 for( j=0; j<6; j++ ) {
335 maple_device_t dev = maple_get_device( i, j );
336 if( dev != NULL ) {
337 if( j == 0 )
338 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
339 else
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 );
344 entry++;
345 }
346 }
347 }
348 }
349 }
350 fprintf( f, "\n" );
351 group++;
352 }
353 return TRUE;
354 }
356 /********************************* Save States *****************************/
358 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
359 #define DREAMCAST_SAVE_VERSION 0x00010000
361 struct save_state_header {
362 char magic[16];
363 uint32_t version;
364 uint32_t module_count;
365 };
367 int dreamcast_load_state( const gchar *filename )
368 {
369 int i,j;
370 uint32_t count, len;
371 int have_read[MAX_MODULES];
372 char tmp[64];
373 struct save_state_header header;
374 FILE *f;
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" );
382 return 1;
383 }
384 if( header.version != DREAMCAST_SAVE_VERSION ) {
385 ERROR( "DreamOn save state version not supported" );
386 return 1;
387 }
388 if( header.module_count > MAX_MODULES ) {
389 ERROR( "DreamOn save state is corrupted (bad module count)" );
390 return 1;
391 }
392 for( i=0; i<MAX_MODULES; i++ ) {
393 have_read[i] = 0;
394 }
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 );
400 return 2;
401 }
402 len = fread_string(tmp, sizeof(tmp), f );
403 if( len > 64 || len < 1 ) {
404 ERROR( "DreamOn save state is corrupted (bad string)" );
405 return 2;
406 }
408 /* Find the matching module by name */
409 for( j=0; j<num_modules; j++ ) {
410 if( strcmp(modules[j]->name,tmp) == 0 ) {
411 have_read[j] = 1;
412 if( modules[j]->load == NULL ) {
413 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
414 return 2;
415 } else if( modules[j]->load(f) != 0 ) {
416 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
417 return 2;
418 }
419 break;
420 }
421 }
422 if( j == num_modules ) {
423 ERROR( "DreamOn save state contains unrecognized section" );
424 return 2;
425 }
426 }
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).
431 */
432 for( j=0; j<num_modules; j++ ) {
433 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
434 modules[j]->reset();
435 }
436 }
437 fclose(f);
438 INFO( "Save state read from %s", filename );
439 }
441 int dreamcast_save_state( const gchar *filename )
442 {
443 int i;
444 FILE *f;
445 struct save_state_header header;
447 f = fopen( filename, "w" );
448 if( f == NULL )
449 return errno;
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++;
457 }
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 );
463 modules[i]->save(f);
464 }
465 }
466 fclose( f );
467 INFO( "Save state written to %s", filename );
468 }
.