Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 934:3acd3b3ee6d1
prev932:2602c5603ce2
next939:6f2302afeb89
author nkeynes
date Fri Dec 26 14:25:23 2008 +0000 (12 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Change RAM regions to use static arrays rather than mmap regions, for a 2-3% performance gain.
General mem cleanups, including some save state fixes that break states again.
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@586
    23
#include <glib/gstrfuncs.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@502
    38
sh4ptr_t *page_map = NULL;
nkeynes@930
    39
mem_region_fn_t *ext_address_space = NULL;
nkeynes@930
    40
nkeynes@930
    41
extern struct mem_region_fn mem_region_unmapped; 
nkeynes@10
    42
nkeynes@17
    43
int mem_load(FILE *f);
nkeynes@17
    44
void mem_save(FILE *f);
nkeynes@15
    45
struct dreamcast_module mem_module =
nkeynes@736
    46
{ "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
nkeynes@15
    47
nkeynes@10
    48
struct mem_region mem_rgn[MAX_MEM_REGIONS];
nkeynes@10
    49
struct mmio_region *io_rgn[MAX_IO_REGIONS];
nkeynes@10
    50
struct mmio_region *P4_io[4096];
nkeynes@10
    51
nkeynes@674
    52
uint32_t num_io_rgns = 0, num_mem_rgns = 0;
nkeynes@10
    53
nkeynes@931
    54
DEFINE_HOOK( mem_page_remapped_hook, mem_page_remapped_hook_t );
nkeynes@931
    55
static void mem_page_remapped( sh4addr_t addr, mem_region_fn_t fn )
nkeynes@931
    56
{
nkeynes@931
    57
    CALL_HOOKS( mem_page_remapped_hook, addr, fn );
nkeynes@931
    58
}
nkeynes@931
    59
nkeynes@931
    60
/********************* The "unmapped" address space ********************/
nkeynes@931
    61
/* Always reads as 0, writes have no effect */
nkeynes@931
    62
int32_t FASTCALL unmapped_read_long( sh4addr_t addr )
nkeynes@931
    63
{
nkeynes@931
    64
    return 0;
nkeynes@931
    65
}
nkeynes@931
    66
void FASTCALL unmapped_write_long( sh4addr_t addr, uint32_t val )
nkeynes@931
    67
{
nkeynes@931
    68
}
nkeynes@931
    69
void FASTCALL unmapped_read_burst( unsigned char *dest, sh4addr_t addr )
nkeynes@931
    70
{
nkeynes@931
    71
    memset( dest, 0, 32 );
nkeynes@931
    72
}
nkeynes@931
    73
void FASTCALL unmapped_write_burst( sh4addr_t addr, unsigned char *src )
nkeynes@931
    74
{
nkeynes@931
    75
}
nkeynes@931
    76
nkeynes@931
    77
struct mem_region_fn mem_region_unmapped = { 
nkeynes@931
    78
        unmapped_read_long, unmapped_write_long, 
nkeynes@931
    79
        unmapped_read_long, unmapped_write_long, 
nkeynes@931
    80
        unmapped_read_long, unmapped_write_long, 
nkeynes@931
    81
        unmapped_read_burst, unmapped_write_burst }; 
nkeynes@931
    82
nkeynes@10
    83
void *mem_alloc_pages( int n )
nkeynes@10
    84
{
nkeynes@10
    85
    void *mem = mmap( NULL, n * 4096,
nkeynes@736
    86
            PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@10
    87
    if( mem == MAP_FAILED ) {
nkeynes@10
    88
        ERROR( "Memory allocation failure! (%s)", strerror(errno) );
nkeynes@10
    89
        return NULL;
nkeynes@10
    90
    }
nkeynes@10
    91
    return mem;
nkeynes@10
    92
}
nkeynes@10
    93
nkeynes@10
    94
nkeynes@10
    95
void mem_init( void )
nkeynes@10
    96
{
nkeynes@930
    97
    int i;
nkeynes@930
    98
    mem_region_fn_t *ptr;
nkeynes@796
    99
    page_map = mmap( NULL, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES,
nkeynes@736
   100
            PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@10
   101
    if( page_map == MAP_FAILED ) {
nkeynes@930
   102
        FATAL( "Unable to allocate page map! (%s)", strerror(errno) );
nkeynes@930
   103
    }
nkeynes@930
   104
    memset( page_map, 0, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES );
nkeynes@930
   105
    
nkeynes@930
   106
    ext_address_space = mmap( NULL, sizeof(mem_region_fn_t) * LXDREAM_PAGE_TABLE_ENTRIES,
nkeynes@930
   107
            PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@930
   108
    if( ext_address_space == MAP_FAILED ) {
nkeynes@930
   109
        FATAL( "Unable to allocate external memory map (%s)", strerror(errno) );
nkeynes@10
   110
    }
nkeynes@10
   111
nkeynes@930
   112
    for( ptr = ext_address_space, i = LXDREAM_PAGE_TABLE_ENTRIES; i > 0; ptr++, i-- ) {
nkeynes@930
   113
        *ptr = &mem_region_unmapped;
nkeynes@930
   114
    }
nkeynes@10
   115
}
nkeynes@10
   116
nkeynes@10
   117
void mem_reset( void )
nkeynes@10
   118
{
nkeynes@10
   119
    /* Restore all mmio registers to their initial settings */
nkeynes@10
   120
    int i, j;
nkeynes@10
   121
    for( i=1; i<num_io_rgns; i++ ) {
nkeynes@10
   122
        for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
nkeynes@10
   123
            if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
nkeynes@736
   124
                    io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
nkeynes@929
   125
                io_rgn[i]->fn.write_long( io_rgn[i]->ports[j].offset,
nkeynes@736
   126
                        io_rgn[i]->ports[j].def_val );
nkeynes@10
   127
            }
nkeynes@10
   128
        }
nkeynes@10
   129
    }
nkeynes@10
   130
}
nkeynes@10
   131
nkeynes@17
   132
void mem_save( FILE *f ) 
nkeynes@17
   133
{
nkeynes@934
   134
    int i, num_ram_regions = 0;
nkeynes@17
   135
    uint32_t len;
nkeynes@736
   136
nkeynes@934
   137
    /* All RAM regions (ROM and non-memory regions don't need to be saved)
nkeynes@934
   138
     * Flash is questionable - for now we save it too */
nkeynes@17
   139
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   140
        if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
nkeynes@934
   141
            num_ram_regions++;
nkeynes@934
   142
        }
nkeynes@934
   143
    }
nkeynes@934
   144
    fwrite( &num_ram_regions, sizeof(num_ram_regions), 1, f );
nkeynes@934
   145
    
nkeynes@934
   146
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   147
        if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
nkeynes@932
   148
            fwrite_string( mem_rgn[i].name, f );
nkeynes@932
   149
            fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
nkeynes@932
   150
            fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
nkeynes@932
   151
            fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
nkeynes@934
   152
            fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
nkeynes@932
   153
        }
nkeynes@17
   154
    }
nkeynes@17
   155
nkeynes@17
   156
    /* All MMIO regions */
nkeynes@17
   157
    fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
nkeynes@17
   158
    for( i=0; i<num_io_rgns; i++ ) {
nkeynes@736
   159
        fwrite_string( io_rgn[i]->id, f );
nkeynes@736
   160
        fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
nkeynes@736
   161
        len = 4096;
nkeynes@736
   162
        fwrite( &len, sizeof(len), 1, f );
nkeynes@736
   163
        fwrite_gzip( io_rgn[i]->mem, len, 1, f );
nkeynes@17
   164
    }
nkeynes@17
   165
}
nkeynes@17
   166
nkeynes@17
   167
int mem_load( FILE *f )
nkeynes@17
   168
{
nkeynes@17
   169
    char tmp[64];
nkeynes@17
   170
    uint32_t len;
nkeynes@18
   171
    uint32_t base, size;
nkeynes@510
   172
    uint32_t flags;
nkeynes@934
   173
    int i, j;
nkeynes@934
   174
    int mem_region_loaded[MAX_MEM_REGIONS];
nkeynes@17
   175
nkeynes@934
   176
    /* All RAM regions */
nkeynes@934
   177
    memset( mem_region_loaded, 0, sizeof(mem_region_loaded) );
nkeynes@18
   178
    fread( &len, sizeof(len), 1, f );
nkeynes@18
   179
    for( i=0; i<len; i++ ) {
nkeynes@934
   180
        fread_string( tmp, sizeof(tmp), f );
nkeynes@934
   181
nkeynes@934
   182
        for( j=0; j<num_mem_rgns; j++ ) {
nkeynes@934
   183
            if( strcasecmp( mem_rgn[j].name, tmp ) == 0 ) {
nkeynes@934
   184
                fread( &base, sizeof(base), 1, f );
nkeynes@934
   185
                fread( &flags, sizeof(flags), 1, f );
nkeynes@934
   186
                fread( &size, sizeof(size), 1, f );
nkeynes@934
   187
                if( base != mem_rgn[j].base ||
nkeynes@934
   188
                    flags != mem_rgn[j].flags ||
nkeynes@934
   189
                    size != mem_rgn[j].size ) {
nkeynes@934
   190
                    ERROR( "Bad memory block %d %s (not mapped to expected region)", i, tmp );
nkeynes@934
   191
                    return -1;
nkeynes@934
   192
                }
nkeynes@934
   193
                if( flags != MEM_FLAG_RAM ) {
nkeynes@934
   194
                    ERROR( "Unexpected memory block %d %s (Not a RAM region)", i, tmp );
nkeynes@934
   195
                    return -1;
nkeynes@934
   196
                }
nkeynes@934
   197
                fread_gzip( mem_rgn[j].mem, size, 1, f );
nkeynes@934
   198
                mem_region_loaded[j] = 1;
nkeynes@932
   199
            }
nkeynes@934
   200
        }
nkeynes@934
   201
    }
nkeynes@934
   202
    /* Make sure we got all the memory regions we expected */
nkeynes@934
   203
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   204
        if( mem_rgn[i].flags == MEM_FLAG_RAM &&
nkeynes@934
   205
            mem_region_loaded[i] == 0 ) {
nkeynes@934
   206
            ERROR( "Missing memory block %s (not found in save state)", mem_rgn[i].name );
nkeynes@934
   207
            return -1;
nkeynes@736
   208
        }
nkeynes@18
   209
    }
nkeynes@18
   210
nkeynes@18
   211
    /* All MMIO regions */
nkeynes@18
   212
    fread( &len, sizeof(len), 1, f );
nkeynes@934
   213
    if( len != num_io_rgns ) {
nkeynes@934
   214
        ERROR( "Unexpected IO region count %d (expected %d)", len, num_io_rgns );
nkeynes@736
   215
        return -1;
nkeynes@934
   216
    }
nkeynes@934
   217
    
nkeynes@18
   218
    for( i=0; i<len; i++ ) {
nkeynes@736
   219
        fread_string( tmp, sizeof(tmp), f );
nkeynes@736
   220
        fread( &base, sizeof(base), 1, f );
nkeynes@736
   221
        fread( &size, sizeof(size), 1, f );
nkeynes@736
   222
        if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
nkeynes@736
   223
                base != io_rgn[i]->base ||
nkeynes@736
   224
                size != 4096 ) {
nkeynes@736
   225
            ERROR( "Bad MMIO region %d %s", i, tmp );
nkeynes@736
   226
            return -1;
nkeynes@736
   227
        }
nkeynes@736
   228
        fread_gzip( io_rgn[i]->mem, size, 1, f );
nkeynes@18
   229
    }
nkeynes@18
   230
    return 0;
nkeynes@17
   231
}
nkeynes@17
   232
nkeynes@50
   233
int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   234
{
nkeynes@502
   235
    sh4ptr_t region;
nkeynes@50
   236
    int len = 4096, total = 0;
nkeynes@50
   237
    uint32_t addr = start;
nkeynes@50
   238
    FILE *f = fopen(file,"w");
nkeynes@19
   239
nkeynes@50
   240
    if( f == NULL )
nkeynes@736
   241
        return errno;
nkeynes@736
   242
nkeynes@50
   243
    while( total < length ) {
nkeynes@736
   244
        region = mem_get_region(addr);
nkeynes@736
   245
        len = 4096 - (addr & 0x0FFF);
nkeynes@736
   246
        if( len > (length-total) ) 
nkeynes@736
   247
            len = (length-total);
nkeynes@736
   248
        if( fwrite( region, len, 1, f ) != 1 ) {
nkeynes@736
   249
            ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
nkeynes@736
   250
            break;
nkeynes@736
   251
        }
nkeynes@736
   252
nkeynes@736
   253
        addr += len;
nkeynes@736
   254
        total += len;
nkeynes@50
   255
    }
nkeynes@50
   256
    fclose( f );
nkeynes@743
   257
    INFO( "Saved %d of %d bytes to %08X", total, length, start );
nkeynes@50
   258
    return 0;
nkeynes@19
   259
}
nkeynes@19
   260
nkeynes@19
   261
int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   262
{
nkeynes@502
   263
    sh4ptr_t region;
nkeynes@19
   264
    int len = 4096, total = 0;
nkeynes@19
   265
    uint32_t addr = start;
nkeynes@19
   266
    struct stat st;
nkeynes@19
   267
    FILE *f = fopen(file,"r");
nkeynes@19
   268
nkeynes@180
   269
    if( f == NULL ) {
nkeynes@736
   270
        WARN( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@736
   271
        return -1;
nkeynes@180
   272
    }
nkeynes@19
   273
    fstat( fileno(f), &st );
nkeynes@72
   274
    if( length == 0 || length == -1 || length > st.st_size )
nkeynes@736
   275
        length = st.st_size;
nkeynes@736
   276
nkeynes@19
   277
    while( total < length ) {
nkeynes@736
   278
        region = mem_get_region(addr);
nkeynes@736
   279
        len = 4096 - (addr & 0x0FFF);
nkeynes@736
   280
        if( len > (length-total) ) 
nkeynes@736
   281
            len = (length-total);
nkeynes@736
   282
        if( fread( region, len, 1, f ) != 1 ) {
nkeynes@736
   283
            ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
nkeynes@736
   284
            break;
nkeynes@736
   285
        }
nkeynes@736
   286
nkeynes@736
   287
        addr += len;
nkeynes@736
   288
        total += len;
nkeynes@19
   289
    }
nkeynes@19
   290
    fclose( f );
nkeynes@19
   291
    INFO( "Loaded %d of %d bytes to %08X", total, length, start );
nkeynes@19
   292
    return 0;
nkeynes@19
   293
}
nkeynes@19
   294
nkeynes@10
   295
struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
nkeynes@934
   296
                                   const char *name, mem_region_fn_t fn, int flags, 
nkeynes@934
   297
                                   uint32_t repeat_offset, uint32_t repeat_until )
nkeynes@10
   298
{
nkeynes@10
   299
    int i;
nkeynes@10
   300
    mem_rgn[num_mem_rgns].base = base;
nkeynes@10
   301
    mem_rgn[num_mem_rgns].size = size;
nkeynes@10
   302
    mem_rgn[num_mem_rgns].flags = flags;
nkeynes@10
   303
    mem_rgn[num_mem_rgns].name = name;
nkeynes@10
   304
    mem_rgn[num_mem_rgns].mem = mem;
nkeynes@929
   305
    mem_rgn[num_mem_rgns].fn = fn;
nkeynes@10
   306
    num_mem_rgns++;
nkeynes@10
   307
nkeynes@146
   308
    do {
nkeynes@930
   309
        for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
nkeynes@934
   310
            if( mem != NULL ) {
nkeynes@934
   311
                page_map[(base>>LXDREAM_PAGE_BITS)+i] = mem + (i<<LXDREAM_PAGE_BITS);
nkeynes@934
   312
            }
nkeynes@930
   313
            ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
nkeynes@931
   314
            mem_page_remapped( base + (i<<LXDREAM_PAGE_BITS), fn );
nkeynes@930
   315
        }
nkeynes@736
   316
        base += repeat_offset;	
nkeynes@146
   317
    } while( base <= repeat_until );
nkeynes@10
   318
nkeynes@10
   319
    return &mem_rgn[num_mem_rgns-1];
nkeynes@10
   320
}
nkeynes@10
   321
nkeynes@934
   322
gboolean mem_load_rom( void *output, const gchar *file, uint32_t size, uint32_t crc )
nkeynes@931
   323
{
nkeynes@934
   324
    if( file != NULL && file[0] != '\0' ) {
nkeynes@934
   325
        FILE *f = fopen(file,"r");
nkeynes@934
   326
        struct stat st;
nkeynes@934
   327
        uint32_t calc_crc;
nkeynes@931
   328
nkeynes@934
   329
        if( f == NULL ) {
nkeynes@934
   330
            ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@736
   331
            return FALSE;
nkeynes@736
   332
        }
nkeynes@461
   333
nkeynes@934
   334
        fstat( fileno(f), &st );
nkeynes@934
   335
        if( st.st_size != size ) {
nkeynes@934
   336
            ERROR( "File '%s' is invalid, expected %d bytes but was %d bytes long.", file, size, st.st_size );
nkeynes@934
   337
            fclose(f);
nkeynes@934
   338
            return FALSE;
nkeynes@934
   339
        }
nkeynes@934
   340
        
nkeynes@934
   341
        if( fread( output, 1, size, f ) != size ) { 
nkeynes@934
   342
            ERROR( "Failed to load file '%s': %s", file, strerror(errno) );
nkeynes@934
   343
            fclose(f);
nkeynes@934
   344
            return FALSE;
nkeynes@934
   345
        }
nkeynes@10
   346
nkeynes@934
   347
        /* CRC check only if we loaded something */
nkeynes@934
   348
        calc_crc = crc32(0L, output, size);
nkeynes@934
   349
        if( calc_crc != crc ) {
nkeynes@934
   350
            WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
nkeynes@934
   351
                   file, calc_crc, crc);
nkeynes@736
   352
        }
nkeynes@934
   353
        /* Even if the CRC fails, continue normally */
nkeynes@934
   354
        return TRUE;
nkeynes@10
   355
    }
nkeynes@543
   356
    return FALSE;
nkeynes@10
   357
}
nkeynes@10
   358
nkeynes@502
   359
sh4ptr_t mem_get_region_by_name( const char *name )
nkeynes@10
   360
{
nkeynes@10
   361
    int i;
nkeynes@10
   362
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@10
   363
        if( strcmp( mem_rgn[i].name, name ) == 0 )
nkeynes@10
   364
            return mem_rgn[i].mem;
nkeynes@10
   365
    }
nkeynes@10
   366
    return NULL;
nkeynes@10
   367
}
nkeynes@10
   368
nkeynes@10
   369
void register_io_region( struct mmio_region *io )
nkeynes@10
   370
{
nkeynes@10
   371
    int i;
nkeynes@736
   372
nkeynes@10
   373
    assert(io);
nkeynes@10
   374
    io->mem = mem_alloc_pages(2);
nkeynes@796
   375
    io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
nkeynes@10
   376
    io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
nkeynes@41
   377
    io->trace_flag = 0;
nkeynes@10
   378
    memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
nkeynes@10
   379
    for( i=0; io->ports[i].id != NULL; i++ ) {
nkeynes@10
   380
        io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
nkeynes@10
   381
        *io->ports[i].val = io->ports[i].def_val;
nkeynes@10
   382
        io->index[io->ports[i].offset>>2] = &io->ports[i];
nkeynes@10
   383
    }
nkeynes@796
   384
    memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
nkeynes@10
   385
    if( (io->base & 0xFF000000) == 0xFF000000 ) {
nkeynes@10
   386
        /* P4 area (on-chip I/O channels */
nkeynes@10
   387
        P4_io[(io->base&0x1FFFFFFF)>>19] = io;
nkeynes@10
   388
    } else {
nkeynes@674
   389
        page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
nkeynes@930
   390
        ext_address_space[io->base>>12] = &io->fn;
nkeynes@931
   391
        mem_page_remapped( io->base, &io->fn );
nkeynes@10
   392
    }
nkeynes@10
   393
    io_rgn[num_io_rgns] = io;
nkeynes@10
   394
    num_io_rgns++;
nkeynes@10
   395
}
nkeynes@10
   396
nkeynes@10
   397
void register_io_regions( struct mmio_region **io )
nkeynes@10
   398
{
nkeynes@10
   399
    while( *io ) register_io_region( *io++ );
nkeynes@10
   400
}
nkeynes@10
   401
nkeynes@934
   402
gboolean mem_has_page( uint32_t addr )
nkeynes@10
   403
{
nkeynes@934
   404
    return ext_address_space[ (addr&0x1FFFFFFF)>>12 ] != &mem_region_unmapped;
nkeynes@10
   405
}
nkeynes@10
   406
nkeynes@502
   407
sh4ptr_t mem_get_region( uint32_t addr )
nkeynes@10
   408
{
nkeynes@502
   409
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@490
   410
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   411
        return NULL;
nkeynes@10
   412
    } else {
nkeynes@10
   413
        return page+(addr&0xFFF);
nkeynes@10
   414
    }
nkeynes@10
   415
}
nkeynes@11
   416
nkeynes@669
   417
void mem_write_long( sh4addr_t addr, uint32_t value )
nkeynes@669
   418
{
nkeynes@934
   419
    ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long(addr, value);
nkeynes@156
   420
}
nkeynes@156
   421
nkeynes@586
   422
struct mmio_region *mem_get_io_region_by_name( const gchar *name )
nkeynes@156
   423
{
nkeynes@586
   424
    int i;
nkeynes@586
   425
    for( i=0; i<num_io_rgns; i++ ) {
nkeynes@736
   426
        if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
nkeynes@736
   427
            return io_rgn[i];
nkeynes@736
   428
        }
nkeynes@586
   429
    }
nkeynes@586
   430
    return NULL;
nkeynes@156
   431
}
nkeynes@156
   432
nkeynes@586
   433
void mem_set_trace( const gchar *tracelist, gboolean flag )
nkeynes@586
   434
{
nkeynes@586
   435
    if( tracelist != NULL ) {
nkeynes@736
   436
        gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
nkeynes@736
   437
        int i;
nkeynes@736
   438
        for( i=0; tracev[i] != NULL; i++ ) {
nkeynes@736
   439
            // Special case "all" - trace everything
nkeynes@736
   440
            if( strcasecmp(tracev[i], "all") == 0 ) {
nkeynes@736
   441
                int j;
nkeynes@736
   442
                for( j=0; j<num_io_rgns; j++ ) {
nkeynes@736
   443
                    io_rgn[j]->trace_flag = flag ? 1 : 0;
nkeynes@736
   444
                }
nkeynes@736
   445
                break;
nkeynes@736
   446
            }
nkeynes@736
   447
            struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
nkeynes@736
   448
            if( region == NULL ) {
nkeynes@736
   449
                WARN( "Unknown IO region '%s'", tracev[i] );
nkeynes@736
   450
            } else {
nkeynes@736
   451
                region->trace_flag = flag ? 1 : 0;
nkeynes@736
   452
            }
nkeynes@736
   453
        }
nkeynes@736
   454
        g_strfreev( tracev );
nkeynes@586
   455
    }
nkeynes@586
   456
}
nkeynes@586
   457
.