Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 466:a6935f46ed78
prev461:63d4de8dcec6
next477:9a373f2ff009
author nkeynes
date Sun Oct 28 07:36:11 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Make the sure initial GD (if any) makes its way to the gui
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.26 2007-10-27 05:47:55 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 int dreamcast_load_state( const gchar *filename )
   224 {
   225     int i,j;
   226     uint32_t len;
   227     int have_read[MAX_MODULES];
   228     char tmp[64];
   229     struct save_state_header header;
   230     FILE *f;
   232     f = fopen( filename, "r" );
   233     if( f == NULL ) return errno;
   235     fread( &header, sizeof(header), 1, f );
   236     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   237 	ERROR( "Not a %s save state file", APP_NAME );
   238 	return 1;
   239     }
   240     if( header.version != DREAMCAST_SAVE_VERSION ) {
   241 	ERROR( "%s save state version not supported", APP_NAME );
   242 	return 1;
   243     }
   244     if( header.module_count > MAX_MODULES ) {
   245 	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
   246 	return 1;
   247     }
   248     for( i=0; i<MAX_MODULES; i++ ) {
   249 	have_read[i] = 0;
   250     }
   252     for( i=0; i<header.module_count; i++ ) {
   253 	fread(tmp, 4, 1, f );
   254 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   255 	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
   256 	    return 2;
   257 	}
   258 	len = fread_string(tmp, sizeof(tmp), f );
   259 	if( len > 64 || len < 1 ) {
   260 	    ERROR( "%s save state is corrupted (bad string)", APP_NAME );
   261 	    return 2;
   262 	}
   264 	/* Find the matching module by name */
   265 	for( j=0; j<num_modules; j++ ) {
   266 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   267 		have_read[j] = 1;
   268 		if( modules[j]->load == NULL ) {
   269 		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
   270 		    return 2;
   271 		} else if( modules[j]->load(f) != 0 ) {
   272 		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
   273 		    return 2;
   274 		}
   275 		break;
   276 	    }
   277 	}
   278 	if( j == num_modules ) {
   279 	    ERROR( "%s save state contains unrecognized section", APP_NAME );
   280 	    return 2;
   281 	}
   282     }
   284     /* Any modules that we didn't load - reset to the default state.
   285      * (ie it's not an error to skip a module if you don't actually
   286      * care about its state).
   287      */
   288     for( j=0; j<num_modules; j++ ) {
   289 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   290 	    modules[j]->reset();
   291 	}
   292     }
   293     fclose(f);
   294     INFO( "Save state read from %s", filename );
   295     return 0;
   296 }
   298 int dreamcast_save_state( const gchar *filename )
   299 {
   300     int i;
   301     FILE *f;
   302     struct save_state_header header;
   304     f = fopen( filename, "w" );
   305     if( f == NULL )
   306 	return errno;
   307     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   308     header.version = DREAMCAST_SAVE_VERSION;
   309     header.module_count = 0;
   311     for( i=0; i<num_modules; i++ ) {
   312 	if( modules[i]->save != NULL )
   313 	    header.module_count++;
   314     }
   315     fwrite( &header, sizeof(header), 1, f );
   316     for( i=0; i<num_modules; i++ ) {
   317 	if( modules[i]->save != NULL ) {
   318 	    fwrite( "BLCK", 4, 1, f );
   319 	    fwrite_string( modules[i]->name, f );
   320 	    modules[i]->save(f);
   321 	}
   322     }
   323     fclose( f );
   324     INFO( "Save state written to %s", filename );
   325     return 0;
   326 }
.