2 * $Id: dreamcast.c,v 1.24 2007-10-17 11:26:45 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 if( mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
71 0x00000000, 0x00200000, 0x89f2b1a1 ) == NULL ) {
72 /* Bios wasn't found. Dump an empty ram region in there for something to do */
73 mem_create_ram_region( 0x00000000, 0x00200000, MEM_REGION_BIOS );
75 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
76 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
77 0x00200000, 0x00020000 );
79 /* Load in the rest of the core modules */
80 dreamcast_register_module( &sh4_module );
81 dreamcast_register_module( &asic_module );
82 dreamcast_register_module( &pvr2_module );
83 dreamcast_register_module( &aica_module );
84 dreamcast_register_module( &maple_module );
85 dreamcast_register_module( &ide_module );
88 void dreamcast_save_flash()
90 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
91 mem_save_block( file, 0x00200000, 0x00020000 );
95 * Constructs a system configuration for the AICA in standalone mode,
98 void dreamcast_configure_aica_only( )
100 dreamcast_register_module( &mem_module );
101 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
102 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
103 dreamcast_register_module( &aica_module );
105 dreamcast_state = STATE_STOPPED;
108 void dreamcast_register_module( dreamcast_module_t module )
110 modules[num_modules++] = module;
111 if( module->init != NULL )
116 void dreamcast_init( void )
118 dreamcast_configure();
119 dreamcast_state = STATE_STOPPED;
122 void dreamcast_reset( void )
125 for( i=0; i<num_modules; i++ ) {
126 if( modules[i]->reset != NULL )
131 void dreamcast_run( void )
134 if( dreamcast_state != STATE_RUNNING ) {
135 for( i=0; i<num_modules; i++ ) {
136 if( modules[i]->start != NULL )
140 dreamcast_state = STATE_RUNNING;
141 while( dreamcast_state == STATE_RUNNING ) {
142 int time_to_run = timeslice_length;
143 for( i=0; i<num_modules; i++ ) {
144 if( modules[i]->run_time_slice != NULL )
145 time_to_run = modules[i]->run_time_slice( time_to_run );
150 for( i=0; i<num_modules; i++ ) {
151 if( modules[i]->stop != NULL )
154 dreamcast_state = STATE_STOPPED;
157 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
161 if( dreamcast_state != STATE_RUNNING ) {
162 for( i=0; i<num_modules; i++ ) {
163 if( modules[i]->start != NULL )
167 dreamcast_state = STATE_RUNNING;
169 if( nanosecs != 0 ) {
170 nanos = 1000000000 - nanosecs;
173 while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
174 int time_to_run = timeslice_length;
175 for( i=0; i<num_modules; i++ ) {
176 if( modules[i]->run_time_slice != NULL )
177 time_to_run = modules[i]->run_time_slice( time_to_run );
179 nanos += time_to_run;
180 if( nanos >= 1000000000 ) {
186 for( i=0; i<num_modules; i++ ) {
187 if( modules[i]->stop != NULL )
190 dreamcast_state = STATE_STOPPED;
193 void dreamcast_stop( void )
195 if( dreamcast_state == STATE_RUNNING )
196 dreamcast_state = STATE_STOPPING;
199 void dreamcast_shutdown()
202 dreamcast_save_flash();
205 gboolean dreamcast_is_running( void )
207 return dreamcast_state == STATE_RUNNING;
210 /********************************* Save States *****************************/
212 struct save_state_header {
215 uint32_t module_count;
218 int dreamcast_load_state( const gchar *filename )
222 int have_read[MAX_MODULES];
224 struct save_state_header header;
227 f = fopen( filename, "r" );
228 if( f == NULL ) return errno;
230 fread( &header, sizeof(header), 1, f );
231 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
232 ERROR( "Not a %s save state file", APP_NAME );
235 if( header.version != DREAMCAST_SAVE_VERSION ) {
236 ERROR( "%s save state version not supported", APP_NAME );
239 if( header.module_count > MAX_MODULES ) {
240 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
243 for( i=0; i<MAX_MODULES; i++ ) {
247 for( i=0; i<header.module_count; i++ ) {
248 fread(tmp, 4, 1, f );
249 if( strncmp(tmp, "BLCK", 4) != 0 ) {
250 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
253 len = fread_string(tmp, sizeof(tmp), f );
254 if( len > 64 || len < 1 ) {
255 ERROR( "%s save state is corrupted (bad string)", APP_NAME );
259 /* Find the matching module by name */
260 for( j=0; j<num_modules; j++ ) {
261 if( strcmp(modules[j]->name,tmp) == 0 ) {
263 if( modules[j]->load == NULL ) {
264 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
266 } else if( modules[j]->load(f) != 0 ) {
267 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
273 if( j == num_modules ) {
274 ERROR( "%s save state contains unrecognized section", APP_NAME );
279 /* Any modules that we didn't load - reset to the default state.
280 * (ie it's not an error to skip a module if you don't actually
281 * care about its state).
283 for( j=0; j<num_modules; j++ ) {
284 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
289 INFO( "Save state read from %s", filename );
293 int dreamcast_save_state( const gchar *filename )
297 struct save_state_header header;
299 f = fopen( filename, "w" );
302 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
303 header.version = DREAMCAST_SAVE_VERSION;
304 header.module_count = 0;
306 for( i=0; i<num_modules; i++ ) {
307 if( modules[i]->save != NULL )
308 header.module_count++;
310 fwrite( &header, sizeof(header), 1, f );
311 for( i=0; i<num_modules; i++ ) {
312 if( modules[i]->save != NULL ) {
313 fwrite( "BLCK", 4, 1, f );
314 fwrite_string( modules[i]->name, f );
319 INFO( "Save state written to %s", filename );
.