Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 480:d28c2992f5ee
prev477:9a373f2ff009
next488:d19a63e47cd9
author nkeynes
date Wed Oct 31 11:53:35 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix miscellaneous warnings
view annotate diff log raw
     1 /**
     2  * $Id: mem.c,v 1.19 2007-10-31 11:53:35 nkeynes Exp $
     3  * mem.c is responsible for creating and maintaining the overall system memory
     4  * map, as visible from the SH4 processor. 
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #define MODULE mem_module
    20 #include <sys/mman.h>
    21 #include <sys/stat.h>
    22 #include <assert.h>
    23 #include <stdint.h>
    24 #include <stdlib.h>
    25 #include <stdio.h>
    26 #include <unistd.h>
    27 #include <fcntl.h>
    28 #include <errno.h>
    29 #include <string.h>
    30 #include <zlib.h>
    31 #include "dream.h"
    32 #include "mem.h"
    33 #include "mmio.h"
    34 #include "dreamcast.h"
    36 char **page_map = NULL;
    38 int mem_load(FILE *f);
    39 void mem_save(FILE *f);
    40 struct dreamcast_module mem_module =
    41     { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
    43 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    44 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    45 struct mmio_region *P4_io[4096];
    47 int num_io_rgns = 0, num_mem_rgns = 0;
    49 void *mem_alloc_pages( int n )
    50 {
    51     void *mem = mmap( NULL, n * 4096,
    52                       PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
    53     if( mem == MAP_FAILED ) {
    54         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    55         return NULL;
    56     }
    57     return mem;
    58 }
    61 void mem_init( void )
    62 {
    63     page_map = mmap( NULL, sizeof(char *) * PAGE_TABLE_ENTRIES,
    64                      PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
    65     if( page_map == MAP_FAILED ) {
    66         ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
    67         page_map = NULL;
    68         return;
    69     }
    71     memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
    72 }
    74 void mem_reset( void )
    75 {
    76     /* Restore all mmio registers to their initial settings */
    77     int i, j;
    78     for( i=1; i<num_io_rgns; i++ ) {
    79         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
    80             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
    81                 io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
    82                 io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
    83                                     io_rgn[i]->ports[j].def_val );
    84             }
    85         }
    86     }
    87 }
    89 void mem_save( FILE *f ) 
    90 {
    91     int i;
    92     uint32_t len;
    94     /* All memory regions */
    95     fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
    96     for( i=0; i<num_mem_rgns; i++ ) {
    97 	fwrite_string( mem_rgn[i].name, f );
    98 	fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
    99 	fwrite( &mem_rgn[i].flags, sizeof(int), 1, f );
   100 	fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   101 	if( mem_rgn[i].flags != MEM_FLAG_ROM )
   102 	    fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   103     }
   105     /* All MMIO regions */
   106     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   107     for( i=0; i<num_io_rgns; i++ ) {
   108 	fwrite_string( io_rgn[i]->id, f );
   109 	fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   110 	len = 4096;
   111 	fwrite( &len, sizeof(len), 1, f );
   112 	fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   113     }
   114 }
   116 int mem_load( FILE *f )
   117 {
   118     char tmp[64];
   119     uint32_t len;
   120     uint32_t base, size;
   121     int flags;
   122     int i;
   124     /* All memory regions */
   125     fread( &len, sizeof(len), 1, f );
   126     if( len != num_mem_rgns )
   127 	return -1;
   128     for( i=0; i<len; i++ ) {
   129 	fread_string( tmp, sizeof(tmp), f );
   130 	fread( &base, sizeof(base), 1, f );
   131 	fread( &flags, sizeof(flags), 1, f );
   132 	fread( &size, sizeof(size), 1, f );
   133 	if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
   134 	    base != mem_rgn[i].base ||
   135 	    flags != mem_rgn[i].flags ||
   136 	    size != mem_rgn[i].size ) {
   137 	    ERROR( "Bad memory region %d %s", i, tmp );
   138 	    return -1;
   139 	}
   140 	if( flags != MEM_FLAG_ROM )
   141 	    fread_gzip( mem_rgn[i].mem, size, 1, f );
   142     }
   144     /* All MMIO regions */
   145     fread( &len, sizeof(len), 1, f );
   146     if( len != num_io_rgns ) 
   147 	return -1;
   148     for( i=0; i<len; i++ ) {
   149 	fread_string( tmp, sizeof(tmp), f );
   150 	fread( &base, sizeof(base), 1, f );
   151 	fread( &size, sizeof(size), 1, f );
   152 	if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   153 	    base != io_rgn[i]->base ||
   154 	    size != 4096 ) {
   155 	    ERROR( "Bad MMIO region %d %s", i, tmp );
   156 	    return -1;
   157 	}
   158 	fread_gzip( io_rgn[i]->mem, size, 1, f );
   159     }
   160     return 0;
   161 }
   163 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   164 {
   165     char *region;
   166     int len = 4096, total = 0;
   167     uint32_t addr = start;
   168     FILE *f = fopen(file,"w");
   170     if( f == NULL )
   171 	return errno;
   173     while( total < length ) {
   174 	region = mem_get_region(addr);
   175 	len = 4096 - (addr & 0x0FFF);
   176 	if( len > (length-total) ) 
   177 	    len = (length-total);
   178 	if( fwrite( region, len, 1, f ) != 1 ) {
   179 	    ERROR( "Unexpected error: %d %d", len, errno );
   180 	    break;
   181 	}
   183 	addr += len;
   184 	total += len;
   185     }
   186     fclose( f );
   187     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   188     return 0;
   189 }
   191 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   192 {
   193     char *region;
   194     int len = 4096, total = 0;
   195     uint32_t addr = start;
   196     struct stat st;
   197     FILE *f = fopen(file,"r");
   199     if( f == NULL ) {
   200 	ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
   201 	return -1;
   202     }
   203     fstat( fileno(f), &st );
   204     if( length == 0 || length == -1 || length > st.st_size )
   205 	length = st.st_size;
   207     while( total < length ) {
   208 	region = mem_get_region(addr);
   209 	len = 4096 - (addr & 0x0FFF);
   210 	if( len > (length-total) ) 
   211 	    len = (length-total);
   212 	if( fread( region, len, 1, f ) != 1 ) {
   213 	    ERROR( "Unexpected error: %d %d", len, errno );
   214 	    break;
   215 	}
   217 	addr += len;
   218 	total += len;
   219     }
   220     fclose( f );
   221     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   222     return 0;
   223 }
   225 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   226                                    const char *name, int flags, uint32_t repeat_offset,
   227 				   uint32_t repeat_until )
   228 {
   229     int i;
   230     mem_rgn[num_mem_rgns].base = base;
   231     mem_rgn[num_mem_rgns].size = size;
   232     mem_rgn[num_mem_rgns].flags = flags;
   233     mem_rgn[num_mem_rgns].name = name;
   234     mem_rgn[num_mem_rgns].mem = mem;
   235     num_mem_rgns++;
   237     do {
   238 	for( i=0; i<size>>PAGE_BITS; i++ )
   239 	    page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
   240 	base += repeat_offset;	
   241     } while( base <= repeat_until );
   243     return &mem_rgn[num_mem_rgns-1];
   244 }
   246 void *mem_create_ram_region( uint32_t base, uint32_t size, const char *name )
   247 {
   248     return mem_create_repeating_ram_region( base, size, name, size, base );
   249 }
   251 void *mem_create_repeating_ram_region( uint32_t base, uint32_t size, const char *name,
   252 				       uint32_t repeat_offset, uint32_t repeat_until )
   253 {
   254     char *mem;
   256     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   257     assert( (size&0x00000FFF) == 0 );
   258     assert( num_mem_rgns < MAX_MEM_REGIONS );
   259     assert( page_map != NULL );
   261     mem = mem_alloc_pages( size>>PAGE_BITS );
   263     mem_map_region( mem, base, size, name, MEM_FLAG_RAM, repeat_offset, repeat_until );
   265     return mem;
   266 }
   268 void *mem_load_rom( const gchar *file, uint32_t base, uint32_t size, uint32_t crc,
   269 		    const gchar *region_name )
   270 {
   271     char *mem;
   272     uint32_t calc_crc;
   274     mem = mem_get_region(base);
   275     if( mem == NULL ) {
   276 	mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
   277 	if( mem == MAP_FAILED ) {
   278 	    ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
   279 	    return NULL;
   280 	}
   281 	mem_map_region( mem, base, size, file, MEM_FLAG_ROM, size, base );
   282     } else {
   283 	mprotect( mem, size, PROT_READ|PROT_WRITE );
   284     }
   286     mem_load_block( file, base, size );
   287     mprotect( mem, size, PROT_READ );
   289     /* CRC check */
   290     calc_crc = crc32(0L, (unsigned char *)mem, size);
   291     if( calc_crc != crc ) {
   292         WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   293               file, calc_crc, crc);
   294     }
   296     return mem;
   297 }
   299 char *mem_get_region_by_name( const char *name )
   300 {
   301     int i;
   302     for( i=0; i<num_mem_rgns; i++ ) {
   303         if( strcmp( mem_rgn[i].name, name ) == 0 )
   304             return mem_rgn[i].mem;
   305     }
   306     return NULL;
   307 }
   309 void register_io_region( struct mmio_region *io )
   310 {
   311     int i;
   313     assert(io);
   314     io->mem = mem_alloc_pages(2);
   315     io->save_mem = io->mem + PAGE_SIZE;
   316     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   317     io->trace_flag = 0;
   318     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   319     for( i=0; io->ports[i].id != NULL; i++ ) {
   320         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   321         *io->ports[i].val = io->ports[i].def_val;
   322         io->index[io->ports[i].offset>>2] = &io->ports[i];
   323     }
   324     memcpy( io->save_mem, io->mem, PAGE_SIZE );
   325     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   326         /* P4 area (on-chip I/O channels */
   327         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   328     } else {
   329         page_map[io->base>>12] = (char *)num_io_rgns;
   330     }
   331     io_rgn[num_io_rgns] = io;
   332     num_io_rgns++;
   333 }
   335 void register_io_regions( struct mmio_region **io )
   336 {
   337     while( *io ) register_io_region( *io++ );
   338 }
   340 int mem_has_page( uint32_t addr )
   341 {
   342     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   343     return page != NULL;
   344 }
   346 char *mem_get_page( uint32_t addr )
   347 {
   348     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   349     return page;
   350 }
   352 char *mem_get_region( uint32_t addr )
   353 {
   354     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   355     if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   356         return NULL;
   357     } else {
   358         return page+(addr&0xFFF);
   359     }
   360 }
   362 struct mmio_region *mem_get_io_region( uint32_t addr )
   363 {
   364     if( addr > 0xFF000000 ) {
   365 	return P4_io[(addr&0x00FFFFFF)>>12];
   366     }
   367     char *page = page_map[(addr&0x1FFFFFFF)>>12];
   368     if( ((uint32_t)page) < MAX_IO_REGIONS ) {
   369 	return io_rgn[(uint32_t)page];
   370     } else {
   371 	return NULL;
   372     }
   373 }
   375 void mem_set_trace( uint32_t addr, int flag )
   376 {
   377     struct mmio_region *region = mem_get_io_region(addr);
   378     if( region != NULL )
   379 	region->trace_flag = flag;
   380 }
.