Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 1296:30ecee61f811
prev1167:2cc6c50d0dd2
author nkeynes
date Sat Jan 26 14:00:48 2013 +1000 (9 years ago)
permissions -rw-r--r--
last change Change glib includes to #include <glib.h> rather than the individual
headers, as recent glib versions are breaking on this
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@10
     3
 * mem.c is responsible for creating and maintaining the overall system memory
nkeynes@10
     4
 * map, as visible from the SH4 processor. 
nkeynes@10
     5
 *
nkeynes@10
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@10
     7
 *
nkeynes@10
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@10
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@10
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@10
    11
 * (at your option) any later version.
nkeynes@10
    12
 *
nkeynes@10
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@10
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@10
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@10
    16
 * GNU General Public License for more details.
nkeynes@10
    17
 */
nkeynes@35
    18
#define MODULE mem_module
nkeynes@10
    19
nkeynes@488
    20
#include <sys/types.h>
nkeynes@10
    21
#include <sys/mman.h>
nkeynes@19
    22
#include <sys/stat.h>
nkeynes@1296
    23
#include <glib.h>
nkeynes@10
    24
#include <assert.h>
nkeynes@10
    25
#include <stdint.h>
nkeynes@10
    26
#include <stdlib.h>
nkeynes@10
    27
#include <stdio.h>
nkeynes@10
    28
#include <unistd.h>
nkeynes@10
    29
#include <fcntl.h>
nkeynes@10
    30
#include <errno.h>
nkeynes@10
    31
#include <string.h>
nkeynes@10
    32
#include <zlib.h>
nkeynes@10
    33
#include "dream.h"
nkeynes@10
    34
#include "mem.h"
nkeynes@10
    35
#include "mmio.h"
nkeynes@10
    36
#include "dreamcast.h"
nkeynes@10
    37
nkeynes@939
    38
#ifndef PAGE_SIZE
nkeynes@939
    39
#define PAGE_SIZE 4096
nkeynes@939
    40
#endif
nkeynes@939
    41
nkeynes@502
    42
sh4ptr_t *page_map = NULL;
nkeynes@930
    43
mem_region_fn_t *ext_address_space = NULL;
nkeynes@930
    44
nkeynes@930
    45
extern struct mem_region_fn mem_region_unmapped; 
nkeynes@10
    46
nkeynes@968
    47
static int mem_load(FILE *f);
nkeynes@968
    48
static void mem_save(FILE *f);
nkeynes@15
    49
struct dreamcast_module mem_module =
nkeynes@736
    50
{ "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
nkeynes@15
    51
nkeynes@10
    52
struct mem_region mem_rgn[MAX_MEM_REGIONS];
nkeynes@10
    53
struct mmio_region *io_rgn[MAX_IO_REGIONS];
nkeynes@10
    54
struct mmio_region *P4_io[4096];
nkeynes@10
    55
nkeynes@674
    56
uint32_t num_io_rgns = 0, num_mem_rgns = 0;
nkeynes@10
    57
nkeynes@931
    58
DEFINE_HOOK( mem_page_remapped_hook, mem_page_remapped_hook_t );
nkeynes@931
    59
static void mem_page_remapped( sh4addr_t addr, mem_region_fn_t fn )
nkeynes@931
    60
{
nkeynes@931
    61
    CALL_HOOKS( mem_page_remapped_hook, addr, fn );
nkeynes@931
    62
}
nkeynes@931
    63
nkeynes@931
    64
/********************* The "unmapped" address space ********************/
nkeynes@931
    65
/* Always reads as 0, writes have no effect */
nkeynes@931
    66
int32_t FASTCALL unmapped_read_long( sh4addr_t addr )
nkeynes@931
    67
{
nkeynes@931
    68
    return 0;
nkeynes@931
    69
}
nkeynes@931
    70
void FASTCALL unmapped_write_long( sh4addr_t addr, uint32_t val )
nkeynes@931
    71
{
nkeynes@931
    72
}
nkeynes@931
    73
void FASTCALL unmapped_read_burst( unsigned char *dest, sh4addr_t addr )
nkeynes@931
    74
{
nkeynes@931
    75
    memset( dest, 0, 32 );
nkeynes@931
    76
}
nkeynes@931
    77
void FASTCALL unmapped_write_burst( sh4addr_t addr, unsigned char *src )
nkeynes@931
    78
{
nkeynes@931
    79
}
nkeynes@931
    80
nkeynes@946
    81
void FASTCALL unmapped_prefetch( sh4addr_t addr )
nkeynes@946
    82
{
nkeynes@946
    83
    /* No effect */
nkeynes@946
    84
}
nkeynes@946
    85
nkeynes@1167
    86
void FASTCALL default_write_burst( sh4addr_t addr, unsigned char *src )
nkeynes@1167
    87
{
nkeynes@1167
    88
    mem_write_fn_t writefn = ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long;
nkeynes@1167
    89
    uint32_t *p = (uint32_t *)src;
nkeynes@1167
    90
    sh4addr_t end = addr + 32;
nkeynes@1167
    91
    while( addr < end ) {
nkeynes@1167
    92
        writefn(addr, *p);
nkeynes@1167
    93
        addr += 4;
nkeynes@1167
    94
        p += 4;
nkeynes@1167
    95
    }
nkeynes@1167
    96
}
nkeynes@1167
    97
nkeynes@1167
    98
void FASTCALL default_read_burst( unsigned char *dest, sh4addr_t addr )
nkeynes@1167
    99
{
nkeynes@1167
   100
    mem_read_fn_t readfn = ext_address_space[(addr&0x1FFFFFFF)>>12]->read_long;
nkeynes@1167
   101
    uint32_t *p = (uint32_t *)dest;
nkeynes@1167
   102
    sh4addr_t end = addr + 32;
nkeynes@1167
   103
    while( addr < end ) {
nkeynes@1167
   104
        *p = readfn(addr);
nkeynes@1167
   105
        addr += 4;
nkeynes@1167
   106
        p += 4;
nkeynes@1167
   107
    }
nkeynes@1167
   108
}
nkeynes@1167
   109
nkeynes@931
   110
struct mem_region_fn mem_region_unmapped = { 
nkeynes@931
   111
        unmapped_read_long, unmapped_write_long, 
nkeynes@931
   112
        unmapped_read_long, unmapped_write_long, 
nkeynes@931
   113
        unmapped_read_long, unmapped_write_long, 
nkeynes@946
   114
        unmapped_read_burst, unmapped_write_burst,
nkeynes@975
   115
        unmapped_prefetch, unmapped_read_long }; 
nkeynes@931
   116
nkeynes@10
   117
void *mem_alloc_pages( int n )
nkeynes@10
   118
{
nkeynes@10
   119
    void *mem = mmap( NULL, n * 4096,
nkeynes@736
   120
            PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@10
   121
    if( mem == MAP_FAILED ) {
nkeynes@10
   122
        ERROR( "Memory allocation failure! (%s)", strerror(errno) );
nkeynes@10
   123
        return NULL;
nkeynes@10
   124
    }
nkeynes@10
   125
    return mem;
nkeynes@10
   126
}
nkeynes@10
   127
nkeynes@939
   128
void mem_unprotect( void *region, uint32_t size )
nkeynes@939
   129
{
nkeynes@939
   130
    /* Force page alignment */
nkeynes@939
   131
    uintptr_t i = (uintptr_t)region;
nkeynes@939
   132
    uintptr_t mask = ~(PAGE_SIZE-1);
nkeynes@939
   133
    void *ptr = (void *)(i & mask);
nkeynes@968
   134
    size_t len = (i & (PAGE_SIZE-1)) + size;
nkeynes@939
   135
    len = (len + (PAGE_SIZE-1)) & mask;
nkeynes@939
   136
    
nkeynes@939
   137
    int status = mprotect( ptr, len, PROT_READ|PROT_WRITE|PROT_EXEC );
nkeynes@939
   138
    assert( status == 0 );
nkeynes@939
   139
}
nkeynes@10
   140
nkeynes@10
   141
void mem_init( void )
nkeynes@10
   142
{
nkeynes@930
   143
    int i;
nkeynes@930
   144
    mem_region_fn_t *ptr;
nkeynes@986
   145
    page_map = (sh4ptr_t *)mmap( NULL, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES,
nkeynes@736
   146
            PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@10
   147
    if( page_map == MAP_FAILED ) {
nkeynes@930
   148
        FATAL( "Unable to allocate page map! (%s)", strerror(errno) );
nkeynes@930
   149
    }
nkeynes@930
   150
    memset( page_map, 0, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES );
nkeynes@930
   151
    
nkeynes@986
   152
    ext_address_space = (mem_region_fn_t *) mmap( NULL, sizeof(mem_region_fn_t) * LXDREAM_PAGE_TABLE_ENTRIES,
nkeynes@930
   153
            PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@930
   154
    if( ext_address_space == MAP_FAILED ) {
nkeynes@930
   155
        FATAL( "Unable to allocate external memory map (%s)", strerror(errno) );
nkeynes@10
   156
    }
nkeynes@10
   157
nkeynes@930
   158
    for( ptr = ext_address_space, i = LXDREAM_PAGE_TABLE_ENTRIES; i > 0; ptr++, i-- ) {
nkeynes@930
   159
        *ptr = &mem_region_unmapped;
nkeynes@930
   160
    }
nkeynes@10
   161
}
nkeynes@10
   162
nkeynes@10
   163
void mem_reset( void )
nkeynes@10
   164
{
nkeynes@10
   165
    /* Restore all mmio registers to their initial settings */
nkeynes@10
   166
    int i, j;
nkeynes@10
   167
    for( i=1; i<num_io_rgns; i++ ) {
nkeynes@10
   168
        for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
nkeynes@10
   169
            if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
nkeynes@736
   170
                    io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
nkeynes@929
   171
                io_rgn[i]->fn.write_long( io_rgn[i]->ports[j].offset,
nkeynes@736
   172
                        io_rgn[i]->ports[j].def_val );
nkeynes@10
   173
            }
nkeynes@10
   174
        }
nkeynes@10
   175
    }
nkeynes@10
   176
}
nkeynes@10
   177
nkeynes@968
   178
static void mem_save( FILE *f ) 
nkeynes@17
   179
{
nkeynes@934
   180
    int i, num_ram_regions = 0;
nkeynes@17
   181
    uint32_t len;
nkeynes@736
   182
nkeynes@934
   183
    /* All RAM regions (ROM and non-memory regions don't need to be saved)
nkeynes@934
   184
     * Flash is questionable - for now we save it too */
nkeynes@17
   185
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   186
        if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
nkeynes@934
   187
            num_ram_regions++;
nkeynes@934
   188
        }
nkeynes@934
   189
    }
nkeynes@934
   190
    fwrite( &num_ram_regions, sizeof(num_ram_regions), 1, f );
nkeynes@934
   191
    
nkeynes@934
   192
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   193
        if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
nkeynes@932
   194
            fwrite_string( mem_rgn[i].name, f );
nkeynes@932
   195
            fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
nkeynes@932
   196
            fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
nkeynes@932
   197
            fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
nkeynes@934
   198
            fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
nkeynes@932
   199
        }
nkeynes@17
   200
    }
nkeynes@17
   201
nkeynes@17
   202
    /* All MMIO regions */
nkeynes@17
   203
    fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
nkeynes@17
   204
    for( i=0; i<num_io_rgns; i++ ) {
nkeynes@736
   205
        fwrite_string( io_rgn[i]->id, f );
nkeynes@736
   206
        fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
nkeynes@736
   207
        len = 4096;
nkeynes@736
   208
        fwrite( &len, sizeof(len), 1, f );
nkeynes@736
   209
        fwrite_gzip( io_rgn[i]->mem, len, 1, f );
nkeynes@17
   210
    }
nkeynes@17
   211
}
nkeynes@17
   212
nkeynes@968
   213
static int mem_load( FILE *f )
nkeynes@17
   214
{
nkeynes@17
   215
    char tmp[64];
nkeynes@17
   216
    uint32_t len;
nkeynes@18
   217
    uint32_t base, size;
nkeynes@510
   218
    uint32_t flags;
nkeynes@934
   219
    int i, j;
nkeynes@934
   220
    int mem_region_loaded[MAX_MEM_REGIONS];
nkeynes@17
   221
nkeynes@934
   222
    /* All RAM regions */
nkeynes@934
   223
    memset( mem_region_loaded, 0, sizeof(mem_region_loaded) );
nkeynes@18
   224
    fread( &len, sizeof(len), 1, f );
nkeynes@18
   225
    for( i=0; i<len; i++ ) {
nkeynes@934
   226
        fread_string( tmp, sizeof(tmp), f );
nkeynes@934
   227
nkeynes@934
   228
        for( j=0; j<num_mem_rgns; j++ ) {
nkeynes@934
   229
            if( strcasecmp( mem_rgn[j].name, tmp ) == 0 ) {
nkeynes@934
   230
                fread( &base, sizeof(base), 1, f );
nkeynes@934
   231
                fread( &flags, sizeof(flags), 1, f );
nkeynes@934
   232
                fread( &size, sizeof(size), 1, f );
nkeynes@934
   233
                if( base != mem_rgn[j].base ||
nkeynes@934
   234
                    flags != mem_rgn[j].flags ||
nkeynes@934
   235
                    size != mem_rgn[j].size ) {
nkeynes@934
   236
                    ERROR( "Bad memory block %d %s (not mapped to expected region)", i, tmp );
nkeynes@934
   237
                    return -1;
nkeynes@934
   238
                }
nkeynes@934
   239
                if( flags != MEM_FLAG_RAM ) {
nkeynes@934
   240
                    ERROR( "Unexpected memory block %d %s (Not a RAM region)", i, tmp );
nkeynes@934
   241
                    return -1;
nkeynes@934
   242
                }
nkeynes@934
   243
                fread_gzip( mem_rgn[j].mem, size, 1, f );
nkeynes@934
   244
                mem_region_loaded[j] = 1;
nkeynes@959
   245
                break;
nkeynes@932
   246
            }
nkeynes@934
   247
        }
nkeynes@934
   248
    }
nkeynes@934
   249
    /* Make sure we got all the memory regions we expected */
nkeynes@934
   250
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   251
        if( mem_rgn[i].flags == MEM_FLAG_RAM &&
nkeynes@934
   252
            mem_region_loaded[i] == 0 ) {
nkeynes@934
   253
            ERROR( "Missing memory block %s (not found in save state)", mem_rgn[i].name );
nkeynes@934
   254
            return -1;
nkeynes@736
   255
        }
nkeynes@18
   256
    }
nkeynes@18
   257
nkeynes@18
   258
    /* All MMIO regions */
nkeynes@18
   259
    fread( &len, sizeof(len), 1, f );
nkeynes@934
   260
    if( len != num_io_rgns ) {
nkeynes@934
   261
        ERROR( "Unexpected IO region count %d (expected %d)", len, num_io_rgns );
nkeynes@736
   262
        return -1;
nkeynes@934
   263
    }
nkeynes@934
   264
    
nkeynes@18
   265
    for( i=0; i<len; i++ ) {
nkeynes@736
   266
        fread_string( tmp, sizeof(tmp), f );
nkeynes@736
   267
        fread( &base, sizeof(base), 1, f );
nkeynes@736
   268
        fread( &size, sizeof(size), 1, f );
nkeynes@736
   269
        if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
nkeynes@736
   270
                base != io_rgn[i]->base ||
nkeynes@736
   271
                size != 4096 ) {
nkeynes@736
   272
            ERROR( "Bad MMIO region %d %s", i, tmp );
nkeynes@736
   273
            return -1;
nkeynes@736
   274
        }
nkeynes@736
   275
        fread_gzip( io_rgn[i]->mem, size, 1, f );
nkeynes@18
   276
    }
nkeynes@18
   277
    return 0;
nkeynes@17
   278
}
nkeynes@17
   279
nkeynes@50
   280
int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   281
{
nkeynes@502
   282
    sh4ptr_t region;
nkeynes@50
   283
    int len = 4096, total = 0;
nkeynes@50
   284
    uint32_t addr = start;
nkeynes@50
   285
    FILE *f = fopen(file,"w");
nkeynes@19
   286
nkeynes@50
   287
    if( f == NULL )
nkeynes@736
   288
        return errno;
nkeynes@736
   289
nkeynes@50
   290
    while( total < length ) {
nkeynes@736
   291
        region = mem_get_region(addr);
nkeynes@736
   292
        len = 4096 - (addr & 0x0FFF);
nkeynes@736
   293
        if( len > (length-total) ) 
nkeynes@736
   294
            len = (length-total);
nkeynes@736
   295
        if( fwrite( region, len, 1, f ) != 1 ) {
nkeynes@736
   296
            ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
nkeynes@736
   297
            break;
nkeynes@736
   298
        }
nkeynes@736
   299
nkeynes@736
   300
        addr += len;
nkeynes@736
   301
        total += len;
nkeynes@50
   302
    }
nkeynes@50
   303
    fclose( f );
nkeynes@1075
   304
    DEBUG( "Saved %d of %d bytes to %08X", total, length, start );
nkeynes@50
   305
    return 0;
nkeynes@19
   306
}
nkeynes@19
   307
nkeynes@19
   308
int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   309
{
nkeynes@502
   310
    sh4ptr_t region;
nkeynes@19
   311
    int len = 4096, total = 0;
nkeynes@19
   312
    uint32_t addr = start;
nkeynes@19
   313
    struct stat st;
nkeynes@19
   314
    FILE *f = fopen(file,"r");
nkeynes@19
   315
nkeynes@180
   316
    if( f == NULL ) {
nkeynes@736
   317
        WARN( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@736
   318
        return -1;
nkeynes@180
   319
    }
nkeynes@19
   320
    fstat( fileno(f), &st );
nkeynes@72
   321
    if( length == 0 || length == -1 || length > st.st_size )
nkeynes@736
   322
        length = st.st_size;
nkeynes@736
   323
nkeynes@19
   324
    while( total < length ) {
nkeynes@736
   325
        region = mem_get_region(addr);
nkeynes@736
   326
        len = 4096 - (addr & 0x0FFF);
nkeynes@736
   327
        if( len > (length-total) ) 
nkeynes@736
   328
            len = (length-total);
nkeynes@736
   329
        if( fread( region, len, 1, f ) != 1 ) {
nkeynes@736
   330
            ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
nkeynes@736
   331
            break;
nkeynes@736
   332
        }
nkeynes@736
   333
nkeynes@736
   334
        addr += len;
nkeynes@736
   335
        total += len;
nkeynes@19
   336
    }
nkeynes@19
   337
    fclose( f );
nkeynes@1075
   338
    DEBUG( "Loaded %d of %d bytes to %08X", total, length, start );
nkeynes@19
   339
    return 0;
nkeynes@19
   340
}
nkeynes@19
   341
nkeynes@10
   342
struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
nkeynes@934
   343
                                   const char *name, mem_region_fn_t fn, int flags, 
nkeynes@934
   344
                                   uint32_t repeat_offset, uint32_t repeat_until )
nkeynes@10
   345
{
nkeynes@10
   346
    int i;
nkeynes@10
   347
    mem_rgn[num_mem_rgns].base = base;
nkeynes@10
   348
    mem_rgn[num_mem_rgns].size = size;
nkeynes@10
   349
    mem_rgn[num_mem_rgns].flags = flags;
nkeynes@10
   350
    mem_rgn[num_mem_rgns].name = name;
nkeynes@10
   351
    mem_rgn[num_mem_rgns].mem = mem;
nkeynes@929
   352
    mem_rgn[num_mem_rgns].fn = fn;
nkeynes@946
   353
    fn->prefetch = unmapped_prefetch;
nkeynes@975
   354
    fn->read_byte_for_write = fn->read_byte;
nkeynes@10
   355
    num_mem_rgns++;
nkeynes@10
   356
nkeynes@146
   357
    do {
nkeynes@930
   358
        for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
nkeynes@934
   359
            if( mem != NULL ) {
nkeynes@968
   360
                page_map[(base>>LXDREAM_PAGE_BITS)+i] = ((unsigned char *)mem) + (i<<LXDREAM_PAGE_BITS);
nkeynes@934
   361
            }
nkeynes@930
   362
            ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
nkeynes@931
   363
            mem_page_remapped( base + (i<<LXDREAM_PAGE_BITS), fn );
nkeynes@930
   364
        }
nkeynes@736
   365
        base += repeat_offset;	
nkeynes@146
   366
    } while( base <= repeat_until );
nkeynes@10
   367
nkeynes@10
   368
    return &mem_rgn[num_mem_rgns-1];
nkeynes@10
   369
}
nkeynes@10
   370
nkeynes@934
   371
gboolean mem_load_rom( void *output, const gchar *file, uint32_t size, uint32_t crc )
nkeynes@931
   372
{
nkeynes@934
   373
    if( file != NULL && file[0] != '\0' ) {
nkeynes@934
   374
        FILE *f = fopen(file,"r");
nkeynes@934
   375
        struct stat st;
nkeynes@934
   376
        uint32_t calc_crc;
nkeynes@931
   377
nkeynes@934
   378
        if( f == NULL ) {
nkeynes@934
   379
            ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@736
   380
            return FALSE;
nkeynes@736
   381
        }
nkeynes@461
   382
nkeynes@934
   383
        fstat( fileno(f), &st );
nkeynes@934
   384
        if( st.st_size != size ) {
nkeynes@934
   385
            ERROR( "File '%s' is invalid, expected %d bytes but was %d bytes long.", file, size, st.st_size );
nkeynes@934
   386
            fclose(f);
nkeynes@934
   387
            return FALSE;
nkeynes@934
   388
        }
nkeynes@934
   389
        
nkeynes@934
   390
        if( fread( output, 1, size, f ) != size ) { 
nkeynes@934
   391
            ERROR( "Failed to load file '%s': %s", file, strerror(errno) );
nkeynes@934
   392
            fclose(f);
nkeynes@934
   393
            return FALSE;
nkeynes@934
   394
        }
nkeynes@10
   395
nkeynes@934
   396
        /* CRC check only if we loaded something */
nkeynes@934
   397
        calc_crc = crc32(0L, output, size);
nkeynes@934
   398
        if( calc_crc != crc ) {
nkeynes@934
   399
            WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
nkeynes@934
   400
                   file, calc_crc, crc);
nkeynes@736
   401
        }
nkeynes@934
   402
        /* Even if the CRC fails, continue normally */
nkeynes@934
   403
        return TRUE;
nkeynes@10
   404
    }
nkeynes@543
   405
    return FALSE;
nkeynes@10
   406
}
nkeynes@10
   407
nkeynes@502
   408
sh4ptr_t mem_get_region_by_name( const char *name )
nkeynes@10
   409
{
nkeynes@10
   410
    int i;
nkeynes@10
   411
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@10
   412
        if( strcmp( mem_rgn[i].name, name ) == 0 )
nkeynes@10
   413
            return mem_rgn[i].mem;
nkeynes@10
   414
    }
nkeynes@10
   415
    return NULL;
nkeynes@10
   416
}
nkeynes@10
   417
nkeynes@10
   418
void register_io_region( struct mmio_region *io )
nkeynes@10
   419
{
nkeynes@10
   420
    int i;
nkeynes@736
   421
nkeynes@10
   422
    assert(io);
nkeynes@10
   423
    io->mem = mem_alloc_pages(2);
nkeynes@796
   424
    io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
nkeynes@10
   425
    io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
nkeynes@41
   426
    io->trace_flag = 0;
nkeynes@1167
   427
    if( io->fn.write_burst == NULL )
nkeynes@1167
   428
        io->fn.write_burst = default_write_burst;
nkeynes@1167
   429
    if( io->fn.read_burst == NULL )
nkeynes@1167
   430
        io->fn.read_burst = default_read_burst;
nkeynes@10
   431
    memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
nkeynes@10
   432
    for( i=0; io->ports[i].id != NULL; i++ ) {
nkeynes@10
   433
        io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
nkeynes@10
   434
        *io->ports[i].val = io->ports[i].def_val;
nkeynes@10
   435
        io->index[io->ports[i].offset>>2] = &io->ports[i];
nkeynes@10
   436
    }
nkeynes@796
   437
    memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
nkeynes@10
   438
    if( (io->base & 0xFF000000) == 0xFF000000 ) {
nkeynes@10
   439
        /* P4 area (on-chip I/O channels */
nkeynes@10
   440
        P4_io[(io->base&0x1FFFFFFF)>>19] = io;
nkeynes@10
   441
    } else {
nkeynes@674
   442
        page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
nkeynes@930
   443
        ext_address_space[io->base>>12] = &io->fn;
nkeynes@931
   444
        mem_page_remapped( io->base, &io->fn );
nkeynes@10
   445
    }
nkeynes@10
   446
    io_rgn[num_io_rgns] = io;
nkeynes@10
   447
    num_io_rgns++;
nkeynes@10
   448
}
nkeynes@10
   449
nkeynes@10
   450
void register_io_regions( struct mmio_region **io )
nkeynes@10
   451
{
nkeynes@10
   452
    while( *io ) register_io_region( *io++ );
nkeynes@10
   453
}
nkeynes@10
   454
nkeynes@934
   455
gboolean mem_has_page( uint32_t addr )
nkeynes@10
   456
{
nkeynes@934
   457
    return ext_address_space[ (addr&0x1FFFFFFF)>>12 ] != &mem_region_unmapped;
nkeynes@10
   458
}
nkeynes@10
   459
nkeynes@502
   460
sh4ptr_t mem_get_region( uint32_t addr )
nkeynes@10
   461
{
nkeynes@502
   462
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@490
   463
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   464
        return NULL;
nkeynes@10
   465
    } else {
nkeynes@10
   466
        return page+(addr&0xFFF);
nkeynes@10
   467
    }
nkeynes@10
   468
}
nkeynes@11
   469
nkeynes@669
   470
void mem_write_long( sh4addr_t addr, uint32_t value )
nkeynes@669
   471
{
nkeynes@934
   472
    ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long(addr, value);
nkeynes@156
   473
}
nkeynes@156
   474
nkeynes@968
   475
static struct mmio_region *mem_get_io_region_by_name( const gchar *name )
nkeynes@156
   476
{
nkeynes@586
   477
    int i;
nkeynes@586
   478
    for( i=0; i<num_io_rgns; i++ ) {
nkeynes@736
   479
        if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
nkeynes@736
   480
            return io_rgn[i];
nkeynes@736
   481
        }
nkeynes@586
   482
    }
nkeynes@586
   483
    return NULL;
nkeynes@156
   484
}
nkeynes@156
   485
nkeynes@586
   486
void mem_set_trace( const gchar *tracelist, gboolean flag )
nkeynes@586
   487
{
nkeynes@586
   488
    if( tracelist != NULL ) {
nkeynes@736
   489
        gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
nkeynes@736
   490
        int i;
nkeynes@736
   491
        for( i=0; tracev[i] != NULL; i++ ) {
nkeynes@736
   492
            // Special case "all" - trace everything
nkeynes@736
   493
            if( strcasecmp(tracev[i], "all") == 0 ) {
nkeynes@736
   494
                int j;
nkeynes@736
   495
                for( j=0; j<num_io_rgns; j++ ) {
nkeynes@736
   496
                    io_rgn[j]->trace_flag = flag ? 1 : 0;
nkeynes@736
   497
                }
nkeynes@736
   498
                break;
nkeynes@736
   499
            }
nkeynes@736
   500
            struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
nkeynes@736
   501
            if( region == NULL ) {
nkeynes@736
   502
                WARN( "Unknown IO region '%s'", tracev[i] );
nkeynes@736
   503
            } else {
nkeynes@736
   504
                region->trace_flag = flag ? 1 : 0;
nkeynes@736
   505
            }
nkeynes@736
   506
        }
nkeynes@736
   507
        g_strfreev( tracev );
nkeynes@586
   508
    }
nkeynes@586
   509
}
nkeynes@586
   510
.