filename | src/dreamcast.c |
changeset | 689:9868667e3525 |
prev | 671:a530ea88eebd |
next | 727:f934967b77a3 |
author | nkeynes |
date | Sun Jun 22 06:49:00 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Big cleanup of the command-line options Add an actual manpage (*gasp*) |
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 gboolean dreamcast_exit_on_stop = FALSE;
43 static gchar *dreamcast_program_name = NULL;
44 static sh4addr_t dreamcast_entry_point = 0xA0000000;
45 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
46 static uint64_t run_time_nanosecs = 0;
48 #define MAX_MODULES 32
49 static int num_modules = 0;
50 dreamcast_module_t modules[MAX_MODULES];
52 /**
53 * The unknown module is used for logging files without an actual module
54 * declaration
55 */
56 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL,
57 NULL, NULL, NULL };
59 /**
60 * This function is responsible for defining how all the pieces of the
61 * dreamcast actually fit together.
62 *
63 * Note currently the locations of the various MMIO pages are hard coded in
64 * the MMIO definitions - they should probably be moved here.
65 */
66 void dreamcast_configure( )
67 {
68 dreamcast_register_module( &eventq_module );
69 /* Register the memory framework */
70 dreamcast_register_module( &mem_module );
72 /* Setup standard memory map */
73 mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
74 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
75 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
76 mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
77 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
78 0x00000000, 0x00200000, 0x89f2b1a1,
79 MEM_REGION_BIOS );
80 mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
81 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
82 0x00200000, 0x00020000 );
84 /* Load in the rest of the core modules */
85 dreamcast_register_module( &sh4_module );
86 dreamcast_register_module( &asic_module );
87 dreamcast_register_module( &pvr2_module );
88 dreamcast_register_module( &aica_module );
89 dreamcast_register_module( &maple_module );
90 dreamcast_register_module( &ide_module );
91 }
93 void dreamcast_config_changed(void)
94 {
95 dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
96 0x00000000, 0x00200000, 0x89f2b1a1,
97 MEM_REGION_BIOS );
98 mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
99 0x00200000, 0x00020000 );
100 }
102 void dreamcast_save_flash()
103 {
104 const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
105 mem_save_block( file, 0x00200000, 0x00020000 );
106 }
108 /**
109 * Constructs a system configuration for the AICA in standalone mode,
110 * ie sound chip only.
111 */
112 void dreamcast_configure_aica_only( )
113 {
114 dreamcast_register_module( &mem_module );
115 mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
116 mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
117 dreamcast_register_module( &aica_module );
118 aica_enable();
119 dreamcast_state = STATE_STOPPED;
120 }
122 void dreamcast_register_module( dreamcast_module_t module )
123 {
124 modules[num_modules++] = module;
125 if( module->init != NULL )
126 module->init();
127 }
129 void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
130 {
131 run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
132 }
134 void dreamcast_set_exit_on_stop( gboolean flag )
135 {
136 dreamcast_exit_on_stop = flag;
137 }
139 void dreamcast_init( void )
140 {
141 dreamcast_configure();
142 dreamcast_state = STATE_STOPPED;
143 }
145 void dreamcast_reset( void )
146 {
147 int i;
148 if( sh4_xlat_is_running() ) {
149 sh4_translate_exit( XLAT_EXIT_SYSRESET );
150 }
151 for( i=0; i<num_modules; i++ ) {
152 if( modules[i]->reset != NULL )
153 modules[i]->reset();
154 }
155 }
157 void dreamcast_run( void )
158 {
159 int i;
161 if( dreamcast_state != STATE_RUNNING ) {
162 for( i=0; i<num_modules; i++ ) {
163 if( modules[i]->start != NULL )
164 modules[i]->start();
165 }
166 }
168 dreamcast_state = STATE_RUNNING;
170 if( run_time_nanosecs != 0 ) {
171 while( dreamcast_state == STATE_RUNNING ) {
172 uint32_t time_to_run = timeslice_length;
173 if( run_time_nanosecs < time_to_run ) {
174 time_to_run = (uint32_t)run_time_nanosecs;
175 }
177 for( i=0; i<num_modules; i++ ) {
178 if( modules[i]->run_time_slice != NULL )
179 time_to_run = modules[i]->run_time_slice( time_to_run );
180 }
182 if( run_time_nanosecs > time_to_run ) {
183 run_time_nanosecs -= time_to_run;
184 } else {
185 run_time_nanosecs = 0; // Finished
186 break;
187 }
188 }
189 } else {
190 while( dreamcast_state == STATE_RUNNING ) {
191 int time_to_run = timeslice_length;
192 for( i=0; i<num_modules; i++ ) {
193 if( modules[i]->run_time_slice != NULL )
194 time_to_run = modules[i]->run_time_slice( time_to_run );
195 }
197 }
198 }
200 for( i=0; i<num_modules; i++ ) {
201 if( modules[i]->stop != NULL )
202 modules[i]->stop();
203 }
204 dreamcast_state = STATE_STOPPED;
206 if( dreamcast_exit_on_stop ) {
207 dreamcast_shutdown();
208 exit(0);
209 }
210 }
212 void dreamcast_stop( void )
213 {
214 if( sh4_xlat_is_running() ) {
215 sh4_translate_exit( XLAT_EXIT_HALT );
216 }
217 if( dreamcast_state == STATE_RUNNING )
218 dreamcast_state = STATE_STOPPING;
219 }
221 void dreamcast_shutdown()
222 {
223 dreamcast_stop();
224 dreamcast_save_flash();
225 #ifdef ENABLE_SH4STATS
226 sh4_stats_print(stdout);
227 #endif
228 }
230 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
231 {
232 if( dreamcast_program_name != NULL ) {
233 g_free(dreamcast_program_name);
234 }
235 dreamcast_program_name = g_strdup(name);
236 dreamcast_entry_point = entry_point;
237 sh4_set_pc(entry_point);
238 bios_install();
239 dcload_install();
240 gui_update_state();
241 }
243 gboolean dreamcast_is_running( void )
244 {
245 return dreamcast_state == STATE_RUNNING;
246 }
248 gboolean dreamcast_can_run(void)
249 {
250 return dreamcast_state != STATE_UNINIT &&
251 (dreamcast_has_bios || dreamcast_program_name != NULL);
252 }
254 /********************************* Save States *****************************/
256 struct save_state_header {
257 char magic[16];
258 uint32_t version;
259 uint32_t module_count;
260 };
262 struct chunk_header {
263 char marker[4]; /* Always BLCK */
264 char name[8]; /* Block (module) name */
265 uint32_t block_length;
266 };
268 /**
269 * Check the save state header to ensure that it is a valid, supported
270 * file.
271 * @return the number of blocks following, or 0 if the file is invalid.
272 */
273 int dreamcast_read_save_state_header( FILE *f )
274 {
275 struct save_state_header header;
276 if( fread( &header, sizeof(header), 1, f ) != 1 ) {
277 return 0;
278 }
279 if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
280 ERROR( "Not a %s save state file", APP_NAME );
281 return 0;
282 }
283 if( header.version != DREAMCAST_SAVE_VERSION ) {
284 ERROR( "%s save state version not supported", APP_NAME );
285 return 0;
286 }
287 if( header.module_count > MAX_MODULES ) {
288 ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
289 return 0;
290 }
291 return header.module_count;
292 }
294 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
295 {
296 struct chunk_header head;
298 memcpy( head.marker, "BLCK", 4 );
299 memset( head.name, 0, 8 );
300 memcpy( head.name, name, strlen(name) );
301 head.block_length = length;
302 return fwrite( &head, sizeof(head), 1, f );
303 }
306 frame_buffer_t dreamcast_load_preview( const gchar *filename )
307 {
308 int i;
309 FILE *f = fopen( filename, "r" );
310 if( f == NULL ) return NULL;
312 int module_count = dreamcast_read_save_state_header(f);
313 if( module_count <= 0 ) {
314 fclose(f);
315 return NULL;
316 }
317 for( i=0; i<module_count; i++ ) {
318 struct chunk_header head;
319 if( fread( &head, sizeof(head), 1, f ) != 1 ) {
320 fclose(f);
321 return NULL;
322 }
323 if( memcmp("BLCK", head.marker, 4) != 0 ) {
324 fclose(f);
325 return NULL;
326 }
328 if( strcmp("PVR2", head.name) == 0 ) {
329 uint32_t buf_count;
330 int has_front;
331 fread( &buf_count, sizeof(buf_count), 1, f );
332 fread( &has_front, sizeof(has_front), 1, f );
333 if( buf_count != 0 && has_front ) {
334 frame_buffer_t result = read_png_from_stream(f);
335 fclose(f);
336 return result;
337 }
338 break;
339 } else {
340 fseek( f, head.block_length, SEEK_CUR );
341 }
342 }
343 return NULL;
344 }
346 int dreamcast_load_state( const gchar *filename )
347 {
348 int i,j;
349 int module_count;
350 int have_read[MAX_MODULES];
352 FILE *f = fopen( filename, "r" );
353 if( f == NULL ) return errno;
355 module_count = dreamcast_read_save_state_header(f);
356 if( module_count <= 0 ) {
357 fclose(f);
358 return 1;
359 }
361 for( i=0; i<MAX_MODULES; i++ ) {
362 have_read[i] = 0;
363 }
365 for( i=0; i<module_count; i++ ) {
366 struct chunk_header chunk;
367 fread( &chunk, sizeof(chunk), 1, f );
368 if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
369 ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
370 fclose(f);
371 return 2;
372 }
374 /* Find the matching module by name */
375 for( j=0; j<num_modules; j++ ) {
376 if( strcmp(modules[j]->name,chunk.name) == 0 ) {
377 have_read[j] = 1;
378 if( modules[j]->load == NULL ) {
379 ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
380 fclose(f);
381 return 2;
382 } else if( modules[j]->load(f) != 0 ) {
383 ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
384 fclose(f);
385 return 2;
386 }
387 break;
388 }
389 }
390 if( j == num_modules ) {
391 fclose(f);
392 ERROR( "%s save state contains unrecognized section", APP_NAME );
393 return 2;
394 }
395 }
397 /* Any modules that we didn't load - reset to the default state.
398 * (ie it's not an error to skip a module if you don't actually
399 * care about its state).
400 */
401 for( j=0; j<num_modules; j++ ) {
402 if( have_read[j] == 0 && modules[j]->reset != NULL ) {
403 modules[j]->reset();
404 }
405 }
406 fclose(f);
407 INFO( "Save state read from %s", filename );
408 return 0;
409 }
411 int dreamcast_save_state( const gchar *filename )
412 {
413 int i;
414 FILE *f;
415 struct save_state_header header;
417 f = fopen( filename, "w" );
418 if( f == NULL )
419 return errno;
420 strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
421 header.version = DREAMCAST_SAVE_VERSION;
422 header.module_count = 0;
424 for( i=0; i<num_modules; i++ ) {
425 if( modules[i]->save != NULL )
426 header.module_count++;
427 }
428 fwrite( &header, sizeof(header), 1, f );
429 for( i=0; i<num_modules; i++ ) {
430 if( modules[i]->save != NULL ) {
431 uint32_t blocklen, posn1, posn2;
432 dreamcast_write_chunk_header( modules[i]->name, 0, f );
433 posn1 = ftell(f);
434 modules[i]->save(f);
435 posn2 = ftell(f);
436 blocklen = posn2 - posn1;
437 fseek( f, posn1-4, SEEK_SET );
438 fwrite( &blocklen, sizeof(blocklen), 1, f );
439 fseek( f, posn2, SEEK_SET );
440 }
441 }
442 fclose( f );
443 INFO( "Save state written to %s", filename );
444 return 0;
445 }
.