Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 1075:1a21750d300c
prev1067:d3c00ffccfcd
next1167:2cc6c50d0dd2
author nkeynes
date Fri Jul 31 13:45:32 2009 +1000 (11 years ago)
permissions -rw-r--r--
last change Remove or change the level of a bunch of INFO messages that shouldn't really
be INFO level
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@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@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@975
    91
        unmapped_prefetch, unmapped_read_long }; 
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@968
   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@986
   121
    page_map = (sh4ptr_t *)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@986
   128
    ext_address_space = (mem_region_fn_t *) 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@968
   154
static 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@968
   189
static 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@959
   221
                break;
nkeynes@932
   222
            }
nkeynes@934
   223
        }
nkeynes@934
   224
    }
nkeynes@934
   225
    /* Make sure we got all the memory regions we expected */
nkeynes@934
   226
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@934
   227
        if( mem_rgn[i].flags == MEM_FLAG_RAM &&
nkeynes@934
   228
            mem_region_loaded[i] == 0 ) {
nkeynes@934
   229
            ERROR( "Missing memory block %s (not found in save state)", mem_rgn[i].name );
nkeynes@934
   230
            return -1;
nkeynes@736
   231
        }
nkeynes@18
   232
    }
nkeynes@18
   233
nkeynes@18
   234
    /* All MMIO regions */
nkeynes@18
   235
    fread( &len, sizeof(len), 1, f );
nkeynes@934
   236
    if( len != num_io_rgns ) {
nkeynes@934
   237
        ERROR( "Unexpected IO region count %d (expected %d)", len, num_io_rgns );
nkeynes@736
   238
        return -1;
nkeynes@934
   239
    }
nkeynes@934
   240
    
nkeynes@18
   241
    for( i=0; i<len; i++ ) {
nkeynes@736
   242
        fread_string( tmp, sizeof(tmp), f );
nkeynes@736
   243
        fread( &base, sizeof(base), 1, f );
nkeynes@736
   244
        fread( &size, sizeof(size), 1, f );
nkeynes@736
   245
        if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
nkeynes@736
   246
                base != io_rgn[i]->base ||
nkeynes@736
   247
                size != 4096 ) {
nkeynes@736
   248
            ERROR( "Bad MMIO region %d %s", i, tmp );
nkeynes@736
   249
            return -1;
nkeynes@736
   250
        }
nkeynes@736
   251
        fread_gzip( io_rgn[i]->mem, size, 1, f );
nkeynes@18
   252
    }
nkeynes@18
   253
    return 0;
nkeynes@17
   254
}
nkeynes@17
   255
nkeynes@50
   256
int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   257
{
nkeynes@502
   258
    sh4ptr_t region;
nkeynes@50
   259
    int len = 4096, total = 0;
nkeynes@50
   260
    uint32_t addr = start;
nkeynes@50
   261
    FILE *f = fopen(file,"w");
nkeynes@19
   262
nkeynes@50
   263
    if( f == NULL )
nkeynes@736
   264
        return errno;
nkeynes@736
   265
nkeynes@50
   266
    while( total < length ) {
nkeynes@736
   267
        region = mem_get_region(addr);
nkeynes@736
   268
        len = 4096 - (addr & 0x0FFF);
nkeynes@736
   269
        if( len > (length-total) ) 
nkeynes@736
   270
            len = (length-total);
nkeynes@736
   271
        if( fwrite( region, len, 1, f ) != 1 ) {
nkeynes@736
   272
            ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
nkeynes@736
   273
            break;
nkeynes@736
   274
        }
nkeynes@736
   275
nkeynes@736
   276
        addr += len;
nkeynes@736
   277
        total += len;
nkeynes@50
   278
    }
nkeynes@50
   279
    fclose( f );
nkeynes@1075
   280
    DEBUG( "Saved %d of %d bytes to %08X", total, length, start );
nkeynes@50
   281
    return 0;
nkeynes@19
   282
}
nkeynes@19
   283
nkeynes@19
   284
int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   285
{
nkeynes@502
   286
    sh4ptr_t region;
nkeynes@19
   287
    int len = 4096, total = 0;
nkeynes@19
   288
    uint32_t addr = start;
nkeynes@19
   289
    struct stat st;
nkeynes@19
   290
    FILE *f = fopen(file,"r");
nkeynes@19
   291
nkeynes@180
   292
    if( f == NULL ) {
nkeynes@736
   293
        WARN( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@736
   294
        return -1;
nkeynes@180
   295
    }
nkeynes@19
   296
    fstat( fileno(f), &st );
nkeynes@72
   297
    if( length == 0 || length == -1 || length > st.st_size )
nkeynes@736
   298
        length = st.st_size;
nkeynes@736
   299
nkeynes@19
   300
    while( total < length ) {
nkeynes@736
   301
        region = mem_get_region(addr);
nkeynes@736
   302
        len = 4096 - (addr & 0x0FFF);
nkeynes@736
   303
        if( len > (length-total) ) 
nkeynes@736
   304
            len = (length-total);
nkeynes@736
   305
        if( fread( region, len, 1, f ) != 1 ) {
nkeynes@736
   306
            ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
nkeynes@736
   307
            break;
nkeynes@736
   308
        }
nkeynes@736
   309
nkeynes@736
   310
        addr += len;
nkeynes@736
   311
        total += len;
nkeynes@19
   312
    }
nkeynes@19
   313
    fclose( f );
nkeynes@1075
   314
    DEBUG( "Loaded %d of %d bytes to %08X", total, length, start );
nkeynes@19
   315
    return 0;
nkeynes@19
   316
}
nkeynes@19
   317
nkeynes@10
   318
struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
nkeynes@934
   319
                                   const char *name, mem_region_fn_t fn, int flags, 
nkeynes@934
   320
                                   uint32_t repeat_offset, uint32_t repeat_until )
nkeynes@10
   321
{
nkeynes@10
   322
    int i;
nkeynes@10
   323
    mem_rgn[num_mem_rgns].base = base;
nkeynes@10
   324
    mem_rgn[num_mem_rgns].size = size;
nkeynes@10
   325
    mem_rgn[num_mem_rgns].flags = flags;
nkeynes@10
   326
    mem_rgn[num_mem_rgns].name = name;
nkeynes@10
   327
    mem_rgn[num_mem_rgns].mem = mem;
nkeynes@929
   328
    mem_rgn[num_mem_rgns].fn = fn;
nkeynes@946
   329
    fn->prefetch = unmapped_prefetch;
nkeynes@975
   330
    fn->read_byte_for_write = fn->read_byte;
nkeynes@10
   331
    num_mem_rgns++;
nkeynes@10
   332
nkeynes@146
   333
    do {
nkeynes@930
   334
        for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
nkeynes@934
   335
            if( mem != NULL ) {
nkeynes@968
   336
                page_map[(base>>LXDREAM_PAGE_BITS)+i] = ((unsigned char *)mem) + (i<<LXDREAM_PAGE_BITS);
nkeynes@934
   337
            }
nkeynes@930
   338
            ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
nkeynes@931
   339
            mem_page_remapped( base + (i<<LXDREAM_PAGE_BITS), fn );
nkeynes@930
   340
        }
nkeynes@736
   341
        base += repeat_offset;	
nkeynes@146
   342
    } while( base <= repeat_until );
nkeynes@10
   343
nkeynes@10
   344
    return &mem_rgn[num_mem_rgns-1];
nkeynes@10
   345
}
nkeynes@10
   346
nkeynes@934
   347
gboolean mem_load_rom( void *output, const gchar *file, uint32_t size, uint32_t crc )
nkeynes@931
   348
{
nkeynes@934
   349
    if( file != NULL && file[0] != '\0' ) {
nkeynes@934
   350
        FILE *f = fopen(file,"r");
nkeynes@934
   351
        struct stat st;
nkeynes@934
   352
        uint32_t calc_crc;
nkeynes@931
   353
nkeynes@934
   354
        if( f == NULL ) {
nkeynes@934
   355
            ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@736
   356
            return FALSE;
nkeynes@736
   357
        }
nkeynes@461
   358
nkeynes@934
   359
        fstat( fileno(f), &st );
nkeynes@934
   360
        if( st.st_size != size ) {
nkeynes@934
   361
            ERROR( "File '%s' is invalid, expected %d bytes but was %d bytes long.", file, size, st.st_size );
nkeynes@934
   362
            fclose(f);
nkeynes@934
   363
            return FALSE;
nkeynes@934
   364
        }
nkeynes@934
   365
        
nkeynes@934
   366
        if( fread( output, 1, size, f ) != size ) { 
nkeynes@934
   367
            ERROR( "Failed to load file '%s': %s", file, strerror(errno) );
nkeynes@934
   368
            fclose(f);
nkeynes@934
   369
            return FALSE;
nkeynes@934
   370
        }
nkeynes@10
   371
nkeynes@934
   372
        /* CRC check only if we loaded something */
nkeynes@934
   373
        calc_crc = crc32(0L, output, size);
nkeynes@934
   374
        if( calc_crc != crc ) {
nkeynes@934
   375
            WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
nkeynes@934
   376
                   file, calc_crc, crc);
nkeynes@736
   377
        }
nkeynes@934
   378
        /* Even if the CRC fails, continue normally */
nkeynes@934
   379
        return TRUE;
nkeynes@10
   380
    }
nkeynes@543
   381
    return FALSE;
nkeynes@10
   382
}
nkeynes@10
   383
nkeynes@502
   384
sh4ptr_t mem_get_region_by_name( const char *name )
nkeynes@10
   385
{
nkeynes@10
   386
    int i;
nkeynes@10
   387
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@10
   388
        if( strcmp( mem_rgn[i].name, name ) == 0 )
nkeynes@10
   389
            return mem_rgn[i].mem;
nkeynes@10
   390
    }
nkeynes@10
   391
    return NULL;
nkeynes@10
   392
}
nkeynes@10
   393
nkeynes@10
   394
void register_io_region( struct mmio_region *io )
nkeynes@10
   395
{
nkeynes@10
   396
    int i;
nkeynes@736
   397
nkeynes@10
   398
    assert(io);
nkeynes@10
   399
    io->mem = mem_alloc_pages(2);
nkeynes@796
   400
    io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
nkeynes@10
   401
    io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
nkeynes@41
   402
    io->trace_flag = 0;
nkeynes@10
   403
    memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
nkeynes@10
   404
    for( i=0; io->ports[i].id != NULL; i++ ) {
nkeynes@10
   405
        io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
nkeynes@10
   406
        *io->ports[i].val = io->ports[i].def_val;
nkeynes@10
   407
        io->index[io->ports[i].offset>>2] = &io->ports[i];
nkeynes@10
   408
    }
nkeynes@796
   409
    memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
nkeynes@10
   410
    if( (io->base & 0xFF000000) == 0xFF000000 ) {
nkeynes@10
   411
        /* P4 area (on-chip I/O channels */
nkeynes@10
   412
        P4_io[(io->base&0x1FFFFFFF)>>19] = io;
nkeynes@10
   413
    } else {
nkeynes@674
   414
        page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
nkeynes@930
   415
        ext_address_space[io->base>>12] = &io->fn;
nkeynes@931
   416
        mem_page_remapped( io->base, &io->fn );
nkeynes@10
   417
    }
nkeynes@10
   418
    io_rgn[num_io_rgns] = io;
nkeynes@10
   419
    num_io_rgns++;
nkeynes@10
   420
}
nkeynes@10
   421
nkeynes@10
   422
void register_io_regions( struct mmio_region **io )
nkeynes@10
   423
{
nkeynes@10
   424
    while( *io ) register_io_region( *io++ );
nkeynes@10
   425
}
nkeynes@10
   426
nkeynes@934
   427
gboolean mem_has_page( uint32_t addr )
nkeynes@10
   428
{
nkeynes@934
   429
    return ext_address_space[ (addr&0x1FFFFFFF)>>12 ] != &mem_region_unmapped;
nkeynes@10
   430
}
nkeynes@10
   431
nkeynes@502
   432
sh4ptr_t mem_get_region( uint32_t addr )
nkeynes@10
   433
{
nkeynes@502
   434
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@490
   435
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   436
        return NULL;
nkeynes@10
   437
    } else {
nkeynes@10
   438
        return page+(addr&0xFFF);
nkeynes@10
   439
    }
nkeynes@10
   440
}
nkeynes@11
   441
nkeynes@669
   442
void mem_write_long( sh4addr_t addr, uint32_t value )
nkeynes@669
   443
{
nkeynes@934
   444
    ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long(addr, value);
nkeynes@156
   445
}
nkeynes@156
   446
nkeynes@968
   447
static struct mmio_region *mem_get_io_region_by_name( const gchar *name )
nkeynes@156
   448
{
nkeynes@586
   449
    int i;
nkeynes@586
   450
    for( i=0; i<num_io_rgns; i++ ) {
nkeynes@736
   451
        if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
nkeynes@736
   452
            return io_rgn[i];
nkeynes@736
   453
        }
nkeynes@586
   454
    }
nkeynes@586
   455
    return NULL;
nkeynes@156
   456
}
nkeynes@156
   457
nkeynes@586
   458
void mem_set_trace( const gchar *tracelist, gboolean flag )
nkeynes@586
   459
{
nkeynes@586
   460
    if( tracelist != NULL ) {
nkeynes@736
   461
        gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
nkeynes@736
   462
        int i;
nkeynes@736
   463
        for( i=0; tracev[i] != NULL; i++ ) {
nkeynes@736
   464
            // Special case "all" - trace everything
nkeynes@736
   465
            if( strcasecmp(tracev[i], "all") == 0 ) {
nkeynes@736
   466
                int j;
nkeynes@736
   467
                for( j=0; j<num_io_rgns; j++ ) {
nkeynes@736
   468
                    io_rgn[j]->trace_flag = flag ? 1 : 0;
nkeynes@736
   469
                }
nkeynes@736
   470
                break;
nkeynes@736
   471
            }
nkeynes@736
   472
            struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
nkeynes@736
   473
            if( region == NULL ) {
nkeynes@736
   474
                WARN( "Unknown IO region '%s'", tracev[i] );
nkeynes@736
   475
            } else {
nkeynes@736
   476
                region->trace_flag = flag ? 1 : 0;
nkeynes@736
   477
            }
nkeynes@736
   478
        }
nkeynes@736
   479
        g_strfreev( tracev );
nkeynes@586
   480
    }
nkeynes@586
   481
}
nkeynes@586
   482
.