Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 480:d28c2992f5ee
prev477:9a373f2ff009
next543:361ec0a70cf2
author nkeynes
date Tue Nov 20 10:27:58 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Move gtk UI into gtkui subdir (prep for non-gtk builds), and protect with
an automake conditional
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.28 2007-10-31 11:53:35 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     mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
    71 		  0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
    72     mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
    73     mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
    74 		    0x00200000, 0x00020000 );
    76     /* Load in the rest of the core modules */
    77     dreamcast_register_module( &sh4_module );
    78     dreamcast_register_module( &asic_module );
    79     dreamcast_register_module( &pvr2_module );
    80     dreamcast_register_module( &aica_module );
    81     dreamcast_register_module( &maple_module );
    82     dreamcast_register_module( &ide_module );
    83 }
    85 void dreamcast_config_changed(void)
    86 {
    87     mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
    88 		  0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
    89     mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
    90 		    0x00200000, 0x00020000 );
    91 }
    93 void dreamcast_save_flash()
    94 {
    95     const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
    96     mem_save_block( file, 0x00200000, 0x00020000 );
    97 }
    99 /**
   100  * Constructs a system configuration for the AICA in standalone mode,
   101  * ie sound chip only.
   102  */
   103 void dreamcast_configure_aica_only( )
   104 {
   105     dreamcast_register_module( &mem_module );
   106     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
   107     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
   108     dreamcast_register_module( &aica_module );
   109     aica_enable();
   110     dreamcast_state = STATE_STOPPED;
   111 }
   113 void dreamcast_register_module( dreamcast_module_t module ) 
   114 {
   115     modules[num_modules++] = module;
   116     if( module->init != NULL )
   117 	module->init();
   118 }
   121 void dreamcast_init( void )
   122 {
   123     dreamcast_configure();
   124     dreamcast_state = STATE_STOPPED;
   125 }
   127 void dreamcast_reset( void )
   128 {
   129     int i;
   130     for( i=0; i<num_modules; i++ ) {
   131 	if( modules[i]->reset != NULL )
   132 	    modules[i]->reset();
   133     }
   134 }
   136 void dreamcast_run( void )
   137 {
   138     int i;
   139     if( dreamcast_state != STATE_RUNNING ) {
   140 	for( i=0; i<num_modules; i++ ) {
   141 	    if( modules[i]->start != NULL )
   142 		modules[i]->start();
   143 	}
   144     }
   145     dreamcast_state = STATE_RUNNING;
   146     while( dreamcast_state == STATE_RUNNING ) {
   147 	int time_to_run = timeslice_length;
   148 	for( i=0; i<num_modules; i++ ) {
   149 	    if( modules[i]->run_time_slice != NULL )
   150 		time_to_run = modules[i]->run_time_slice( time_to_run );
   151 	}
   153     }
   155     for( i=0; i<num_modules; i++ ) {
   156 	if( modules[i]->stop != NULL )
   157 	    modules[i]->stop();
   158     }
   159     dreamcast_state = STATE_STOPPED;
   160 }
   162 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
   163 {
   165     int i;
   166     if( dreamcast_state != STATE_RUNNING ) {
   167 	for( i=0; i<num_modules; i++ ) {
   168 	    if( modules[i]->start != NULL )
   169 		modules[i]->start();
   170 	}
   171     }
   172     dreamcast_state = STATE_RUNNING;
   173     uint32_t nanos = 0;
   174     if( nanosecs != 0 ) {
   175         nanos = 1000000000 - nanosecs;
   176 	seconds++;
   177     }
   178     while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
   179 	int time_to_run = timeslice_length;
   180 	for( i=0; i<num_modules; i++ ) {
   181 	    if( modules[i]->run_time_slice != NULL )
   182 		time_to_run = modules[i]->run_time_slice( time_to_run );
   183 	}
   184 	nanos += time_to_run;
   185 	if( nanos >= 1000000000 ) {
   186 	    nanos -= 1000000000;
   187 	    seconds--;
   188 	}
   189     }
   191     for( i=0; i<num_modules; i++ ) {
   192 	if( modules[i]->stop != NULL )
   193 	    modules[i]->stop();
   194     }
   195     dreamcast_state = STATE_STOPPED;
   196 }
   198 void dreamcast_stop( void )
   199 {
   200     if( dreamcast_state == STATE_RUNNING )
   201 	dreamcast_state = STATE_STOPPING;
   202 }
   204 void dreamcast_shutdown()
   205 {
   206     dreamcast_stop();
   207     dreamcast_save_flash();
   208 }
   210 gboolean dreamcast_is_running( void )
   211 {
   212     return dreamcast_state == STATE_RUNNING;
   213 }
   215 /********************************* Save States *****************************/
   217 struct save_state_header {
   218     char magic[16];
   219     uint32_t version;
   220     uint32_t module_count;
   221 };
   223 struct chunk_header {
   224     char marker[4]; /* Always BLCK */
   225     char name[8]; /* Block (module) name */
   226     uint32_t block_length;
   227 };
   229 /**
   230  * Check the save state header to ensure that it is a valid, supported
   231  * file. 
   232  * @return the number of blocks following, or 0 if the file is invalid.
   233  */
   234 int dreamcast_read_save_state_header( FILE *f )
   235 {
   236     struct save_state_header header;
   237     if( fread( &header, sizeof(header), 1, f ) != 1 ) {
   238 	return 0;
   239     }
   240     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   241 	ERROR( "Not a %s save state file", APP_NAME );
   242 	return 0;
   243     }
   244     if( header.version != DREAMCAST_SAVE_VERSION ) {
   245 	ERROR( "%s save state version not supported", APP_NAME );
   246 	return 0;
   247     }
   248     if( header.module_count > MAX_MODULES ) {
   249 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   250 	return 0;
   251     }
   252     return header.module_count;
   253 }
   255 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
   256 {
   257     struct chunk_header head;
   259     memcpy( head.marker, "BLCK", 4 );
   260     memset( head.name, 0, 8 );
   261     memcpy( head.name, name, strlen(name) );
   262     head.block_length = length;
   263     return fwrite( &head, sizeof(head), 1, f );
   264 }
   267 frame_buffer_t dreamcast_load_preview( const gchar *filename )
   268 {
   269     int i;
   270     FILE *f = fopen( filename, "r" );
   271     if( f == NULL ) return NULL;
   273     int module_count = dreamcast_read_save_state_header(f);
   274     if( module_count <= 0 ) {
   275 	fclose(f);
   276 	return NULL;
   277     }
   278     for( i=0; i<module_count; i++ ) {
   279 	struct chunk_header head;
   280 	if( fread( &head, sizeof(head), 1, f ) != 1 ) {
   281 	    fclose(f);
   282 	    return NULL;
   283 	}
   284 	if( memcmp("BLCK", head.marker, 4) != 0 ) {
   285 	    fclose(f);
   286 	    return NULL;
   287 	}
   289 	if( strcmp("PVR2", head.name) == 0 ) {
   290 	    uint32_t buf_count;
   291 	    int has_front;
   292 	    fread( &buf_count, sizeof(buf_count), 1, f );
   293 	    fread( &has_front, sizeof(has_front), 1, f );
   294 	    if( buf_count != 0 && has_front ) {
   295 		frame_buffer_t result = read_png_from_stream(f);
   296 		fclose(f);
   297 		return result;
   298 	    }
   299 	    break;
   300 	} else {
   301 	    fseek( f, head.block_length, SEEK_CUR );
   302 	}
   303     }
   304     return NULL;
   305 }
   307 int dreamcast_load_state( const gchar *filename )
   308 {
   309     int i,j;
   310     int module_count;
   311     int have_read[MAX_MODULES];
   313     FILE *f = fopen( filename, "r" );
   314     if( f == NULL ) return errno;
   316     module_count = dreamcast_read_save_state_header(f);
   317     if( module_count <= 0 ) {
   318 	fclose(f);
   319 	return 1;
   320     }
   322     for( i=0; i<MAX_MODULES; i++ ) {
   323 	have_read[i] = 0;
   324     }
   326     for( i=0; i<module_count; i++ ) {
   327 	struct chunk_header chunk;
   328 	fread( &chunk, sizeof(chunk), 1, f );
   329 	if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
   330 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   331 	    fclose(f);
   332 	    return 2;
   333 	}
   335 	/* Find the matching module by name */
   336 	for( j=0; j<num_modules; j++ ) {
   337 	    if( strcmp(modules[j]->name,chunk.name) == 0 ) {
   338 		have_read[j] = 1;
   339 		if( modules[j]->load == NULL ) {
   340 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   341 		    fclose(f);
   342 		    return 2;
   343 		} else if( modules[j]->load(f) != 0 ) {
   344 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   345 		    fclose(f);
   346 		    return 2;
   347 		}
   348 		break;
   349 	    }
   350 	}
   351 	if( j == num_modules ) {
   352 	    fclose(f);
   353 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   354 	    return 2;
   355 	}
   356     }
   358     /* Any modules that we didn't load - reset to the default state.
   359      * (ie it's not an error to skip a module if you don't actually
   360      * care about its state).
   361      */
   362     for( j=0; j<num_modules; j++ ) {
   363 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   364 	    modules[j]->reset();
   365 	}
   366     }
   367     fclose(f);
   368     INFO( "Save state read from %s", filename );
   369     return 0;
   370 }
   372 int dreamcast_save_state( const gchar *filename )
   373 {
   374     int i;
   375     FILE *f;
   376     struct save_state_header header;
   378     f = fopen( filename, "w" );
   379     if( f == NULL )
   380 	return errno;
   381     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   382     header.version = DREAMCAST_SAVE_VERSION;
   383     header.module_count = 0;
   385     for( i=0; i<num_modules; i++ ) {
   386 	if( modules[i]->save != NULL )
   387 	    header.module_count++;
   388     }
   389     fwrite( &header, sizeof(header), 1, f );
   390     for( i=0; i<num_modules; i++ ) {
   391 	if( modules[i]->save != NULL ) {
   392 	    uint32_t blocklen, posn1, posn2;
   393 	    dreamcast_write_chunk_header( modules[i]->name, 0, f );
   394 	    posn1 = ftell(f);
   395 	    modules[i]->save(f);
   396 	    posn2 = ftell(f);
   397 	    blocklen = posn2 - posn1;
   398 	    fseek( f, posn1-4, SEEK_SET );
   399 	    fwrite( &blocklen, sizeof(blocklen), 1, f );
   400 	    fseek( f, posn2, SEEK_SET );
   401 	}
   402     }
   403     fclose( f );
   404     INFO( "Save state written to %s", filename );
   405     return 0;
   406 }
.