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