Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 892:126aa7db6162
prev891:de9d08282160
next929:fd8cb0c82f5f
next953:f4a156508ad1
author nkeynes
date Wed Oct 29 23:51:58 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Use regparam calling conventions for all functions called from translated code,
along with a few other high-use functions. Can probably extend this to all functions,
but as it is this is a nice performance boost
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 "dream.h"
    24 #include "mem.h"
    25 #include "dreamcast.h"
    26 #include "asic.h"
    27 #include "syscall.h"
    28 #include "gui.h"
    29 #include "aica/aica.h"
    30 #include "gdrom/ide.h"
    31 #include "maple/maple.h"
    32 #include "sh4/sh4.h"
    33 #include "sh4/sh4core.h"
    35 /**
    36  * Current state of the DC virtual machine
    37  */
    38 typedef enum { STATE_UNINIT=0, STATE_RUNNING, 
    39                STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
    41 static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
    42 static gboolean dreamcast_has_bios = FALSE;
    43 static gboolean dreamcast_has_flash = FALSE;
    44 static gboolean dreamcast_exit_on_stop = FALSE;
    45 static gchar *dreamcast_program_name = NULL;
    46 static sh4addr_t dreamcast_entry_point = 0xA0000000;
    47 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
    48 static uint64_t run_time_nanosecs = 0;
    50 #define MAX_MODULES 32
    51 static int num_modules = 0;
    52 dreamcast_module_t modules[MAX_MODULES];
    54 /**
    55  * The unknown module is used for logging files without an actual module
    56  * declaration
    57  */
    58 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
    59         NULL, NULL, NULL };
    61 /**
    62  * This function is responsible for defining how all the pieces of the
    63  * dreamcast actually fit together. 
    64  *
    65  * Note currently the locations of the various MMIO pages are hard coded in
    66  * the MMIO definitions - they should probably be moved here.
    67  */
    68 void dreamcast_configure( )
    69 {
    70     const char *bios_path = lxdream_get_config_value(CONFIG_BIOS_PATH);
    71     const char *flash_path = lxdream_get_config_value(CONFIG_FLASH_PATH);
    73     dreamcast_register_module( &eventq_module );
    74     /* Register the memory framework */
    75     dreamcast_register_module( &mem_module );
    77     /* Setup standard memory map */
    78     mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
    79     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    80     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    81     mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
    82     dreamcast_has_bios = mem_load_rom( bios_path, 0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
    83     mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
    84     if( flash_path != NULL && flash_path[0] != '\0' ) {
    85         mem_load_block( flash_path, 0x00200000, 0x00020000 );
    86     }
    87     dreamcast_has_flash = TRUE;
    89     /* Load in the rest of the core modules */
    90     dreamcast_register_module( &sh4_module );
    91     dreamcast_register_module( &asic_module );
    92     dreamcast_register_module( &pvr2_module );
    93     dreamcast_register_module( &aica_module );
    94     dreamcast_register_module( &maple_module );
    95     dreamcast_register_module( &ide_module );
    96 }
    98 void dreamcast_config_changed(void)
    99 {
   100     const char *bios_path = lxdream_get_config_value(CONFIG_BIOS_PATH);
   101     const char *flash_path = lxdream_get_config_value(CONFIG_FLASH_PATH);
   102     dreamcast_has_bios = mem_load_rom( bios_path, 0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
   103     if( flash_path != NULL && flash_path[0] != '\0' ) {
   104         mem_load_block( flash_path, 0x00200000, 0x00020000 );
   105     }
   106 }
   108 void dreamcast_save_flash()
   109 {
   110     if( dreamcast_has_flash ) {
   111         const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
   112         mem_save_block( file, 0x00200000, 0x00020000 );
   113     }
   114 }
   116 /**
   117  * Constructs a system configuration for the AICA in standalone mode,
   118  * ie sound chip only.
   119  */
   120 void dreamcast_configure_aica_only( )
   121 {
   122     dreamcast_register_module( &mem_module );
   123     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
   124     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
   125     dreamcast_register_module( &aica_module );
   126     aica_enable();
   127     dreamcast_state = STATE_STOPPED;
   128 }
   130 void dreamcast_register_module( dreamcast_module_t module ) 
   131 {
   132     modules[num_modules++] = module;
   133     if( module->init != NULL )
   134         module->init();
   135 }
   137 void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
   138 {
   139     run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
   140 }
   142 void dreamcast_set_exit_on_stop( gboolean flag )
   143 {
   144     dreamcast_exit_on_stop = flag;
   145 }
   147 void dreamcast_init( void )
   148 {
   149     dreamcast_configure();
   150     dreamcast_state = STATE_STOPPED;
   151 }
   153 void dreamcast_reset( void )
   154 {
   155     sh4_core_exit(CORE_EXIT_SYSRESET);
   156     int i;
   157     for( i=0; i<num_modules; i++ ) {
   158         if( modules[i]->reset != NULL )
   159             modules[i]->reset();
   160     }
   161 }
   163 void dreamcast_run( void )
   164 {
   165     int i;
   167     if( !dreamcast_can_run() ) {
   168         ERROR(_("No program is loaded, and no BIOS is configured (required to boot a CD image). To continue, either load a binary program, or set the path to your BIOS file in the Path Preferences"));
   169         return;
   170     }
   172     if( dreamcast_state != STATE_RUNNING ) {
   173         for( i=0; i<num_modules; i++ ) {
   174             if( modules[i]->start != NULL )
   175                 modules[i]->start();
   176         }
   177     }
   179     if( maple_should_grab() ) {
   180         gui_set_use_grab(TRUE);
   181     }
   183     dreamcast_state = STATE_RUNNING;
   185     if( run_time_nanosecs != 0 ) {
   186         while( dreamcast_state == STATE_RUNNING ) {
   187             uint32_t time_to_run = timeslice_length;
   188             if( run_time_nanosecs < time_to_run ) {
   189                 time_to_run = (uint32_t)run_time_nanosecs;
   190             }
   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             if( run_time_nanosecs > time_to_run ) {
   198                 run_time_nanosecs -= time_to_run;
   199             } else {
   200                 run_time_nanosecs = 0; // Finished
   201                 break;
   202             }
   203         }
   204     } else {
   205         while( dreamcast_state == STATE_RUNNING ) {
   206             int time_to_run = timeslice_length;
   207             for( i=0; i<num_modules; i++ ) {
   208                 if( modules[i]->run_time_slice != NULL )
   209                     time_to_run = modules[i]->run_time_slice( time_to_run );
   210             }
   212         }
   213     }
   215     gui_set_use_grab(FALSE);
   217     for( i=0; i<num_modules; i++ ) {
   218         if( modules[i]->stop != NULL )
   219             modules[i]->stop();
   220     }
   221     dreamcast_state = STATE_STOPPED;
   223     if( dreamcast_exit_on_stop ) {
   224         dreamcast_shutdown();
   225         exit(0);
   226     }
   227 }
   229 void dreamcast_stop( void )
   230 {
   231     sh4_core_exit(CORE_EXIT_HALT); // returns only if not inside SH4 core
   232     if( dreamcast_state == STATE_RUNNING )
   233         dreamcast_state = STATE_STOPPING;
   234 }
   236 void dreamcast_shutdown()
   237 {
   238     // Don't do a dreamcast_stop - if we're calling this out of SH4 code,
   239     // it's a shutdown-and-quit event
   240     if( dreamcast_state == STATE_RUNNING )
   241         dreamcast_state = STATE_STOPPING;
   242     dreamcast_save_flash();
   243 #ifdef ENABLE_SH4STATS
   244     sh4_stats_print(stdout);
   245 #endif
   246 }
   248 void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
   249 {
   250     if( dreamcast_program_name != NULL ) {
   251         g_free(dreamcast_program_name);
   252     }
   253     dreamcast_program_name = g_strdup(name);
   254     dreamcast_entry_point = entry_point;
   255     sh4_set_pc(entry_point);
   256     bios_install();
   257     dcload_install();
   258     gui_update_state();
   259 }
   261 gboolean dreamcast_is_running( void )
   262 {
   263     return dreamcast_state == STATE_RUNNING;
   264 }
   266 gboolean dreamcast_can_run(void)
   267 {
   268     return dreamcast_state != STATE_UNINIT &&
   269     (dreamcast_has_bios || dreamcast_program_name != NULL);
   270 }
   272 /********************************* Save States *****************************/
   274 struct save_state_header {
   275     char magic[16];
   276     uint32_t version;
   277     uint32_t module_count;
   278 };
   280 struct chunk_header {
   281     char marker[4]; /* Always BLCK */
   282     char name[8]; /* Block (module) name */
   283     uint32_t block_length;
   284 };
   286 /**
   287  * Check the save state header to ensure that it is a valid, supported
   288  * file. 
   289  * @return the number of blocks following, or 0 if the file is invalid.
   290  */
   291 int dreamcast_read_save_state_header( FILE *f, char *error, int errorlen )
   292 {
   293     struct save_state_header header;
   294     if( fread( &header, sizeof(header), 1, f ) != 1 ) {
   295         return 0;
   296     }
   297     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   298     	if( error != NULL )
   299     		snprintf( error, errorlen, _("File is not a %s save state"), APP_NAME );
   300         return 0;
   301     }
   302     if( header.version != DREAMCAST_SAVE_VERSION ) {
   303     	if( error != NULL )
   304     		snprintf( error, errorlen, _("Unsupported %s save state version"), APP_NAME );
   305         return 0;
   306     }
   307     if( header.module_count > MAX_MODULES ) {
   308     	if( error != NULL )
   309     		snprintf( error, errorlen, _("%s save state is corrupted (bad module count)"), APP_NAME );
   310         return 0;
   311     }
   312     return header.module_count;
   313 }
   315 int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
   316 {
   317     struct chunk_header head;
   319     memcpy( head.marker, "BLCK", 4 );
   320     memset( head.name, 0, 8 );
   321     memcpy( head.name, name, strlen(name) );
   322     head.block_length = length;
   323     return fwrite( &head, sizeof(head), 1, f );
   324 }
   327 frame_buffer_t dreamcast_load_preview( const gchar *filename )
   328 {
   329     int i;
   330     FILE *f = fopen( filename, "r" );
   331     if( f == NULL ) return NULL;
   333     int module_count = dreamcast_read_save_state_header(f, NULL, 0);
   334     if( module_count <= 0 ) {
   335         fclose(f);
   336         return NULL;
   337     }
   338     for( i=0; i<module_count; i++ ) {
   339         struct chunk_header head;
   340         if( fread( &head, sizeof(head), 1, f ) != 1 ) {
   341             fclose(f);
   342             return NULL;
   343         }
   344         if( memcmp("BLCK", head.marker, 4) != 0 ) {
   345             fclose(f);
   346             return NULL;
   347         }
   349         if( strcmp("PVR2", head.name) == 0 ) {
   350             uint32_t buf_count;
   351             int has_front;
   352             fread( &buf_count, sizeof(buf_count), 1, f );
   353             fread( &has_front, sizeof(has_front), 1, f );
   354             if( buf_count != 0 && has_front ) {
   355                 frame_buffer_t result = read_png_from_stream(f);
   356                 fclose(f);
   357                 return result;
   358             }
   359             break;
   360         } else {
   361             fseek( f, head.block_length, SEEK_CUR );
   362         }
   363     }
   364     return NULL;
   365 }
   367 int dreamcast_load_state( const gchar *filename )
   368 {
   369     int i,j;
   370     int module_count;
   371     char error[128];
   372     int have_read[MAX_MODULES];
   374     FILE *f = fopen( filename, "r" );
   375     if( f == NULL ) return errno;
   377     module_count = dreamcast_read_save_state_header(f, error, sizeof(error));
   378     if( module_count <= 0 ) {
   379     	ERROR( error );
   380         fclose(f);
   381         return 1;
   382     }
   384     for( i=0; i<MAX_MODULES; i++ ) {
   385         have_read[i] = 0;
   386     }
   388     for( i=0; i<module_count; i++ ) {
   389         struct chunk_header chunk;
   390         fread( &chunk, sizeof(chunk), 1, f );
   391         if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
   392             ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   393             fclose(f);
   394             return 2;
   395         }
   397         /* Find the matching module by name */
   398         for( j=0; j<num_modules; j++ ) {
   399             if( strcmp(modules[j]->name,chunk.name) == 0 ) {
   400                 have_read[j] = 1;
   401                 if( modules[j]->load == NULL ) {
   402                     ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   403                     fclose(f);
   404                     return 2;
   405                 } else if( modules[j]->load(f) != 0 ) {
   406                     ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   407                     fclose(f);
   408                     return 2;
   409                 }
   410                 break;
   411             }
   412         }
   413         if( j == num_modules ) {
   414             fclose(f);
   415             ERROR( "%s save state contains unrecognized section", APP_NAME );
   416             return 2;
   417         }
   418     }
   420     /* Any modules that we didn't load - reset to the default state.
   421      * (ie it's not an error to skip a module if you don't actually
   422      * care about its state).
   423      */
   424     for( j=0; j<num_modules; j++ ) {
   425         if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   426             modules[j]->reset();
   427         }
   428     }
   429     fclose(f);
   430     INFO( "Save state read from %s", filename );
   431     return 0;
   432 }
   434 int dreamcast_save_state( const gchar *filename )
   435 {
   436     int i;
   437     FILE *f;
   438     struct save_state_header header;
   440     f = fopen( filename, "w" );
   441     if( f == NULL )
   442         return errno;
   443     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   444     header.version = DREAMCAST_SAVE_VERSION;
   445     header.module_count = 0;
   447     for( i=0; i<num_modules; i++ ) {
   448         if( modules[i]->save != NULL )
   449             header.module_count++;
   450     }
   451     fwrite( &header, sizeof(header), 1, f );
   452     for( i=0; i<num_modules; i++ ) {
   453         if( modules[i]->save != NULL ) {
   454             uint32_t blocklen, posn1, posn2;
   455             dreamcast_write_chunk_header( modules[i]->name, 0, f );
   456             posn1 = ftell(f);
   457             modules[i]->save(f);
   458             posn2 = ftell(f);
   459             blocklen = posn2 - posn1;
   460             fseek( f, posn1-4, SEEK_SET );
   461             fwrite( &blocklen, sizeof(blocklen), 1, f );
   462             fseek( f, posn2, SEEK_SET );
   463         }
   464     }
   465     fclose( f );
   466     INFO( "Save state written to %s", filename );
   467     return 0;
   468 }
.