2 * $Id: dreamcast.c,v 1.11 2005-12-25 05:56: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.
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;
140 /***************************** User Configuration **************************/
142 void dreamcast_load_config( const gchar *filename )
148 void dreamcast_save_config( const gchar *filename )
154 /********************************* Save States *****************************/
156 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
157 #define DREAMCAST_SAVE_VERSION 0x00010000
159 struct save_state_header {
162 uint32_t module_count;
165 int dreamcast_load_state( const gchar *filename )
169 int have_read[MAX_MODULES];
171 struct save_state_header header;
174 f = fopen( filename, "r" );
175 if( f == NULL ) return errno;
177 fread( &header, sizeof(header), 1, f );
178 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
179 ERROR( "Not a DreamOn save state file" );
182 if( header.version != DREAMCAST_SAVE_VERSION ) {
183 ERROR( "DreamOn save state version not supported" );
186 if( header.module_count > MAX_MODULES ) {
187 ERROR( "DreamOn save state is corrupted (bad module count)" );
190 for( i=0; i<MAX_MODULES; i++ ) {
194 for( i=0; i<header.module_count; i++ ) {
195 fread(tmp, 4, 1, f );
196 if( strncmp(tmp, "BLCK", 4) != 0 ) {
197 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
200 len = fread_string(tmp, sizeof(tmp), f );
201 if( len > 64 || len < 1 ) {
202 ERROR( "DreamOn save state is corrupted (bad string)" );
206 /* Find the matching module by name */
207 for( j=0; j<num_modules; j++ ) {
208 if( strcmp(modules[j]->name,tmp) == 0 ) {
210 if( modules[j]->load == NULL ) {
211 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
213 } else if( modules[j]->load(f) != 0 ) {
214 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
220 if( j == num_modules ) {
221 ERROR( "DreamOn save state contains unrecognized section" );
226 /* Any modules that we didn't load - reset to the default state.
227 * (ie it's not an error to skip a module if you don't actually
228 * care about its state).
230 for( j=0; j<num_modules; j++ ) {
231 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
236 INFO( "Save state read from %s", filename );
239 int dreamcast_save_state( const gchar *filename )
243 struct save_state_header header;
245 f = fopen( filename, "w" );
248 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
249 header.version = DREAMCAST_SAVE_VERSION;
250 header.module_count = 0;
252 for( i=0; i<num_modules; i++ ) {
253 if( modules[i]->save != NULL )
254 header.module_count++;
256 fwrite( &header, sizeof(header), 1, f );
257 for( i=0; i<num_modules; i++ ) {
258 if( modules[i]->save != NULL ) {
259 fwrite( "BLCK", 4, 1, f );
260 fwrite_string( modules[i]->name, f );
265 INFO( "Save state written to %s", filename );
.