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