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.
25 #include "dreamcast.h"
29 #include "aica/aica.h"
30 #include "gdrom/ide.h"
31 #include "maple/maple.h"
33 #include "sh4/sh4core.h"
36 * Current state of the DC virtual machine
38 typedef enum { STATE_UNINIT=0, STATE_RUNNING,
39 STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
41 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
42 static gboolean dreamcast_has_bios = FALSE;
43 static gboolean dreamcast_has_flash = FALSE;
44 static gboolean dreamcast_exit_on_stop = FALSE;
45 static gchar *dreamcast_program_name = NULL;
46 static sh4addr_t dreamcast_entry_point = 0xA0000000;
47 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
48 static uint64_t run_time_nanosecs = 0;
50 #define MAX_MODULES 32
51 static int num_modules = 0;
52 dreamcast_module_t modules[MAX_MODULES];
55 * The unknown module is used for logging files without an actual module
58 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
62 * This function is responsible for defining how all the pieces of the
63 * dreamcast actually fit together.
65 * Note currently the locations of the various MMIO pages are hard coded in
66 * the MMIO definitions - they should probably be moved here.
68 void dreamcast_configure( )
70 dreamcast_register_module( &eventq_module );
71 /* Register the memory framework */
72 dreamcast_register_module( &mem_module );
74 /* Setup standard memory map */
75 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
76 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
77 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
78 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
79 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
80 0x00000000, 0x00200000, 0x89f2b1a1,
82 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
83 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
84 0x00200000, 0x00020000 );
85 dreamcast_has_flash = TRUE;
87 /* Load in the rest of the core modules */
88 dreamcast_register_module( &sh4_module );
89 dreamcast_register_module( &asic_module );
90 dreamcast_register_module( &pvr2_module );
91 dreamcast_register_module( &aica_module );
92 dreamcast_register_module( &maple_module );
93 dreamcast_register_module( &ide_module );
96 void dreamcast_config_changed(void)
98 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
99 0x00000000, 0x00200000, 0x89f2b1a1,
101 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
102 0x00200000, 0x00020000 );
105 void dreamcast_save_flash()
107 if( dreamcast_has_flash ) {
108 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
109 mem_save_block( file, 0x00200000, 0x00020000 );
114 * Constructs a system configuration for the AICA in standalone mode,
115 * ie sound chip only.
117 void dreamcast_configure_aica_only( )
119 dreamcast_register_module( &mem_module );
120 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
121 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
122 dreamcast_register_module( &aica_module );
124 dreamcast_state = STATE_STOPPED;
127 void dreamcast_register_module( dreamcast_module_t module )
129 modules[num_modules++] = module;
130 if( module->init != NULL )
134 void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
136 run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
139 void dreamcast_set_exit_on_stop( gboolean flag )
141 dreamcast_exit_on_stop = flag;
144 void dreamcast_init( void )
146 dreamcast_configure();
147 dreamcast_state = STATE_STOPPED;
150 void dreamcast_reset( void )
152 sh4_core_exit(CORE_EXIT_SYSRESET);
154 for( i=0; i<num_modules; i++ ) {
155 if( modules[i]->reset != NULL )
160 void dreamcast_run( void )
164 if( dreamcast_state != STATE_RUNNING ) {
165 for( i=0; i<num_modules; i++ ) {
166 if( modules[i]->start != NULL )
171 dreamcast_state = STATE_RUNNING;
173 if( run_time_nanosecs != 0 ) {
174 while( dreamcast_state == STATE_RUNNING ) {
175 uint32_t time_to_run = timeslice_length;
176 if( run_time_nanosecs < time_to_run ) {
177 time_to_run = (uint32_t)run_time_nanosecs;
180 for( i=0; i<num_modules; i++ ) {
181 if( modules[i]->run_time_slice != NULL )
182 time_to_run = modules[i]->run_time_slice( time_to_run );
185 if( run_time_nanosecs > time_to_run ) {
186 run_time_nanosecs -= time_to_run;
188 run_time_nanosecs = 0; // Finished
193 while( dreamcast_state == STATE_RUNNING ) {
194 int time_to_run = timeslice_length;
195 for( i=0; i<num_modules; i++ ) {
196 if( modules[i]->run_time_slice != NULL )
197 time_to_run = modules[i]->run_time_slice( time_to_run );
203 for( i=0; i<num_modules; i++ ) {
204 if( modules[i]->stop != NULL )
207 dreamcast_state = STATE_STOPPED;
209 if( dreamcast_exit_on_stop ) {
210 dreamcast_shutdown();
215 void dreamcast_stop( void )
217 sh4_core_exit(CORE_EXIT_HALT); // returns only if not inside SH4 core
218 if( dreamcast_state == STATE_RUNNING )
219 dreamcast_state = STATE_STOPPING;
222 void dreamcast_shutdown()
224 // Don't do a dreamcast_stop - if we're calling this out of SH4 code,
225 // it's a shutdown-and-quit event
226 if( dreamcast_state == STATE_RUNNING )
227 dreamcast_state = STATE_STOPPING;
228 dreamcast_save_flash();
229 #ifdef ENABLE_SH4STATS
230 sh4_stats_print(stdout);
234 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
236 if( dreamcast_program_name != NULL ) {
237 g_free(dreamcast_program_name);
239 dreamcast_program_name = g_strdup(name);
240 dreamcast_entry_point = entry_point;
241 sh4_set_pc(entry_point);
247 gboolean dreamcast_is_running( void )
249 return dreamcast_state == STATE_RUNNING;
252 gboolean dreamcast_can_run(void)
254 return dreamcast_state != STATE_UNINIT &&
255 (dreamcast_has_bios || dreamcast_program_name != NULL);
258 /********************************* Save States *****************************/
260 struct save_state_header {
263 uint32_t module_count;
266 struct chunk_header {
267 char marker[4]; /* Always BLCK */
268 char name[8]; /* Block (module) name */
269 uint32_t block_length;
273 * Check the save state header to ensure that it is a valid, supported
275 * @return the number of blocks following, or 0 if the file is invalid.
277 int dreamcast_read_save_state_header( FILE *f )
279 struct save_state_header header;
280 if( fread( &header, sizeof(header), 1, f ) != 1 ) {
283 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
284 ERROR( "Not a %s save state file", APP_NAME );
287 if( header.version != DREAMCAST_SAVE_VERSION ) {
288 ERROR( "%s save state version not supported", APP_NAME );
291 if( header.module_count > MAX_MODULES ) {
292 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
295 return header.module_count;
298 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
300 struct chunk_header head;
302 memcpy( head.marker, "BLCK", 4 );
303 memset( head.name, 0, 8 );
304 memcpy( head.name, name, strlen(name) );
305 head.block_length = length;
306 return fwrite( &head, sizeof(head), 1, f );
310 frame_buffer_t dreamcast_load_preview( const gchar *filename )
313 FILE *f = fopen( filename, "r" );
314 if( f == NULL ) return NULL;
316 int module_count = dreamcast_read_save_state_header(f);
317 if( module_count <= 0 ) {
321 for( i=0; i<module_count; i++ ) {
322 struct chunk_header head;
323 if( fread( &head, sizeof(head), 1, f ) != 1 ) {
327 if( memcmp("BLCK", head.marker, 4) != 0 ) {
332 if( strcmp("PVR2", head.name) == 0 ) {
335 fread( &buf_count, sizeof(buf_count), 1, f );
336 fread( &has_front, sizeof(has_front), 1, f );
337 if( buf_count != 0 && has_front ) {
338 frame_buffer_t result = read_png_from_stream(f);
344 fseek( f, head.block_length, SEEK_CUR );
350 int dreamcast_load_state( const gchar *filename )
354 int have_read[MAX_MODULES];
356 FILE *f = fopen( filename, "r" );
357 if( f == NULL ) return errno;
359 module_count = dreamcast_read_save_state_header(f);
360 if( module_count <= 0 ) {
365 for( i=0; i<MAX_MODULES; i++ ) {
369 for( i=0; i<module_count; i++ ) {
370 struct chunk_header chunk;
371 fread( &chunk, sizeof(chunk), 1, f );
372 if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
373 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
378 /* Find the matching module by name */
379 for( j=0; j<num_modules; j++ ) {
380 if( strcmp(modules[j]->name,chunk.name) == 0 ) {
382 if( modules[j]->load == NULL ) {
383 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
386 } else if( modules[j]->load(f) != 0 ) {
387 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
394 if( j == num_modules ) {
396 ERROR( "%s save state contains unrecognized section", APP_NAME );
401 /* Any modules that we didn't load - reset to the default state.
402 * (ie it's not an error to skip a module if you don't actually
403 * care about its state).
405 for( j=0; j<num_modules; j++ ) {
406 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
411 INFO( "Save state read from %s", filename );
415 int dreamcast_save_state( const gchar *filename )
419 struct save_state_header header;
421 f = fopen( filename, "w" );
424 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
425 header.version = DREAMCAST_SAVE_VERSION;
426 header.module_count = 0;
428 for( i=0; i<num_modules; i++ ) {
429 if( modules[i]->save != NULL )
430 header.module_count++;
432 fwrite( &header, sizeof(header), 1, f );
433 for( i=0; i<num_modules; i++ ) {
434 if( modules[i]->save != NULL ) {
435 uint32_t blocklen, posn1, posn2;
436 dreamcast_write_chunk_header( modules[i]->name, 0, f );
440 blocklen = posn2 - posn1;
441 fseek( f, posn1-4, SEEK_SET );
442 fwrite( &blocklen, sizeof(blocklen), 1, f );
443 fseek( f, posn2, SEEK_SET );
447 INFO( "Save state written to %s", filename );
.