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