filename | src/dreamcast.c |
changeset | 740:dd11269ee48b |
prev | 736:a02d1475ccfd |
next | 742:e730ba4938f6 |
author | nkeynes |
date | Wed Jul 16 10:40:10 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Rationalize the two SH4 run slice impls into sh4.c, and tidy up the vm exits. Fixes broken soft-reset with emulator core Fixes broken build without translator |
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 "dream.h"
24 #include "mem.h"
25 #include "dreamcast.h"
26 #include "asic.h"
27 #include "syscall.h"
28 #include "gui.h"
29 #include "aica/aica.h"
30 #include "gdrom/ide.h"
31 #include "maple/maple.h"
32 #include "sh4/sh4.h"
33 #include "sh4/sh4core.h"
35 /**
36 * Current state of the DC virtual machine
37 */
38 typedef enum { STATE_UNINIT=0, STATE_RUNNING,
39 STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
41 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
42 static gboolean dreamcast_has_bios = FALSE;
43 static gboolean dreamcast_exit_on_stop = FALSE;
44 static gchar *dreamcast_program_name = NULL;
45 static sh4addr_t dreamcast_entry_point = 0xA0000000;
46 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
47 static uint64_t run_time_nanosecs = 0;
49 #define MAX_MODULES 32
50 static int num_modules = 0;
51 dreamcast_module_t modules[MAX_MODULES];
53 /**
54 * The unknown module is used for logging files without an actual module
55 * declaration
56 */
57 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
58 NULL, NULL, NULL };
60 /**
61 * This function is responsible for defining how all the pieces of the
62 * dreamcast actually fit together.
63 *
64 * Note currently the locations of the various MMIO pages are hard coded in
65 * the MMIO definitions - they should probably be moved here.
66 */
67 void dreamcast_configure( )
68 {
69 dreamcast_register_module( &eventq_module );
70 /* Register the memory framework */
71 dreamcast_register_module( &mem_module );
73 /* Setup standard memory map */
74 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
75 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
76 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
77 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
78 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
79 0x00000000, 0x00200000, 0x89f2b1a1,
80 MEM_REGION_BIOS );
81 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
82 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
83 0x00200000, 0x00020000 );
85 /* Load in the rest of the core modules */
86 dreamcast_register_module( &sh4_module );
87 dreamcast_register_module( &asic_module );
88 dreamcast_register_module( &pvr2_module );
89 dreamcast_register_module( &aica_module );
90 dreamcast_register_module( &maple_module );
91 dreamcast_register_module( &ide_module );
92 }
94 void dreamcast_config_changed(void)
95 {
96 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
97 0x00000000, 0x00200000, 0x89f2b1a1,
98 MEM_REGION_BIOS );
99 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
100 0x00200000, 0x00020000 );
101 }
103 void dreamcast_save_flash()
104 {
105 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
106 mem_save_block( file, 0x00200000, 0x00020000 );
107 }
109 /**
110 * Constructs a system configuration for the AICA in standalone mode,
111 * ie sound chip only.
112 */
113 void dreamcast_configure_aica_only( )
114 {
115 dreamcast_register_module( &mem_module );
116 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
117 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
118 dreamcast_register_module( &aica_module );
119 aica_enable();
120 dreamcast_state = STATE_STOPPED;
121 }
123 void dreamcast_register_module( dreamcast_module_t module )
124 {
125 modules[num_modules++] = module;
126 if( module->init != NULL )
127 module->init();
128 }
130 void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
131 {
132 run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
133 }
135 void dreamcast_set_exit_on_stop( gboolean flag )
136 {
137 dreamcast_exit_on_stop = flag;
138 }
140 void dreamcast_init( void )
141 {
142 dreamcast_configure();
143 dreamcast_state = STATE_STOPPED;
144 }
146 void dreamcast_reset( void )
147 {
148 int i;
149 for( i=0; i<num_modules; i++ ) {
150 if( modules[i]->reset != NULL )
151 modules[i]->reset();
152 }
153 }
155 void dreamcast_run( void )
156 {
157 int i;
159 if( dreamcast_state != STATE_RUNNING ) {
160 for( i=0; i<num_modules; i++ ) {
161 if( modules[i]->start != NULL )
162 modules[i]->start();
163 }
164 }
166 dreamcast_state = STATE_RUNNING;
168 if( run_time_nanosecs != 0 ) {
169 while( dreamcast_state == STATE_RUNNING ) {
170 uint32_t time_to_run = timeslice_length;
171 if( run_time_nanosecs < time_to_run ) {
172 time_to_run = (uint32_t)run_time_nanosecs;
173 }
175 for( i=0; i<num_modules; i++ ) {
176 if( modules[i]->run_time_slice != NULL )
177 time_to_run = modules[i]->run_time_slice( time_to_run );
178 }
180 if( run_time_nanosecs > time_to_run ) {
181 run_time_nanosecs -= time_to_run;
182 } else {
183 run_time_nanosecs = 0; // Finished
184 break;
185 }
186 }
187 } else {
188 while( dreamcast_state == STATE_RUNNING ) {
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 }
195 }
196 }
198 for( i=0; i<num_modules; i++ ) {
199 if( modules[i]->stop != NULL )
200 modules[i]->stop();
201 }
202 dreamcast_state = STATE_STOPPED;
204 if( dreamcast_exit_on_stop ) {
205 dreamcast_shutdown();
206 exit(0);
207 }
208 }
210 void dreamcast_stop( void )
211 {
212 sh4_core_exit(CORE_EXIT_HALT); // returns only if not inside SH4 core
213 if( dreamcast_state == STATE_RUNNING )
214 dreamcast_state = STATE_STOPPING;
215 }
217 void dreamcast_shutdown()
218 {
219 // Don't do a dreamcast_stop - if we're calling this out of SH4 code,
220 // it's a shutdown-and-quit event
221 if( dreamcast_state == STATE_RUNNING )
222 dreamcast_state = STATE_STOPPING;
223 dreamcast_save_flash();
224 #ifdef ENABLE_SH4STATS
225 sh4_stats_print(stdout);
226 #endif
227 }
229 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
230 {
231 if( dreamcast_program_name != NULL ) {
232 g_free(dreamcast_program_name);
233 }
234 dreamcast_program_name = g_strdup(name);
235 dreamcast_entry_point = entry_point;
236 sh4_set_pc(entry_point);
237 bios_install();
238 dcload_install();
239 gui_update_state();
240 }
242 gboolean dreamcast_is_running( void )
243 {
244 return dreamcast_state == STATE_RUNNING;
245 }
247 gboolean dreamcast_can_run(void)
248 {
249 return dreamcast_state != STATE_UNINIT &&
250 (dreamcast_has_bios || dreamcast_program_name != NULL);
251 }
253 /********************************* Save States *****************************/
255 struct save_state_header {
256 char magic[16];
257 uint32_t version;
258 uint32_t module_count;
259 };
261 struct chunk_header {
262 char marker[4]; /* Always BLCK */
263 char name[8]; /* Block (module) name */
264 uint32_t block_length;
265 };
267 /**
268 * Check the save state header to ensure that it is a valid, supported
269 * file.
270 * @return the number of blocks following, or 0 if the file is invalid.
271 */
272 int dreamcast_read_save_state_header( FILE *f )
273 {
274 struct save_state_header header;
275 if( fread( &header, sizeof(header), 1, f ) != 1 ) {
276 return 0;
277 }
278 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
279 ERROR( "Not a %s save state file", APP_NAME );
280 return 0;
281 }
282 if( header.version != DREAMCAST_SAVE_VERSION ) {
283 ERROR( "%s save state version not supported", APP_NAME );
284 return 0;
285 }
286 if( header.module_count > MAX_MODULES ) {
287 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
288 return 0;
289 }
290 return header.module_count;
291 }
293 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
294 {
295 struct chunk_header head;
297 memcpy( head.marker, "BLCK", 4 );
298 memset( head.name, 0, 8 );
299 memcpy( head.name, name, strlen(name) );
300 head.block_length = length;
301 return fwrite( &head, sizeof(head), 1, f );
302 }
305 frame_buffer_t dreamcast_load_preview( const gchar *filename )
306 {
307 int i;
308 FILE *f = fopen( filename, "r" );
309 if( f == NULL ) return NULL;
311 int module_count = dreamcast_read_save_state_header(f);
312 if( module_count <= 0 ) {
313 fclose(f);
314 return NULL;
315 }
316 for( i=0; i<module_count; i++ ) {
317 struct chunk_header head;
318 if( fread( &head, sizeof(head), 1, f ) != 1 ) {
319 fclose(f);
320 return NULL;
321 }
322 if( memcmp("BLCK", head.marker, 4) != 0 ) {
323 fclose(f);
324 return NULL;
325 }
327 if( strcmp("PVR2", head.name) == 0 ) {
328 uint32_t buf_count;
329 int has_front;
330 fread( &buf_count, sizeof(buf_count), 1, f );
331 fread( &has_front, sizeof(has_front), 1, f );
332 if( buf_count != 0 && has_front ) {
333 frame_buffer_t result = read_png_from_stream(f);
334 fclose(f);
335 return result;
336 }
337 break;
338 } else {
339 fseek( f, head.block_length, SEEK_CUR );
340 }
341 }
342 return NULL;
343 }
345 int dreamcast_load_state( const gchar *filename )
346 {
347 int i,j;
348 int module_count;
349 int have_read[MAX_MODULES];
351 FILE *f = fopen( filename, "r" );
352 if( f == NULL ) return errno;
354 module_count = dreamcast_read_save_state_header(f);
355 if( module_count <= 0 ) {
356 fclose(f);
357 return 1;
358 }
360 for( i=0; i<MAX_MODULES; i++ ) {
361 have_read[i] = 0;
362 }
364 for( i=0; i<module_count; i++ ) {
365 struct chunk_header chunk;
366 fread( &chunk, sizeof(chunk), 1, f );
367 if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
368 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
369 fclose(f);
370 return 2;
371 }
373 /* Find the matching module by name */
374 for( j=0; j<num_modules; j++ ) {
375 if( strcmp(modules[j]->name,chunk.name) == 0 ) {
376 have_read[j] = 1;
377 if( modules[j]->load == NULL ) {
378 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
379 fclose(f);
380 return 2;
381 } else if( modules[j]->load(f) != 0 ) {
382 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
383 fclose(f);
384 return 2;
385 }
386 break;
387 }
388 }
389 if( j == num_modules ) {
390 fclose(f);
391 ERROR( "%s save state contains unrecognized section", APP_NAME );
392 return 2;
393 }
394 }
396 /* Any modules that we didn't load - reset to the default state.
397 * (ie it's not an error to skip a module if you don't actually
398 * care about its state).
399 */
400 for( j=0; j<num_modules; j++ ) {
401 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
402 modules[j]->reset();
403 }
404 }
405 fclose(f);
406 INFO( "Save state read from %s", filename );
407 return 0;
408 }
410 int dreamcast_save_state( const gchar *filename )
411 {
412 int i;
413 FILE *f;
414 struct save_state_header header;
416 f = fopen( filename, "w" );
417 if( f == NULL )
418 return errno;
419 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
420 header.version = DREAMCAST_SAVE_VERSION;
421 header.module_count = 0;
423 for( i=0; i<num_modules; i++ ) {
424 if( modules[i]->save != NULL )
425 header.module_count++;
426 }
427 fwrite( &header, sizeof(header), 1, f );
428 for( i=0; i<num_modules; i++ ) {
429 if( modules[i]->save != NULL ) {
430 uint32_t blocklen, posn1, posn2;
431 dreamcast_write_chunk_header( modules[i]->name, 0, f );
432 posn1 = ftell(f);
433 modules[i]->save(f);
434 posn2 = ftell(f);
435 blocklen = posn2 - posn1;
436 fseek( f, posn1-4, SEEK_SET );
437 fwrite( &blocklen, sizeof(blocklen), 1, f );
438 fseek( f, posn2, SEEK_SET );
439 }
440 }
441 fclose( f );
442 INFO( "Save state written to %s", filename );
443 return 0;
444 }
.