Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 26:ad258e3daaa5
prev25:fa2d21d57942
next27:1ef09a52cd1e
author nkeynes
date Sat Dec 24 08:02:18 2005 +0000 (15 years ago)
permissions -rw-r--r--
last change More structure reorgs/fixes
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@26
     2
 * $Id: dreamcast.c,v 1.9 2005-12-24 08:02:14 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
#include "modules.h"
nkeynes@15
    29
nkeynes@26
    30
/**
nkeynes@26
    31
 * Current state of the DC virtual machine
nkeynes@26
    32
 */
nkeynes@26
    33
#define STATE_UNINIT 0
nkeynes@26
    34
#define STATE_RUNNING 1
nkeynes@26
    35
#define STATE_STOPPING 2
nkeynes@26
    36
#define STATE_STOPPED 3 
nkeynes@26
    37
static volatile int dreamcast_state = STATE_UNINIT;
nkeynes@26
    38
static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
nkeynes@26
    39
static char *dreamcast_config = "DEFAULT";
nkeynes@2
    40
nkeynes@15
    41
#define MAX_MODULES 32
nkeynes@15
    42
static int num_modules = 0;
nkeynes@16
    43
dreamcast_module_t modules[MAX_MODULES];
nkeynes@15
    44
nkeynes@16
    45
/**
nkeynes@16
    46
 * This function is responsible for defining how all the pieces of the
nkeynes@26
    47
 * dreamcast actually fit together. 
nkeynes@16
    48
 *
nkeynes@16
    49
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    50
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    51
 */
nkeynes@15
    52
void dreamcast_configure( )
nkeynes@15
    53
{
nkeynes@16
    54
    /* Register the memory framework */
nkeynes@15
    55
    dreamcast_register_module( &mem_module );
nkeynes@16
    56
nkeynes@16
    57
    /* Setup standard memory map */
nkeynes@16
    58
    mem_create_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN );
nkeynes@16
    59
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@16
    60
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@16
    61
    mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
nkeynes@16
    62
    mem_load_rom( "dcboot.rom", 0x00000000, 0x00200000, 0x89f2b1a1 );
nkeynes@16
    63
    mem_load_rom( "dcflash.rom",0x00200000, 0x00020000, 0x357c3568 );
nkeynes@16
    64
nkeynes@16
    65
    /* Load in the rest of the core modules */
nkeynes@15
    66
    dreamcast_register_module( &sh4_module );
nkeynes@15
    67
    dreamcast_register_module( &asic_module );
nkeynes@15
    68
    dreamcast_register_module( &pvr2_module );
nkeynes@15
    69
    dreamcast_register_module( &aica_module );
nkeynes@15
    70
    dreamcast_register_module( &maple_module );
nkeynes@15
    71
    dreamcast_register_module( &ide_module );
nkeynes@16
    72
nkeynes@16
    73
    /* Attach any default maple devices, ie a pair of controllers */
nkeynes@16
    74
    maple_device_t controller1 = controller_new();
nkeynes@16
    75
    maple_device_t controller2 = controller_new();
nkeynes@16
    76
    maple_attach_device( controller1, 0, 0 );
nkeynes@16
    77
    maple_attach_device( controller2, 1, 0 );
nkeynes@15
    78
}
nkeynes@15
    79
nkeynes@15
    80
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
    81
{
nkeynes@15
    82
    modules[num_modules++] = module;
nkeynes@16
    83
    if( module->init != NULL )
nkeynes@16
    84
	module->init();
nkeynes@15
    85
}
nkeynes@15
    86
nkeynes@15
    87
nkeynes@2
    88
void dreamcast_init( void )
nkeynes@2
    89
{
nkeynes@15
    90
    dreamcast_configure();
nkeynes@23
    91
    dreamcast_state = STATE_STOPPED;
nkeynes@2
    92
}
nkeynes@2
    93
nkeynes@2
    94
void dreamcast_reset( void )
nkeynes@2
    95
{
nkeynes@15
    96
    int i;
nkeynes@15
    97
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
    98
	if( modules[i]->reset != NULL )
nkeynes@15
    99
	    modules[i]->reset();
nkeynes@15
   100
    }
nkeynes@2
   101
}
nkeynes@2
   102
nkeynes@23
   103
void dreamcast_run( void )
nkeynes@15
   104
{
nkeynes@15
   105
    int i;
nkeynes@23
   106
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@23
   107
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   108
	    if( modules[i]->start != NULL )
nkeynes@23
   109
		modules[i]->start();
nkeynes@23
   110
	}
nkeynes@15
   111
    }
nkeynes@23
   112
    dreamcast_state = STATE_RUNNING;
nkeynes@23
   113
    while( dreamcast_state == STATE_RUNNING ) {
nkeynes@23
   114
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   115
	    if( modules[i]->run_time_slice != NULL )
nkeynes@26
   116
		modules[i]->run_time_slice( timeslice_length );
nkeynes@23
   117
	}
nkeynes@23
   118
nkeynes@23
   119
    }
nkeynes@23
   120
nkeynes@15
   121
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   122
	if( modules[i]->stop != NULL )
nkeynes@15
   123
	    modules[i]->stop();
nkeynes@15
   124
    }
nkeynes@23
   125
    dreamcast_state = STATE_STOPPED;
nkeynes@23
   126
    update_gui();
nkeynes@2
   127
}
nkeynes@15
   128
nkeynes@23
   129
void dreamcast_stop( void )
nkeynes@23
   130
{
nkeynes@23
   131
    if( dreamcast_state == STATE_RUNNING )
nkeynes@23
   132
	dreamcast_state = STATE_STOPPING;
nkeynes@23
   133
}
nkeynes@17
   134
nkeynes@26
   135
gboolean dreamcast_is_running( void )
nkeynes@26
   136
{
nkeynes@26
   137
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   138
}
nkeynes@26
   139
nkeynes@26
   140
nkeynes@26
   141
/********************************* Save States *****************************/
nkeynes@26
   142
nkeynes@26
   143
#define DREAMCAST_SAVE_MAGIC "%!-DreamOn!Save\0"
nkeynes@26
   144
#define DREAMCAST_SAVE_VERSION 0x00010000
nkeynes@26
   145
nkeynes@26
   146
struct save_state_header {
nkeynes@26
   147
    char magic[16];
nkeynes@26
   148
    uint32_t version;
nkeynes@26
   149
    uint32_t module_count;
nkeynes@26
   150
};
nkeynes@26
   151
nkeynes@18
   152
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   153
{
nkeynes@17
   154
    int i,j;
nkeynes@17
   155
    uint32_t count, len;
nkeynes@17
   156
    int have_read[MAX_MODULES];
nkeynes@17
   157
    char tmp[64];
nkeynes@15
   158
    struct save_state_header header;
nkeynes@18
   159
    FILE *f;
nkeynes@18
   160
nkeynes@18
   161
    f = fopen( filename, "r" );
nkeynes@18
   162
    if( f == NULL ) return errno;
nkeynes@15
   163
nkeynes@15
   164
    fread( &header, sizeof(header), 1, f );
nkeynes@15
   165
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@15
   166
	ERROR( "Not a DreamOn save state file" );
nkeynes@17
   167
	return 1;
nkeynes@15
   168
    }
nkeynes@15
   169
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@15
   170
	ERROR( "DreamOn save state version not supported" );
nkeynes@17
   171
	return 1;
nkeynes@17
   172
    }
nkeynes@18
   173
    if( header.module_count > MAX_MODULES ) {
nkeynes@18
   174
	ERROR( "DreamOn save state is corrupted (bad module count)" );
nkeynes@17
   175
	return 1;
nkeynes@17
   176
    }
nkeynes@17
   177
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@17
   178
	have_read[i] = 0;
nkeynes@15
   179
    }
nkeynes@15
   180
nkeynes@18
   181
    for( i=0; i<header.module_count; i++ ) {
nkeynes@17
   182
	fread(tmp, 4, 1, f );
nkeynes@18
   183
	if( strncmp(tmp, "BLCK", 4) != 0 ) {
nkeynes@18
   184
	    ERROR( "DreamOn save state is corrupted (missing block header %d)", i );
nkeynes@17
   185
	    return 2;
nkeynes@17
   186
	}
nkeynes@17
   187
	len = fread_string(tmp, sizeof(tmp), f );
nkeynes@17
   188
	if( len > 64 || len < 1 ) {
nkeynes@18
   189
	    ERROR( "DreamOn save state is corrupted (bad string)" );
nkeynes@17
   190
	    return 2;
nkeynes@17
   191
	}
nkeynes@17
   192
	
nkeynes@17
   193
	/* Find the matching module by name */
nkeynes@17
   194
	for( j=0; j<num_modules; j++ ) {
nkeynes@17
   195
	    if( strcmp(modules[j]->name,tmp) == 0 ) {
nkeynes@17
   196
		have_read[j] = 1;
nkeynes@17
   197
		if( modules[j]->load == NULL ) {
nkeynes@18
   198
		    ERROR( "DreamOn save state is corrupted (no loader for %s)", modules[j]->name );
nkeynes@17
   199
		    return 2;
nkeynes@17
   200
		} else if( modules[j]->load(f) != 0 ) {
nkeynes@18
   201
		    ERROR( "DreamOn save state is corrupted (%s failed)", modules[j]->name );
nkeynes@17
   202
		    return 2;
nkeynes@17
   203
		}
nkeynes@17
   204
		break;
nkeynes@17
   205
	    }
nkeynes@17
   206
	}
nkeynes@17
   207
	if( j == num_modules ) {
nkeynes@17
   208
	    ERROR( "DreamOn save state contains unrecognized section" );
nkeynes@17
   209
	    return 2;
nkeynes@17
   210
	}
nkeynes@17
   211
    }
nkeynes@17
   212
    
nkeynes@17
   213
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   214
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   215
     * care about its state).
nkeynes@17
   216
     */
nkeynes@17
   217
    for( j=0; j<num_modules; j++ ) {
nkeynes@17
   218
	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@17
   219
	    modules[j]->reset();
nkeynes@17
   220
	}
nkeynes@15
   221
    }
nkeynes@18
   222
    fclose(f);
nkeynes@18
   223
    INFO( "Save state read from %s", filename );
nkeynes@15
   224
}
nkeynes@15
   225
nkeynes@18
   226
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   227
{
nkeynes@15
   228
    int i;
nkeynes@18
   229
    FILE *f;
nkeynes@15
   230
    struct save_state_header header;
nkeynes@15
   231
    
nkeynes@18
   232
    f = fopen( filename, "w" );
nkeynes@18
   233
    if( f == NULL )
nkeynes@18
   234
	return errno;
nkeynes@15
   235
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   236
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   237
    header.module_count = 0;
nkeynes@18
   238
nkeynes@18
   239
    for( i=0; i<num_modules; i++ ) {
nkeynes@18
   240
	if( modules[i]->save != NULL )
nkeynes@18
   241
	    header.module_count++;
nkeynes@18
   242
    }
nkeynes@15
   243
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   244
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   245
	if( modules[i]->save != NULL ) {
nkeynes@17
   246
	    fwrite( "BLCK", 4, 1, f );
nkeynes@17
   247
	    fwrite_string( modules[i]->name, f );
nkeynes@15
   248
	    modules[i]->save(f);
nkeynes@15
   249
	}
nkeynes@15
   250
    }
nkeynes@18
   251
    fclose( f );
nkeynes@18
   252
    INFO( "Save state written to %s", filename );
nkeynes@15
   253
}
nkeynes@15
   254
.