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