Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 461:63d4de8dcec6
prev450:207461e79f21
next466:a6935f46ed78
author nkeynes
date Tue Oct 23 10:48:24 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Reload BIOS/flash when changed in configuration.
Fix mem_load_rom to deal with repeat loads and dodgy files
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@461
     2
 * $Id: dreamcast.c,v 1.25 2007-10-23 10:48:24 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@450
    22
#include "config.h"
nkeynes@2
    23
#include "mem.h"
nkeynes@11
    24
#include "aica/aica.h"
nkeynes@2
    25
#include "asic.h"
nkeynes@2
    26
#include "dreamcast.h"
nkeynes@25
    27
#include "gdrom/ide.h"
nkeynes@25
    28
#include "maple/maple.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@422
    39
const 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@35
    46
 * The unknown module is used for logging files without an actual module
nkeynes@35
    47
 * declaration
nkeynes@35
    48
 */
nkeynes@35
    49
struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
nkeynes@35
    50
					   NULL, NULL, NULL };
nkeynes@35
    51
nkeynes@35
    52
/**
nkeynes@16
    53
 * This function is responsible for defining how all the pieces of the
nkeynes@26
    54
 * dreamcast actually fit together. 
nkeynes@16
    55
 *
nkeynes@16
    56
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    57
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    58
 */
nkeynes@15
    59
void dreamcast_configure( )
nkeynes@15
    60
{
nkeynes@265
    61
    dreamcast_register_module( &eventq_module );
nkeynes@16
    62
    /* Register the memory framework */
nkeynes@15
    63
    dreamcast_register_module( &mem_module );
nkeynes@16
    64
nkeynes@16
    65
    /* Setup standard memory map */
nkeynes@146
    66
    mem_create_repeating_ram_region( 0x0C000000, 16 MB, MEM_REGION_MAIN, 0x01000000, 0x0F000000 );
nkeynes@16
    67
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@16
    68
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@16
    69
    mem_create_ram_region( 0x05000000, 8 MB, MEM_REGION_VIDEO );
nkeynes@450
    70
    if( mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
nkeynes@180
    71
		      0x00000000, 0x00200000, 0x89f2b1a1 ) == NULL ) {
nkeynes@180
    72
	/* Bios wasn't found. Dump an empty ram region in there for something to do */
nkeynes@180
    73
	mem_create_ram_region( 0x00000000, 0x00200000, MEM_REGION_BIOS );
nkeynes@180
    74
    }
nkeynes@146
    75
    mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
nkeynes@450
    76
    mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
nkeynes@167
    77
		    0x00200000, 0x00020000 );
nkeynes@16
    78
nkeynes@16
    79
    /* Load in the rest of the core modules */
nkeynes@15
    80
    dreamcast_register_module( &sh4_module );
nkeynes@15
    81
    dreamcast_register_module( &asic_module );
nkeynes@15
    82
    dreamcast_register_module( &pvr2_module );
nkeynes@15
    83
    dreamcast_register_module( &aica_module );
nkeynes@15
    84
    dreamcast_register_module( &maple_module );
nkeynes@15
    85
    dreamcast_register_module( &ide_module );
nkeynes@15
    86
}
nkeynes@15
    87
nkeynes@461
    88
void dreamcast_config_changed(void)
nkeynes@461
    89
{
nkeynes@461
    90
    mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
nkeynes@461
    91
		  0x00000000, 0x00200000, 0x89f2b1a1 );
nkeynes@461
    92
    mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
nkeynes@461
    93
		    0x00200000, 0x00020000 );
nkeynes@461
    94
}
nkeynes@461
    95
nkeynes@277
    96
void dreamcast_save_flash()
nkeynes@277
    97
{
nkeynes@450
    98
    const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
nkeynes@277
    99
    mem_save_block( file, 0x00200000, 0x00020000 );
nkeynes@277
   100
}
nkeynes@277
   101
nkeynes@71
   102
/**
nkeynes@71
   103
 * Constructs a system configuration for the AICA in standalone mode,
nkeynes@71
   104
 * ie sound chip only.
nkeynes@71
   105
 */
nkeynes@71
   106
void dreamcast_configure_aica_only( )
nkeynes@71
   107
{
nkeynes@71
   108
    dreamcast_register_module( &mem_module );
nkeynes@71
   109
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@71
   110
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@71
   111
    dreamcast_register_module( &aica_module );
nkeynes@89
   112
    aica_enable();
nkeynes@71
   113
    dreamcast_state = STATE_STOPPED;
nkeynes@71
   114
}
nkeynes@71
   115
nkeynes@15
   116
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
   117
{
nkeynes@15
   118
    modules[num_modules++] = module;
nkeynes@16
   119
    if( module->init != NULL )
nkeynes@16
   120
	module->init();
nkeynes@15
   121
}
nkeynes@15
   122
nkeynes@15
   123
nkeynes@2
   124
void dreamcast_init( void )
nkeynes@2
   125
{
nkeynes@15
   126
    dreamcast_configure();
nkeynes@23
   127
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   128
}
nkeynes@2
   129
nkeynes@2
   130
void dreamcast_reset( void )
nkeynes@2
   131
{
nkeynes@15
   132
    int i;
nkeynes@15
   133
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   134
	if( modules[i]->reset != NULL )
nkeynes@15
   135
	    modules[i]->reset();
nkeynes@15
   136
    }
nkeynes@2
   137
}
nkeynes@2
   138
nkeynes@23
   139
void dreamcast_run( void )
nkeynes@15
   140
{
nkeynes@15
   141
    int i;
nkeynes@23
   142
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@23
   143
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   144
	    if( modules[i]->start != NULL )
nkeynes@23
   145
		modules[i]->start();
nkeynes@23
   146
	}
nkeynes@15
   147
    }
nkeynes@23
   148
    dreamcast_state = STATE_RUNNING;
nkeynes@23
   149
    while( dreamcast_state == STATE_RUNNING ) {
nkeynes@27
   150
	int time_to_run = timeslice_length;
nkeynes@23
   151
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   152
	    if( modules[i]->run_time_slice != NULL )
nkeynes@27
   153
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@23
   154
	}
nkeynes@23
   155
nkeynes@23
   156
    }
nkeynes@23
   157
nkeynes@15
   158
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   159
	if( modules[i]->stop != NULL )
nkeynes@15
   160
	    modules[i]->stop();
nkeynes@15
   161
    }
nkeynes@23
   162
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   163
}
nkeynes@15
   164
nkeynes@372
   165
void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
nkeynes@372
   166
{
nkeynes@372
   167
    
nkeynes@372
   168
    int i;
nkeynes@372
   169
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@372
   170
	for( i=0; i<num_modules; i++ ) {
nkeynes@372
   171
	    if( modules[i]->start != NULL )
nkeynes@372
   172
		modules[i]->start();
nkeynes@372
   173
	}
nkeynes@372
   174
    }
nkeynes@372
   175
    dreamcast_state = STATE_RUNNING;
nkeynes@372
   176
    uint32_t nanos = 0;
nkeynes@372
   177
    if( nanosecs != 0 ) {
nkeynes@372
   178
        nanos = 1000000000 - nanosecs;
nkeynes@372
   179
	seconds++;
nkeynes@372
   180
    }
nkeynes@372
   181
    while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
nkeynes@372
   182
	int time_to_run = timeslice_length;
nkeynes@372
   183
	for( i=0; i<num_modules; i++ ) {
nkeynes@372
   184
	    if( modules[i]->run_time_slice != NULL )
nkeynes@372
   185
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@372
   186
	}
nkeynes@372
   187
	nanos += time_to_run;
nkeynes@372
   188
	if( nanos >= 1000000000 ) {
nkeynes@372
   189
	    nanos -= 1000000000;
nkeynes@372
   190
	    seconds--;
nkeynes@372
   191
	}
nkeynes@372
   192
    }
nkeynes@372
   193
nkeynes@372
   194
    for( i=0; i<num_modules; i++ ) {
nkeynes@372
   195
	if( modules[i]->stop != NULL )
nkeynes@372
   196
	    modules[i]->stop();
nkeynes@372
   197
    }
nkeynes@372
   198
    dreamcast_state = STATE_STOPPED;
nkeynes@372
   199
}
nkeynes@372
   200
nkeynes@23
   201
void dreamcast_stop( void )
nkeynes@23
   202
{
nkeynes@23
   203
    if( dreamcast_state == STATE_RUNNING )
nkeynes@23
   204
	dreamcast_state = STATE_STOPPING;
nkeynes@23
   205
}
nkeynes@17
   206
nkeynes@277
   207
void dreamcast_shutdown()
nkeynes@277
   208
{
nkeynes@277
   209
    dreamcast_stop();
nkeynes@277
   210
    dreamcast_save_flash();
nkeynes@277
   211
}
nkeynes@277
   212
nkeynes@26
   213
gboolean dreamcast_is_running( void )
nkeynes@26
   214
{
nkeynes@26
   215
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   216
}
nkeynes@26
   217
nkeynes@26
   218
/********************************* Save States *****************************/
nkeynes@26
   219
nkeynes@26
   220
struct save_state_header {
nkeynes@26
   221
    char magic[16];
nkeynes@26
   222
    uint32_t version;
nkeynes@26
   223
    uint32_t module_count;
nkeynes@26
   224
};
nkeynes@26
   225
nkeynes@18
   226
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   227
{
nkeynes@17
   228
    int i,j;
nkeynes@422
   229
    uint32_t len;
nkeynes@17
   230
    int have_read[MAX_MODULES];
nkeynes@17
   231
    char tmp[64];
nkeynes@15
   232
    struct save_state_header header;
nkeynes@18
   233
    FILE *f;
nkeynes@18
   234
nkeynes@18
   235
    f = fopen( filename, "r" );
nkeynes@18
   236
    if( f == NULL ) return errno;
nkeynes@15
   237
nkeynes@15
   238
    fread( &header, sizeof(header), 1, f );
nkeynes@15
   239
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@180
   240
	ERROR( "Not a %s save state file", APP_NAME );
nkeynes@17
   241
	return 1;
nkeynes@15
   242
    }
nkeynes@15
   243
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@180
   244
	ERROR( "%s save state version not supported", APP_NAME );
nkeynes@17
   245
	return 1;
nkeynes@17
   246
    }
nkeynes@18
   247
    if( header.module_count > MAX_MODULES ) {
nkeynes@180
   248
	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
nkeynes@17
   249
	return 1;
nkeynes@17
   250
    }
nkeynes@17
   251
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@17
   252
	have_read[i] = 0;
nkeynes@15
   253
    }
nkeynes@15
   254
nkeynes@18
   255
    for( i=0; i<header.module_count; i++ ) {
nkeynes@17
   256
	fread(tmp, 4, 1, f );
nkeynes@18
   257
	if( strncmp(tmp, "BLCK", 4) != 0 ) {
nkeynes@180
   258
	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
nkeynes@17
   259
	    return 2;
nkeynes@17
   260
	}
nkeynes@17
   261
	len = fread_string(tmp, sizeof(tmp), f );
nkeynes@17
   262
	if( len > 64 || len < 1 ) {
nkeynes@180
   263
	    ERROR( "%s save state is corrupted (bad string)", APP_NAME );
nkeynes@17
   264
	    return 2;
nkeynes@17
   265
	}
nkeynes@17
   266
	
nkeynes@17
   267
	/* Find the matching module by name */
nkeynes@17
   268
	for( j=0; j<num_modules; j++ ) {
nkeynes@17
   269
	    if( strcmp(modules[j]->name,tmp) == 0 ) {
nkeynes@17
   270
		have_read[j] = 1;
nkeynes@17
   271
		if( modules[j]->load == NULL ) {
nkeynes@180
   272
		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
nkeynes@17
   273
		    return 2;
nkeynes@17
   274
		} else if( modules[j]->load(f) != 0 ) {
nkeynes@180
   275
		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
nkeynes@17
   276
		    return 2;
nkeynes@17
   277
		}
nkeynes@17
   278
		break;
nkeynes@17
   279
	    }
nkeynes@17
   280
	}
nkeynes@17
   281
	if( j == num_modules ) {
nkeynes@180
   282
	    ERROR( "%s save state contains unrecognized section", APP_NAME );
nkeynes@17
   283
	    return 2;
nkeynes@17
   284
	}
nkeynes@17
   285
    }
nkeynes@17
   286
    
nkeynes@17
   287
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   288
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   289
     * care about its state).
nkeynes@17
   290
     */
nkeynes@17
   291
    for( j=0; j<num_modules; j++ ) {
nkeynes@17
   292
	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@17
   293
	    modules[j]->reset();
nkeynes@17
   294
	}
nkeynes@15
   295
    }
nkeynes@18
   296
    fclose(f);
nkeynes@18
   297
    INFO( "Save state read from %s", filename );
nkeynes@422
   298
    return 0;
nkeynes@15
   299
}
nkeynes@15
   300
nkeynes@18
   301
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   302
{
nkeynes@15
   303
    int i;
nkeynes@18
   304
    FILE *f;
nkeynes@15
   305
    struct save_state_header header;
nkeynes@15
   306
    
nkeynes@18
   307
    f = fopen( filename, "w" );
nkeynes@18
   308
    if( f == NULL )
nkeynes@18
   309
	return errno;
nkeynes@15
   310
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   311
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   312
    header.module_count = 0;
nkeynes@18
   313
nkeynes@18
   314
    for( i=0; i<num_modules; i++ ) {
nkeynes@18
   315
	if( modules[i]->save != NULL )
nkeynes@18
   316
	    header.module_count++;
nkeynes@18
   317
    }
nkeynes@15
   318
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   319
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   320
	if( modules[i]->save != NULL ) {
nkeynes@17
   321
	    fwrite( "BLCK", 4, 1, f );
nkeynes@17
   322
	    fwrite_string( modules[i]->name, f );
nkeynes@15
   323
	    modules[i]->save(f);
nkeynes@15
   324
	}
nkeynes@15
   325
    }
nkeynes@18
   326
    fclose( f );
nkeynes@18
   327
    INFO( "Save state written to %s", filename );
nkeynes@422
   328
    return 0;
nkeynes@15
   329
}
nkeynes@15
   330
.