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