Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 71:fcde57dbf968
prev35:21a4be098304
next89:939afb9f0f98
author nkeynes
date Tue Jan 10 13:59:35 2006 +0000 (16 years ago)
permissions -rw-r--r--
last change Add AICA-only configuration
view annotate diff log raw
     1 /**
     2  * $Id: dreamcast.c,v 1.13 2006-01-10 13:59:35 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     dreamcast_state = STATE_STOPPED;
    97 }
    99 void dreamcast_register_module( dreamcast_module_t module ) 
   100 {
   101     modules[num_modules++] = module;
   102     if( module->init != NULL )
   103 	module->init();
   104 }
   107 void dreamcast_init( void )
   108 {
   109     dreamcast_configure();
   110     dreamcast_state = STATE_STOPPED;
   111 }
   113 void dreamcast_reset( void )
   114 {
   115     int i;
   116     for( i=0; i<num_modules; i++ ) {
   117 	if( modules[i]->reset != NULL )
   118 	    modules[i]->reset();
   119     }
   120 }
   122 void dreamcast_run( void )
   123 {
   124     int i;
   125     if( dreamcast_state != STATE_RUNNING ) {
   126 	for( i=0; i<num_modules; i++ ) {
   127 	    if( modules[i]->start != NULL )
   128 		modules[i]->start();
   129 	}
   130     }
   131     dreamcast_state = STATE_RUNNING;
   132     while( dreamcast_state == STATE_RUNNING ) {
   133 	int time_to_run = timeslice_length;
   134 	for( i=0; i<num_modules; i++ ) {
   135 	    if( modules[i]->run_time_slice != NULL )
   136 		time_to_run = modules[i]->run_time_slice( time_to_run );
   137 	}
   139     }
   141     for( i=0; i<num_modules; i++ ) {
   142 	if( modules[i]->stop != NULL )
   143 	    modules[i]->stop();
   144     }
   145     dreamcast_state = STATE_STOPPED;
   146 }
   148 void dreamcast_stop( void )
   149 {
   150     if( dreamcast_state == STATE_RUNNING )
   151 	dreamcast_state = STATE_STOPPING;
   152 }
   154 gboolean dreamcast_is_running( void )
   155 {
   156     return dreamcast_state == STATE_RUNNING;
   157 }
   159 /***************************** User Configuration **************************/
   161 void dreamcast_load_config( const gchar *filename )
   162 {
   165 }
   167 void dreamcast_save_config( const gchar *filename )
   168 {
   171 }
   173 /********************************* Save States *****************************/
   175 #define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
   176 #define DREAMCAST_SAVE_VERSION 0x00010000
   178 struct save_state_header {
   179     char magic[16];
   180     uint32_t version;
   181     uint32_t module_count;
   182 };
   184 int dreamcast_load_state( const gchar *filename )
   185 {
   186     int i,j;
   187     uint32_t count, len;
   188     int have_read[MAX_MODULES];
   189     char tmp[64];
   190     struct save_state_header header;
   191     FILE *f;
   193     f = fopen( filename, "r" );
   194     if( f == NULL ) return errno;
   196     fread( &header, sizeof(header), 1, f );
   197     if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
   198 	ERROR( "Not a DreamOn save state file" );
   199 	return 1;
   200     }
   201     if( header.version != DREAMCAST_SAVE_VERSION ) {
   202 	ERROR( "DreamOn save state version not supported" );
   203 	return 1;
   204     }
   205     if( header.module_count > MAX_MODULES ) {
   206 	ERROR( "DreamOn save state is corrupted (bad module count)" );
   207 	return 1;
   208     }
   209     for( i=0; i<MAX_MODULES; i++ ) {
   210 	have_read[i] = 0;
   211     }
   213     for( i=0; i<header.module_count; i++ ) {
   214 	fread(tmp, 4, 1, f );
   215 	if( strncmp(tmp, "BLCK", 4) != 0 ) {
   216 	    ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
   217 	    return 2;
   218 	}
   219 	len = fread_string(tmp, sizeof(tmp), f );
   220 	if( len > 64 || len < 1 ) {
   221 	    ERROR( "DreamOn save state is corrupted (bad string)" );
   222 	    return 2;
   223 	}
   225 	/* Find the matching module by name */
   226 	for( j=0; j<num_modules; j++ ) {
   227 	    if( strcmp(modules[j]->name,tmp) == 0 ) {
   228 		have_read[j] = 1;
   229 		if( modules[j]->load == NULL ) {
   230 		    ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
   231 		    return 2;
   232 		} else if( modules[j]->load(f) != 0 ) {
   233 		    ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
   234 		    return 2;
   235 		}
   236 		break;
   237 	    }
   238 	}
   239 	if( j == num_modules ) {
   240 	    ERROR( "DreamOn save state contains unrecognized section" );
   241 	    return 2;
   242 	}
   243     }
   245     /* Any modules that we didn't load - reset to the default state.
   246      * (ie it's not an error to skip a module if you don't actually
   247      * care about its state).
   248      */
   249     for( j=0; j<num_modules; j++ ) {
   250 	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
   251 	    modules[j]->reset();
   252 	}
   253     }
   254     fclose(f);
   255     INFO( "Save state read from %s", filename );
   256 }
   258 int dreamcast_save_state( const gchar *filename )
   259 {
   260     int i;
   261     FILE *f;
   262     struct save_state_header header;
   264     f = fopen( filename, "w" );
   265     if( f == NULL )
   266 	return errno;
   267     strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
   268     header.version = DREAMCAST_SAVE_VERSION;
   269     header.module_count = 0;
   271     for( i=0; i<num_modules; i++ ) {
   272 	if( modules[i]->save != NULL )
   273 	    header.module_count++;
   274     }
   275     fwrite( &header, sizeof(header), 1, f );
   276     for( i=0; i<num_modules; i++ ) {
   277 	if( modules[i]->save != NULL ) {
   278 	    fwrite( "BLCK", 4, 1, f );
   279 	    fwrite_string( modules[i]->name, f );
   280 	    modules[i]->save(f);
   281 	}
   282     }
   283     fclose( f );
   284     INFO( "Save state written to %s", filename );
   285 }
.