filename | src/dreamcast.c |
changeset | 35:21a4be098304 |
prev | 30:89b30313d757 |
next | 71:fcde57dbf968 |
author | nkeynes |
date | Tue Jan 10 13:58:35 2006 +0000 (18 years ago) |
permissions | -rw-r--r-- |
last change | Force redisasm when switching cpus Handle emits when gui hasn't been created yet (dump to stderr) |
view | annotate | diff | log | raw |
1 /**
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...
6 *
7 * Copyright (c) 2005 Nathan Keynes.
8 *
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.
13 *
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.
18 */
20 #include <errno.h>
21 #include "dream.h"
22 #include "mem.h"
23 #include "aica/aica.h"
24 #include "asic.h"
25 #include "dreamcast.h"
26 #include "gdrom/ide.h"
27 #include "maple/maple.h"
29 /**
30 * Current state of the DC virtual machine
31 */
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];
44 /**
45 * The unknown module is used for logging files without an actual module
46 * declaration
47 */
48 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
49 NULL, NULL, NULL };
51 /**
52 * This function is responsible for defining how all the pieces of the
53 * dreamcast actually fit together.
54 *
55 * Note currently the locations of the various MMIO pages are hard coded in
56 * the MMIO definitions - they should probably be moved here.
57 */
58 void dreamcast_configure( )
59 {
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 );
84 }
86 void dreamcast_register_module( dreamcast_module_t module )
87 {
88 modules[num_modules++] = module;
89 if( module->init != NULL )
90 module->init();
91 }
94 void dreamcast_init( void )
95 {
96 dreamcast_configure();
97 dreamcast_state = STATE_STOPPED;
98 }
100 void dreamcast_reset( void )
101 {
102 int i;
103 for( i=0; i<num_modules; i++ ) {
104 if( modules[i]->reset != NULL )
105 modules[i]->reset();
106 }
107 }
109 void dreamcast_run( void )
110 {
111 int i;
112 if( dreamcast_state != STATE_RUNNING ) {
113 for( i=0; i<num_modules; i++ ) {
114 if( modules[i]->start != NULL )
115 modules[i]->start();
116 }
117 }
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 );
124 }
126 }
128 for( i=0; i<num_modules; i++ ) {
129 if( modules[i]->stop != NULL )
130 modules[i]->stop();
131 }
132 dreamcast_state = STATE_STOPPED;
133 }
135 void dreamcast_stop( void )
136 {
137 if( dreamcast_state == STATE_RUNNING )
138 dreamcast_state = STATE_STOPPING;
139 }
141 gboolean dreamcast_is_running( void )
142 {
143 return dreamcast_state == STATE_RUNNING;
144 }
146 /***************************** User Configuration **************************/
148 void dreamcast_load_config( const gchar *filename )
149 {
152 }
154 void dreamcast_save_config( const gchar *filename )
155 {
158 }
160 /********************************* Save States *****************************/
162 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
163 #define DREAMCAST_SAVE_VERSION 0x00010000
165 struct save_state_header {
166 char magic[16];
167 uint32_t version;
168 uint32_t module_count;
169 };
171 int dreamcast_load_state( const gchar *filename )
172 {
173 int i,j;
174 uint32_t count, len;
175 int have_read[MAX_MODULES];
176 char tmp[64];
177 struct save_state_header header;
178 FILE *f;
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" );
186 return 1;
187 }
188 if( header.version != DREAMCAST_SAVE_VERSION ) {
189 ERROR( "DreamOn save state version not supported" );
190 return 1;
191 }
192 if( header.module_count > MAX_MODULES ) {
193 ERROR( "DreamOn save state is corrupted (bad module count)" );
194 return 1;
195 }
196 for( i=0; i<MAX_MODULES; i++ ) {
197 have_read[i] = 0;
198 }
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 );
204 return 2;
205 }
206 len = fread_string(tmp, sizeof(tmp), f );
207 if( len > 64 || len < 1 ) {
208 ERROR( "DreamOn save state is corrupted (bad string)" );
209 return 2;
210 }
212 /* Find the matching module by name */
213 for( j=0; j<num_modules; j++ ) {
214 if( strcmp(modules[j]->name,tmp) == 0 ) {
215 have_read[j] = 1;
216 if( modules[j]->load == NULL ) {
217 ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
218 return 2;
219 } else if( modules[j]->load(f) != 0 ) {
220 ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
221 return 2;
222 }
223 break;
224 }
225 }
226 if( j == num_modules ) {
227 ERROR( "DreamOn save state contains unrecognized section" );
228 return 2;
229 }
230 }
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).
235 */
236 for( j=0; j<num_modules; j++ ) {
237 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
238 modules[j]->reset();
239 }
240 }
241 fclose(f);
242 INFO( "Save state read from %s", filename );
243 }
245 int dreamcast_save_state( const gchar *filename )
246 {
247 int i;
248 FILE *f;
249 struct save_state_header header;
251 f = fopen( filename, "w" );
252 if( f == NULL )
253 return errno;
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++;
261 }
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 );
267 modules[i]->save(f);
268 }
269 }
270 fclose( f );
271 INFO( "Save state written to %s", filename );
272 }
.