filename | src/dreamcast.c |
changeset | 669:ab344e42bca9 |
prev | 586:2a3ba82cf243 |
next | 671:a530ea88eebd |
author | nkeynes |
date | Mon May 12 10:00:13 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Cleanup most of the -Wall warnings (getting a bit sloppy...) Convert FP code to use fixed banks rather than indirect pointer (3-4% faster this way now) |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
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 <glib.h>
22 #include "lxdream.h"
23 #include "mem.h"
24 #include "dreamcast.h"
25 #include "asic.h"
26 #include "syscall.h"
27 #include "gui.h"
28 #include "aica/aica.h"
29 #include "gdrom/ide.h"
30 #include "maple/maple.h"
31 #include "sh4/sh4.h"
32 #include "sh4/sh4trans.h"
34 /**
35 * Current state of the DC virtual machine
36 */
37 typedef enum { STATE_UNINIT=0, STATE_RUNNING,
38 STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
40 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
41 static gboolean dreamcast_has_bios = FALSE;
42 static gchar *dreamcast_program_name = NULL;
43 static sh4addr_t dreamcast_entry_point = 0xA0000000;
44 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
46 #define MAX_MODULES 32
47 static int num_modules = 0;
48 dreamcast_module_t modules[MAX_MODULES];
50 /**
51 * The unknown module is used for logging files without an actual module
52 * declaration
53 */
54 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
55 NULL, NULL, NULL };
57 /**
58 * This function is responsible for defining how all the pieces of the
59 * dreamcast actually fit together.
60 *
61 * Note currently the locations of the various MMIO pages are hard coded in
62 * the MMIO definitions - they should probably be moved here.
63 */
64 void dreamcast_configure( )
65 {
66 dreamcast_register_module( &eventq_module );
67 /* Register the memory framework */
68 dreamcast_register_module( &mem_module );
70 /* Setup standard memory map */
71 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
72 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
73 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
74 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
75 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
76 0x00000000, 0x00200000, 0x89f2b1a1,
77 MEM_REGION_BIOS );
78 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
79 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
80 0x00200000, 0x00020000 );
82 /* Load in the rest of the core modules */
83 dreamcast_register_module( &sh4_module );
84 dreamcast_register_module( &asic_module );
85 dreamcast_register_module( &pvr2_module );
86 dreamcast_register_module( &aica_module );
87 dreamcast_register_module( &maple_module );
88 dreamcast_register_module( &ide_module );
89 }
91 void dreamcast_config_changed(void)
92 {
93 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
94 0x00000000, 0x00200000, 0x89f2b1a1,
95 MEM_REGION_BIOS );
96 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
97 0x00200000, 0x00020000 );
98 }
100 void dreamcast_save_flash()
101 {
102 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
103 mem_save_block( file, 0x00200000, 0x00020000 );
104 }
106 /**
107 * Constructs a system configuration for the AICA in standalone mode,
108 * ie sound chip only.
109 */
110 void dreamcast_configure_aica_only( )
111 {
112 dreamcast_register_module( &mem_module );
113 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
114 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
115 dreamcast_register_module( &aica_module );
116 aica_enable();
117 dreamcast_state = STATE_STOPPED;
118 }
120 void dreamcast_register_module( dreamcast_module_t module )
121 {
122 modules[num_modules++] = module;
123 if( module->init != NULL )
124 module->init();
125 }
128 void dreamcast_init( void )
129 {
130 dreamcast_configure();
131 dreamcast_state = STATE_STOPPED;
132 }
134 void dreamcast_reset( void )
135 {
136 int i;
137 if( sh4_xlat_is_running() ) {
138 sh4_translate_exit( XLAT_EXIT_SYSRESET );
139 }
140 for( i=0; i<num_modules; i++ ) {
141 if( modules[i]->reset != NULL )
142 modules[i]->reset();
143 }
144 }
146 void dreamcast_run( void )
147 {
148 int i;
149 if( dreamcast_state != STATE_RUNNING ) {
150 for( i=0; i<num_modules; i++ ) {
151 if( modules[i]->start != NULL )
152 modules[i]->start();
153 }
154 }
155 dreamcast_state = STATE_RUNNING;
156 while( dreamcast_state == STATE_RUNNING ) {
157 int time_to_run = timeslice_length;
158 for( i=0; i<num_modules; i++ ) {
159 if( modules[i]->run_time_slice != NULL )
160 time_to_run = modules[i]->run_time_slice( time_to_run );
161 }
163 }
165 for( i=0; i<num_modules; i++ ) {
166 if( modules[i]->stop != NULL )
167 modules[i]->stop();
168 }
169 dreamcast_state = STATE_STOPPED;
170 }
172 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
173 {
175 int i;
176 if( dreamcast_state != STATE_RUNNING ) {
177 for( i=0; i<num_modules; i++ ) {
178 if( modules[i]->start != NULL )
179 modules[i]->start();
180 }
181 }
182 dreamcast_state = STATE_RUNNING;
183 uint32_t nanos = 0;
184 if( nanosecs != 0 ) {
185 nanos = 1000000000 - nanosecs;
186 seconds++;
187 }
188 while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
189 int time_to_run = timeslice_length;
190 for( i=0; i<num_modules; i++ ) {
191 if( modules[i]->run_time_slice != NULL )
192 time_to_run = modules[i]->run_time_slice( time_to_run );
193 }
194 nanos += time_to_run;
195 if( nanos >= 1000000000 ) {
196 nanos -= 1000000000;
197 seconds--;
198 }
199 }
201 for( i=0; i<num_modules; i++ ) {
202 if( modules[i]->stop != NULL )
203 modules[i]->stop();
204 }
205 dreamcast_state = STATE_STOPPED;
206 }
208 void dreamcast_stop( void )
209 {
210 if( sh4_xlat_is_running() ) {
211 sh4_translate_exit( XLAT_EXIT_HALT );
212 }
213 if( dreamcast_state == STATE_RUNNING )
214 dreamcast_state = STATE_STOPPING;
215 }
217 void dreamcast_shutdown()
218 {
219 dreamcast_stop();
220 dreamcast_save_flash();
221 }
223 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
224 {
225 if( dreamcast_program_name != NULL ) {
226 g_free(dreamcast_program_name);
227 }
228 dreamcast_program_name = g_strdup(name);
229 dreamcast_entry_point = entry_point;
230 sh4_set_pc(entry_point);
231 bios_install();
232 dcload_install();
233 gui_update_state();
234 }
236 gboolean dreamcast_is_running( void )
237 {
238 return dreamcast_state == STATE_RUNNING;
239 }
241 gboolean dreamcast_can_run(void)
242 {
243 return dreamcast_state != STATE_UNINIT &&
244 (dreamcast_has_bios || dreamcast_program_name != NULL);
245 }
247 /********************************* Save States *****************************/
249 struct save_state_header {
250 char magic[16];
251 uint32_t version;
252 uint32_t module_count;
253 };
255 struct chunk_header {
256 char marker[4]; /* Always BLCK */
257 char name[8]; /* Block (module) name */
258 uint32_t block_length;
259 };
261 /**
262 * Check the save state header to ensure that it is a valid, supported
263 * file.
264 * @return the number of blocks following, or 0 if the file is invalid.
265 */
266 int dreamcast_read_save_state_header( FILE *f )
267 {
268 struct save_state_header header;
269 if( fread( &header, sizeof(header), 1, f ) != 1 ) {
270 return 0;
271 }
272 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
273 ERROR( "Not a %s save state file", APP_NAME );
274 return 0;
275 }
276 if( header.version != DREAMCAST_SAVE_VERSION ) {
277 ERROR( "%s save state version not supported", APP_NAME );
278 return 0;
279 }
280 if( header.module_count > MAX_MODULES ) {
281 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
282 return 0;
283 }
284 return header.module_count;
285 }
287 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
288 {
289 struct chunk_header head;
291 memcpy( head.marker, "BLCK", 4 );
292 memset( head.name, 0, 8 );
293 memcpy( head.name, name, strlen(name) );
294 head.block_length = length;
295 return fwrite( &head, sizeof(head), 1, f );
296 }
299 frame_buffer_t dreamcast_load_preview( const gchar *filename )
300 {
301 int i;
302 FILE *f = fopen( filename, "r" );
303 if( f == NULL ) return NULL;
305 int module_count = dreamcast_read_save_state_header(f);
306 if( module_count <= 0 ) {
307 fclose(f);
308 return NULL;
309 }
310 for( i=0; i<module_count; i++ ) {
311 struct chunk_header head;
312 if( fread( &head, sizeof(head), 1, f ) != 1 ) {
313 fclose(f);
314 return NULL;
315 }
316 if( memcmp("BLCK", head.marker, 4) != 0 ) {
317 fclose(f);
318 return NULL;
319 }
321 if( strcmp("PVR2", head.name) == 0 ) {
322 uint32_t buf_count;
323 int has_front;
324 fread( &buf_count, sizeof(buf_count), 1, f );
325 fread( &has_front, sizeof(has_front), 1, f );
326 if( buf_count != 0 && has_front ) {
327 frame_buffer_t result = read_png_from_stream(f);
328 fclose(f);
329 return result;
330 }
331 break;
332 } else {
333 fseek( f, head.block_length, SEEK_CUR );
334 }
335 }
336 return NULL;
337 }
339 int dreamcast_load_state( const gchar *filename )
340 {
341 int i,j;
342 int module_count;
343 int have_read[MAX_MODULES];
345 FILE *f = fopen( filename, "r" );
346 if( f == NULL ) return errno;
348 module_count = dreamcast_read_save_state_header(f);
349 if( module_count <= 0 ) {
350 fclose(f);
351 return 1;
352 }
354 for( i=0; i<MAX_MODULES; i++ ) {
355 have_read[i] = 0;
356 }
358 for( i=0; i<module_count; i++ ) {
359 struct chunk_header chunk;
360 fread( &chunk, sizeof(chunk), 1, f );
361 if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
362 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
363 fclose(f);
364 return 2;
365 }
367 /* Find the matching module by name */
368 for( j=0; j<num_modules; j++ ) {
369 if( strcmp(modules[j]->name,chunk.name) == 0 ) {
370 have_read[j] = 1;
371 if( modules[j]->load == NULL ) {
372 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
373 fclose(f);
374 return 2;
375 } else if( modules[j]->load(f) != 0 ) {
376 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
377 fclose(f);
378 return 2;
379 }
380 break;
381 }
382 }
383 if( j == num_modules ) {
384 fclose(f);
385 ERROR( "%s save state contains unrecognized section", APP_NAME );
386 return 2;
387 }
388 }
390 /* Any modules that we didn't load - reset to the default state.
391 * (ie it's not an error to skip a module if you don't actually
392 * care about its state).
393 */
394 for( j=0; j<num_modules; j++ ) {
395 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
396 modules[j]->reset();
397 }
398 }
399 fclose(f);
400 INFO( "Save state read from %s", filename );
401 return 0;
402 }
404 int dreamcast_save_state( const gchar *filename )
405 {
406 int i;
407 FILE *f;
408 struct save_state_header header;
410 f = fopen( filename, "w" );
411 if( f == NULL )
412 return errno;
413 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
414 header.version = DREAMCAST_SAVE_VERSION;
415 header.module_count = 0;
417 for( i=0; i<num_modules; i++ ) {
418 if( modules[i]->save != NULL )
419 header.module_count++;
420 }
421 fwrite( &header, sizeof(header), 1, f );
422 for( i=0; i<num_modules; i++ ) {
423 if( modules[i]->save != NULL ) {
424 uint32_t blocklen, posn1, posn2;
425 dreamcast_write_chunk_header( modules[i]->name, 0, f );
426 posn1 = ftell(f);
427 modules[i]->save(f);
428 posn2 = ftell(f);
429 blocklen = posn2 - posn1;
430 fseek( f, posn1-4, SEEK_SET );
431 fwrite( &blocklen, sizeof(blocklen), 1, f );
432 fseek( f, posn2, SEEK_SET );
433 }
434 }
435 fclose( f );
436 INFO( "Save state written to %s", filename );
437 return 0;
438 }
.