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 (15 years ago)
permissions -rw-r--r--
last change Add AICA-only configuration
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@71
     2
 * $Id: dreamcast.c,v 1.13 2006-01-10 13:59:35 nkeynes Exp $
nkeynes@26
     3
 * Central switchboard for the system. This pulls all the individual modules
nkeynes@26
     4
 * together into some kind of coherent structure. This is also where you'd
nkeynes@26
     5
 * add Naomi support, if I ever get a board to play with...
nkeynes@26
     6
 *
nkeynes@26
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@26
     8
 *
nkeynes@26
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@26
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@26
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@26
    12
 * (at your option) any later version.
nkeynes@26
    13
 *
nkeynes@26
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@26
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@26
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@26
    17
 * GNU General Public License for more details.
nkeynes@26
    18
 */
nkeynes@26
    19
nkeynes@18
    20
#include <errno.h>
nkeynes@2
    21
#include "dream.h"
nkeynes@2
    22
#include "mem.h"
nkeynes@11
    23
#include "aica/aica.h"
nkeynes@2
    24
#include "asic.h"
nkeynes@2
    25
#include "dreamcast.h"
nkeynes@25
    26
#include "gdrom/ide.h"
nkeynes@25
    27
#include "maple/maple.h"
nkeynes@15
    28
nkeynes@26
    29
/**
nkeynes@26
    30
 * Current state of the DC virtual machine
nkeynes@26
    31
 */
nkeynes@26
    32
#define STATE_UNINIT 0
nkeynes@26
    33
#define STATE_RUNNING 1
nkeynes@26
    34
#define STATE_STOPPING 2
nkeynes@26
    35
#define STATE_STOPPED 3 
nkeynes@26
    36
static volatile int dreamcast_state = STATE_UNINIT;
nkeynes@26
    37
static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
nkeynes@26
    38
static char *dreamcast_config = "DEFAULT";
nkeynes@2
    39
nkeynes@15
    40
#define MAX_MODULES 32
nkeynes@15
    41
static int num_modules = 0;
nkeynes@16
    42
dreamcast_module_t modules[MAX_MODULES];
nkeynes@15
    43
nkeynes@16
    44
/**
nkeynes@35
    45
 * The unknown module is used for logging files without an actual module
nkeynes@35
    46
 * declaration
nkeynes@35
    47
 */
nkeynes@35
    48
struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
nkeynes@35
    49
					   NULL, NULL, NULL };
nkeynes@35
    50
nkeynes@35
    51
/**
nkeynes@16
    52
 * This function is responsible for defining how all the pieces of the
nkeynes@26
    53
 * dreamcast actually fit together. 
nkeynes@16
    54
 *
nkeynes@16
    55
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    56
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    57
 */
nkeynes@15
    58
void dreamcast_configure( )
nkeynes@15
    59
{
nkeynes@16
    60
    /* Register the memory framework */
nkeynes@15
    61
    dreamcast_register_module( &mem_module );
nkeynes@16
    62
nkeynes@16
    63
    /* Setup standard memory map */
nkeynes@16
    64
    mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
nkeynes@16
    65
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@16
    66
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@16
    67
    mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
nkeynes@16
    68
    mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
nkeynes@16
    69
    mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
nkeynes@16
    70
nkeynes@16
    71
    /* Load in the rest of the core modules */
nkeynes@15
    72
    dreamcast_register_module( &sh4_module );
nkeynes@15
    73
    dreamcast_register_module( &asic_module );
nkeynes@15
    74
    dreamcast_register_module( &pvr2_module );
nkeynes@15
    75
    dreamcast_register_module( &aica_module );
nkeynes@15
    76
    dreamcast_register_module( &maple_module );
nkeynes@15
    77
    dreamcast_register_module( &ide_module );
nkeynes@16
    78
nkeynes@16
    79
    /* Attach any default maple devices, ie a pair of controllers */
nkeynes@16
    80
    maple_device_t controller1 = controller_new();
nkeynes@16
    81
    maple_device_t controller2 = controller_new();
nkeynes@16
    82
    maple_attach_device( controller1, 0, 0 );
nkeynes@16
    83
    maple_attach_device( controller2, 1, 0 );
nkeynes@15
    84
}
nkeynes@15
    85
nkeynes@71
    86
/**
nkeynes@71
    87
 * Constructs a system configuration for the AICA in standalone mode,
nkeynes@71
    88
 * ie sound chip only.
nkeynes@71
    89
 */
nkeynes@71
    90
void dreamcast_configure_aica_only( )
nkeynes@71
    91
{
nkeynes@71
    92
    dreamcast_register_module( &mem_module );
nkeynes@71
    93
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@71
    94
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@71
    95
    dreamcast_register_module( &aica_module );
nkeynes@71
    96
    dreamcast_state = STATE_STOPPED;
nkeynes@71
    97
}
nkeynes@71
    98
nkeynes@15
    99
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
   100
{
nkeynes@15
   101
    modules[num_modules++] = module;
nkeynes@16
   102
    if( module->init != NULL )
nkeynes@16
   103
	module->init();
nkeynes@15
   104
}
nkeynes@15
   105
nkeynes@15
   106
nkeynes@2
   107
void dreamcast_init( void )
nkeynes@2
   108
{
nkeynes@15
   109
    dreamcast_configure();
nkeynes@23
   110
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   111
}
nkeynes@2
   112
nkeynes@2
   113
void dreamcast_reset( void )
nkeynes@2
   114
{
nkeynes@15
   115
    int i;
nkeynes@15
   116
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   117
	if( modules[i]->reset != NULL )
nkeynes@15
   118
	    modules[i]->reset();
nkeynes@15
   119
    }
nkeynes@2
   120
}
nkeynes@2
   121
nkeynes@23
   122
void dreamcast_run( void )
nkeynes@15
   123
{
nkeynes@15
   124
    int i;
nkeynes@23
   125
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@23
   126
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   127
	    if( modules[i]->start != NULL )
nkeynes@23
   128
		modules[i]->start();
nkeynes@23
   129
	}
nkeynes@15
   130
    }
nkeynes@23
   131
    dreamcast_state = STATE_RUNNING;
nkeynes@23
   132
    while( dreamcast_state == STATE_RUNNING ) {
nkeynes@27
   133
	int time_to_run = timeslice_length;
nkeynes@23
   134
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   135
	    if( modules[i]->run_time_slice != NULL )
nkeynes@27
   136
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@23
   137
	}
nkeynes@23
   138
nkeynes@23
   139
    }
nkeynes@23
   140
nkeynes@15
   141
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   142
	if( modules[i]->stop != NULL )
nkeynes@15
   143
	    modules[i]->stop();
nkeynes@15
   144
    }
nkeynes@23
   145
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   146
}
nkeynes@15
   147
nkeynes@23
   148
void dreamcast_stop( void )
nkeynes@23
   149
{
nkeynes@23
   150
    if( dreamcast_state == STATE_RUNNING )
nkeynes@23
   151
	dreamcast_state = STATE_STOPPING;
nkeynes@23
   152
}
nkeynes@17
   153
nkeynes@26
   154
gboolean dreamcast_is_running( void )
nkeynes@26
   155
{
nkeynes@26
   156
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   157
}
nkeynes@26
   158
nkeynes@30
   159
/***************************** User Configuration **************************/
nkeynes@30
   160
nkeynes@30
   161
void dreamcast_load_config( const gchar *filename )
nkeynes@30
   162
{
nkeynes@30
   163
nkeynes@30
   164
nkeynes@30
   165
}
nkeynes@30
   166
nkeynes@30
   167
void dreamcast_save_config( const gchar *filename )
nkeynes@30
   168
{
nkeynes@30
   169
nkeynes@30
   170
nkeynes@30
   171
}
nkeynes@26
   172
nkeynes@26
   173
/********************************* Save States *****************************/
nkeynes@26
   174
nkeynes@26
   175
#define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
nkeynes@26
   176
#define DREAMCAST_SAVE_VERSION 0x00010000
nkeynes@26
   177
nkeynes@26
   178
struct save_state_header {
nkeynes@26
   179
    char magic[16];
nkeynes@26
   180
    uint32_t version;
nkeynes@26
   181
    uint32_t module_count;
nkeynes@26
   182
};
nkeynes@26
   183
nkeynes@18
   184
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   185
{
nkeynes@17
   186
    int i,j;
nkeynes@17
   187
    uint32_t count, len;
nkeynes@17
   188
    int have_read[MAX_MODULES];
nkeynes@17
   189
    char tmp[64];
nkeynes@15
   190
    struct save_state_header header;
nkeynes@18
   191
    FILE *f;
nkeynes@18
   192
nkeynes@18
   193
    f = fopen( filename, "r" );
nkeynes@18
   194
    if( f == NULL ) return errno;
nkeynes@15
   195
nkeynes@15
   196
    fread( &header, sizeof(header), 1, f );
nkeynes@15
   197
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@15
   198
	ERROR( "Not a DreamOn save state file" );
nkeynes@17
   199
	return 1;
nkeynes@15
   200
    }
nkeynes@15
   201
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@15
   202
	ERROR( "DreamOn save state version not supported" );
nkeynes@17
   203
	return 1;
nkeynes@17
   204
    }
nkeynes@18
   205
    if( header.module_count > MAX_MODULES ) {
nkeynes@18
   206
	ERROR( "DreamOn save state is corrupted (bad module count)" );
nkeynes@17
   207
	return 1;
nkeynes@17
   208
    }
nkeynes@17
   209
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@17
   210
	have_read[i] = 0;
nkeynes@15
   211
    }
nkeynes@15
   212
nkeynes@18
   213
    for( i=0; i<header.module_count; i++ ) {
nkeynes@17
   214
	fread(tmp, 4, 1, f );
nkeynes@18
   215
	if( strncmp(tmp, "BLCK", 4) != 0 ) {
nkeynes@18
   216
	    ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
nkeynes@17
   217
	    return 2;
nkeynes@17
   218
	}
nkeynes@17
   219
	len = fread_string(tmp, sizeof(tmp), f );
nkeynes@17
   220
	if( len > 64 || len < 1 ) {
nkeynes@18
   221
	    ERROR( "DreamOn save state is corrupted (bad string)" );
nkeynes@17
   222
	    return 2;
nkeynes@17
   223
	}
nkeynes@17
   224
	
nkeynes@17
   225
	/* Find the matching module by name */
nkeynes@17
   226
	for( j=0; j<num_modules; j++ ) {
nkeynes@17
   227
	    if( strcmp(modules[j]->name,tmp) == 0 ) {
nkeynes@17
   228
		have_read[j] = 1;
nkeynes@17
   229
		if( modules[j]->load == NULL ) {
nkeynes@18
   230
		    ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
nkeynes@17
   231
		    return 2;
nkeynes@17
   232
		} else if( modules[j]->load(f) != 0 ) {
nkeynes@18
   233
		    ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
nkeynes@17
   234
		    return 2;
nkeynes@17
   235
		}
nkeynes@17
   236
		break;
nkeynes@17
   237
	    }
nkeynes@17
   238
	}
nkeynes@17
   239
	if( j == num_modules ) {
nkeynes@17
   240
	    ERROR( "DreamOn save state contains unrecognized section" );
nkeynes@17
   241
	    return 2;
nkeynes@17
   242
	}
nkeynes@17
   243
    }
nkeynes@17
   244
    
nkeynes@17
   245
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   246
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   247
     * care about its state).
nkeynes@17
   248
     */
nkeynes@17
   249
    for( j=0; j<num_modules; j++ ) {
nkeynes@17
   250
	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@17
   251
	    modules[j]->reset();
nkeynes@17
   252
	}
nkeynes@15
   253
    }
nkeynes@18
   254
    fclose(f);
nkeynes@18
   255
    INFO( "Save state read from %s", filename );
nkeynes@15
   256
}
nkeynes@15
   257
nkeynes@18
   258
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   259
{
nkeynes@15
   260
    int i;
nkeynes@18
   261
    FILE *f;
nkeynes@15
   262
    struct save_state_header header;
nkeynes@15
   263
    
nkeynes@18
   264
    f = fopen( filename, "w" );
nkeynes@18
   265
    if( f == NULL )
nkeynes@18
   266
	return errno;
nkeynes@15
   267
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   268
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   269
    header.module_count = 0;
nkeynes@18
   270
nkeynes@18
   271
    for( i=0; i<num_modules; i++ ) {
nkeynes@18
   272
	if( modules[i]->save != NULL )
nkeynes@18
   273
	    header.module_count++;
nkeynes@18
   274
    }
nkeynes@15
   275
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   276
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   277
	if( modules[i]->save != NULL ) {
nkeynes@17
   278
	    fwrite( "BLCK", 4, 1, f );
nkeynes@17
   279
	    fwrite_string( modules[i]->name, f );
nkeynes@15
   280
	    modules[i]->save(f);
nkeynes@15
   281
	}
nkeynes@15
   282
    }
nkeynes@18
   283
    fclose( f );
nkeynes@18
   284
    INFO( "Save state written to %s", filename );
nkeynes@15
   285
}
nkeynes@15
   286
.