Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 502:c4ecae2b1b5e
prev490:1e0f9940e064
next510:41ce7a074f4e
author nkeynes
date Thu Nov 08 11:54:16 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Add sh4ptr_t type, start converting bare pointer refs to it
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@502
     2
 * $Id: mem.c,v 1.22 2007-11-08 11:54:16 nkeynes Exp $
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@10
    23
#include <assert.h>
nkeynes@10
    24
#include <stdint.h>
nkeynes@10
    25
#include <stdlib.h>
nkeynes@10
    26
#include <stdio.h>
nkeynes@10
    27
#include <unistd.h>
nkeynes@10
    28
#include <fcntl.h>
nkeynes@10
    29
#include <errno.h>
nkeynes@10
    30
#include <string.h>
nkeynes@10
    31
#include <zlib.h>
nkeynes@10
    32
#include "dream.h"
nkeynes@10
    33
#include "mem.h"
nkeynes@10
    34
#include "mmio.h"
nkeynes@10
    35
#include "dreamcast.h"
nkeynes@10
    36
nkeynes@502
    37
sh4ptr_t *page_map = NULL;
nkeynes@10
    38
nkeynes@17
    39
int mem_load(FILE *f);
nkeynes@17
    40
void mem_save(FILE *f);
nkeynes@15
    41
struct dreamcast_module mem_module =
nkeynes@23
    42
    { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
nkeynes@15
    43
nkeynes@10
    44
struct mem_region mem_rgn[MAX_MEM_REGIONS];
nkeynes@10
    45
struct mmio_region *io_rgn[MAX_IO_REGIONS];
nkeynes@10
    46
struct mmio_region *P4_io[4096];
nkeynes@10
    47
nkeynes@490
    48
uintptr_t num_io_rgns = 0, num_mem_rgns = 0;
nkeynes@10
    49
nkeynes@10
    50
void *mem_alloc_pages( int n )
nkeynes@10
    51
{
nkeynes@10
    52
    void *mem = mmap( NULL, n * 4096,
nkeynes@488
    53
                      PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@10
    54
    if( mem == MAP_FAILED ) {
nkeynes@10
    55
        ERROR( "Memory allocation failure! (%s)", strerror(errno) );
nkeynes@10
    56
        return NULL;
nkeynes@10
    57
    }
nkeynes@10
    58
    return mem;
nkeynes@10
    59
}
nkeynes@10
    60
nkeynes@10
    61
nkeynes@10
    62
void mem_init( void )
nkeynes@10
    63
{
nkeynes@10
    64
    page_map = mmap( NULL, sizeof(char *) * PAGE_TABLE_ENTRIES,
nkeynes@488
    65
                     PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@10
    66
    if( page_map == MAP_FAILED ) {
nkeynes@10
    67
        ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
nkeynes@10
    68
        page_map = NULL;
nkeynes@10
    69
        return;
nkeynes@10
    70
    }
nkeynes@10
    71
nkeynes@10
    72
    memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
nkeynes@10
    73
}
nkeynes@10
    74
nkeynes@10
    75
void mem_reset( void )
nkeynes@10
    76
{
nkeynes@10
    77
    /* Restore all mmio registers to their initial settings */
nkeynes@10
    78
    int i, j;
nkeynes@10
    79
    for( i=1; i<num_io_rgns; i++ ) {
nkeynes@10
    80
        for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
nkeynes@10
    81
            if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
nkeynes@10
    82
                io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
nkeynes@10
    83
                io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
nkeynes@10
    84
                                    io_rgn[i]->ports[j].def_val );
nkeynes@10
    85
            }
nkeynes@10
    86
        }
nkeynes@10
    87
    }
nkeynes@10
    88
}
nkeynes@10
    89
nkeynes@17
    90
void mem_save( FILE *f ) 
nkeynes@17
    91
{
nkeynes@17
    92
    int i;
nkeynes@17
    93
    uint32_t len;
nkeynes@17
    94
    
nkeynes@17
    95
    /* All memory regions */
nkeynes@17
    96
    fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
nkeynes@17
    97
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@17
    98
	fwrite_string( mem_rgn[i].name, f );
nkeynes@17
    99
	fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
nkeynes@17
   100
	fwrite( &mem_rgn[i].flags, sizeof(int), 1, f );
nkeynes@17
   101
	fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
nkeynes@18
   102
	if( mem_rgn[i].flags != MEM_FLAG_ROM )
nkeynes@477
   103
	    fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
nkeynes@17
   104
    }
nkeynes@17
   105
nkeynes@17
   106
    /* All MMIO regions */
nkeynes@17
   107
    fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
nkeynes@17
   108
    for( i=0; i<num_io_rgns; i++ ) {
nkeynes@17
   109
	fwrite_string( io_rgn[i]->id, f );
nkeynes@17
   110
	fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
nkeynes@18
   111
	len = 4096;
nkeynes@18
   112
	fwrite( &len, sizeof(len), 1, f );
nkeynes@477
   113
	fwrite_gzip( io_rgn[i]->mem, len, 1, f );
nkeynes@17
   114
    }
nkeynes@17
   115
}
nkeynes@17
   116
nkeynes@17
   117
int mem_load( FILE *f )
nkeynes@17
   118
{
nkeynes@17
   119
    char tmp[64];
nkeynes@17
   120
    uint32_t len;
nkeynes@18
   121
    uint32_t base, size;
nkeynes@18
   122
    int flags;
nkeynes@17
   123
    int i;
nkeynes@17
   124
nkeynes@17
   125
    /* All memory regions */
nkeynes@18
   126
    fread( &len, sizeof(len), 1, f );
nkeynes@18
   127
    if( len != num_mem_rgns )
nkeynes@18
   128
	return -1;
nkeynes@18
   129
    for( i=0; i<len; i++ ) {
nkeynes@18
   130
	fread_string( tmp, sizeof(tmp), f );
nkeynes@18
   131
	fread( &base, sizeof(base), 1, f );
nkeynes@18
   132
	fread( &flags, sizeof(flags), 1, f );
nkeynes@18
   133
	fread( &size, sizeof(size), 1, f );
nkeynes@18
   134
	if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
nkeynes@18
   135
	    base != mem_rgn[i].base ||
nkeynes@18
   136
	    flags != mem_rgn[i].flags ||
nkeynes@18
   137
	    size != mem_rgn[i].size ) {
nkeynes@18
   138
	    ERROR( "Bad memory region %d %s", i, tmp );
nkeynes@18
   139
	    return -1;
nkeynes@18
   140
	}
nkeynes@18
   141
	if( flags != MEM_FLAG_ROM )
nkeynes@477
   142
	    fread_gzip( mem_rgn[i].mem, size, 1, f );
nkeynes@18
   143
    }
nkeynes@18
   144
nkeynes@18
   145
    /* All MMIO regions */
nkeynes@18
   146
    fread( &len, sizeof(len), 1, f );
nkeynes@18
   147
    if( len != num_io_rgns ) 
nkeynes@18
   148
	return -1;
nkeynes@18
   149
    for( i=0; i<len; i++ ) {
nkeynes@18
   150
	fread_string( tmp, sizeof(tmp), f );
nkeynes@18
   151
	fread( &base, sizeof(base), 1, f );
nkeynes@18
   152
	fread( &size, sizeof(size), 1, f );
nkeynes@18
   153
	if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
nkeynes@18
   154
	    base != io_rgn[i]->base ||
nkeynes@18
   155
	    size != 4096 ) {
nkeynes@18
   156
	    ERROR( "Bad MMIO region %d %s", i, tmp );
nkeynes@18
   157
	    return -1;
nkeynes@18
   158
	}
nkeynes@477
   159
	fread_gzip( io_rgn[i]->mem, size, 1, f );
nkeynes@18
   160
    }
nkeynes@18
   161
    return 0;
nkeynes@17
   162
}
nkeynes@17
   163
nkeynes@50
   164
int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   165
{
nkeynes@502
   166
    sh4ptr_t region;
nkeynes@50
   167
    int len = 4096, total = 0;
nkeynes@50
   168
    uint32_t addr = start;
nkeynes@50
   169
    FILE *f = fopen(file,"w");
nkeynes@19
   170
nkeynes@50
   171
    if( f == NULL )
nkeynes@50
   172
	return errno;
nkeynes@50
   173
    
nkeynes@50
   174
    while( total < length ) {
nkeynes@50
   175
	region = mem_get_region(addr);
nkeynes@50
   176
	len = 4096 - (addr & 0x0FFF);
nkeynes@50
   177
	if( len > (length-total) ) 
nkeynes@50
   178
	    len = (length-total);
nkeynes@50
   179
	if( fwrite( region, len, 1, f ) != 1 ) {
nkeynes@50
   180
	    ERROR( "Unexpected error: %d %d", len, errno );
nkeynes@50
   181
	    break;
nkeynes@50
   182
	}
nkeynes@50
   183
	    
nkeynes@50
   184
	addr += len;
nkeynes@50
   185
	total += len;
nkeynes@50
   186
    }
nkeynes@50
   187
    fclose( f );
nkeynes@50
   188
    INFO( "Loaded %d of %d bytes to %08X", total, length, start );
nkeynes@50
   189
    return 0;
nkeynes@19
   190
}
nkeynes@19
   191
nkeynes@19
   192
int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
nkeynes@19
   193
{
nkeynes@502
   194
    sh4ptr_t region;
nkeynes@19
   195
    int len = 4096, total = 0;
nkeynes@19
   196
    uint32_t addr = start;
nkeynes@19
   197
    struct stat st;
nkeynes@19
   198
    FILE *f = fopen(file,"r");
nkeynes@19
   199
nkeynes@180
   200
    if( f == NULL ) {
nkeynes@180
   201
	ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
nkeynes@180
   202
	return -1;
nkeynes@180
   203
    }
nkeynes@19
   204
    fstat( fileno(f), &st );
nkeynes@72
   205
    if( length == 0 || length == -1 || length > st.st_size )
nkeynes@19
   206
	length = st.st_size;
nkeynes@19
   207
    
nkeynes@19
   208
    while( total < length ) {
nkeynes@19
   209
	region = mem_get_region(addr);
nkeynes@19
   210
	len = 4096 - (addr & 0x0FFF);
nkeynes@19
   211
	if( len > (length-total) ) 
nkeynes@19
   212
	    len = (length-total);
nkeynes@19
   213
	if( fread( region, len, 1, f ) != 1 ) {
nkeynes@19
   214
	    ERROR( "Unexpected error: %d %d", len, errno );
nkeynes@19
   215
	    break;
nkeynes@19
   216
	}
nkeynes@19
   217
	    
nkeynes@19
   218
	addr += len;
nkeynes@19
   219
	total += len;
nkeynes@19
   220
    }
nkeynes@19
   221
    fclose( f );
nkeynes@19
   222
    INFO( "Loaded %d of %d bytes to %08X", total, length, start );
nkeynes@19
   223
    return 0;
nkeynes@19
   224
}
nkeynes@19
   225
nkeynes@10
   226
struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
nkeynes@422
   227
                                   const char *name, int flags, uint32_t repeat_offset,
nkeynes@146
   228
				   uint32_t repeat_until )
nkeynes@10
   229
{
nkeynes@10
   230
    int i;
nkeynes@10
   231
    mem_rgn[num_mem_rgns].base = base;
nkeynes@10
   232
    mem_rgn[num_mem_rgns].size = size;
nkeynes@10
   233
    mem_rgn[num_mem_rgns].flags = flags;
nkeynes@10
   234
    mem_rgn[num_mem_rgns].name = name;
nkeynes@10
   235
    mem_rgn[num_mem_rgns].mem = mem;
nkeynes@10
   236
    num_mem_rgns++;
nkeynes@10
   237
nkeynes@146
   238
    do {
nkeynes@146
   239
	for( i=0; i<size>>PAGE_BITS; i++ )
nkeynes@146
   240
	    page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
nkeynes@146
   241
	base += repeat_offset;	
nkeynes@146
   242
    } while( base <= repeat_until );
nkeynes@10
   243
nkeynes@10
   244
    return &mem_rgn[num_mem_rgns-1];
nkeynes@10
   245
}
nkeynes@10
   246
nkeynes@422
   247
void *mem_create_ram_region( uint32_t base, uint32_t size, const char *name )
nkeynes@10
   248
{
nkeynes@146
   249
    return mem_create_repeating_ram_region( base, size, name, size, base );
nkeynes@146
   250
}
nkeynes@146
   251
nkeynes@422
   252
void *mem_create_repeating_ram_region( uint32_t base, uint32_t size, const char *name,
nkeynes@146
   253
				       uint32_t repeat_offset, uint32_t repeat_until )
nkeynes@146
   254
{
nkeynes@10
   255
    char *mem;
nkeynes@10
   256
    
nkeynes@10
   257
    assert( (base&0xFFFFF000) == base ); /* must be page aligned */
nkeynes@10
   258
    assert( (size&0x00000FFF) == 0 );
nkeynes@10
   259
    assert( num_mem_rgns < MAX_MEM_REGIONS );
nkeynes@10
   260
    assert( page_map != NULL );
nkeynes@10
   261
nkeynes@10
   262
    mem = mem_alloc_pages( size>>PAGE_BITS );
nkeynes@10
   263
nkeynes@146
   264
    mem_map_region( mem, base, size, name, MEM_FLAG_RAM, repeat_offset, repeat_until );
nkeynes@146
   265
    
nkeynes@10
   266
    return mem;
nkeynes@10
   267
}
nkeynes@10
   268
nkeynes@466
   269
void *mem_load_rom( const gchar *file, uint32_t base, uint32_t size, uint32_t crc,
nkeynes@466
   270
		    const gchar *region_name )
nkeynes@10
   271
{
nkeynes@502
   272
    sh4ptr_t mem;
nkeynes@10
   273
    uint32_t calc_crc;
nkeynes@488
   274
    int status;
nkeynes@461
   275
nkeynes@461
   276
    mem = mem_get_region(base);
nkeynes@461
   277
    if( mem == NULL ) {
nkeynes@488
   278
	mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0 );
nkeynes@461
   279
	if( mem == MAP_FAILED ) {
nkeynes@461
   280
	    ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
nkeynes@461
   281
	    return NULL;
nkeynes@461
   282
	}
nkeynes@461
   283
	mem_map_region( mem, base, size, file, MEM_FLAG_ROM, size, base );
nkeynes@461
   284
    } else {
nkeynes@461
   285
	mprotect( mem, size, PROT_READ|PROT_WRITE );
nkeynes@10
   286
    }
nkeynes@461
   287
nkeynes@488
   288
    status = mem_load_block( file, base, size );
nkeynes@461
   289
    mprotect( mem, size, PROT_READ );
nkeynes@10
   290
nkeynes@488
   291
    if( status == 0 ) {
nkeynes@488
   292
	/* CRC check only if we loaded something */
nkeynes@502
   293
	calc_crc = crc32(0L, (sh4ptr_t)mem, size);
nkeynes@488
   294
	if( calc_crc != crc ) {
nkeynes@488
   295
	    WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
nkeynes@488
   296
		  file, calc_crc, crc);
nkeynes@488
   297
	}
nkeynes@10
   298
    }
nkeynes@18
   299
    
nkeynes@10
   300
    return mem;
nkeynes@10
   301
}
nkeynes@10
   302
nkeynes@502
   303
sh4ptr_t mem_get_region_by_name( const char *name )
nkeynes@10
   304
{
nkeynes@10
   305
    int i;
nkeynes@10
   306
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@10
   307
        if( strcmp( mem_rgn[i].name, name ) == 0 )
nkeynes@10
   308
            return mem_rgn[i].mem;
nkeynes@10
   309
    }
nkeynes@10
   310
    return NULL;
nkeynes@10
   311
}
nkeynes@10
   312
nkeynes@10
   313
void register_io_region( struct mmio_region *io )
nkeynes@10
   314
{
nkeynes@10
   315
    int i;
nkeynes@10
   316
    
nkeynes@10
   317
    assert(io);
nkeynes@10
   318
    io->mem = mem_alloc_pages(2);
nkeynes@10
   319
    io->save_mem = io->mem + PAGE_SIZE;
nkeynes@10
   320
    io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
nkeynes@41
   321
    io->trace_flag = 0;
nkeynes@10
   322
    memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
nkeynes@10
   323
    for( i=0; io->ports[i].id != NULL; i++ ) {
nkeynes@10
   324
        io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
nkeynes@10
   325
        *io->ports[i].val = io->ports[i].def_val;
nkeynes@10
   326
        io->index[io->ports[i].offset>>2] = &io->ports[i];
nkeynes@10
   327
    }
nkeynes@10
   328
    memcpy( io->save_mem, io->mem, PAGE_SIZE );
nkeynes@10
   329
    if( (io->base & 0xFF000000) == 0xFF000000 ) {
nkeynes@10
   330
        /* P4 area (on-chip I/O channels */
nkeynes@10
   331
        P4_io[(io->base&0x1FFFFFFF)>>19] = io;
nkeynes@10
   332
    } else {
nkeynes@502
   333
        page_map[io->base>>12] = (sh4ptr_t )num_io_rgns;
nkeynes@10
   334
    }
nkeynes@10
   335
    io_rgn[num_io_rgns] = io;
nkeynes@10
   336
    num_io_rgns++;
nkeynes@10
   337
}
nkeynes@10
   338
nkeynes@10
   339
void register_io_regions( struct mmio_region **io )
nkeynes@10
   340
{
nkeynes@10
   341
    while( *io ) register_io_region( *io++ );
nkeynes@10
   342
}
nkeynes@10
   343
nkeynes@10
   344
int mem_has_page( uint32_t addr )
nkeynes@10
   345
{
nkeynes@502
   346
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   347
    return page != NULL;
nkeynes@10
   348
}
nkeynes@10
   349
nkeynes@502
   350
sh4ptr_t mem_get_page( uint32_t addr )
nkeynes@10
   351
{
nkeynes@502
   352
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   353
    return page;
nkeynes@10
   354
}
nkeynes@10
   355
nkeynes@502
   356
sh4ptr_t mem_get_region( uint32_t addr )
nkeynes@10
   357
{
nkeynes@502
   358
    sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@490
   359
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   360
        return NULL;
nkeynes@10
   361
    } else {
nkeynes@10
   362
        return page+(addr&0xFFF);
nkeynes@10
   363
    }
nkeynes@10
   364
}
nkeynes@11
   365
nkeynes@156
   366
struct mmio_region *mem_get_io_region( uint32_t addr )
nkeynes@156
   367
{
nkeynes@156
   368
    if( addr > 0xFF000000 ) {
nkeynes@156
   369
	return P4_io[(addr&0x00FFFFFF)>>12];
nkeynes@156
   370
    }
nkeynes@502
   371
    sh4ptr_t page = page_map[(addr&0x1FFFFFFF)>>12];
nkeynes@490
   372
    if( ((uintptr_t)page) < MAX_IO_REGIONS ) {
nkeynes@490
   373
	return io_rgn[(uintptr_t)page];
nkeynes@156
   374
    } else {
nkeynes@156
   375
	return NULL;
nkeynes@156
   376
    }
nkeynes@156
   377
}
nkeynes@156
   378
nkeynes@156
   379
void mem_set_trace( uint32_t addr, int flag )
nkeynes@156
   380
{
nkeynes@156
   381
    struct mmio_region *region = mem_get_io_region(addr);
nkeynes@156
   382
    if( region != NULL )
nkeynes@156
   383
	region->trace_flag = flag;
nkeynes@156
   384
}
nkeynes@156
   385
.