Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 543:361ec0a70cf2
prev480:d28c2992f5ee
next561:533f6b478071
next586:2a3ba82cf243
author nkeynes
date Thu Nov 29 09:28:28 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Refactor GLX support and implement pbuffer rendering support
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 <glib.h>
    22 #include "dream.h"
    23 #include "config.h"
    24 #include "mem.h"
    25 #include "aica/aica.h"
    26 #include "asic.h"
    27 #include "dreamcast.h"
    28 #include "gdrom/ide.h"
    29 #include "maple/maple.h"
    31 /**
    32  * Current state of the DC virtual machine
    33  */
    34 typedef enum { STATE_UNINIT=0, STATE_RUNNING, 
    35 	       STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
    37 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
    38 static gboolean dreamcast_has_bios = FALSE;
    39 static gchar *dreamcast_program_name = NULL;
    40 static sh4addr_t dreamcast_entry_point = 0xA0000000;
    41 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
    43 #define MAX_MODULES 32
    44 static int num_modules = 0;
    45 dreamcast_module_t modules[MAX_MODULES];
    47 /**
    48  * The unknown module is used for logging files without an actual module
    49  * declaration
    50  */
    51 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
    52 					   NULL, NULL, NULL };
    54 /**
    55  * This function is responsible for defining how all the pieces of the
    56  * dreamcast actually fit together. 
    57  *
    58  * Note currently the locations of the various MMIO pages are hard coded in
    59  * the MMIO definitions - they should probably be moved here.
    60  */
    61 void dreamcast_configure( )
    62 {
    63     dreamcast_register_module( &eventq_module );
    64     /* Register the memory framework */
    65     dreamcast_register_module( &mem_module );
    67     /* Setup standard memory map */
    68     mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
    69     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    70     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    71     mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
    72     dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
    73 				       0x00000000, 0x00200000, 0x89f2b1a1,
    74 				       MEM_REGION_BIOS );
    75     mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
    76     mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
    77 		    0x00200000, 0x00020000 );
    79     /* Load in the rest of the core modules */
    80     dreamcast_register_module( &sh4_module );
    81     dreamcast_register_module( &asic_module );
    82     dreamcast_register_module( &pvr2_module );
    83     dreamcast_register_module( &aica_module );
    84     dreamcast_register_module( &maple_module );
    85     dreamcast_register_module( &ide_module );
    86 }
    88 void dreamcast_config_changed(void)
    89 {
    90     dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
    91 				       0x00000000, 0x00200000, 0x89f2b1a1, 
    92 				       MEM_REGION_BIOS );
    93     mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
    94 		    0x00200000, 0x00020000 );
    95 }
    97 void dreamcast_save_flash()
    98 {
    99     const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
   100     mem_save_block( file, 0x00200000, 0x00020000 );
   101 }
   103 /**
   104  * Constructs a system configuration for the AICA in standalone mode,
   105  * ie sound chip only.
   106  */
   107 void dreamcast_configure_aica_only( )
   108 {
   109     dreamcast_register_module( &mem_module );
   110     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
   111     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
   112     dreamcast_register_module( &aica_module );
   113     aica_enable();
   114     dreamcast_state = STATE_STOPPED;
   115 }
   117 void dreamcast_register_module( dreamcast_module_t module ) 
   118 {
   119     modules[num_modules++] = module;
   120     if( module->init != NULL )
   121 	module->init();
   122 }
   125 void dreamcast_init( void )
   126 {
   127     dreamcast_configure();
   128     dreamcast_state = STATE_STOPPED;
   129 }
   131 void dreamcast_reset( void )
   132 {
   133     int i;
   134     for( i=0; i<num_modules; i++ ) {
   135 	if( modules[i]->reset != NULL )
   136 	    modules[i]->reset();
   137     }
   138 }
   140 void dreamcast_run( void )
   141 {
   142     int i;
   143     if( dreamcast_state != STATE_RUNNING ) {
   144 	for( i=0; i<num_modules; i++ ) {
   145 	    if( modules[i]->start != NULL )
   146 		modules[i]->start();
   147 	}
   148     }
   149     dreamcast_state = STATE_RUNNING;
   150     while( dreamcast_state == STATE_RUNNING ) {
   151 	int time_to_run = timeslice_length;
   152 	for( i=0; i<num_modules; i++ ) {
   153 	    if( modules[i]->run_time_slice != NULL )
   154 		time_to_run = modules[i]->run_time_slice( time_to_run );
   155 	}
   157     }
   159     for( i=0; i<num_modules; i++ ) {
   160 	if( modules[i]->stop != NULL )
   161 	    modules[i]->stop();
   162     }
   163     dreamcast_state = STATE_STOPPED;
   164 }
   166 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
   167 {
   169     int i;
   170     if( dreamcast_state != STATE_RUNNING ) {
   171 	for( i=0; i<num_modules; i++ ) {
   172 	    if( modules[i]->start != NULL )
   173 		modules[i]->start();
   174 	}
   175     }
   176     dreamcast_state = STATE_RUNNING;
   177     uint32_t nanos = 0;
   178     if( nanosecs != 0 ) {
   179         nanos = 1000000000 - nanosecs;
   180 	seconds++;
   181     }
   182     while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
   183 	int time_to_run = timeslice_length;
   184 	for( i=0; i<num_modules; i++ ) {
   185 	    if( modules[i]->run_time_slice != NULL )
   186 		time_to_run = modules[i]->run_time_slice( time_to_run );
   187 	}
   188 	nanos += time_to_run;
   189 	if( nanos >= 1000000000 ) {
   190 	    nanos -= 1000000000;
   191 	    seconds--;
   192 	}
   193     }
   195     for( i=0; i<num_modules; i++ ) {
   196 	if( modules[i]->stop != NULL )
   197 	    modules[i]->stop();
   198     }
   199     dreamcast_state = STATE_STOPPED;
   200 }
   202 void dreamcast_stop( void )
   203 {
   204     if( dreamcast_state == STATE_RUNNING )
   205 	dreamcast_state = STATE_STOPPING;
   206 }
   208 void dreamcast_shutdown()
   209 {
   210     dreamcast_stop();
   211     dreamcast_save_flash();
   212 }
   214 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
   215 {
   216     if( dreamcast_program_name != NULL ) {
   217         g_free(dreamcast_program_name);
   218     }
   219     dreamcast_program_name = g_strdup(name);
   220     dreamcast_entry_point = entry_point;
   221     sh4_set_pc(entry_point);
   222     if( !dreamcast_has_bios ) {
   223         bios_install();
   224     }
   225     dcload_install();
   226     gui_update_state();
   227 }
   229 gboolean dreamcast_is_running( void )
   230 {
   231     return dreamcast_state == STATE_RUNNING;
   232 }
   234 gboolean dreamcast_can_run(void)
   235 {
   236     return dreamcast_state != STATE_UNINIT &&
   237       (dreamcast_has_bios || dreamcast_program_name != NULL);
   238 }
   240 /********************************* Save States *****************************/
   242 struct save_state_header {
   243     char magic[16];
   244     uint32_t version;
   245     uint32_t module_count;
   246 };
   248 struct chunk_header {
   249     char marker[4]; /* Always BLCK */
   250     char name[8]; /* Block (module) name */
   251     uint32_t block_length;
   252 };
   254 /**
   255  * Check the save state header to ensure that it is a valid, supported
   256  * file. 
   257  * @return the number of blocks following, or 0 if the file is invalid.
   258  */
   259 int dreamcast_read_save_state_header( FILE *f )
   260 {
   261     struct save_state_header header;
   262     if( fread( &header, sizeof(header), 1, f ) != 1 ) {
   263 	return 0;
   264     }
   265     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   266 	ERROR( "Not a %s save state file", APP_NAME );
   267 	return 0;
   268     }
   269     if( header.version != DREAMCAST_SAVE_VERSION ) {
   270 	ERROR( "%s save state version not supported", APP_NAME );
   271 	return 0;
   272     }
   273     if( header.module_count > MAX_MODULES ) {
   274 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   275 	return 0;
   276     }
   277     return header.module_count;
   278 }
   280 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
   281 {
   282     struct chunk_header head;
   284     memcpy( head.marker, "BLCK", 4 );
   285     memset( head.name, 0, 8 );
   286     memcpy( head.name, name, strlen(name) );
   287     head.block_length = length;
   288     return fwrite( &head, sizeof(head), 1, f );
   289 }
   292 frame_buffer_t dreamcast_load_preview( const gchar *filename )
   293 {
   294     int i;
   295     FILE *f = fopen( filename, "r" );
   296     if( f == NULL ) return NULL;
   298     int module_count = dreamcast_read_save_state_header(f);
   299     if( module_count <= 0 ) {
   300 	fclose(f);
   301 	return NULL;
   302     }
   303     for( i=0; i<module_count; i++ ) {
   304 	struct chunk_header head;
   305 	if( fread( &head, sizeof(head), 1, f ) != 1 ) {
   306 	    fclose(f);
   307 	    return NULL;
   308 	}
   309 	if( memcmp("BLCK", head.marker, 4) != 0 ) {
   310 	    fclose(f);
   311 	    return NULL;
   312 	}
   314 	if( strcmp("PVR2", head.name) == 0 ) {
   315 	    uint32_t buf_count;
   316 	    int has_front;
   317 	    fread( &buf_count, sizeof(buf_count), 1, f );
   318 	    fread( &has_front, sizeof(has_front), 1, f );
   319 	    if( buf_count != 0 && has_front ) {
   320 		frame_buffer_t result = read_png_from_stream(f);
   321 		fclose(f);
   322 		return result;
   323 	    }
   324 	    break;
   325 	} else {
   326 	    fseek( f, head.block_length, SEEK_CUR );
   327 	}
   328     }
   329     return NULL;
   330 }
   332 int dreamcast_load_state( const gchar *filename )
   333 {
   334     int i,j;
   335     int module_count;
   336     int have_read[MAX_MODULES];
   338     FILE *f = fopen( filename, "r" );
   339     if( f == NULL ) return errno;
   341     module_count = dreamcast_read_save_state_header(f);
   342     if( module_count <= 0 ) {
   343 	fclose(f);
   344 	return 1;
   345     }
   347     for( i=0; i<MAX_MODULES; i++ ) {
   348 	have_read[i] = 0;
   349     }
   351     for( i=0; i<module_count; i++ ) {
   352 	struct chunk_header chunk;
   353 	fread( &chunk, sizeof(chunk), 1, f );
   354 	if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
   355 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   356 	    fclose(f);
   357 	    return 2;
   358 	}
   360 	/* Find the matching module by name */
   361 	for( j=0; j<num_modules; j++ ) {
   362 	    if( strcmp(modules[j]->name,chunk.name) == 0 ) {
   363 		have_read[j] = 1;
   364 		if( modules[j]->load == NULL ) {
   365 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   366 		    fclose(f);
   367 		    return 2;
   368 		} else if( modules[j]->load(f) != 0 ) {
   369 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   370 		    fclose(f);
   371 		    return 2;
   372 		}
   373 		break;
   374 	    }
   375 	}
   376 	if( j == num_modules ) {
   377 	    fclose(f);
   378 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   379 	    return 2;
   380 	}
   381     }
   383     /* Any modules that we didn't load - reset to the default state.
   384      * (ie it's not an error to skip a module if you don't actually
   385      * care about its state).
   386      */
   387     for( j=0; j<num_modules; j++ ) {
   388 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   389 	    modules[j]->reset();
   390 	}
   391     }
   392     fclose(f);
   393     INFO( "Save state read from %s", filename );
   394     return 0;
   395 }
   397 int dreamcast_save_state( const gchar *filename )
   398 {
   399     int i;
   400     FILE *f;
   401     struct save_state_header header;
   403     f = fopen( filename, "w" );
   404     if( f == NULL )
   405 	return errno;
   406     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   407     header.version = DREAMCAST_SAVE_VERSION;
   408     header.module_count = 0;
   410     for( i=0; i<num_modules; i++ ) {
   411 	if( modules[i]->save != NULL )
   412 	    header.module_count++;
   413     }
   414     fwrite( &header, sizeof(header), 1, f );
   415     for( i=0; i<num_modules; i++ ) {
   416 	if( modules[i]->save != NULL ) {
   417 	    uint32_t blocklen, posn1, posn2;
   418 	    dreamcast_write_chunk_header( modules[i]->name, 0, f );
   419 	    posn1 = ftell(f);
   420 	    modules[i]->save(f);
   421 	    posn2 = ftell(f);
   422 	    blocklen = posn2 - posn1;
   423 	    fseek( f, posn1-4, SEEK_SET );
   424 	    fwrite( &blocklen, sizeof(blocklen), 1, f );
   425 	    fseek( f, posn2, SEEK_SET );
   426 	}
   427     }
   428     fclose( f );
   429     INFO( "Save state written to %s", filename );
   430     return 0;
   431 }
.