Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 671:a530ea88eebd
prev669:ab344e42bca9
next689:9868667e3525
author nkeynes
date Sun Jun 01 00:47:45 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change First cut of the Cocoa GUI implementation
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 #ifdef ENABLE_SH4STATS
   222     sh4_stats_print(stdout);
   223 #endif
   224 }
   226 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
   227 {
   228     if( dreamcast_program_name != NULL ) {
   229         g_free(dreamcast_program_name);
   230     }
   231     dreamcast_program_name = g_strdup(name);
   232     dreamcast_entry_point = entry_point;
   233     sh4_set_pc(entry_point);
   234     bios_install();
   235     dcload_install();
   236     gui_update_state();
   237 }
   239 gboolean dreamcast_is_running( void )
   240 {
   241     return dreamcast_state == STATE_RUNNING;
   242 }
   244 gboolean dreamcast_can_run(void)
   245 {
   246     return dreamcast_state != STATE_UNINIT &&
   247       (dreamcast_has_bios || dreamcast_program_name != NULL);
   248 }
   250 /********************************* Save States *****************************/
   252 struct save_state_header {
   253     char magic[16];
   254     uint32_t version;
   255     uint32_t module_count;
   256 };
   258 struct chunk_header {
   259     char marker[4]; /* Always BLCK */
   260     char name[8]; /* Block (module) name */
   261     uint32_t block_length;
   262 };
   264 /**
   265  * Check the save state header to ensure that it is a valid, supported
   266  * file. 
   267  * @return the number of blocks following, or 0 if the file is invalid.
   268  */
   269 int dreamcast_read_save_state_header( FILE *f )
   270 {
   271     struct save_state_header header;
   272     if( fread( &header, sizeof(header), 1, f ) != 1 ) {
   273 	return 0;
   274     }
   275     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   276 	ERROR( "Not a %s save state file", APP_NAME );
   277 	return 0;
   278     }
   279     if( header.version != DREAMCAST_SAVE_VERSION ) {
   280 	ERROR( "%s save state version not supported", APP_NAME );
   281 	return 0;
   282     }
   283     if( header.module_count > MAX_MODULES ) {
   284 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   285 	return 0;
   286     }
   287     return header.module_count;
   288 }
   290 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
   291 {
   292     struct chunk_header head;
   294     memcpy( head.marker, "BLCK", 4 );
   295     memset( head.name, 0, 8 );
   296     memcpy( head.name, name, strlen(name) );
   297     head.block_length = length;
   298     return fwrite( &head, sizeof(head), 1, f );
   299 }
   302 frame_buffer_t dreamcast_load_preview( const gchar *filename )
   303 {
   304     int i;
   305     FILE *f = fopen( filename, "r" );
   306     if( f == NULL ) return NULL;
   308     int module_count = dreamcast_read_save_state_header(f);
   309     if( module_count <= 0 ) {
   310 	fclose(f);
   311 	return NULL;
   312     }
   313     for( i=0; i<module_count; i++ ) {
   314 	struct chunk_header head;
   315 	if( fread( &head, sizeof(head), 1, f ) != 1 ) {
   316 	    fclose(f);
   317 	    return NULL;
   318 	}
   319 	if( memcmp("BLCK", head.marker, 4) != 0 ) {
   320 	    fclose(f);
   321 	    return NULL;
   322 	}
   324 	if( strcmp("PVR2", head.name) == 0 ) {
   325 	    uint32_t buf_count;
   326 	    int has_front;
   327 	    fread( &buf_count, sizeof(buf_count), 1, f );
   328 	    fread( &has_front, sizeof(has_front), 1, f );
   329 	    if( buf_count != 0 && has_front ) {
   330 		frame_buffer_t result = read_png_from_stream(f);
   331 		fclose(f);
   332 		return result;
   333 	    }
   334 	    break;
   335 	} else {
   336 	    fseek( f, head.block_length, SEEK_CUR );
   337 	}
   338     }
   339     return NULL;
   340 }
   342 int dreamcast_load_state( const gchar *filename )
   343 {
   344     int i,j;
   345     int module_count;
   346     int have_read[MAX_MODULES];
   348     FILE *f = fopen( filename, "r" );
   349     if( f == NULL ) return errno;
   351     module_count = dreamcast_read_save_state_header(f);
   352     if( module_count <= 0 ) {
   353 	fclose(f);
   354 	return 1;
   355     }
   357     for( i=0; i<MAX_MODULES; i++ ) {
   358 	have_read[i] = 0;
   359     }
   361     for( i=0; i<module_count; i++ ) {
   362 	struct chunk_header chunk;
   363 	fread( &chunk, sizeof(chunk), 1, f );
   364 	if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
   365 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   366 	    fclose(f);
   367 	    return 2;
   368 	}
   370 	/* Find the matching module by name */
   371 	for( j=0; j<num_modules; j++ ) {
   372 	    if( strcmp(modules[j]->name,chunk.name) == 0 ) {
   373 		have_read[j] = 1;
   374 		if( modules[j]->load == NULL ) {
   375 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   376 		    fclose(f);
   377 		    return 2;
   378 		} else if( modules[j]->load(f) != 0 ) {
   379 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   380 		    fclose(f);
   381 		    return 2;
   382 		}
   383 		break;
   384 	    }
   385 	}
   386 	if( j == num_modules ) {
   387 	    fclose(f);
   388 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   389 	    return 2;
   390 	}
   391     }
   393     /* Any modules that we didn't load - reset to the default state.
   394      * (ie it's not an error to skip a module if you don't actually
   395      * care about its state).
   396      */
   397     for( j=0; j<num_modules; j++ ) {
   398 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   399 	    modules[j]->reset();
   400 	}
   401     }
   402     fclose(f);
   403     INFO( "Save state read from %s", filename );
   404     return 0;
   405 }
   407 int dreamcast_save_state( const gchar *filename )
   408 {
   409     int i;
   410     FILE *f;
   411     struct save_state_header header;
   413     f = fopen( filename, "w" );
   414     if( f == NULL )
   415 	return errno;
   416     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   417     header.version = DREAMCAST_SAVE_VERSION;
   418     header.module_count = 0;
   420     for( i=0; i<num_modules; i++ ) {
   421 	if( modules[i]->save != NULL )
   422 	    header.module_count++;
   423     }
   424     fwrite( &header, sizeof(header), 1, f );
   425     for( i=0; i<num_modules; i++ ) {
   426 	if( modules[i]->save != NULL ) {
   427 	    uint32_t blocklen, posn1, posn2;
   428 	    dreamcast_write_chunk_header( modules[i]->name, 0, f );
   429 	    posn1 = ftell(f);
   430 	    modules[i]->save(f);
   431 	    posn2 = ftell(f);
   432 	    blocklen = posn2 - posn1;
   433 	    fseek( f, posn1-4, SEEK_SET );
   434 	    fwrite( &blocklen, sizeof(blocklen), 1, f );
   435 	    fseek( f, posn2, SEEK_SET );
   436 	}
   437     }
   438     fclose( f );
   439     INFO( "Save state written to %s", filename );
   440     return 0;
   441 }
.