Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 586:2a3ba82cf243
prev543:361ec0a70cf2
next669:ab344e42bca9
author nkeynes
date Fri Feb 08 00:06:56 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix LDS/STS to FPUL/FPSCR to check the FPU disabled bit. Fixes
the linux 2.4.0-test8 kernel boot
(this wasn't exactly very well documented in the original manual)
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 "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"
    30 #include "sh4/sh4trans.h"
    32 /**
    33  * Current state of the DC virtual machine
    34  */
    35 typedef enum { STATE_UNINIT=0, STATE_RUNNING, 
    36 	       STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
    38 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
    39 static gboolean dreamcast_has_bios = FALSE;
    40 static gchar *dreamcast_program_name = NULL;
    41 static sh4addr_t dreamcast_entry_point = 0xA0000000;
    42 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
    44 #define MAX_MODULES 32
    45 static int num_modules = 0;
    46 dreamcast_module_t modules[MAX_MODULES];
    48 /**
    49  * The unknown module is used for logging files without an actual module
    50  * declaration
    51  */
    52 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
    53 					   NULL, NULL, NULL };
    55 /**
    56  * This function is responsible for defining how all the pieces of the
    57  * dreamcast actually fit together. 
    58  *
    59  * Note currently the locations of the various MMIO pages are hard coded in
    60  * the MMIO definitions - they should probably be moved here.
    61  */
    62 void dreamcast_configure( )
    63 {
    64     dreamcast_register_module( &eventq_module );
    65     /* Register the memory framework */
    66     dreamcast_register_module( &mem_module );
    68     /* Setup standard memory map */
    69     mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
    70     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    71     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    72     mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
    73     dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
    74 				       0x00000000, 0x00200000, 0x89f2b1a1,
    75 				       MEM_REGION_BIOS );
    76     mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
    77     mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
    78 		    0x00200000, 0x00020000 );
    80     /* Load in the rest of the core modules */
    81     dreamcast_register_module( &sh4_module );
    82     dreamcast_register_module( &asic_module );
    83     dreamcast_register_module( &pvr2_module );
    84     dreamcast_register_module( &aica_module );
    85     dreamcast_register_module( &maple_module );
    86     dreamcast_register_module( &ide_module );
    87 }
    89 void dreamcast_config_changed(void)
    90 {
    91     dreamcast_has_bios = mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
    92 				       0x00000000, 0x00200000, 0x89f2b1a1, 
    93 				       MEM_REGION_BIOS );
    94     mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
    95 		    0x00200000, 0x00020000 );
    96 }
    98 void dreamcast_save_flash()
    99 {
   100     const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
   101     mem_save_block( file, 0x00200000, 0x00020000 );
   102 }
   104 /**
   105  * Constructs a system configuration for the AICA in standalone mode,
   106  * ie sound chip only.
   107  */
   108 void dreamcast_configure_aica_only( )
   109 {
   110     dreamcast_register_module( &mem_module );
   111     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
   112     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
   113     dreamcast_register_module( &aica_module );
   114     aica_enable();
   115     dreamcast_state = STATE_STOPPED;
   116 }
   118 void dreamcast_register_module( dreamcast_module_t module ) 
   119 {
   120     modules[num_modules++] = module;
   121     if( module->init != NULL )
   122 	module->init();
   123 }
   126 void dreamcast_init( void )
   127 {
   128     dreamcast_configure();
   129     dreamcast_state = STATE_STOPPED;
   130 }
   132 void dreamcast_reset( void )
   133 {
   134     int i;
   135     if( sh4_xlat_is_running() ) {
   136 	sh4_translate_exit( XLAT_EXIT_SYSRESET );
   137     }
   138     for( i=0; i<num_modules; i++ ) {
   139 	if( modules[i]->reset != NULL )
   140 	    modules[i]->reset();
   141     }
   142 }
   144 void dreamcast_run( void )
   145 {
   146     int i;
   147     if( dreamcast_state != STATE_RUNNING ) {
   148 	for( i=0; i<num_modules; i++ ) {
   149 	    if( modules[i]->start != NULL )
   150 		modules[i]->start();
   151 	}
   152     }
   153     dreamcast_state = STATE_RUNNING;
   154     while( dreamcast_state == STATE_RUNNING ) {
   155 	int time_to_run = timeslice_length;
   156 	for( i=0; i<num_modules; i++ ) {
   157 	    if( modules[i]->run_time_slice != NULL )
   158 		time_to_run = modules[i]->run_time_slice( time_to_run );
   159 	}
   161     }
   163     for( i=0; i<num_modules; i++ ) {
   164 	if( modules[i]->stop != NULL )
   165 	    modules[i]->stop();
   166     }
   167     dreamcast_state = STATE_STOPPED;
   168 }
   170 void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
   171 {
   173     int i;
   174     if( dreamcast_state != STATE_RUNNING ) {
   175 	for( i=0; i<num_modules; i++ ) {
   176 	    if( modules[i]->start != NULL )
   177 		modules[i]->start();
   178 	}
   179     }
   180     dreamcast_state = STATE_RUNNING;
   181     uint32_t nanos = 0;
   182     if( nanosecs != 0 ) {
   183         nanos = 1000000000 - nanosecs;
   184 	seconds++;
   185     }
   186     while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
   187 	int time_to_run = timeslice_length;
   188 	for( i=0; i<num_modules; i++ ) {
   189 	    if( modules[i]->run_time_slice != NULL )
   190 		time_to_run = modules[i]->run_time_slice( time_to_run );
   191 	}
   192 	nanos += time_to_run;
   193 	if( nanos >= 1000000000 ) {
   194 	    nanos -= 1000000000;
   195 	    seconds--;
   196 	}
   197     }
   199     for( i=0; i<num_modules; i++ ) {
   200 	if( modules[i]->stop != NULL )
   201 	    modules[i]->stop();
   202     }
   203     dreamcast_state = STATE_STOPPED;
   204 }
   206 void dreamcast_stop( void )
   207 {
   208     if( sh4_xlat_is_running() ) {
   209 	sh4_translate_exit( XLAT_EXIT_HALT );
   210     }
   211     if( dreamcast_state == STATE_RUNNING )
   212 	dreamcast_state = STATE_STOPPING;
   213 }
   215 void dreamcast_shutdown()
   216 {
   217     dreamcast_stop();
   218     dreamcast_save_flash();
   219 }
   221 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
   222 {
   223     if( dreamcast_program_name != NULL ) {
   224         g_free(dreamcast_program_name);
   225     }
   226     dreamcast_program_name = g_strdup(name);
   227     dreamcast_entry_point = entry_point;
   228     sh4_set_pc(entry_point);
   229     bios_install();
   230     dcload_install();
   231     gui_update_state();
   232 }
   234 gboolean dreamcast_is_running( void )
   235 {
   236     return dreamcast_state == STATE_RUNNING;
   237 }
   239 gboolean dreamcast_can_run(void)
   240 {
   241     return dreamcast_state != STATE_UNINIT &&
   242       (dreamcast_has_bios || dreamcast_program_name != NULL);
   243 }
   245 /********************************* Save States *****************************/
   247 struct save_state_header {
   248     char magic[16];
   249     uint32_t version;
   250     uint32_t module_count;
   251 };
   253 struct chunk_header {
   254     char marker[4]; /* Always BLCK */
   255     char name[8]; /* Block (module) name */
   256     uint32_t block_length;
   257 };
   259 /**
   260  * Check the save state header to ensure that it is a valid, supported
   261  * file. 
   262  * @return the number of blocks following, or 0 if the file is invalid.
   263  */
   264 int dreamcast_read_save_state_header( FILE *f )
   265 {
   266     struct save_state_header header;
   267     if( fread( &header, sizeof(header), 1, f ) != 1 ) {
   268 	return 0;
   269     }
   270     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   271 	ERROR( "Not a %s save state file", APP_NAME );
   272 	return 0;
   273     }
   274     if( header.version != DREAMCAST_SAVE_VERSION ) {
   275 	ERROR( "%s save state version not supported", APP_NAME );
   276 	return 0;
   277     }
   278     if( header.module_count > MAX_MODULES ) {
   279 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   280 	return 0;
   281     }
   282     return header.module_count;
   283 }
   285 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
   286 {
   287     struct chunk_header head;
   289     memcpy( head.marker, "BLCK", 4 );
   290     memset( head.name, 0, 8 );
   291     memcpy( head.name, name, strlen(name) );
   292     head.block_length = length;
   293     return fwrite( &head, sizeof(head), 1, f );
   294 }
   297 frame_buffer_t dreamcast_load_preview( const gchar *filename )
   298 {
   299     int i;
   300     FILE *f = fopen( filename, "r" );
   301     if( f == NULL ) return NULL;
   303     int module_count = dreamcast_read_save_state_header(f);
   304     if( module_count <= 0 ) {
   305 	fclose(f);
   306 	return NULL;
   307     }
   308     for( i=0; i<module_count; i++ ) {
   309 	struct chunk_header head;
   310 	if( fread( &head, sizeof(head), 1, f ) != 1 ) {
   311 	    fclose(f);
   312 	    return NULL;
   313 	}
   314 	if( memcmp("BLCK", head.marker, 4) != 0 ) {
   315 	    fclose(f);
   316 	    return NULL;
   317 	}
   319 	if( strcmp("PVR2", head.name) == 0 ) {
   320 	    uint32_t buf_count;
   321 	    int has_front;
   322 	    fread( &buf_count, sizeof(buf_count), 1, f );
   323 	    fread( &has_front, sizeof(has_front), 1, f );
   324 	    if( buf_count != 0 && has_front ) {
   325 		frame_buffer_t result = read_png_from_stream(f);
   326 		fclose(f);
   327 		return result;
   328 	    }
   329 	    break;
   330 	} else {
   331 	    fseek( f, head.block_length, SEEK_CUR );
   332 	}
   333     }
   334     return NULL;
   335 }
   337 int dreamcast_load_state( const gchar *filename )
   338 {
   339     int i,j;
   340     int module_count;
   341     int have_read[MAX_MODULES];
   343     FILE *f = fopen( filename, "r" );
   344     if( f == NULL ) return errno;
   346     module_count = dreamcast_read_save_state_header(f);
   347     if( module_count <= 0 ) {
   348 	fclose(f);
   349 	return 1;
   350     }
   352     for( i=0; i<MAX_MODULES; i++ ) {
   353 	have_read[i] = 0;
   354     }
   356     for( i=0; i<module_count; i++ ) {
   357 	struct chunk_header chunk;
   358 	fread( &chunk, sizeof(chunk), 1, f );
   359 	if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
   360 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   361 	    fclose(f);
   362 	    return 2;
   363 	}
   365 	/* Find the matching module by name */
   366 	for( j=0; j<num_modules; j++ ) {
   367 	    if( strcmp(modules[j]->name,chunk.name) == 0 ) {
   368 		have_read[j] = 1;
   369 		if( modules[j]->load == NULL ) {
   370 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   371 		    fclose(f);
   372 		    return 2;
   373 		} else if( modules[j]->load(f) != 0 ) {
   374 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   375 		    fclose(f);
   376 		    return 2;
   377 		}
   378 		break;
   379 	    }
   380 	}
   381 	if( j == num_modules ) {
   382 	    fclose(f);
   383 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   384 	    return 2;
   385 	}
   386     }
   388     /* Any modules that we didn't load - reset to the default state.
   389      * (ie it's not an error to skip a module if you don't actually
   390      * care about its state).
   391      */
   392     for( j=0; j<num_modules; j++ ) {
   393 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   394 	    modules[j]->reset();
   395 	}
   396     }
   397     fclose(f);
   398     INFO( "Save state read from %s", filename );
   399     return 0;
   400 }
   402 int dreamcast_save_state( const gchar *filename )
   403 {
   404     int i;
   405     FILE *f;
   406     struct save_state_header header;
   408     f = fopen( filename, "w" );
   409     if( f == NULL )
   410 	return errno;
   411     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   412     header.version = DREAMCAST_SAVE_VERSION;
   413     header.module_count = 0;
   415     for( i=0; i<num_modules; i++ ) {
   416 	if( modules[i]->save != NULL )
   417 	    header.module_count++;
   418     }
   419     fwrite( &header, sizeof(header), 1, f );
   420     for( i=0; i<num_modules; i++ ) {
   421 	if( modules[i]->save != NULL ) {
   422 	    uint32_t blocklen, posn1, posn2;
   423 	    dreamcast_write_chunk_header( modules[i]->name, 0, f );
   424 	    posn1 = ftell(f);
   425 	    modules[i]->save(f);
   426 	    posn2 = ftell(f);
   427 	    blocklen = posn2 - posn1;
   428 	    fseek( f, posn1-4, SEEK_SET );
   429 	    fwrite( &blocklen, sizeof(blocklen), 1, f );
   430 	    fseek( f, posn2, SEEK_SET );
   431 	}
   432     }
   433     fclose( f );
   434     INFO( "Save state written to %s", filename );
   435     return 0;
   436 }
.