Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 1067:d3c00ffccfcd
prev1065:bc1cc0c54917
prev946:d41ee7994db7
next1075:1a21750d300c
author nkeynes
date Sun Jul 05 13:54:48 2009 +1000 (10 years ago)
permissions -rw-r--r--
last change No-op merge lxdream-mem to tip to remove head (Long since merged in
actuality)
view annotate diff log raw
     1 /**
     2  * $Id$
     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/types.h>
    21 #include <sys/mman.h>
    22 #include <sys/stat.h>
    23 #include <glib/gstrfuncs.h>
    24 #include <assert.h>
    25 #include <stdint.h>
    26 #include <stdlib.h>
    27 #include <stdio.h>
    28 #include <unistd.h>
    29 #include <fcntl.h>
    30 #include <errno.h>
    31 #include <string.h>
    32 #include <zlib.h>
    33 #include "dream.h"
    34 #include "mem.h"
    35 #include "mmio.h"
    36 #include "dreamcast.h"
    38 #ifndef PAGE_SIZE
    39 #define PAGE_SIZE 4096
    40 #endif
    42 sh4ptr_t *page_map = NULL;
    43 mem_region_fn_t *ext_address_space = NULL;
    45 extern struct mem_region_fn mem_region_unmapped; 
    47 static int mem_load(FILE *f);
    48 static void mem_save(FILE *f);
    49 struct dreamcast_module mem_module =
    50 { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
    52 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    53 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    54 struct mmio_region *P4_io[4096];
    56 uint32_t num_io_rgns = 0, num_mem_rgns = 0;
    58 DEFINE_HOOK( mem_page_remapped_hook, mem_page_remapped_hook_t );
    59 static void mem_page_remapped( sh4addr_t addr, mem_region_fn_t fn )
    60 {
    61     CALL_HOOKS( mem_page_remapped_hook, addr, fn );
    62 }
    64 /********************* The "unmapped" address space ********************/
    65 /* Always reads as 0, writes have no effect */
    66 int32_t FASTCALL unmapped_read_long( sh4addr_t addr )
    67 {
    68     return 0;
    69 }
    70 void FASTCALL unmapped_write_long( sh4addr_t addr, uint32_t val )
    71 {
    72 }
    73 void FASTCALL unmapped_read_burst( unsigned char *dest, sh4addr_t addr )
    74 {
    75     memset( dest, 0, 32 );
    76 }
    77 void FASTCALL unmapped_write_burst( sh4addr_t addr, unsigned char *src )
    78 {
    79 }
    81 void FASTCALL unmapped_prefetch( sh4addr_t addr )
    82 {
    83     /* No effect */
    84 }
    86 struct mem_region_fn mem_region_unmapped = { 
    87         unmapped_read_long, unmapped_write_long, 
    88         unmapped_read_long, unmapped_write_long, 
    89         unmapped_read_long, unmapped_write_long, 
    90         unmapped_read_burst, unmapped_write_burst,
    91         unmapped_prefetch, unmapped_read_long }; 
    93 void *mem_alloc_pages( int n )
    94 {
    95     void *mem = mmap( NULL, n * 4096,
    96             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    97     if( mem == MAP_FAILED ) {
    98         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    99         return NULL;
   100     }
   101     return mem;
   102 }
   104 void mem_unprotect( void *region, uint32_t size )
   105 {
   106     /* Force page alignment */
   107     uintptr_t i = (uintptr_t)region;
   108     uintptr_t mask = ~(PAGE_SIZE-1);
   109     void *ptr = (void *)(i & mask);
   110     size_t len = (i & (PAGE_SIZE-1)) + size;
   111     len = (len + (PAGE_SIZE-1)) & mask;
   113     int status = mprotect( ptr, len, PROT_READ|PROT_WRITE|PROT_EXEC );
   114     assert( status == 0 );
   115 }
   117 void mem_init( void )
   118 {
   119     int i;
   120     mem_region_fn_t *ptr;
   121     page_map = (sh4ptr_t *)mmap( NULL, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES,
   122             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   123     if( page_map == MAP_FAILED ) {
   124         FATAL( "Unable to allocate page map! (%s)", strerror(errno) );
   125     }
   126     memset( page_map, 0, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES );
   128     ext_address_space = (mem_region_fn_t *) mmap( NULL, sizeof(mem_region_fn_t) * LXDREAM_PAGE_TABLE_ENTRIES,
   129             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   130     if( ext_address_space == MAP_FAILED ) {
   131         FATAL( "Unable to allocate external memory map (%s)", strerror(errno) );
   132     }
   134     for( ptr = ext_address_space, i = LXDREAM_PAGE_TABLE_ENTRIES; i > 0; ptr++, i-- ) {
   135         *ptr = &mem_region_unmapped;
   136     }
   137 }
   139 void mem_reset( void )
   140 {
   141     /* Restore all mmio registers to their initial settings */
   142     int i, j;
   143     for( i=1; i<num_io_rgns; i++ ) {
   144         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
   145             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
   146                     io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
   147                 io_rgn[i]->fn.write_long( io_rgn[i]->ports[j].offset,
   148                         io_rgn[i]->ports[j].def_val );
   149             }
   150         }
   151     }
   152 }
   154 static void mem_save( FILE *f ) 
   155 {
   156     int i, num_ram_regions = 0;
   157     uint32_t len;
   159     /* All RAM regions (ROM and non-memory regions don't need to be saved)
   160      * Flash is questionable - for now we save it too */
   161     for( i=0; i<num_mem_rgns; i++ ) {
   162         if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
   163             num_ram_regions++;
   164         }
   165     }
   166     fwrite( &num_ram_regions, sizeof(num_ram_regions), 1, f );
   168     for( i=0; i<num_mem_rgns; i++ ) {
   169         if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
   170             fwrite_string( mem_rgn[i].name, f );
   171             fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
   172             fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
   173             fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   174             fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   175         }
   176     }
   178     /* All MMIO regions */
   179     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   180     for( i=0; i<num_io_rgns; i++ ) {
   181         fwrite_string( io_rgn[i]->id, f );
   182         fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   183         len = 4096;
   184         fwrite( &len, sizeof(len), 1, f );
   185         fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   186     }
   187 }
   189 static int mem_load( FILE *f )
   190 {
   191     char tmp[64];
   192     uint32_t len;
   193     uint32_t base, size;
   194     uint32_t flags;
   195     int i, j;
   196     int mem_region_loaded[MAX_MEM_REGIONS];
   198     /* All RAM regions */
   199     memset( mem_region_loaded, 0, sizeof(mem_region_loaded) );
   200     fread( &len, sizeof(len), 1, f );
   201     for( i=0; i<len; i++ ) {
   202         fread_string( tmp, sizeof(tmp), f );
   204         for( j=0; j<num_mem_rgns; j++ ) {
   205             if( strcasecmp( mem_rgn[j].name, tmp ) == 0 ) {
   206                 fread( &base, sizeof(base), 1, f );
   207                 fread( &flags, sizeof(flags), 1, f );
   208                 fread( &size, sizeof(size), 1, f );
   209                 if( base != mem_rgn[j].base ||
   210                     flags != mem_rgn[j].flags ||
   211                     size != mem_rgn[j].size ) {
   212                     ERROR( "Bad memory block %d %s (not mapped to expected region)", i, tmp );
   213                     return -1;
   214                 }
   215                 if( flags != MEM_FLAG_RAM ) {
   216                     ERROR( "Unexpected memory block %d %s (Not a RAM region)", i, tmp );
   217                     return -1;
   218                 }
   219                 fread_gzip( mem_rgn[j].mem, size, 1, f );
   220                 mem_region_loaded[j] = 1;
   221                 break;
   222             }
   223         }
   224     }
   225     /* Make sure we got all the memory regions we expected */
   226     for( i=0; i<num_mem_rgns; i++ ) {
   227         if( mem_rgn[i].flags == MEM_FLAG_RAM &&
   228             mem_region_loaded[i] == 0 ) {
   229             ERROR( "Missing memory block %s (not found in save state)", mem_rgn[i].name );
   230             return -1;
   231         }
   232     }
   234     /* All MMIO regions */
   235     fread( &len, sizeof(len), 1, f );
   236     if( len != num_io_rgns ) {
   237         ERROR( "Unexpected IO region count %d (expected %d)", len, num_io_rgns );
   238         return -1;
   239     }
   241     for( i=0; i<len; i++ ) {
   242         fread_string( tmp, sizeof(tmp), f );
   243         fread( &base, sizeof(base), 1, f );
   244         fread( &size, sizeof(size), 1, f );
   245         if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   246                 base != io_rgn[i]->base ||
   247                 size != 4096 ) {
   248             ERROR( "Bad MMIO region %d %s", i, tmp );
   249             return -1;
   250         }
   251         fread_gzip( io_rgn[i]->mem, size, 1, f );
   252     }
   253     return 0;
   254 }
   256 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   257 {
   258     sh4ptr_t region;
   259     int len = 4096, total = 0;
   260     uint32_t addr = start;
   261     FILE *f = fopen(file,"w");
   263     if( f == NULL )
   264         return errno;
   266     while( total < length ) {
   267         region = mem_get_region(addr);
   268         len = 4096 - (addr & 0x0FFF);
   269         if( len > (length-total) ) 
   270             len = (length-total);
   271         if( fwrite( region, len, 1, f ) != 1 ) {
   272             ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
   273             break;
   274         }
   276         addr += len;
   277         total += len;
   278     }
   279     fclose( f );
   280     INFO( "Saved %d of %d bytes to %08X", total, length, start );
   281     return 0;
   282 }
   284 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   285 {
   286     sh4ptr_t region;
   287     int len = 4096, total = 0;
   288     uint32_t addr = start;
   289     struct stat st;
   290     FILE *f = fopen(file,"r");
   292     if( f == NULL ) {
   293         WARN( "Unable to load file '%s': %s", file, strerror(errno) );
   294         return -1;
   295     }
   296     fstat( fileno(f), &st );
   297     if( length == 0 || length == -1 || length > st.st_size )
   298         length = st.st_size;
   300     while( total < length ) {
   301         region = mem_get_region(addr);
   302         len = 4096 - (addr & 0x0FFF);
   303         if( len > (length-total) ) 
   304             len = (length-total);
   305         if( fread( region, len, 1, f ) != 1 ) {
   306             ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
   307             break;
   308         }
   310         addr += len;
   311         total += len;
   312     }
   313     fclose( f );
   314     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   315     return 0;
   316 }
   318 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   319                                    const char *name, mem_region_fn_t fn, int flags, 
   320                                    uint32_t repeat_offset, uint32_t repeat_until )
   321 {
   322     int i;
   323     mem_rgn[num_mem_rgns].base = base;
   324     mem_rgn[num_mem_rgns].size = size;
   325     mem_rgn[num_mem_rgns].flags = flags;
   326     mem_rgn[num_mem_rgns].name = name;
   327     mem_rgn[num_mem_rgns].mem = mem;
   328     mem_rgn[num_mem_rgns].fn = fn;
   329     fn->prefetch = unmapped_prefetch;
   330     fn->read_byte_for_write = fn->read_byte;
   331     num_mem_rgns++;
   333     do {
   334         for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
   335             if( mem != NULL ) {
   336                 page_map[(base>>LXDREAM_PAGE_BITS)+i] = ((unsigned char *)mem) + (i<<LXDREAM_PAGE_BITS);
   337             }
   338             ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
   339             mem_page_remapped( base + (i<<LXDREAM_PAGE_BITS), fn );
   340         }
   341         base += repeat_offset;	
   342     } while( base <= repeat_until );
   344     return &mem_rgn[num_mem_rgns-1];
   345 }
   347 gboolean mem_load_rom( void *output, const gchar *file, uint32_t size, uint32_t crc )
   348 {
   349     if( file != NULL && file[0] != '\0' ) {
   350         FILE *f = fopen(file,"r");
   351         struct stat st;
   352         uint32_t calc_crc;
   354         if( f == NULL ) {
   355             ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
   356             return FALSE;
   357         }
   359         fstat( fileno(f), &st );
   360         if( st.st_size != size ) {
   361             ERROR( "File '%s' is invalid, expected %d bytes but was %d bytes long.", file, size, st.st_size );
   362             fclose(f);
   363             return FALSE;
   364         }
   366         if( fread( output, 1, size, f ) != size ) { 
   367             ERROR( "Failed to load file '%s': %s", file, strerror(errno) );
   368             fclose(f);
   369             return FALSE;
   370         }
   372         /* CRC check only if we loaded something */
   373         calc_crc = crc32(0L, output, size);
   374         if( calc_crc != crc ) {
   375             WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   376                    file, calc_crc, crc);
   377         }
   378         /* Even if the CRC fails, continue normally */
   379         return TRUE;
   380     }
   381     return FALSE;
   382 }
   384 sh4ptr_t mem_get_region_by_name( const char *name )
   385 {
   386     int i;
   387     for( i=0; i<num_mem_rgns; i++ ) {
   388         if( strcmp( mem_rgn[i].name, name ) == 0 )
   389             return mem_rgn[i].mem;
   390     }
   391     return NULL;
   392 }
   394 void register_io_region( struct mmio_region *io )
   395 {
   396     int i;
   398     assert(io);
   399     io->mem = mem_alloc_pages(2);
   400     io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
   401     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   402     io->trace_flag = 0;
   403     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   404     for( i=0; io->ports[i].id != NULL; i++ ) {
   405         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   406         *io->ports[i].val = io->ports[i].def_val;
   407         io->index[io->ports[i].offset>>2] = &io->ports[i];
   408     }
   409     memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
   410     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   411         /* P4 area (on-chip I/O channels */
   412         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   413     } else {
   414         page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
   415         ext_address_space[io->base>>12] = &io->fn;
   416         mem_page_remapped( io->base, &io->fn );
   417     }
   418     io_rgn[num_io_rgns] = io;
   419     num_io_rgns++;
   420 }
   422 void register_io_regions( struct mmio_region **io )
   423 {
   424     while( *io ) register_io_region( *io++ );
   425 }
   427 gboolean mem_has_page( uint32_t addr )
   428 {
   429     return ext_address_space[ (addr&0x1FFFFFFF)>>12 ] != &mem_region_unmapped;
   430 }
   432 sh4ptr_t mem_get_region( uint32_t addr )
   433 {
   434     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   435     if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   436         return NULL;
   437     } else {
   438         return page+(addr&0xFFF);
   439     }
   440 }
   442 void mem_write_long( sh4addr_t addr, uint32_t value )
   443 {
   444     ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long(addr, value);
   445 }
   447 static struct mmio_region *mem_get_io_region_by_name( const gchar *name )
   448 {
   449     int i;
   450     for( i=0; i<num_io_rgns; i++ ) {
   451         if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
   452             return io_rgn[i];
   453         }
   454     }
   455     return NULL;
   456 }
   458 void mem_set_trace( const gchar *tracelist, gboolean flag )
   459 {
   460     if( tracelist != NULL ) {
   461         gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
   462         int i;
   463         for( i=0; tracev[i] != NULL; i++ ) {
   464             // Special case "all" - trace everything
   465             if( strcasecmp(tracev[i], "all") == 0 ) {
   466                 int j;
   467                 for( j=0; j<num_io_rgns; j++ ) {
   468                     io_rgn[j]->trace_flag = flag ? 1 : 0;
   469                 }
   470                 break;
   471             }
   472             struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
   473             if( region == NULL ) {
   474                 WARN( "Unknown IO region '%s'", tracev[i] );
   475             } else {
   476                 region->trace_flag = flag ? 1 : 0;
   477             }
   478         }
   479         g_strfreev( tracev );
   480     }
   481 }
.