Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 477:9a373f2ff009
prev466:a6935f46ed78
next480:d28c2992f5ee
author nkeynes
date Wed Oct 31 09:10:23 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Add save/restore of render buffers in save states
Gzip memory blocks in save states
Move front-buffer management back to pvr2
Add screenshot preview when loading save states
Various minor fixes and cleanups
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@477
     2
 * $Id: dreamcast.c,v 1.27 2007-10-31 09:10:23 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@466
    70
    mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
nkeynes@466
    71
		  0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
nkeynes@146
    72
    mem_create_ram_region( 0x00200000, 0x00020000, MEM_REGION_FLASH );
nkeynes@450
    73
    mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
nkeynes@167
    74
		    0x00200000, 0x00020000 );
nkeynes@16
    75
nkeynes@16
    76
    /* Load in the rest of the core modules */
nkeynes@15
    77
    dreamcast_register_module( &sh4_module );
nkeynes@15
    78
    dreamcast_register_module( &asic_module );
nkeynes@15
    79
    dreamcast_register_module( &pvr2_module );
nkeynes@15
    80
    dreamcast_register_module( &aica_module );
nkeynes@15
    81
    dreamcast_register_module( &maple_module );
nkeynes@15
    82
    dreamcast_register_module( &ide_module );
nkeynes@15
    83
}
nkeynes@15
    84
nkeynes@461
    85
void dreamcast_config_changed(void)
nkeynes@461
    86
{
nkeynes@461
    87
    mem_load_rom( lxdream_get_config_value(CONFIG_BIOS_PATH),
nkeynes@466
    88
		  0x00000000, 0x00200000, 0x89f2b1a1, MEM_REGION_BIOS );
nkeynes@461
    89
    mem_load_block( lxdream_get_config_value(CONFIG_FLASH_PATH),
nkeynes@461
    90
		    0x00200000, 0x00020000 );
nkeynes@461
    91
}
nkeynes@461
    92
nkeynes@277
    93
void dreamcast_save_flash()
nkeynes@277
    94
{
nkeynes@450
    95
    const char *file = lxdream_get_config_value(CONFIG_FLASH_PATH);
nkeynes@277
    96
    mem_save_block( file, 0x00200000, 0x00020000 );
nkeynes@277
    97
}
nkeynes@277
    98
nkeynes@71
    99
/**
nkeynes@71
   100
 * Constructs a system configuration for the AICA in standalone mode,
nkeynes@71
   101
 * ie sound chip only.
nkeynes@71
   102
 */
nkeynes@71
   103
void dreamcast_configure_aica_only( )
nkeynes@71
   104
{
nkeynes@71
   105
    dreamcast_register_module( &mem_module );
nkeynes@71
   106
    mem_create_ram_region( 0x00800000, 2 MB, MEM_REGION_AUDIO );
nkeynes@71
   107
    mem_create_ram_region( 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH );
nkeynes@71
   108
    dreamcast_register_module( &aica_module );
nkeynes@89
   109
    aica_enable();
nkeynes@71
   110
    dreamcast_state = STATE_STOPPED;
nkeynes@71
   111
}
nkeynes@71
   112
nkeynes@15
   113
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
   114
{
nkeynes@15
   115
    modules[num_modules++] = module;
nkeynes@16
   116
    if( module->init != NULL )
nkeynes@16
   117
	module->init();
nkeynes@15
   118
}
nkeynes@15
   119
nkeynes@15
   120
nkeynes@2
   121
void dreamcast_init( void )
nkeynes@2
   122
{
nkeynes@15
   123
    dreamcast_configure();
nkeynes@23
   124
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   125
}
nkeynes@2
   126
nkeynes@2
   127
void dreamcast_reset( void )
nkeynes@2
   128
{
nkeynes@15
   129
    int i;
nkeynes@15
   130
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   131
	if( modules[i]->reset != NULL )
nkeynes@15
   132
	    modules[i]->reset();
nkeynes@15
   133
    }
nkeynes@2
   134
}
nkeynes@2
   135
nkeynes@23
   136
void dreamcast_run( void )
nkeynes@15
   137
{
nkeynes@15
   138
    int i;
nkeynes@23
   139
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@23
   140
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   141
	    if( modules[i]->start != NULL )
nkeynes@23
   142
		modules[i]->start();
nkeynes@23
   143
	}
nkeynes@15
   144
    }
nkeynes@23
   145
    dreamcast_state = STATE_RUNNING;
nkeynes@23
   146
    while( dreamcast_state == STATE_RUNNING ) {
nkeynes@27
   147
	int time_to_run = timeslice_length;
nkeynes@23
   148
	for( i=0; i<num_modules; i++ ) {
nkeynes@23
   149
	    if( modules[i]->run_time_slice != NULL )
nkeynes@27
   150
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@23
   151
	}
nkeynes@23
   152
nkeynes@23
   153
    }
nkeynes@23
   154
nkeynes@15
   155
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   156
	if( modules[i]->stop != NULL )
nkeynes@15
   157
	    modules[i]->stop();
nkeynes@15
   158
    }
nkeynes@23
   159
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   160
}
nkeynes@15
   161
nkeynes@372
   162
void dreamcast_run_for( unsigned int seconds, unsigned int nanosecs )
nkeynes@372
   163
{
nkeynes@372
   164
    
nkeynes@372
   165
    int i;
nkeynes@372
   166
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@372
   167
	for( i=0; i<num_modules; i++ ) {
nkeynes@372
   168
	    if( modules[i]->start != NULL )
nkeynes@372
   169
		modules[i]->start();
nkeynes@372
   170
	}
nkeynes@372
   171
    }
nkeynes@372
   172
    dreamcast_state = STATE_RUNNING;
nkeynes@372
   173
    uint32_t nanos = 0;
nkeynes@372
   174
    if( nanosecs != 0 ) {
nkeynes@372
   175
        nanos = 1000000000 - nanosecs;
nkeynes@372
   176
	seconds++;
nkeynes@372
   177
    }
nkeynes@372
   178
    while( dreamcast_state == STATE_RUNNING && seconds != 0 ) {
nkeynes@372
   179
	int time_to_run = timeslice_length;
nkeynes@372
   180
	for( i=0; i<num_modules; i++ ) {
nkeynes@372
   181
	    if( modules[i]->run_time_slice != NULL )
nkeynes@372
   182
		time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@372
   183
	}
nkeynes@372
   184
	nanos += time_to_run;
nkeynes@372
   185
	if( nanos >= 1000000000 ) {
nkeynes@372
   186
	    nanos -= 1000000000;
nkeynes@372
   187
	    seconds--;
nkeynes@372
   188
	}
nkeynes@372
   189
    }
nkeynes@372
   190
nkeynes@372
   191
    for( i=0; i<num_modules; i++ ) {
nkeynes@372
   192
	if( modules[i]->stop != NULL )
nkeynes@372
   193
	    modules[i]->stop();
nkeynes@372
   194
    }
nkeynes@372
   195
    dreamcast_state = STATE_STOPPED;
nkeynes@372
   196
}
nkeynes@372
   197
nkeynes@23
   198
void dreamcast_stop( void )
nkeynes@23
   199
{
nkeynes@23
   200
    if( dreamcast_state == STATE_RUNNING )
nkeynes@23
   201
	dreamcast_state = STATE_STOPPING;
nkeynes@23
   202
}
nkeynes@17
   203
nkeynes@277
   204
void dreamcast_shutdown()
nkeynes@277
   205
{
nkeynes@277
   206
    dreamcast_stop();
nkeynes@277
   207
    dreamcast_save_flash();
nkeynes@277
   208
}
nkeynes@277
   209
nkeynes@26
   210
gboolean dreamcast_is_running( void )
nkeynes@26
   211
{
nkeynes@26
   212
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   213
}
nkeynes@26
   214
nkeynes@26
   215
/********************************* Save States *****************************/
nkeynes@26
   216
nkeynes@26
   217
struct save_state_header {
nkeynes@26
   218
    char magic[16];
nkeynes@26
   219
    uint32_t version;
nkeynes@26
   220
    uint32_t module_count;
nkeynes@26
   221
};
nkeynes@26
   222
nkeynes@477
   223
struct chunk_header {
nkeynes@477
   224
    char marker[4]; /* Always BLCK */
nkeynes@477
   225
    char name[8]; /* Block (module) name */
nkeynes@477
   226
    uint32_t block_length;
nkeynes@477
   227
};
nkeynes@477
   228
nkeynes@477
   229
/**
nkeynes@477
   230
 * Check the save state header to ensure that it is a valid, supported
nkeynes@477
   231
 * file. 
nkeynes@477
   232
 * @return the number of blocks following, or 0 if the file is invalid.
nkeynes@477
   233
 */
nkeynes@477
   234
int dreamcast_read_save_state_header( FILE *f )
nkeynes@477
   235
{
nkeynes@477
   236
    struct save_state_header header;
nkeynes@477
   237
    if( fread( &header, sizeof(header), 1, f ) != 1 ) {
nkeynes@477
   238
	return 0;
nkeynes@477
   239
    }
nkeynes@477
   240
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@477
   241
	ERROR( "Not a %s save state file", APP_NAME );
nkeynes@477
   242
	return 0;
nkeynes@477
   243
    }
nkeynes@477
   244
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@477
   245
	ERROR( "%s save state version not supported", APP_NAME );
nkeynes@477
   246
	return 0;
nkeynes@477
   247
    }
nkeynes@477
   248
    if( header.module_count > MAX_MODULES ) {
nkeynes@477
   249
	ERROR( "%s save state is corrupted (bad module count)", APP_NAME );
nkeynes@477
   250
	return 0;
nkeynes@477
   251
    }
nkeynes@477
   252
    return header.module_count;
nkeynes@477
   253
}
nkeynes@477
   254
nkeynes@477
   255
int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
nkeynes@477
   256
{
nkeynes@477
   257
    struct chunk_header head;
nkeynes@477
   258
nkeynes@477
   259
    memcpy( head.marker, "BLCK", 4 );
nkeynes@477
   260
    memset( head.name, 0, 8 );
nkeynes@477
   261
    memcpy( head.name, name, strlen(name) );
nkeynes@477
   262
    head.block_length = length;
nkeynes@477
   263
    return fwrite( &head, sizeof(head), 1, f );
nkeynes@477
   264
}
nkeynes@477
   265
nkeynes@477
   266
nkeynes@477
   267
frame_buffer_t dreamcast_load_preview( const gchar *filename )
nkeynes@477
   268
{
nkeynes@477
   269
    int i;
nkeynes@477
   270
    FILE *f = fopen( filename, "r" );
nkeynes@477
   271
    if( f == NULL ) return NULL;
nkeynes@477
   272
    
nkeynes@477
   273
    int module_count = dreamcast_read_save_state_header(f);
nkeynes@477
   274
    if( module_count <= 0 ) {
nkeynes@477
   275
	fclose(f);
nkeynes@477
   276
	return NULL;
nkeynes@477
   277
    }
nkeynes@477
   278
    for( i=0; i<module_count; i++ ) {
nkeynes@477
   279
	struct chunk_header head;
nkeynes@477
   280
	if( fread( &head, sizeof(head), 1, f ) != 1 ) {
nkeynes@477
   281
	    fclose(f);
nkeynes@477
   282
	    return NULL;
nkeynes@477
   283
	}
nkeynes@477
   284
	if( memcmp("BLCK", head.marker, 4) != 0 ) {
nkeynes@477
   285
	    fclose(f);
nkeynes@477
   286
	    return NULL;
nkeynes@477
   287
	}
nkeynes@477
   288
nkeynes@477
   289
	if( strcmp("PVR2", head.name) == 0 ) {
nkeynes@477
   290
	    uint32_t buf_count;
nkeynes@477
   291
	    int has_front;
nkeynes@477
   292
	    fread( &buf_count, sizeof(buf_count), 1, f );
nkeynes@477
   293
	    fread( &has_front, sizeof(has_front), 1, f );
nkeynes@477
   294
	    if( buf_count != 0 && has_front ) {
nkeynes@477
   295
		frame_buffer_t result = read_png_from_stream(f);
nkeynes@477
   296
		fclose(f);
nkeynes@477
   297
		return result;
nkeynes@477
   298
	    }
nkeynes@477
   299
	    break;
nkeynes@477
   300
	} else {
nkeynes@477
   301
	    fseek( f, head.block_length, SEEK_CUR );
nkeynes@477
   302
	}
nkeynes@477
   303
    }
nkeynes@477
   304
    return NULL;
nkeynes@477
   305
}
nkeynes@477
   306
nkeynes@18
   307
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   308
{
nkeynes@17
   309
    int i,j;
nkeynes@422
   310
    uint32_t len;
nkeynes@477
   311
    int module_count;
nkeynes@17
   312
    int have_read[MAX_MODULES];
nkeynes@18
   313
nkeynes@477
   314
    FILE *f = fopen( filename, "r" );
nkeynes@18
   315
    if( f == NULL ) return errno;
nkeynes@15
   316
nkeynes@477
   317
    module_count = dreamcast_read_save_state_header(f);
nkeynes@477
   318
    if( module_count <= 0 ) {
nkeynes@477
   319
	fclose(f);
nkeynes@17
   320
	return 1;
nkeynes@15
   321
    }
nkeynes@477
   322
nkeynes@17
   323
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@17
   324
	have_read[i] = 0;
nkeynes@15
   325
    }
nkeynes@15
   326
nkeynes@477
   327
    for( i=0; i<module_count; i++ ) {
nkeynes@477
   328
	struct chunk_header chunk;
nkeynes@477
   329
	fread( &chunk, sizeof(chunk), 1, f );
nkeynes@477
   330
	if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
nkeynes@180
   331
	    ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
nkeynes@477
   332
	    fclose(f);
nkeynes@17
   333
	    return 2;
nkeynes@17
   334
	}
nkeynes@17
   335
	
nkeynes@17
   336
	/* Find the matching module by name */
nkeynes@17
   337
	for( j=0; j<num_modules; j++ ) {
nkeynes@477
   338
	    if( strcmp(modules[j]->name,chunk.name) == 0 ) {
nkeynes@17
   339
		have_read[j] = 1;
nkeynes@17
   340
		if( modules[j]->load == NULL ) {
nkeynes@180
   341
		    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
nkeynes@477
   342
		    fclose(f);
nkeynes@17
   343
		    return 2;
nkeynes@17
   344
		} else if( modules[j]->load(f) != 0 ) {
nkeynes@180
   345
		    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
nkeynes@477
   346
		    fclose(f);
nkeynes@17
   347
		    return 2;
nkeynes@17
   348
		}
nkeynes@17
   349
		break;
nkeynes@17
   350
	    }
nkeynes@17
   351
	}
nkeynes@17
   352
	if( j == num_modules ) {
nkeynes@477
   353
	    fclose(f);
nkeynes@180
   354
	    ERROR( "%s save state contains unrecognized section", APP_NAME );
nkeynes@17
   355
	    return 2;
nkeynes@17
   356
	}
nkeynes@17
   357
    }
nkeynes@17
   358
    
nkeynes@17
   359
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   360
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   361
     * care about its state).
nkeynes@17
   362
     */
nkeynes@17
   363
    for( j=0; j<num_modules; j++ ) {
nkeynes@17
   364
	if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@17
   365
	    modules[j]->reset();
nkeynes@17
   366
	}
nkeynes@15
   367
    }
nkeynes@18
   368
    fclose(f);
nkeynes@18
   369
    INFO( "Save state read from %s", filename );
nkeynes@422
   370
    return 0;
nkeynes@15
   371
}
nkeynes@15
   372
nkeynes@18
   373
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   374
{
nkeynes@15
   375
    int i;
nkeynes@18
   376
    FILE *f;
nkeynes@15
   377
    struct save_state_header header;
nkeynes@15
   378
    
nkeynes@18
   379
    f = fopen( filename, "w" );
nkeynes@18
   380
    if( f == NULL )
nkeynes@18
   381
	return errno;
nkeynes@15
   382
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   383
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   384
    header.module_count = 0;
nkeynes@18
   385
nkeynes@18
   386
    for( i=0; i<num_modules; i++ ) {
nkeynes@18
   387
	if( modules[i]->save != NULL )
nkeynes@18
   388
	    header.module_count++;
nkeynes@18
   389
    }
nkeynes@15
   390
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   391
    for( i=0; i<num_modules; i++ ) {
nkeynes@15
   392
	if( modules[i]->save != NULL ) {
nkeynes@477
   393
	    uint32_t blocklen, posn1, posn2;
nkeynes@477
   394
	    dreamcast_write_chunk_header( modules[i]->name, 0, f );
nkeynes@477
   395
	    posn1 = ftell(f);
nkeynes@15
   396
	    modules[i]->save(f);
nkeynes@477
   397
	    posn2 = ftell(f);
nkeynes@477
   398
	    blocklen = posn2 - posn1;
nkeynes@477
   399
	    fseek( f, posn1-4, SEEK_SET );
nkeynes@477
   400
	    fwrite( &blocklen, sizeof(blocklen), 1, f );
nkeynes@477
   401
	    fseek( f, posn2, SEEK_SET );
nkeynes@15
   402
	}
nkeynes@15
   403
    }
nkeynes@18
   404
    fclose( f );
nkeynes@18
   405
    INFO( "Save state written to %s", filename );
nkeynes@422
   406
    return 0;
nkeynes@15
   407
}
nkeynes@15
   408
.