2 * $Id: dreamcast.c,v 1.13 2006-01-10 13:59: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.
23 #include "aica/aica.h"
25 #include "dreamcast.h"
26 #include "gdrom/ide.h"
27 #include "maple/maple.h"
30 * Current state of the DC virtual machine
32 #define STATE_UNINIT 0
33 #define STATE_RUNNING 1
34 #define STATE_STOPPING 2
35 #define STATE_STOPPED 3
36 static volatile int dreamcast_state = STATE_UNINIT;
37 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
38 static char *dreamcast_config = "DEFAULT";
40 #define MAX_MODULES 32
41 static int num_modules = 0;
42 dreamcast_module_t modules[MAX_MODULES];
45 * The unknown module is used for logging files without an actual module
48 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
52 * This function is responsible for defining how all the pieces of the
53 * dreamcast actually fit together.
55 * Note currently the locations of the various MMIO pages are hard coded in
56 * the MMIO definitions - they should probably be moved here.
58 void dreamcast_configure( )
60 /* Register the memory framework */
61 dreamcast_register_module( &mem_module );
63 /* Setup standard memory map */
64 mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
65 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
66 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
67 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
68 mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
69 mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
71 /* Load in the rest of the core modules */
72 dreamcast_register_module( &sh4_module );
73 dreamcast_register_module( &asic_module );
74 dreamcast_register_module( &pvr2_module );
75 dreamcast_register_module( &aica_module );
76 dreamcast_register_module( &maple_module );
77 dreamcast_register_module( &ide_module );
79 /* Attach any default maple devices, ie a pair of controllers */
80 maple_device_t controller1 = controller_new();
81 maple_device_t controller2 = controller_new();
82 maple_attach_device( controller1, 0, 0 );
83 maple_attach_device( controller2, 1, 0 );
87 * Constructs a system configuration for the AICA in standalone mode,
90 void dreamcast_configure_aica_only( )
92 dreamcast_register_module( &mem_module );
93 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
94 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
95 dreamcast_register_module( &aica_module );
96 dreamcast_state = STATE_STOPPED;
99 void dreamcast_register_module( dreamcast_module_t module )
101 modules[num_modules++] = module;
102 if( module->init != NULL )
107 void dreamcast_init( void )
109 dreamcast_configure();
110 dreamcast_state = STATE_STOPPED;
113 void dreamcast_reset( void )
116 for( i=0; i<num_modules; i++ ) {
117 if( modules[i]->reset != NULL )
122 void dreamcast_run( void )
125 if( dreamcast_state != STATE_RUNNING ) {
126 for( i=0; i<num_modules; i++ ) {
127 if( modules[i]->start != NULL )
131 dreamcast_state = STATE_RUNNING;
132 while( dreamcast_state == STATE_RUNNING ) {
133 int time_to_run = timeslice_length;
134 for( i=0; i<num_modules; i++ ) {
135 if( modules[i]->run_time_slice != NULL )
136 time_to_run = modules[i]->run_time_slice( time_to_run );
141 for( i=0; i<num_modules; i++ ) {
142 if( modules[i]->stop != NULL )
145 dreamcast_state = STATE_STOPPED;
148 void dreamcast_stop( void )
150 if( dreamcast_state == STATE_RUNNING )
151 dreamcast_state = STATE_STOPPING;
154 gboolean dreamcast_is_running( void )
156 return dreamcast_state == STATE_RUNNING;
159 /***************************** User Configuration **************************/
161 void dreamcast_load_config( const gchar *filename )
167 void dreamcast_save_config( const gchar *filename )
173 /********************************* Save States *****************************/
175 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
176 #define DREAMCAST_SAVE_VERSION 0x00010000
178 struct save_state_header {
181 uint32_t module_count;
184 int dreamcast_load_state( const gchar *filename )
188 int have_read[MAX_MODULES];
190 struct save_state_header header;
193 f = fopen( filename, "r" );
194 if( f == NULL ) return errno;
196 fread( &header, sizeof(header), 1, f );
197 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
198 ERROR( "Not a DreamOn save state file" );
201 if( header.version != DREAMCAST_SAVE_VERSION ) {
202 ERROR( "DreamOn save state version not supported" );
205 if( header.module_count > MAX_MODULES ) {
206 ERROR( "DreamOn save state is corrupted (bad module count)" );
209 for( i=0; i<MAX_MODULES; i++ ) {
213 for( i=0; i<header.module_count; i++ ) {
214 fread(tmp, 4, 1, f );
215 if( strncmp(tmp, "BLCK", 4) != 0 ) {
216 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
219 len = fread_string(tmp, sizeof(tmp), f );
220 if( len > 64 || len < 1 ) {
221 ERROR( "DreamOn save state is corrupted (bad string)" );
225 /* Find the matching module by name */
226 for( j=0; j<num_modules; j++ ) {
227 if( strcmp(modules[j]->name,tmp) == 0 ) {
229 if( modules[j]->load == NULL ) {
230 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
232 } else if( modules[j]->load(f) != 0 ) {
233 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
239 if( j == num_modules ) {
240 ERROR( "DreamOn save state contains unrecognized section" );
245 /* Any modules that we didn't load - reset to the default state.
246 * (ie it's not an error to skip a module if you don't actually
247 * care about its state).
249 for( j=0; j<num_modules; j++ ) {
250 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
255 INFO( "Save state read from %s", filename );
258 int dreamcast_save_state( const gchar *filename )
262 struct save_state_header header;
264 f = fopen( filename, "w" );
267 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
268 header.version = DREAMCAST_SAVE_VERSION;
269 header.module_count = 0;
271 for( i=0; i<num_modules; i++ ) {
272 if( modules[i]->save != NULL )
273 header.module_count++;
275 fwrite( &header, sizeof(header), 1, f );
276 for( i=0; i<num_modules; i++ ) {
277 if( modules[i]->save != NULL ) {
278 fwrite( "BLCK", 4, 1, f );
279 fwrite_string( modules[i]->name, f );
284 INFO( "Save state written to %s", filename );
.