Search
lxdream.org :: lxdream/src/dreamcast.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/dreamcast.c
changeset 1067:d3c00ffccfcd
prev1065:bc1cc0c54917
prev951:63483914846f
next1072:d82e04e6d497
author nkeynes
date Sun Jul 05 13:54:48 2009 +1000 (12 years ago)
permissions -rw-r--r--
last change No-op merge lxdream-mem to tip to remove head (Long since merged in
actuality)
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@586
     2
 * $Id$
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@543
    21
#include <glib.h>
nkeynes@1038
    22
#include <unistd.h>
nkeynes@669
    23
#include "lxdream.h"
nkeynes@1041
    24
#include "lxpaths.h"
nkeynes@740
    25
#include "dream.h"
nkeynes@2
    26
#include "mem.h"
nkeynes@669
    27
#include "dreamcast.h"
nkeynes@669
    28
#include "asic.h"
nkeynes@669
    29
#include "syscall.h"
nkeynes@669
    30
#include "gui.h"
nkeynes@11
    31
#include "aica/aica.h"
nkeynes@25
    32
#include "gdrom/ide.h"
nkeynes@25
    33
#include "maple/maple.h"
nkeynes@934
    34
#include "pvr2/pvr2.h"
nkeynes@669
    35
#include "sh4/sh4.h"
nkeynes@740
    36
#include "sh4/sh4core.h"
nkeynes@1034
    37
#include "vmu/vmulist.h"
nkeynes@15
    38
nkeynes@26
    39
/**
nkeynes@26
    40
 * Current state of the DC virtual machine
nkeynes@26
    41
 */
nkeynes@543
    42
typedef enum { STATE_UNINIT=0, STATE_RUNNING, 
nkeynes@736
    43
               STATE_STOPPING, STATE_STOPPED } dreamcast_state_t;
nkeynes@736
    44
    
nkeynes@543
    45
static volatile dreamcast_state_t dreamcast_state = STATE_UNINIT;
nkeynes@543
    46
static gboolean dreamcast_has_bios = FALSE;
nkeynes@742
    47
static gboolean dreamcast_has_flash = FALSE;
nkeynes@689
    48
static gboolean dreamcast_exit_on_stop = FALSE;
nkeynes@543
    49
static gchar *dreamcast_program_name = NULL;
nkeynes@543
    50
static sh4addr_t dreamcast_entry_point = 0xA0000000;
nkeynes@26
    51
static uint32_t timeslice_length = DEFAULT_TIMESLICE_LENGTH;
nkeynes@689
    52
static uint64_t run_time_nanosecs = 0;
nkeynes@1038
    53
static unsigned int quick_save_state = -1;
nkeynes@2
    54
nkeynes@15
    55
#define MAX_MODULES 32
nkeynes@15
    56
static int num_modules = 0;
nkeynes@16
    57
dreamcast_module_t modules[MAX_MODULES];
nkeynes@15
    58
nkeynes@16
    59
/**
nkeynes@35
    60
 * The unknown module is used for logging files without an actual module
nkeynes@35
    61
 * declaration
nkeynes@35
    62
 */
nkeynes@35
    63
struct dreamcast_module unknown_module = { "****", NULL, NULL, NULL, NULL, 
nkeynes@736
    64
        NULL, NULL, NULL };
nkeynes@35
    65
nkeynes@929
    66
extern struct mem_region_fn mem_region_sdram;
nkeynes@929
    67
extern struct mem_region_fn mem_region_vram32;
nkeynes@929
    68
extern struct mem_region_fn mem_region_vram64;
nkeynes@929
    69
extern struct mem_region_fn mem_region_audioram;
nkeynes@931
    70
extern struct mem_region_fn mem_region_audioscratch;
nkeynes@929
    71
extern struct mem_region_fn mem_region_flashram;
nkeynes@929
    72
extern struct mem_region_fn mem_region_bootrom;
nkeynes@931
    73
extern struct mem_region_fn mem_region_pvr2ta;
nkeynes@931
    74
extern struct mem_region_fn mem_region_pvr2yuv;
nkeynes@931
    75
extern struct mem_region_fn mem_region_pvr2vdma1;
nkeynes@931
    76
extern struct mem_region_fn mem_region_pvr2vdma2;
nkeynes@929
    77
nkeynes@934
    78
unsigned char dc_main_ram[16 MB];
nkeynes@934
    79
unsigned char dc_boot_rom[2 MB];
nkeynes@934
    80
unsigned char dc_flash_ram[128 KB];
nkeynes@934
    81
nkeynes@35
    82
/**
nkeynes@16
    83
 * This function is responsible for defining how all the pieces of the
nkeynes@26
    84
 * dreamcast actually fit together. 
nkeynes@16
    85
 *
nkeynes@16
    86
 * Note currently the locations of the various MMIO pages are hard coded in
nkeynes@16
    87
 * the MMIO definitions - they should probably be moved here.
nkeynes@16
    88
 */
nkeynes@15
    89
void dreamcast_configure( )
nkeynes@15
    90
{
nkeynes@1036
    91
    char *bios_path = lxdream_get_global_config_path_value(CONFIG_BIOS_PATH);
nkeynes@1036
    92
    char *flash_path = lxdream_get_global_config_path_value(CONFIG_FLASH_PATH);
nkeynes@825
    93
    
nkeynes@265
    94
    dreamcast_register_module( &eventq_module );
nkeynes@16
    95
    /* Register the memory framework */
nkeynes@15
    96
    dreamcast_register_module( &mem_module );
nkeynes@16
    97
nkeynes@16
    98
    /* Setup standard memory map */
nkeynes@934
    99
    mem_map_region( dc_boot_rom,     0x00000000, 2 MB,   MEM_REGION_BIOS,         &mem_region_bootrom, MEM_FLAG_ROM, 2 MB, 0 );
nkeynes@934
   100
    mem_map_region( dc_flash_ram,    0x00200000, 128 KB, MEM_REGION_FLASH,        &mem_region_flashram, MEM_FLAG_RAM, 128 KB, 0 );
nkeynes@934
   101
    mem_map_region( aica_main_ram,   0x00800000, 2 MB,   MEM_REGION_AUDIO,        &mem_region_audioram, MEM_FLAG_RAM, 2 MB, 0 );
nkeynes@934
   102
    mem_map_region( aica_scratch_ram,0x00703000, 8 KB,   MEM_REGION_AUDIO_SCRATCH,&mem_region_audioscratch, MEM_FLAG_RAM, 8 KB, 0 );
nkeynes@934
   103
    mem_map_region( NULL,            0x04000000, 8 MB,   MEM_REGION_VIDEO64,      &mem_region_vram64, 0, 8 MB, 0 );
nkeynes@934
   104
    mem_map_region( pvr2_main_ram,   0x05000000, 8 MB,   MEM_REGION_VIDEO,        &mem_region_vram32, MEM_FLAG_RAM, 8 MB, 0 ); 
nkeynes@934
   105
    mem_map_region( dc_main_ram,     0x0C000000, 16 MB,  MEM_REGION_MAIN,         &mem_region_sdram, MEM_FLAG_RAM, 0x01000000, 0x0F000000 );
nkeynes@934
   106
    mem_map_region( NULL,            0x10000000, 8 MB,   MEM_REGION_PVR2TA,       &mem_region_pvr2ta, 0, 0x02000000, 0x12000000 );
nkeynes@934
   107
    mem_map_region( NULL,            0x10800000, 8 MB,   MEM_REGION_PVR2YUV,      &mem_region_pvr2yuv, 0, 0x02000000, 0x12800000 );
nkeynes@934
   108
    mem_map_region( NULL,            0x11000000, 16 MB,  MEM_REGION_PVR2VDMA1,    &mem_region_pvr2vdma1, 0, 16 MB, 0 );
nkeynes@934
   109
    mem_map_region( NULL,            0x13000000, 16 MB,  MEM_REGION_PVR2VDMA2,    &mem_region_pvr2vdma2, 0, 16 MB, 0 );
nkeynes@934
   110
    
nkeynes@934
   111
    dreamcast_has_bios = mem_load_rom( dc_boot_rom, bios_path, 2 MB, 0x89f2b1a1 );
nkeynes@825
   112
    if( flash_path != NULL && flash_path[0] != '\0' ) {
nkeynes@825
   113
        mem_load_block( flash_path, 0x00200000, 0x00020000 );
nkeynes@825
   114
    }
nkeynes@742
   115
    dreamcast_has_flash = TRUE;
nkeynes@16
   116
nkeynes@16
   117
    /* Load in the rest of the core modules */
nkeynes@15
   118
    dreamcast_register_module( &sh4_module );
nkeynes@15
   119
    dreamcast_register_module( &asic_module );
nkeynes@15
   120
    dreamcast_register_module( &pvr2_module );
nkeynes@15
   121
    dreamcast_register_module( &aica_module );
nkeynes@15
   122
    dreamcast_register_module( &maple_module );
nkeynes@15
   123
    dreamcast_register_module( &ide_module );
nkeynes@1038
   124
nkeynes@1036
   125
    g_free(bios_path);
nkeynes@1036
   126
    g_free(flash_path);
nkeynes@15
   127
}
nkeynes@15
   128
nkeynes@461
   129
void dreamcast_config_changed(void)
nkeynes@461
   130
{
nkeynes@1036
   131
    char *bios_path = lxdream_get_global_config_path_value(CONFIG_BIOS_PATH);
nkeynes@1036
   132
    char *flash_path = lxdream_get_global_config_path_value(CONFIG_FLASH_PATH);
nkeynes@934
   133
    dreamcast_has_bios = mem_load_rom( dc_boot_rom, bios_path, 2 MB, 0x89f2b1a1 );
nkeynes@825
   134
    if( flash_path != NULL && flash_path[0] != '\0' ) {
nkeynes@825
   135
        mem_load_block( flash_path, 0x00200000, 0x00020000 );
nkeynes@825
   136
    }
nkeynes@1036
   137
    g_free(bios_path);
nkeynes@1036
   138
    g_free(flash_path);
nkeynes@1041
   139
    reset_gui_paths();
nkeynes@461
   140
}
nkeynes@461
   141
nkeynes@277
   142
void dreamcast_save_flash()
nkeynes@277
   143
{
nkeynes@742
   144
    if( dreamcast_has_flash ) {
nkeynes@1036
   145
        char *file = lxdream_get_global_config_path_value(CONFIG_FLASH_PATH);
nkeynes@742
   146
        mem_save_block( file, 0x00200000, 0x00020000 );
nkeynes@1036
   147
        g_free(file);
nkeynes@742
   148
    }
nkeynes@277
   149
}
nkeynes@277
   150
nkeynes@71
   151
/**
nkeynes@71
   152
 * Constructs a system configuration for the AICA in standalone mode,
nkeynes@71
   153
 * ie sound chip only.
nkeynes@71
   154
 */
nkeynes@71
   155
void dreamcast_configure_aica_only( )
nkeynes@71
   156
{
nkeynes@71
   157
    dreamcast_register_module( &mem_module );
nkeynes@934
   158
    mem_map_region( aica_main_ram, 0x00800000, 2 MB, MEM_REGION_AUDIO, &mem_region_audioram, MEM_FLAG_RAM, 2 MB, 0 );
nkeynes@934
   159
    mem_map_region( aica_scratch_ram, 0x00703000, 8 KB, MEM_REGION_AUDIO_SCRATCH, &mem_region_audioscratch, MEM_FLAG_RAM, 8 KB, 0 );
nkeynes@71
   160
    dreamcast_register_module( &aica_module );
nkeynes@89
   161
    aica_enable();
nkeynes@71
   162
    dreamcast_state = STATE_STOPPED;
nkeynes@71
   163
}
nkeynes@71
   164
nkeynes@15
   165
void dreamcast_register_module( dreamcast_module_t module ) 
nkeynes@15
   166
{
nkeynes@15
   167
    modules[num_modules++] = module;
nkeynes@16
   168
    if( module->init != NULL )
nkeynes@736
   169
        module->init();
nkeynes@15
   170
}
nkeynes@15
   171
nkeynes@689
   172
void dreamcast_set_run_time( uint32_t secs, uint32_t nanosecs )
nkeynes@689
   173
{
nkeynes@689
   174
    run_time_nanosecs = (((uint64_t)secs) * 1000000000) + nanosecs;
nkeynes@689
   175
}
nkeynes@689
   176
nkeynes@689
   177
void dreamcast_set_exit_on_stop( gboolean flag )
nkeynes@689
   178
{
nkeynes@689
   179
    dreamcast_exit_on_stop = flag;
nkeynes@689
   180
}
nkeynes@15
   181
nkeynes@2
   182
void dreamcast_init( void )
nkeynes@2
   183
{
nkeynes@15
   184
    dreamcast_configure();
nkeynes@23
   185
    dreamcast_state = STATE_STOPPED;
nkeynes@2
   186
}
nkeynes@2
   187
nkeynes@2
   188
void dreamcast_reset( void )
nkeynes@2
   189
{
nkeynes@766
   190
    sh4_core_exit(CORE_EXIT_SYSRESET);
nkeynes@15
   191
    int i;
nkeynes@15
   192
    for( i=0; i<num_modules; i++ ) {
nkeynes@736
   193
        if( modules[i]->reset != NULL )
nkeynes@736
   194
            modules[i]->reset();
nkeynes@15
   195
    }
nkeynes@2
   196
}
nkeynes@2
   197
nkeynes@23
   198
void dreamcast_run( void )
nkeynes@15
   199
{
nkeynes@15
   200
    int i;
nkeynes@889
   201
    
nkeynes@889
   202
    if( !dreamcast_can_run() ) {
nkeynes@889
   203
        ERROR(_("No program is loaded, and no BIOS is configured (required to boot a CD image). To continue, either load a binary program, or set the path to your BIOS file in the Path Preferences"));
nkeynes@889
   204
        return;
nkeynes@889
   205
    }
nkeynes@889
   206
    
nkeynes@23
   207
    if( dreamcast_state != STATE_RUNNING ) {
nkeynes@689
   208
        for( i=0; i<num_modules; i++ ) {
nkeynes@689
   209
            if( modules[i]->start != NULL )
nkeynes@689
   210
                modules[i]->start();
nkeynes@689
   211
        }
nkeynes@15
   212
    }
nkeynes@839
   213
    
nkeynes@839
   214
    if( maple_should_grab() ) {
nkeynes@839
   215
        gui_set_use_grab(TRUE);
nkeynes@839
   216
    }
nkeynes@736
   217
nkeynes@23
   218
    dreamcast_state = STATE_RUNNING;
nkeynes@736
   219
nkeynes@689
   220
    if( run_time_nanosecs != 0 ) {
nkeynes@689
   221
        while( dreamcast_state == STATE_RUNNING ) {
nkeynes@689
   222
            uint32_t time_to_run = timeslice_length;
nkeynes@689
   223
            if( run_time_nanosecs < time_to_run ) {
nkeynes@689
   224
                time_to_run = (uint32_t)run_time_nanosecs;
nkeynes@689
   225
            }
nkeynes@736
   226
nkeynes@689
   227
            for( i=0; i<num_modules; i++ ) {
nkeynes@689
   228
                if( modules[i]->run_time_slice != NULL )
nkeynes@689
   229
                    time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@689
   230
            }
nkeynes@736
   231
nkeynes@689
   232
            if( run_time_nanosecs > time_to_run ) {
nkeynes@689
   233
                run_time_nanosecs -= time_to_run;
nkeynes@689
   234
            } else {
nkeynes@689
   235
                run_time_nanosecs = 0; // Finished
nkeynes@689
   236
                break;
nkeynes@689
   237
            }
nkeynes@689
   238
        }
nkeynes@689
   239
    } else {
nkeynes@689
   240
        while( dreamcast_state == STATE_RUNNING ) {
nkeynes@689
   241
            int time_to_run = timeslice_length;
nkeynes@689
   242
            for( i=0; i<num_modules; i++ ) {
nkeynes@689
   243
                if( modules[i]->run_time_slice != NULL )
nkeynes@689
   244
                    time_to_run = modules[i]->run_time_slice( time_to_run );
nkeynes@689
   245
            }
nkeynes@23
   246
nkeynes@689
   247
        }
nkeynes@23
   248
    }
nkeynes@23
   249
nkeynes@839
   250
    gui_set_use_grab(FALSE);
nkeynes@839
   251
    
nkeynes@15
   252
    for( i=0; i<num_modules; i++ ) {
nkeynes@689
   253
        if( modules[i]->stop != NULL )
nkeynes@689
   254
            modules[i]->stop();
nkeynes@15
   255
    }
nkeynes@1034
   256
    
nkeynes@1034
   257
    vmulist_save_all();
nkeynes@23
   258
    dreamcast_state = STATE_STOPPED;
nkeynes@736
   259
nkeynes@689
   260
    if( dreamcast_exit_on_stop ) {
nkeynes@689
   261
        dreamcast_shutdown();
nkeynes@689
   262
        exit(0);
nkeynes@372
   263
    }
nkeynes@372
   264
}
nkeynes@372
   265
nkeynes@23
   266
void dreamcast_stop( void )
nkeynes@23
   267
{
nkeynes@740
   268
    sh4_core_exit(CORE_EXIT_HALT); // returns only if not inside SH4 core
nkeynes@23
   269
    if( dreamcast_state == STATE_RUNNING )
nkeynes@736
   270
        dreamcast_state = STATE_STOPPING;
nkeynes@23
   271
}
nkeynes@17
   272
nkeynes@277
   273
void dreamcast_shutdown()
nkeynes@277
   274
{
nkeynes@727
   275
    // Don't do a dreamcast_stop - if we're calling this out of SH4 code,
nkeynes@727
   276
    // it's a shutdown-and-quit event
nkeynes@727
   277
    if( dreamcast_state == STATE_RUNNING )
nkeynes@727
   278
        dreamcast_state = STATE_STOPPING;
nkeynes@277
   279
    dreamcast_save_flash();
nkeynes@1034
   280
    vmulist_save_all();
nkeynes@671
   281
#ifdef ENABLE_SH4STATS
nkeynes@671
   282
    sh4_stats_print(stdout);
nkeynes@671
   283
#endif
nkeynes@277
   284
}
nkeynes@277
   285
nkeynes@543
   286
void dreamcast_program_loaded( const gchar *name, sh4addr_t entry_point )
nkeynes@543
   287
{
nkeynes@543
   288
    if( dreamcast_program_name != NULL ) {
nkeynes@543
   289
        g_free(dreamcast_program_name);
nkeynes@543
   290
    }
nkeynes@543
   291
    dreamcast_program_name = g_strdup(name);
nkeynes@543
   292
    dreamcast_entry_point = entry_point;
nkeynes@543
   293
    sh4_set_pc(entry_point);
nkeynes@951
   294
    sh4_write_sr( sh4_read_sr() & (~SR_BL) ); /* Unmask interrupts */
nkeynes@586
   295
    bios_install();
nkeynes@543
   296
    dcload_install();
nkeynes@543
   297
    gui_update_state();
nkeynes@543
   298
}
nkeynes@543
   299
nkeynes@26
   300
gboolean dreamcast_is_running( void )
nkeynes@26
   301
{
nkeynes@26
   302
    return dreamcast_state == STATE_RUNNING;
nkeynes@26
   303
}
nkeynes@26
   304
nkeynes@543
   305
gboolean dreamcast_can_run(void)
nkeynes@543
   306
{
nkeynes@543
   307
    return dreamcast_state != STATE_UNINIT &&
nkeynes@736
   308
    (dreamcast_has_bios || dreamcast_program_name != NULL);
nkeynes@543
   309
}
nkeynes@543
   310
nkeynes@26
   311
/********************************* Save States *****************************/
nkeynes@26
   312
nkeynes@26
   313
struct save_state_header {
nkeynes@26
   314
    char magic[16];
nkeynes@26
   315
    uint32_t version;
nkeynes@26
   316
    uint32_t module_count;
nkeynes@26
   317
};
nkeynes@26
   318
nkeynes@477
   319
struct chunk_header {
nkeynes@477
   320
    char marker[4]; /* Always BLCK */
nkeynes@477
   321
    char name[8]; /* Block (module) name */
nkeynes@477
   322
    uint32_t block_length;
nkeynes@477
   323
};
nkeynes@477
   324
nkeynes@477
   325
/**
nkeynes@477
   326
 * Check the save state header to ensure that it is a valid, supported
nkeynes@477
   327
 * file. 
nkeynes@477
   328
 * @return the number of blocks following, or 0 if the file is invalid.
nkeynes@477
   329
 */
nkeynes@891
   330
int dreamcast_read_save_state_header( FILE *f, char *error, int errorlen )
nkeynes@477
   331
{
nkeynes@477
   332
    struct save_state_header header;
nkeynes@477
   333
    if( fread( &header, sizeof(header), 1, f ) != 1 ) {
nkeynes@736
   334
        return 0;
nkeynes@477
   335
    }
nkeynes@477
   336
    if( strncmp( header.magic, DREAMCAST_SAVE_MAGIC, 16 ) != 0 ) {
nkeynes@891
   337
    	if( error != NULL )
nkeynes@891
   338
    		snprintf( error, errorlen, _("File is not a %s save state"), APP_NAME );
nkeynes@736
   339
        return 0;
nkeynes@477
   340
    }
nkeynes@477
   341
    if( header.version != DREAMCAST_SAVE_VERSION ) {
nkeynes@891
   342
    	if( error != NULL )
nkeynes@892
   343
    		snprintf( error, errorlen, _("Unsupported %s save state version"), APP_NAME );
nkeynes@736
   344
        return 0;
nkeynes@477
   345
    }
nkeynes@477
   346
    if( header.module_count > MAX_MODULES ) {
nkeynes@891
   347
    	if( error != NULL )
nkeynes@891
   348
    		snprintf( error, errorlen, _("%s save state is corrupted (bad module count)"), APP_NAME );
nkeynes@736
   349
        return 0;
nkeynes@477
   350
    }
nkeynes@477
   351
    return header.module_count;
nkeynes@477
   352
}
nkeynes@477
   353
nkeynes@477
   354
int dreamcast_write_chunk_header( const gchar *name, uint32_t length, FILE *f )
nkeynes@477
   355
{
nkeynes@477
   356
    struct chunk_header head;
nkeynes@477
   357
nkeynes@477
   358
    memcpy( head.marker, "BLCK", 4 );
nkeynes@477
   359
    memset( head.name, 0, 8 );
nkeynes@477
   360
    memcpy( head.name, name, strlen(name) );
nkeynes@477
   361
    head.block_length = length;
nkeynes@477
   362
    return fwrite( &head, sizeof(head), 1, f );
nkeynes@477
   363
}
nkeynes@477
   364
nkeynes@477
   365
nkeynes@477
   366
frame_buffer_t dreamcast_load_preview( const gchar *filename )
nkeynes@477
   367
{
nkeynes@477
   368
    int i;
nkeynes@477
   369
    FILE *f = fopen( filename, "r" );
nkeynes@477
   370
    if( f == NULL ) return NULL;
nkeynes@736
   371
nkeynes@891
   372
    int module_count = dreamcast_read_save_state_header(f, NULL, 0);
nkeynes@477
   373
    if( module_count <= 0 ) {
nkeynes@736
   374
        fclose(f);
nkeynes@736
   375
        return NULL;
nkeynes@477
   376
    }
nkeynes@477
   377
    for( i=0; i<module_count; i++ ) {
nkeynes@736
   378
        struct chunk_header head;
nkeynes@736
   379
        if( fread( &head, sizeof(head), 1, f ) != 1 ) {
nkeynes@736
   380
            fclose(f);
nkeynes@736
   381
            return NULL;
nkeynes@736
   382
        }
nkeynes@736
   383
        if( memcmp("BLCK", head.marker, 4) != 0 ) {
nkeynes@736
   384
            fclose(f);
nkeynes@736
   385
            return NULL;
nkeynes@736
   386
        }
nkeynes@477
   387
nkeynes@736
   388
        if( strcmp("PVR2", head.name) == 0 ) {
nkeynes@736
   389
            uint32_t buf_count;
nkeynes@736
   390
            int has_front;
nkeynes@736
   391
            fread( &buf_count, sizeof(buf_count), 1, f );
nkeynes@736
   392
            fread( &has_front, sizeof(has_front), 1, f );
nkeynes@736
   393
            if( buf_count != 0 && has_front ) {
nkeynes@736
   394
                frame_buffer_t result = read_png_from_stream(f);
nkeynes@736
   395
                fclose(f);
nkeynes@736
   396
                return result;
nkeynes@736
   397
            }
nkeynes@736
   398
            break;
nkeynes@736
   399
        } else {
nkeynes@736
   400
            fseek( f, head.block_length, SEEK_CUR );
nkeynes@736
   401
        }
nkeynes@477
   402
    }
nkeynes@477
   403
    return NULL;
nkeynes@477
   404
}
nkeynes@477
   405
nkeynes@18
   406
int dreamcast_load_state( const gchar *filename )
nkeynes@15
   407
{
nkeynes@17
   408
    int i,j;
nkeynes@477
   409
    int module_count;
nkeynes@891
   410
    char error[128];
nkeynes@17
   411
    int have_read[MAX_MODULES];
nkeynes@18
   412
nkeynes@477
   413
    FILE *f = fopen( filename, "r" );
nkeynes@18
   414
    if( f == NULL ) return errno;
nkeynes@15
   415
nkeynes@891
   416
    module_count = dreamcast_read_save_state_header(f, error, sizeof(error));
nkeynes@477
   417
    if( module_count <= 0 ) {
nkeynes@891
   418
    	ERROR( error );
nkeynes@736
   419
        fclose(f);
nkeynes@736
   420
        return 1;
nkeynes@15
   421
    }
nkeynes@477
   422
nkeynes@17
   423
    for( i=0; i<MAX_MODULES; i++ ) {
nkeynes@736
   424
        have_read[i] = 0;
nkeynes@15
   425
    }
nkeynes@15
   426
nkeynes@477
   427
    for( i=0; i<module_count; i++ ) {
nkeynes@736
   428
        struct chunk_header chunk;
nkeynes@736
   429
        fread( &chunk, sizeof(chunk), 1, f );
nkeynes@736
   430
        if( strncmp(chunk.marker, "BLCK", 4) != 0 ) {
nkeynes@736
   431
            ERROR( "%s save state is corrupted (missing block header %d)", APP_NAME, i );
nkeynes@736
   432
            fclose(f);
nkeynes@736
   433
            return 2;
nkeynes@736
   434
        }
nkeynes@736
   435
nkeynes@736
   436
        /* Find the matching module by name */
nkeynes@736
   437
        for( j=0; j<num_modules; j++ ) {
nkeynes@736
   438
            if( strcmp(modules[j]->name,chunk.name) == 0 ) {
nkeynes@736
   439
                have_read[j] = 1;
nkeynes@736
   440
                if( modules[j]->load == NULL ) {
nkeynes@736
   441
                    ERROR( "%s save state is corrupted (no loader for %s)", APP_NAME, modules[j]->name );
nkeynes@736
   442
                    fclose(f);
nkeynes@736
   443
                    return 2;
nkeynes@736
   444
                } else if( modules[j]->load(f) != 0 ) {
nkeynes@736
   445
                    ERROR( "%s save state is corrupted (%s failed)", APP_NAME, modules[j]->name );
nkeynes@736
   446
                    fclose(f);
nkeynes@736
   447
                    return 2;
nkeynes@736
   448
                }
nkeynes@736
   449
                break;
nkeynes@736
   450
            }
nkeynes@736
   451
        }
nkeynes@736
   452
        if( j == num_modules ) {
nkeynes@736
   453
            fclose(f);
nkeynes@736
   454
            ERROR( "%s save state contains unrecognized section", APP_NAME );
nkeynes@736
   455
            return 2;
nkeynes@736
   456
        }
nkeynes@17
   457
    }
nkeynes@736
   458
nkeynes@17
   459
    /* Any modules that we didn't load - reset to the default state.
nkeynes@17
   460
     * (ie it's not an error to skip a module if you don't actually
nkeynes@17
   461
     * care about its state).
nkeynes@17
   462
     */
nkeynes@17
   463
    for( j=0; j<num_modules; j++ ) {
nkeynes@736
   464
        if( have_read[j] == 0 && modules[j]->reset != NULL ) {
nkeynes@736
   465
            modules[j]->reset();
nkeynes@736
   466
        }
nkeynes@15
   467
    }
nkeynes@18
   468
    fclose(f);
nkeynes@18
   469
    INFO( "Save state read from %s", filename );
nkeynes@422
   470
    return 0;
nkeynes@15
   471
}
nkeynes@15
   472
nkeynes@18
   473
int dreamcast_save_state( const gchar *filename )
nkeynes@15
   474
{
nkeynes@15
   475
    int i;
nkeynes@18
   476
    FILE *f;
nkeynes@15
   477
    struct save_state_header header;
nkeynes@736
   478
nkeynes@18
   479
    f = fopen( filename, "w" );
nkeynes@18
   480
    if( f == NULL )
nkeynes@736
   481
        return errno;
nkeynes@15
   482
    strcpy( header.magic, DREAMCAST_SAVE_MAGIC );
nkeynes@15
   483
    header.version = DREAMCAST_SAVE_VERSION;
nkeynes@18
   484
    header.module_count = 0;
nkeynes@18
   485
nkeynes@18
   486
    for( i=0; i<num_modules; i++ ) {
nkeynes@736
   487
        if( modules[i]->save != NULL )
nkeynes@736
   488
            header.module_count++;
nkeynes@18
   489
    }
nkeynes@15
   490
    fwrite( &header, sizeof(header), 1, f );
nkeynes@15
   491
    for( i=0; i<num_modules; i++ ) {
nkeynes@736
   492
        if( modules[i]->save != NULL ) {
nkeynes@736
   493
            uint32_t blocklen, posn1, posn2;
nkeynes@736
   494
            dreamcast_write_chunk_header( modules[i]->name, 0, f );
nkeynes@736
   495
            posn1 = ftell(f);
nkeynes@736
   496
            modules[i]->save(f);
nkeynes@736
   497
            posn2 = ftell(f);
nkeynes@736
   498
            blocklen = posn2 - posn1;
nkeynes@736
   499
            fseek( f, posn1-4, SEEK_SET );
nkeynes@736
   500
            fwrite( &blocklen, sizeof(blocklen), 1, f );
nkeynes@736
   501
            fseek( f, posn2, SEEK_SET );
nkeynes@736
   502
        }
nkeynes@15
   503
    }
nkeynes@18
   504
    fclose( f );
nkeynes@18
   505
    INFO( "Save state written to %s", filename );
nkeynes@422
   506
    return 0;
nkeynes@15
   507
}
nkeynes@15
   508
nkeynes@1038
   509
/********************** Quick save state support ***********************/
nkeynes@1038
   510
/* This section doesn't necessarily belong here, but it probably makes the
nkeynes@1038
   511
 * most sense here next to the regular save/load functions
nkeynes@1038
   512
 */
nkeynes@1038
   513
nkeynes@1038
   514
static gchar *get_quick_state_filename( int state )
nkeynes@1038
   515
{
nkeynes@1038
   516
    gchar *path = lxdream_get_global_config_path_value(CONFIG_SAVE_PATH);
nkeynes@1038
   517
    gchar *str = g_strdup_printf( QUICK_STATE_FILENAME, path, state );
nkeynes@1038
   518
    g_free( path );
nkeynes@1038
   519
    return str;
nkeynes@1038
   520
}
nkeynes@1038
   521
nkeynes@1038
   522
nkeynes@1038
   523
static void dreamcast_quick_state_init()
nkeynes@1038
   524
{
nkeynes@1038
   525
    const char *state = lxdream_get_global_config_value(CONFIG_QUICK_STATE);
nkeynes@1038
   526
    if( state != NULL ) {
nkeynes@1038
   527
        quick_save_state = atoi(state);
nkeynes@1038
   528
        if( quick_save_state > MAX_QUICK_STATE ) {
nkeynes@1038
   529
            quick_save_state = 0;
nkeynes@1038
   530
        }
nkeynes@1038
   531
    } else {
nkeynes@1038
   532
        quick_save_state = 0;
nkeynes@1038
   533
    }
nkeynes@1038
   534
}
nkeynes@1038
   535
nkeynes@1038
   536
void dreamcast_quick_save()
nkeynes@1038
   537
{
nkeynes@1038
   538
    if( quick_save_state == -1 ) 
nkeynes@1038
   539
        dreamcast_quick_state_init();
nkeynes@1038
   540
    gchar *str = get_quick_state_filename(quick_save_state);
nkeynes@1038
   541
    dreamcast_save_state(str);
nkeynes@1038
   542
    g_free(str);
nkeynes@1038
   543
}
nkeynes@1038
   544
nkeynes@1038
   545
void dreamcast_quick_load()
nkeynes@1038
   546
{
nkeynes@1038
   547
    if( quick_save_state == -1 ) 
nkeynes@1038
   548
        dreamcast_quick_state_init();
nkeynes@1038
   549
    gchar *str = get_quick_state_filename(quick_save_state);
nkeynes@1038
   550
    dreamcast_load_state(str);
nkeynes@1038
   551
    g_free(str);
nkeynes@1038
   552
}
nkeynes@1038
   553
nkeynes@1038
   554
unsigned int dreamcast_get_quick_state( )
nkeynes@1038
   555
{
nkeynes@1038
   556
    if( quick_save_state == -1 ) 
nkeynes@1038
   557
        dreamcast_quick_state_init();
nkeynes@1038
   558
    return quick_save_state;
nkeynes@1038
   559
}
nkeynes@1038
   560
nkeynes@1038
   561
void dreamcast_set_quick_state( unsigned int state )
nkeynes@1038
   562
{
nkeynes@1038
   563
    if( state <= MAX_QUICK_STATE && state != quick_save_state ) {
nkeynes@1038
   564
        quick_save_state = state;
nkeynes@1038
   565
        char buf[3];
nkeynes@1038
   566
        sprintf( buf, "%d", quick_save_state );
nkeynes@1038
   567
        lxdream_set_global_config_value(CONFIG_QUICK_STATE, buf);
nkeynes@1038
   568
        lxdream_save_config();
nkeynes@1038
   569
    }
nkeynes@1038
   570
}
nkeynes@1038
   571
nkeynes@1038
   572
gboolean dreamcast_has_quick_state( unsigned int state )
nkeynes@1038
   573
{
nkeynes@1038
   574
    gchar *str = get_quick_state_filename(state);
nkeynes@1038
   575
    int result = access(str, R_OK);
nkeynes@1038
   576
    g_free(str);
nkeynes@1038
   577
    return result == 0 ? TRUE : FALSE;
nkeynes@1038
   578
}
nkeynes@1038
   579
nkeynes@934
   580
/********************* The Boot ROM address space **********************/
nkeynes@934
   581
static int32_t FASTCALL ext_bootrom_read_long( sh4addr_t addr )
nkeynes@934
   582
{
nkeynes@934
   583
    return *((int32_t *)(dc_boot_rom + (addr&0x001FFFFF)));
nkeynes@934
   584
}
nkeynes@934
   585
static int32_t FASTCALL ext_bootrom_read_word( sh4addr_t addr )
nkeynes@934
   586
{
nkeynes@934
   587
    return SIGNEXT16(*((int16_t *)(dc_boot_rom + (addr&0x001FFFFF))));
nkeynes@934
   588
}
nkeynes@934
   589
static int32_t FASTCALL ext_bootrom_read_byte( sh4addr_t addr )
nkeynes@934
   590
{
nkeynes@934
   591
    return SIGNEXT8(*((int16_t *)(dc_boot_rom + (addr&0x001FFFFF))));
nkeynes@934
   592
}
nkeynes@934
   593
static void FASTCALL ext_bootrom_read_burst( unsigned char *dest, sh4addr_t addr )
nkeynes@934
   594
{
nkeynes@934
   595
    memcpy( dest, dc_boot_rom +(addr&0x001FFFFF), 32 );
nkeynes@934
   596
}
nkeynes@934
   597
nkeynes@934
   598
struct mem_region_fn mem_region_bootrom = { 
nkeynes@934
   599
        ext_bootrom_read_long, unmapped_write_long, 
nkeynes@934
   600
        ext_bootrom_read_word, unmapped_write_long, 
nkeynes@934
   601
        ext_bootrom_read_byte, unmapped_write_long, 
nkeynes@934
   602
        ext_bootrom_read_burst, unmapped_write_burst }; 
nkeynes@934
   603
nkeynes@934
   604
/********************* The Flash RAM address space **********************/
nkeynes@934
   605
static int32_t FASTCALL ext_flashram_read_long( sh4addr_t addr )
nkeynes@934
   606
{
nkeynes@934
   607
    return *((int32_t *)(dc_flash_ram + (addr&0x0001FFFF)));
nkeynes@934
   608
}
nkeynes@934
   609
static int32_t FASTCALL ext_flashram_read_word( sh4addr_t addr )
nkeynes@934
   610
{
nkeynes@934
   611
    return SIGNEXT16(*((int16_t *)(dc_flash_ram + (addr&0x0001FFFF))));
nkeynes@934
   612
}
nkeynes@934
   613
static int32_t FASTCALL ext_flashram_read_byte( sh4addr_t addr )
nkeynes@934
   614
{
nkeynes@934
   615
    return SIGNEXT8(*((int16_t *)(dc_flash_ram + (addr&0x0001FFFF))));
nkeynes@934
   616
}
nkeynes@934
   617
static void FASTCALL ext_flashram_write_long( sh4addr_t addr, uint32_t val )
nkeynes@934
   618
{
nkeynes@934
   619
    *(uint32_t *)(dc_flash_ram + (addr&0x0001FFFF)) = val;
nkeynes@934
   620
    asic_g2_write_word();
nkeynes@934
   621
}
nkeynes@934
   622
static void FASTCALL ext_flashram_write_word( sh4addr_t addr, uint32_t val )
nkeynes@934
   623
{
nkeynes@934
   624
    *(uint16_t *)(dc_flash_ram + (addr&0x0001FFFF)) = (uint16_t)val;
nkeynes@934
   625
    asic_g2_write_word();
nkeynes@934
   626
}
nkeynes@934
   627
static void FASTCALL ext_flashram_write_byte( sh4addr_t addr, uint32_t val )
nkeynes@934
   628
{
nkeynes@934
   629
    *(uint8_t *)(dc_flash_ram + (addr&0x0001FFFF)) = (uint8_t)val;
nkeynes@934
   630
    asic_g2_write_word();
nkeynes@934
   631
}
nkeynes@934
   632
static void FASTCALL ext_flashram_read_burst( unsigned char *dest, sh4addr_t addr )
nkeynes@934
   633
{
nkeynes@934
   634
    memcpy( dest, dc_flash_ram+(addr&0x0001FFFF), 32 );
nkeynes@934
   635
}
nkeynes@934
   636
static void FASTCALL ext_flashram_write_burst( sh4addr_t addr, unsigned char *src )
nkeynes@934
   637
{
nkeynes@934
   638
    memcpy( dc_flash_ram+(addr&0x0001FFFF), src, 32 );
nkeynes@934
   639
}
nkeynes@934
   640
nkeynes@934
   641
struct mem_region_fn mem_region_flashram = { ext_flashram_read_long, ext_flashram_write_long, 
nkeynes@934
   642
        ext_flashram_read_word, ext_flashram_write_word, 
nkeynes@934
   643
        ext_flashram_read_byte, ext_flashram_write_byte, 
nkeynes@934
   644
        ext_flashram_read_burst, ext_flashram_write_burst }; 
nkeynes@934
   645
.