2 * $Id: dreamcast.c,v 1.12 2005-12-26 03:54:52 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 );
86 void dreamcast_register_module( dreamcast_module_t module )
88 modules[num_modules++] = module;
89 if( module->init != NULL )
94 void dreamcast_init( void )
96 dreamcast_configure();
97 dreamcast_state = STATE_STOPPED;
100 void dreamcast_reset( void )
103 for( i=0; i<num_modules; i++ ) {
104 if( modules[i]->reset != NULL )
109 void dreamcast_run( void )
112 if( dreamcast_state != STATE_RUNNING ) {
113 for( i=0; i<num_modules; i++ ) {
114 if( modules[i]->start != NULL )
118 dreamcast_state = STATE_RUNNING;
119 while( dreamcast_state == STATE_RUNNING ) {
120 int time_to_run = timeslice_length;
121 for( i=0; i<num_modules; i++ ) {
122 if( modules[i]->run_time_slice != NULL )
123 time_to_run = modules[i]->run_time_slice( time_to_run );
128 for( i=0; i<num_modules; i++ ) {
129 if( modules[i]->stop != NULL )
132 dreamcast_state = STATE_STOPPED;
135 void dreamcast_stop( void )
137 if( dreamcast_state == STATE_RUNNING )
138 dreamcast_state = STATE_STOPPING;
141 gboolean dreamcast_is_running( void )
143 return dreamcast_state == STATE_RUNNING;
146 /***************************** User Configuration **************************/
148 void dreamcast_load_config( const gchar *filename )
154 void dreamcast_save_config( const gchar *filename )
160 /********************************* Save States *****************************/
162 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
163 #define DREAMCAST_SAVE_VERSION 0x00010000
165 struct save_state_header {
168 uint32_t module_count;
171 int dreamcast_load_state( const gchar *filename )
175 int have_read[MAX_MODULES];
177 struct save_state_header header;
180 f = fopen( filename, "r" );
181 if( f == NULL ) return errno;
183 fread( &header, sizeof(header), 1, f );
184 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
185 ERROR( "Not a DreamOn save state file" );
188 if( header.version != DREAMCAST_SAVE_VERSION ) {
189 ERROR( "DreamOn save state version not supported" );
192 if( header.module_count > MAX_MODULES ) {
193 ERROR( "DreamOn save state is corrupted (bad module count)" );
196 for( i=0; i<MAX_MODULES; i++ ) {
200 for( i=0; i<header.module_count; i++ ) {
201 fread(tmp, 4, 1, f );
202 if( strncmp(tmp, "BLCK", 4) != 0 ) {
203 ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
206 len = fread_string(tmp, sizeof(tmp), f );
207 if( len > 64 || len < 1 ) {
208 ERROR( "DreamOn save state is corrupted (bad string)" );
212 /* Find the matching module by name */
213 for( j=0; j<num_modules; j++ ) {
214 if( strcmp(modules[j]->name,tmp) == 0 ) {
216 if( modules[j]->load == NULL ) {
217 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
219 } else if( modules[j]->load(f) != 0 ) {
220 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
226 if( j == num_modules ) {
227 ERROR( "DreamOn save state contains unrecognized section" );
232 /* Any modules that we didn't load - reset to the default state.
233 * (ie it's not an error to skip a module if you don't actually
234 * care about its state).
236 for( j=0; j<num_modules; j++ ) {
237 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
242 INFO( "Save state read from %s", filename );
245 int dreamcast_save_state( const gchar *filename )
249 struct save_state_header header;
251 f = fopen( filename, "w" );
254 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
255 header.version = DREAMCAST_SAVE_VERSION;
256 header.module_count = 0;
258 for( i=0; i<num_modules; i++ ) {
259 if( modules[i]->save != NULL )
260 header.module_count++;
262 fwrite( &header, sizeof(header), 1, f );
263 for( i=0; i<num_modules; i++ ) {
264 if( modules[i]->save != NULL ) {
265 fwrite( "BLCK", 4, 1, f );
266 fwrite_string( modules[i]->name, f );
271 INFO( "Save state written to %s", filename );
.