2 * $Id: dreamcast.c,v 1.26 2007-10-27 05:47:55 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 int dreamcast_load_state( const gchar *filename )
227 int have_read[MAX_MODULES];
229 struct save_state_header header;
232 f = fopen( filename, "r" );
233 if( f == NULL ) return errno;
235 fread( &header, sizeof(header), 1, f );
236 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
237 ERROR( "Not a %s save state file", APP_NAME );
240 if( header.version != DREAMCAST_SAVE_VERSION ) {
241 ERROR( "%s save state version not supported", APP_NAME );
244 if( header.module_count > MAX_MODULES ) {
245 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
248 for( i=0; i<MAX_MODULES; i++ ) {
252 for( i=0; i<header.module_count; i++ ) {
253 fread(tmp, 4, 1, f );
254 if( strncmp(tmp, "BLCK", 4) != 0 ) {
255 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
258 len = fread_string(tmp, sizeof(tmp), f );
259 if( len > 64 || len < 1 ) {
260 ERROR( "%s save state is corrupted (bad string)", APP_NAME );
264 /* Find the matching module by name */
265 for( j=0; j<num_modules; j++ ) {
266 if( strcmp(modules[j]->name,tmp) == 0 ) {
268 if( modules[j]->load == NULL ) {
269 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
271 } else if( modules[j]->load(f) != 0 ) {
272 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
278 if( j == num_modules ) {
279 ERROR( "%s save state contains unrecognized section", APP_NAME );
284 /* Any modules that we didn't load - reset to the default state.
285 * (ie it's not an error to skip a module if you don't actually
286 * care about its state).
288 for( j=0; j<num_modules; j++ ) {
289 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
294 INFO( "Save state read from %s", filename );
298 int dreamcast_save_state( const gchar *filename )
302 struct save_state_header header;
304 f = fopen( filename, "w" );
307 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
308 header.version = DREAMCAST_SAVE_VERSION;
309 header.module_count = 0;
311 for( i=0; i<num_modules; i++ ) {
312 if( modules[i]->save != NULL )
313 header.module_count++;
315 fwrite( &header, sizeof(header), 1, f );
316 for( i=0; i<num_modules; i++ ) {
317 if( modules[i]->save != NULL ) {
318 fwrite( "BLCK", 4, 1, f );
319 fwrite_string( modules[i]->name, f );
324 INFO( "Save state written to %s", filename );
.