filename | src/dreamcast.c |
changeset | 461:63d4de8dcec6 |
prev | 450:207461e79f21 |
next | 466:a6935f46ed78 |
author | nkeynes |
date | Wed Oct 24 21:23:22 2007 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Fix long standing texcache management bug (invalidate palette was not placing the invalidated entries on the free list). |
view | annotate | diff | log | raw |
1 /**
2 * $Id: dreamcast.c,v 1.25 2007-10-23 10:48:24 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 "config.h"
23 #include "mem.h"
24 #include "aica/aica.h"
25 #include "asic.h"
26 #include "dreamcast.h"
27 #include "gdrom/ide.h"
28 #include "maple/maple.h"
30 /**
31 * Current state of the DC virtual machine
32 */
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 const char *dreamcast_config = "DEFAULT";
41 #define MAX_MODULES 32
42 static int num_modules = 0;
43 dreamcast_module_t modules[MAX_MODULES];
45 /**
46 * The unknown module is used for logging files without an actual module
47 * declaration
48 */
49 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
50 NULL, NULL, NULL };
52 /**
53 * This function is responsible for defining how all the pieces of the
54 * dreamcast actually fit together.
55 *
56 * Note currently the locations of the various MMIO pages are hard coded in
57 * the MMIO definitions - they should probably be moved here.
58 */
59 void dreamcast_configure( )
60 {
61 dreamcast_register_module( &eventq_module );
62 /* Register the memory framework */
63 dreamcast_register_module( &mem_module );
65 /* Setup standard memory map */
66 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
67 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
68 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
69 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
70 if( mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
71 0x00000000, 0x00200000, 0x89f2b1a1 ) == NULL ) {
72 /* Bios wasn't found. Dump an empty ram region in there for something to do */
73 mem_create_ram_region( 0x00000000, 0x00200000, MEM_REGION_BIOS );
74 }
75 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
76 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
77 0x00200000, 0x00020000 );
79 /* Load in the rest of the core modules */
80 dreamcast_register_module( &sh4_module );
81 dreamcast_register_module( &asic_module );
82 dreamcast_register_module( &pvr2_module );
83 dreamcast_register_module( &aica_module );
84 dreamcast_register_module( &maple_module );
85 dreamcast_register_module( &ide_module );
86 }
88 void dreamcast_config_changed(void)
89 {
90 mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
91 0x00000000, 0x00200000, 0x89f2b1a1 );
92 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
93 0x00200000, 0x00020000 );
94 }
96 void dreamcast_save_flash()
97 {
98 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
99 mem_save_block( file, 0x00200000, 0x00020000 );
100 }
102 /**
103 * Constructs a system configuration for the AICA in standalone mode,
104 * ie sound chip only.
105 */
106 void dreamcast_configure_aica_only( )
107 {
108 dreamcast_register_module( &mem_module );
109 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
110 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
111 dreamcast_register_module( &aica_module );
112 aica_enable();
113 dreamcast_state = STATE_STOPPED;
114 }
116 void dreamcast_register_module( dreamcast_module_t module )
117 {
118 modules[num_modules++] = module;
119 if( module->init != NULL )
120 module->init();
121 }
124 void dreamcast_init( void )
125 {
126 dreamcast_configure();
127 dreamcast_state = STATE_STOPPED;
128 }
130 void dreamcast_reset( void )
131 {
132 int i;
133 for( i=0; i<num_modules; i++ ) {
134 if( modules[i]->reset != NULL )
135 modules[i]->reset();
136 }
137 }
139 void dreamcast_run( void )
140 {
141 int i;
142 if( dreamcast_state != STATE_RUNNING ) {
143 for( i=0; i<num_modules; i++ ) {
144 if( modules[i]->start != NULL )
145 modules[i]->start();
146 }
147 }
148 dreamcast_state = STATE_RUNNING;
149 while( dreamcast_state == STATE_RUNNING ) {
150 int time_to_run = timeslice_length;
151 for( i=0; i<num_modules; i++ ) {
152 if( modules[i]->run_time_slice != NULL )
153 time_to_run = modules[i]->run_time_slice( time_to_run );
154 }
156 }
158 for( i=0; i<num_modules; i++ ) {
159 if( modules[i]->stop != NULL )
160 modules[i]->stop();
161 }
162 dreamcast_state = STATE_STOPPED;
163 }
165 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
166 {
168 int i;
169 if( dreamcast_state != STATE_RUNNING ) {
170 for( i=0; i<num_modules; i++ ) {
171 if( modules[i]->start != NULL )
172 modules[i]->start();
173 }
174 }
175 dreamcast_state = STATE_RUNNING;
176 uint32_t nanos = 0;
177 if( nanosecs != 0 ) {
178 nanos = 1000000000 - nanosecs;
179 seconds++;
180 }
181 while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
182 int time_to_run = timeslice_length;
183 for( i=0; i<num_modules; i++ ) {
184 if( modules[i]->run_time_slice != NULL )
185 time_to_run = modules[i]->run_time_slice( time_to_run );
186 }
187 nanos += time_to_run;
188 if( nanos >= 1000000000 ) {
189 nanos -= 1000000000;
190 seconds--;
191 }
192 }
194 for( i=0; i<num_modules; i++ ) {
195 if( modules[i]->stop != NULL )
196 modules[i]->stop();
197 }
198 dreamcast_state = STATE_STOPPED;
199 }
201 void dreamcast_stop( void )
202 {
203 if( dreamcast_state == STATE_RUNNING )
204 dreamcast_state = STATE_STOPPING;
205 }
207 void dreamcast_shutdown()
208 {
209 dreamcast_stop();
210 dreamcast_save_flash();
211 }
213 gboolean dreamcast_is_running( void )
214 {
215 return dreamcast_state == STATE_RUNNING;
216 }
218 /********************************* Save States *****************************/
220 struct save_state_header {
221 char magic[16];
222 uint32_t version;
223 uint32_t module_count;
224 };
226 int dreamcast_load_state( const gchar *filename )
227 {
228 int i,j;
229 uint32_t len;
230 int have_read[MAX_MODULES];
231 char tmp[64];
232 struct save_state_header header;
233 FILE *f;
235 f = fopen( filename, "r" );
236 if( f == NULL ) return errno;
238 fread( &header, sizeof(header), 1, f );
239 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
240 ERROR( "Not a %s save state file", APP_NAME );
241 return 1;
242 }
243 if( header.version != DREAMCAST_SAVE_VERSION ) {
244 ERROR( "%s save state version not supported", APP_NAME );
245 return 1;
246 }
247 if( header.module_count > MAX_MODULES ) {
248 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
249 return 1;
250 }
251 for( i=0; i<MAX_MODULES; i++ ) {
252 have_read[i] = 0;
253 }
255 for( i=0; i<header.module_count; i++ ) {
256 fread(tmp, 4, 1, f );
257 if( strncmp(tmp, "BLCK", 4) != 0 ) {
258 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
259 return 2;
260 }
261 len = fread_string(tmp, sizeof(tmp), f );
262 if( len > 64 || len < 1 ) {
263 ERROR( "%s save state is corrupted (bad string)", APP_NAME );
264 return 2;
265 }
267 /* Find the matching module by name */
268 for( j=0; j<num_modules; j++ ) {
269 if( strcmp(modules[j]->name,tmp) == 0 ) {
270 have_read[j] = 1;
271 if( modules[j]->load == NULL ) {
272 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
273 return 2;
274 } else if( modules[j]->load(f) != 0 ) {
275 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
276 return 2;
277 }
278 break;
279 }
280 }
281 if( j == num_modules ) {
282 ERROR( "%s save state contains unrecognized section", APP_NAME );
283 return 2;
284 }
285 }
287 /* Any modules that we didn't load - reset to the default state.
288 * (ie it's not an error to skip a module if you don't actually
289 * care about its state).
290 */
291 for( j=0; j<num_modules; j++ ) {
292 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
293 modules[j]->reset();
294 }
295 }
296 fclose(f);
297 INFO( "Save state read from %s", filename );
298 return 0;
299 }
301 int dreamcast_save_state( const gchar *filename )
302 {
303 int i;
304 FILE *f;
305 struct save_state_header header;
307 f = fopen( filename, "w" );
308 if( f == NULL )
309 return errno;
310 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
311 header.version = DREAMCAST_SAVE_VERSION;
312 header.module_count = 0;
314 for( i=0; i<num_modules; i++ ) {
315 if( modules[i]->save != NULL )
316 header.module_count++;
317 }
318 fwrite( &header, sizeof(header), 1, f );
319 for( i=0; i<num_modules; i++ ) {
320 if( modules[i]->save != NULL ) {
321 fwrite( "BLCK", 4, 1, f );
322 fwrite_string( modules[i]->name, f );
323 modules[i]->save(f);
324 }
325 }
326 fclose( f );
327 INFO( "Save state written to %s", filename );
328 return 0;
329 }
.