Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 30:89b30313d757
prev27:1ef09a52cd1e
next35:21a4be098304
author nkeynes
date Mon Dec 26 03:10:41 2005 +0000 (18 years ago)
permissions -rw-r--r--
last change Update inst counter when switching CPUs
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.11 2005-12-25 05:56: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 "mem.h"
    23 #include "aica/aica.h"
    24 #include "asic.h"
    25 #include "dreamcast.h"
    26 #include "gdrom/ide.h"
    27 #include "maple/maple.h"
    28 #include "modules.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 static char *dreamcast_config = "DEFAULT";
    41 #define MAX_MODULES 32
    42 static int num_modules = 0;
    43 dreamcast_module_t modules[MAX_MODULES];
    45 /**
    46  * This function is responsible for defining how all the pieces of the
    47  * dreamcast actually fit together. 
    48  *
    49  * Note currently the locations of the various MMIO pages are hard coded in
    50  * the MMIO definitions - they should probably be moved here.
    51  */
    52 void dreamcast_configure( )
    53 {
    54     /* Register the memory framework */
    55     dreamcast_register_module( &mem_module );
    57     /* Setup standard memory map */
    58     mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
    59     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    60     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    61     mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
    62     mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
    63     mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
    65     /* Load in the rest of the core modules */
    66     dreamcast_register_module( &sh4_module );
    67     dreamcast_register_module( &asic_module );
    68     dreamcast_register_module( &pvr2_module );
    69     dreamcast_register_module( &aica_module );
    70     dreamcast_register_module( &maple_module );
    71     dreamcast_register_module( &ide_module );
    73     /* Attach any default maple devices, ie a pair of controllers */
    74     maple_device_t controller1 = controller_new();
    75     maple_device_t controller2 = controller_new();
    76     maple_attach_device( controller1, 0, 0 );
    77     maple_attach_device( controller2, 1, 0 );
    78 }
    80 void dreamcast_register_module( dreamcast_module_t module ) 
    81 {
    82     modules[num_modules++] = module;
    83     if( module->init != NULL )
    84 	module->init();
    85 }
    88 void dreamcast_init( void )
    89 {
    90     dreamcast_configure();
    91     dreamcast_state = STATE_STOPPED;
    92 }
    94 void dreamcast_reset( void )
    95 {
    96     int i;
    97     for( i=0; i<num_modules; i++ ) {
    98 	if( modules[i]->reset != NULL )
    99 	    modules[i]->reset();
   100     }
   101 }
   103 void dreamcast_run( void )
   104 {
   105     int i;
   106     if( dreamcast_state != STATE_RUNNING ) {
   107 	for( i=0; i<num_modules; i++ ) {
   108 	    if( modules[i]->start != NULL )
   109 		modules[i]->start();
   110 	}
   111     }
   112     dreamcast_state = STATE_RUNNING;
   113     while( dreamcast_state == STATE_RUNNING ) {
   114 	int time_to_run = timeslice_length;
   115 	for( i=0; i<num_modules; i++ ) {
   116 	    if( modules[i]->run_time_slice != NULL )
   117 		time_to_run = modules[i]->run_time_slice( time_to_run );
   118 	}
   120     }
   122     for( i=0; i<num_modules; i++ ) {
   123 	if( modules[i]->stop != NULL )
   124 	    modules[i]->stop();
   125     }
   126     dreamcast_state = STATE_STOPPED;
   127 }
   129 void dreamcast_stop( void )
   130 {
   131     if( dreamcast_state == STATE_RUNNING )
   132 	dreamcast_state = STATE_STOPPING;
   133 }
   135 gboolean dreamcast_is_running( void )
   136 {
   137     return dreamcast_state == STATE_RUNNING;
   138 }
   140 /***************************** User Configuration **************************/
   142 void dreamcast_load_config( const gchar *filename )
   143 {
   146 }
   148 void dreamcast_save_config( const gchar *filename )
   149 {
   152 }
   154 /********************************* Save States *****************************/
   156 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
   157 #define DREAMCAST_SAVE_VERSION 0x00010000
   159 struct save_state_header {
   160     char magic[16];
   161     uint32_t version;
   162     uint32_t module_count;
   163 };
   165 int dreamcast_load_state( const gchar *filename )
   166 {
   167     int i,j;
   168     uint32_t count, len;
   169     int have_read[MAX_MODULES];
   170     char tmp[64];
   171     struct save_state_header header;
   172     FILE *f;
   174     f = fopen( filename, "r" );
   175     if( f == NULL ) return errno;
   177     fread( &header, sizeof(header), 1, f );
   178     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   179 	ERROR( "Not a DreamOn save state file" );
   180 	return 1;
   181     }
   182     if( header.version != DREAMCAST_SAVE_VERSION ) {
   183 	ERROR( "DreamOn save state version not supported" );
   184 	return 1;
   185     }
   186     if( header.module_count > MAX_MODULES ) {
   187 	ERROR( "DreamOn save state is corrupted (bad module count)" );
   188 	return 1;
   189     }
   190     for( i=0; i<MAX_MODULES; i++ ) {
   191 	have_read[i] = 0;
   192     }
   194     for( i=0; i<header.module_count; i++ ) {
   195 	fread(tmp, 4, 1, f );
   196 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   197 	    ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
   198 	    return 2;
   199 	}
   200 	len = fread_string(tmp, sizeof(tmp), f );
   201 	if( len > 64 || len < 1 ) {
   202 	    ERROR( "DreamOn save state is corrupted (bad string)" );
   203 	    return 2;
   204 	}
   206 	/* Find the matching module by name */
   207 	for( j=0; j<num_modules; j++ ) {
   208 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   209 		have_read[j] = 1;
   210 		if( modules[j]->load == NULL ) {
   211 		    ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
   212 		    return 2;
   213 		} else if( modules[j]->load(f) != 0 ) {
   214 		    ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
   215 		    return 2;
   216 		}
   217 		break;
   218 	    }
   219 	}
   220 	if( j == num_modules ) {
   221 	    ERROR( "DreamOn save state contains unrecognized section" );
   222 	    return 2;
   223 	}
   224     }
   226     /* Any modules that we didn't load - reset to the default state.
   227      * (ie it's not an error to skip a module if you don't actually
   228      * care about its state).
   229      */
   230     for( j=0; j<num_modules; j++ ) {
   231 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   232 	    modules[j]->reset();
   233 	}
   234     }
   235     fclose(f);
   236     INFO( "Save state read from %s", filename );
   237 }
   239 int dreamcast_save_state( const gchar *filename )
   240 {
   241     int i;
   242     FILE *f;
   243     struct save_state_header header;
   245     f = fopen( filename, "w" );
   246     if( f == NULL )
   247 	return errno;
   248     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   249     header.version = DREAMCAST_SAVE_VERSION;
   250     header.module_count = 0;
   252     for( i=0; i<num_modules; i++ ) {
   253 	if( modules[i]->save != NULL )
   254 	    header.module_count++;
   255     }
   256     fwrite( &header, sizeof(header), 1, f );
   257     for( i=0; i<num_modules; i++ ) {
   258 	if( modules[i]->save != NULL ) {
   259 	    fwrite( "BLCK", 4, 1, f );
   260 	    fwrite_string( modules[i]->name, f );
   261 	    modules[i]->save(f);
   262 	}
   263     }
   264     fclose( f );
   265     INFO( "Save state written to %s", filename );
   266 }
.