2 * $Id: dreamcast.c,v 1.28 2007-10-31 11:53:35 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.
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 const 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 dreamcast_register_module( &eventq_module );
62 /* Register the memory framework */
63 dreamcast_register_module( &mem_module );
65 /* Setup standard memory map */
66 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
67 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
68 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
69 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
70 mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
71 0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
72 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
73 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
74 0x00200000, 0x00020000 );
76 /* Load in the rest of the core modules */
77 dreamcast_register_module( &sh4_module );
78 dreamcast_register_module( &asic_module );
79 dreamcast_register_module( &pvr2_module );
80 dreamcast_register_module( &aica_module );
81 dreamcast_register_module( &maple_module );
82 dreamcast_register_module( &ide_module );
85 void dreamcast_config_changed(void)
87 mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
88 0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
89 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
90 0x00200000, 0x00020000 );
93 void dreamcast_save_flash()
95 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
96 mem_save_block( file, 0x00200000, 0x00020000 );
100 * Constructs a system configuration for the AICA in standalone mode,
101 * ie sound chip only.
103 void dreamcast_configure_aica_only( )
105 dreamcast_register_module( &mem_module );
106 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
107 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
108 dreamcast_register_module( &aica_module );
110 dreamcast_state = STATE_STOPPED;
113 void dreamcast_register_module( dreamcast_module_t module )
115 modules[num_modules++] = module;
116 if( module->init != NULL )
121 void dreamcast_init( void )
123 dreamcast_configure();
124 dreamcast_state = STATE_STOPPED;
127 void dreamcast_reset( void )
130 for( i=0; i<num_modules; i++ ) {
131 if( modules[i]->reset != NULL )
136 void dreamcast_run( void )
139 if( dreamcast_state != STATE_RUNNING ) {
140 for( i=0; i<num_modules; i++ ) {
141 if( modules[i]->start != NULL )
145 dreamcast_state = STATE_RUNNING;
146 while( dreamcast_state == STATE_RUNNING ) {
147 int time_to_run = timeslice_length;
148 for( i=0; i<num_modules; i++ ) {
149 if( modules[i]->run_time_slice != NULL )
150 time_to_run = modules[i]->run_time_slice( time_to_run );
155 for( i=0; i<num_modules; i++ ) {
156 if( modules[i]->stop != NULL )
159 dreamcast_state = STATE_STOPPED;
162 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
166 if( dreamcast_state != STATE_RUNNING ) {
167 for( i=0; i<num_modules; i++ ) {
168 if( modules[i]->start != NULL )
172 dreamcast_state = STATE_RUNNING;
174 if( nanosecs != 0 ) {
175 nanos = 1000000000 - nanosecs;
178 while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
179 int time_to_run = timeslice_length;
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 );
184 nanos += time_to_run;
185 if( nanos >= 1000000000 ) {
191 for( i=0; i<num_modules; i++ ) {
192 if( modules[i]->stop != NULL )
195 dreamcast_state = STATE_STOPPED;
198 void dreamcast_stop( void )
200 if( dreamcast_state == STATE_RUNNING )
201 dreamcast_state = STATE_STOPPING;
204 void dreamcast_shutdown()
207 dreamcast_save_flash();
210 gboolean dreamcast_is_running( void )
212 return dreamcast_state == STATE_RUNNING;
215 /********************************* Save States *****************************/
217 struct save_state_header {
220 uint32_t module_count;
223 struct chunk_header {
224 char marker[4]; /* Always BLCK */
225 char name[8]; /* Block (module) name */
226 uint32_t block_length;
230 * Check the save state header to ensure that it is a valid, supported
232 * @return the number of blocks following, or 0 if the file is invalid.
234 int dreamcast_read_save_state_header( FILE *f )
236 struct save_state_header header;
237 if( fread( &header, sizeof(header), 1, f ) != 1 ) {
240 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
241 ERROR( "Not a %s save state file", APP_NAME );
244 if( header.version != DREAMCAST_SAVE_VERSION ) {
245 ERROR( "%s save state version not supported", APP_NAME );
248 if( header.module_count > MAX_MODULES ) {
249 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
252 return header.module_count;
255 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
257 struct chunk_header head;
259 memcpy( head.marker, "BLCK", 4 );
260 memset( head.name, 0, 8 );
261 memcpy( head.name, name, strlen(name) );
262 head.block_length = length;
263 return fwrite( &head, sizeof(head), 1, f );
267 frame_buffer_t dreamcast_load_preview( const gchar *filename )
270 FILE *f = fopen( filename, "r" );
271 if( f == NULL ) return NULL;
273 int module_count = dreamcast_read_save_state_header(f);
274 if( module_count <= 0 ) {
278 for( i=0; i<module_count; i++ ) {
279 struct chunk_header head;
280 if( fread( &head, sizeof(head), 1, f ) != 1 ) {
284 if( memcmp("BLCK", head.marker, 4) != 0 ) {
289 if( strcmp("PVR2", head.name) == 0 ) {
292 fread( &buf_count, sizeof(buf_count), 1, f );
293 fread( &has_front, sizeof(has_front), 1, f );
294 if( buf_count != 0 && has_front ) {
295 frame_buffer_t result = read_png_from_stream(f);
301 fseek( f, head.block_length, SEEK_CUR );
307 int dreamcast_load_state( const gchar *filename )
311 int have_read[MAX_MODULES];
313 FILE *f = fopen( filename, "r" );
314 if( f == NULL ) return errno;
316 module_count = dreamcast_read_save_state_header(f);
317 if( module_count <= 0 ) {
322 for( i=0; i<MAX_MODULES; i++ ) {
326 for( i=0; i<module_count; i++ ) {
327 struct chunk_header chunk;
328 fread( &chunk, sizeof(chunk), 1, f );
329 if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
330 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
335 /* Find the matching module by name */
336 for( j=0; j<num_modules; j++ ) {
337 if( strcmp(modules[j]->name,chunk.name) == 0 ) {
339 if( modules[j]->load == NULL ) {
340 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
343 } else if( modules[j]->load(f) != 0 ) {
344 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
351 if( j == num_modules ) {
353 ERROR( "%s save state contains unrecognized section", APP_NAME );
358 /* Any modules that we didn't load - reset to the default state.
359 * (ie it's not an error to skip a module if you don't actually
360 * care about its state).
362 for( j=0; j<num_modules; j++ ) {
363 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
368 INFO( "Save state read from %s", filename );
372 int dreamcast_save_state( const gchar *filename )
376 struct save_state_header header;
378 f = fopen( filename, "w" );
381 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
382 header.version = DREAMCAST_SAVE_VERSION;
383 header.module_count = 0;
385 for( i=0; i<num_modules; i++ ) {
386 if( modules[i]->save != NULL )
387 header.module_count++;
389 fwrite( &header, sizeof(header), 1, f );
390 for( i=0; i<num_modules; i++ ) {
391 if( modules[i]->save != NULL ) {
392 uint32_t blocklen, posn1, posn2;
393 dreamcast_write_chunk_header( modules[i]->name, 0, f );
397 blocklen = posn2 - posn1;
398 fseek( f, posn1-4, SEEK_SET );
399 fwrite( &blocklen, sizeof(blocklen), 1, f );
400 fseek( f, posn2, SEEK_SET );
404 INFO( "Save state written to %s", filename );
.