Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 89:939afb9f0f98
prev71:fcde57dbf968
next144:7f0714e89aaa
author nkeynes
date Sun Feb 05 04:04:25 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Install the bios hooks if we're loading a demo
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.14 2006-01-22 22:42:05 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"
    29 /**
    30  * Current state of the DC virtual machine
    31  */
    32 #define STATE_UNINIT 0
    33 #define STATE_RUNNING 1
    34 #define STATE_STOPPING 2
    35 #define STATE_STOPPED 3 
    36 static volatile int dreamcast_state = STATE_UNINIT;
    37 static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
    38 static char *dreamcast_config = "DEFAULT";
    40 #define MAX_MODULES 32
    41 static int num_modules = 0;
    42 dreamcast_module_t modules[MAX_MODULES];
    44 /**
    45  * The unknown module is used for logging files without an actual module
    46  * declaration
    47  */
    48 struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
    49 					   NULL, NULL, NULL };
    51 /**
    52  * This function is responsible for defining how all the pieces of the
    53  * dreamcast actually fit together. 
    54  *
    55  * Note currently the locations of the various MMIO pages are hard coded in
    56  * the MMIO definitions - they should probably be moved here.
    57  */
    58 void dreamcast_configure( )
    59 {
    60     /* Register the memory framework */
    61     dreamcast_register_module( &mem_module );
    63     /* Setup standard memory map */
    64     mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
    65     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    66     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    67     mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
    68     mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
    69     mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
    71     /* Load in the rest of the core modules */
    72     dreamcast_register_module( &sh4_module );
    73     dreamcast_register_module( &asic_module );
    74     dreamcast_register_module( &pvr2_module );
    75     dreamcast_register_module( &aica_module );
    76     dreamcast_register_module( &maple_module );
    77     dreamcast_register_module( &ide_module );
    79     /* Attach any default maple devices, ie a pair of controllers */
    80     maple_device_t controller1 = controller_new();
    81     maple_device_t controller2 = controller_new();
    82     maple_attach_device( controller1, 0, 0 );
    83     maple_attach_device( controller2, 1, 0 );
    84 }
    86 /**
    87  * Constructs a system configuration for the AICA in standalone mode,
    88  * ie sound chip only.
    89  */
    90 void dreamcast_configure_aica_only( )
    91 {
    92     dreamcast_register_module( &mem_module );
    93     mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
    94     mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
    95     dreamcast_register_module( &aica_module );
    96     aica_enable();
    97     dreamcast_state = STATE_STOPPED;
    98 }
   100 void dreamcast_register_module( dreamcast_module_t module ) 
   101 {
   102     modules[num_modules++] = module;
   103     if( module->init != NULL )
   104 	module->init();
   105 }
   108 void dreamcast_init( void )
   109 {
   110     dreamcast_configure();
   111     dreamcast_state = STATE_STOPPED;
   112 }
   114 void dreamcast_reset( void )
   115 {
   116     int i;
   117     for( i=0; i<num_modules; i++ ) {
   118 	if( modules[i]->reset != NULL )
   119 	    modules[i]->reset();
   120     }
   121 }
   123 void dreamcast_run( void )
   124 {
   125     int i;
   126     if( dreamcast_state != STATE_RUNNING ) {
   127 	for( i=0; i<num_modules; i++ ) {
   128 	    if( modules[i]->start != NULL )
   129 		modules[i]->start();
   130 	}
   131     }
   132     dreamcast_state = STATE_RUNNING;
   133     while( dreamcast_state == STATE_RUNNING ) {
   134 	int time_to_run = timeslice_length;
   135 	for( i=0; i<num_modules; i++ ) {
   136 	    if( modules[i]->run_time_slice != NULL )
   137 		time_to_run = modules[i]->run_time_slice( time_to_run );
   138 	}
   140     }
   142     for( i=0; i<num_modules; i++ ) {
   143 	if( modules[i]->stop != NULL )
   144 	    modules[i]->stop();
   145     }
   146     dreamcast_state = STATE_STOPPED;
   147 }
   149 void dreamcast_stop( void )
   150 {
   151     if( dreamcast_state == STATE_RUNNING )
   152 	dreamcast_state = STATE_STOPPING;
   153 }
   155 gboolean dreamcast_is_running( void )
   156 {
   157     return dreamcast_state == STATE_RUNNING;
   158 }
   160 /***************************** User Configuration **************************/
   162 void dreamcast_load_config( const gchar *filename )
   163 {
   166 }
   168 void dreamcast_save_config( const gchar *filename )
   169 {
   172 }
   174 /********************************* Save States *****************************/
   176 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
   177 #define DREAMCAST_SAVE_VERSION 0x00010000
   179 struct save_state_header {
   180     char magic[16];
   181     uint32_t version;
   182     uint32_t module_count;
   183 };
   185 int dreamcast_load_state( const gchar *filename )
   186 {
   187     int i,j;
   188     uint32_t count, len;
   189     int have_read[MAX_MODULES];
   190     char tmp[64];
   191     struct save_state_header header;
   192     FILE *f;
   194     f = fopen( filename, "r" );
   195     if( f == NULL ) return errno;
   197     fread( &header, sizeof(header), 1, f );
   198     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   199 	ERROR( "Not a DreamOn save state file" );
   200 	return 1;
   201     }
   202     if( header.version != DREAMCAST_SAVE_VERSION ) {
   203 	ERROR( "DreamOn save state version not supported" );
   204 	return 1;
   205     }
   206     if( header.module_count > MAX_MODULES ) {
   207 	ERROR( "DreamOn save state is corrupted (bad module count)" );
   208 	return 1;
   209     }
   210     for( i=0; i<MAX_MODULES; i++ ) {
   211 	have_read[i] = 0;
   212     }
   214     for( i=0; i<header.module_count; i++ ) {
   215 	fread(tmp, 4, 1, f );
   216 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   217 	    ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
   218 	    return 2;
   219 	}
   220 	len = fread_string(tmp, sizeof(tmp), f );
   221 	if( len > 64 || len < 1 ) {
   222 	    ERROR( "DreamOn save state is corrupted (bad string)" );
   223 	    return 2;
   224 	}
   226 	/* Find the matching module by name */
   227 	for( j=0; j<num_modules; j++ ) {
   228 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   229 		have_read[j] = 1;
   230 		if( modules[j]->load == NULL ) {
   231 		    ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
   232 		    return 2;
   233 		} else if( modules[j]->load(f) != 0 ) {
   234 		    ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
   235 		    return 2;
   236 		}
   237 		break;
   238 	    }
   239 	}
   240 	if( j == num_modules ) {
   241 	    ERROR( "DreamOn save state contains unrecognized section" );
   242 	    return 2;
   243 	}
   244     }
   246     /* Any modules that we didn't load - reset to the default state.
   247      * (ie it's not an error to skip a module if you don't actually
   248      * care about its state).
   249      */
   250     for( j=0; j<num_modules; j++ ) {
   251 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   252 	    modules[j]->reset();
   253 	}
   254     }
   255     fclose(f);
   256     INFO( "Save state read from %s", filename );
   257 }
   259 int dreamcast_save_state( const gchar *filename )
   260 {
   261     int i;
   262     FILE *f;
   263     struct save_state_header header;
   265     f = fopen( filename, "w" );
   266     if( f == NULL )
   267 	return errno;
   268     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   269     header.version = DREAMCAST_SAVE_VERSION;
   270     header.module_count = 0;
   272     for( i=0; i<num_modules; i++ ) {
   273 	if( modules[i]->save != NULL )
   274 	    header.module_count++;
   275     }
   276     fwrite( &header, sizeof(header), 1, f );
   277     for( i=0; i<num_modules; i++ ) {
   278 	if( modules[i]->save != NULL ) {
   279 	    fwrite( "BLCK", 4, 1, f );
   280 	    fwrite_string( modules[i]->name, f );
   281 	    modules[i]->save(f);
   282 	}
   283     }
   284     fclose( f );
   285     INFO( "Save state written to %s", filename );
   286 }
.