2 * $Id: dreamcast.c,v 1.10 2005-12-25 01:28:36 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"
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 static char *dreamcast_config = "DEFAULT";
41 #define MAX_MODULES 32
42 static int num_modules = 0;
43 dreamcast_module_t modules[MAX_MODULES];
46 * This function is responsible for defining how all the pieces of the
47 * dreamcast actually fit together.
49 * Note currently the locations of the various MMIO pages are hard coded in
50 * the MMIO definitions - they should probably be moved here.
52 void dreamcast_configure( )
54 /* Register the memory framework */
55 dreamcast_register_module( &mem_module );
57 /* Setup standard memory map */
58 mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
59 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
60 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
61 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
62 mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
63 mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
65 /* Load in the rest of the core modules */
66 dreamcast_register_module( &sh4_module );
67 dreamcast_register_module( &asic_module );
68 dreamcast_register_module( &pvr2_module );
69 dreamcast_register_module( &aica_module );
70 dreamcast_register_module( &maple_module );
71 dreamcast_register_module( &ide_module );
73 /* Attach any default maple devices, ie a pair of controllers */
74 maple_device_t controller1 = controller_new();
75 maple_device_t controller2 = controller_new();
76 maple_attach_device( controller1, 0, 0 );
77 maple_attach_device( controller2, 1, 0 );
80 void dreamcast_register_module( dreamcast_module_t module )
82 modules[num_modules++] = module;
83 if( module->init != NULL )
88 void dreamcast_init( void )
90 dreamcast_configure();
91 dreamcast_state = STATE_STOPPED;
94 void dreamcast_reset( void )
97 for( i=0; i<num_modules; i++ ) {
98 if( modules[i]->reset != NULL )
103 void dreamcast_run( void )
106 if( dreamcast_state != STATE_RUNNING ) {
107 for( i=0; i<num_modules; i++ ) {
108 if( modules[i]->start != NULL )
112 dreamcast_state = STATE_RUNNING;
113 while( dreamcast_state == STATE_RUNNING ) {
114 int time_to_run = timeslice_length;
115 for( i=0; i<num_modules; i++ ) {
116 if( modules[i]->run_time_slice != NULL )
117 time_to_run = modules[i]->run_time_slice( time_to_run );
122 for( i=0; i<num_modules; i++ ) {
123 if( modules[i]->stop != NULL )
126 dreamcast_state = STATE_STOPPED;
129 void dreamcast_stop( void )
131 if( dreamcast_state == STATE_RUNNING )
132 dreamcast_state = STATE_STOPPING;
135 gboolean dreamcast_is_running( void )
137 return dreamcast_state == STATE_RUNNING;
141 /********************************* Save States *****************************/
143 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
144 #define DREAMCAST_SAVE_VERSION 0x00010000
146 struct save_state_header {
149 uint32_t module_count;
152 int dreamcast_load_state( const gchar *filename )
156 int have_read[MAX_MODULES];
158 struct save_state_header header;
161 f = fopen( filename, "r" );
162 if( f == NULL ) return errno;
164 fread( &header, sizeof(header), 1, f );
165 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
166 ERROR( "Not a DreamOn save state file" );
169 if( header.version != DREAMCAST_SAVE_VERSION ) {
170 ERROR( "DreamOn save state version not supported" );
173 if( header.module_count > MAX_MODULES ) {
174 ERROR( "DreamOn save state is corrupted (bad module count)" );
177 for( i=0; i<MAX_MODULES; i++ ) {
181 for( i=0; i<header.module_count; i++ ) {
182 fread(tmp, 4, 1, f );
183 if( strncmp(tmp, "BLCK", 4) != 0 ) {
184 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
187 len = fread_string(tmp, sizeof(tmp), f );
188 if( len > 64 || len < 1 ) {
189 ERROR( "DreamOn save state is corrupted (bad string)" );
193 /* Find the matching module by name */
194 for( j=0; j<num_modules; j++ ) {
195 if( strcmp(modules[j]->name,tmp) == 0 ) {
197 if( modules[j]->load == NULL ) {
198 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
200 } else if( modules[j]->load(f) != 0 ) {
201 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
207 if( j == num_modules ) {
208 ERROR( "DreamOn save state contains unrecognized section" );
213 /* Any modules that we didn't load - reset to the default state.
214 * (ie it's not an error to skip a module if you don't actually
215 * care about its state).
217 for( j=0; j<num_modules; j++ ) {
218 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
223 INFO( "Save state read from %s", filename );
226 int dreamcast_save_state( const gchar *filename )
230 struct save_state_header header;
232 f = fopen( filename, "w" );
235 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
236 header.version = DREAMCAST_SAVE_VERSION;
237 header.module_count = 0;
239 for( i=0; i<num_modules; i++ ) {
240 if( modules[i]->save != NULL )
241 header.module_count++;
243 fwrite( &header, sizeof(header), 1, f );
244 for( i=0; i<num_modules; i++ ) {
245 if( modules[i]->save != NULL ) {
246 fwrite( "BLCK", 4, 1, f );
247 fwrite_string( modules[i]->name, f );
252 INFO( "Save state written to %s", filename );
.