Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 934:3acd3b3ee6d1
prev932:2602c5603ce2
next939:6f2302afeb89
author nkeynes
date Fri Dec 26 14:25:23 2008 +0000 (13 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Change RAM regions to use static arrays rather than mmap regions, for a 2-3% performance gain.
General mem cleanups, including some save state fixes that break states again.
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 sh4ptr_t *page_map = NULL;
    39 mem_region_fn_t *ext_address_space = NULL;
    41 extern struct mem_region_fn mem_region_unmapped; 
    43 int mem_load(FILE *f);
    44 void mem_save(FILE *f);
    45 struct dreamcast_module mem_module =
    46 { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
    48 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    49 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    50 struct mmio_region *P4_io[4096];
    52 uint32_t num_io_rgns = 0, num_mem_rgns = 0;
    54 DEFINE_HOOK( mem_page_remapped_hook, mem_page_remapped_hook_t );
    55 static void mem_page_remapped( sh4addr_t addr, mem_region_fn_t fn )
    56 {
    57     CALL_HOOKS( mem_page_remapped_hook, addr, fn );
    58 }
    60 /********************* The "unmapped" address space ********************/
    61 /* Always reads as 0, writes have no effect */
    62 int32_t FASTCALL unmapped_read_long( sh4addr_t addr )
    63 {
    64     return 0;
    65 }
    66 void FASTCALL unmapped_write_long( sh4addr_t addr, uint32_t val )
    67 {
    68 }
    69 void FASTCALL unmapped_read_burst( unsigned char *dest, sh4addr_t addr )
    70 {
    71     memset( dest, 0, 32 );
    72 }
    73 void FASTCALL unmapped_write_burst( sh4addr_t addr, unsigned char *src )
    74 {
    75 }
    77 struct mem_region_fn mem_region_unmapped = { 
    78         unmapped_read_long, unmapped_write_long, 
    79         unmapped_read_long, unmapped_write_long, 
    80         unmapped_read_long, unmapped_write_long, 
    81         unmapped_read_burst, unmapped_write_burst }; 
    83 void *mem_alloc_pages( int n )
    84 {
    85     void *mem = mmap( NULL, n * 4096,
    86             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    87     if( mem == MAP_FAILED ) {
    88         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    89         return NULL;
    90     }
    91     return mem;
    92 }
    95 void mem_init( void )
    96 {
    97     int i;
    98     mem_region_fn_t *ptr;
    99     page_map = mmap( NULL, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES,
   100             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   101     if( page_map == MAP_FAILED ) {
   102         FATAL( "Unable to allocate page map! (%s)", strerror(errno) );
   103     }
   104     memset( page_map, 0, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES );
   106     ext_address_space = mmap( NULL, sizeof(mem_region_fn_t) * LXDREAM_PAGE_TABLE_ENTRIES,
   107             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   108     if( ext_address_space == MAP_FAILED ) {
   109         FATAL( "Unable to allocate external memory map (%s)", strerror(errno) );
   110     }
   112     for( ptr = ext_address_space, i = LXDREAM_PAGE_TABLE_ENTRIES; i > 0; ptr++, i-- ) {
   113         *ptr = &mem_region_unmapped;
   114     }
   115 }
   117 void mem_reset( void )
   118 {
   119     /* Restore all mmio registers to their initial settings */
   120     int i, j;
   121     for( i=1; i<num_io_rgns; i++ ) {
   122         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
   123             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
   124                     io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
   125                 io_rgn[i]->fn.write_long( io_rgn[i]->ports[j].offset,
   126                         io_rgn[i]->ports[j].def_val );
   127             }
   128         }
   129     }
   130 }
   132 void mem_save( FILE *f ) 
   133 {
   134     int i, num_ram_regions = 0;
   135     uint32_t len;
   137     /* All RAM regions (ROM and non-memory regions don't need to be saved)
   138      * Flash is questionable - for now we save it too */
   139     for( i=0; i<num_mem_rgns; i++ ) {
   140         if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
   141             num_ram_regions++;
   142         }
   143     }
   144     fwrite( &num_ram_regions, sizeof(num_ram_regions), 1, f );
   146     for( i=0; i<num_mem_rgns; i++ ) {
   147         if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
   148             fwrite_string( mem_rgn[i].name, f );
   149             fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
   150             fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
   151             fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   152             fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   153         }
   154     }
   156     /* All MMIO regions */
   157     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   158     for( i=0; i<num_io_rgns; i++ ) {
   159         fwrite_string( io_rgn[i]->id, f );
   160         fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   161         len = 4096;
   162         fwrite( &len, sizeof(len), 1, f );
   163         fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   164     }
   165 }
   167 int mem_load( FILE *f )
   168 {
   169     char tmp[64];
   170     uint32_t len;
   171     uint32_t base, size;
   172     uint32_t flags;
   173     int i, j;
   174     int mem_region_loaded[MAX_MEM_REGIONS];
   176     /* All RAM regions */
   177     memset( mem_region_loaded, 0, sizeof(mem_region_loaded) );
   178     fread( &len, sizeof(len), 1, f );
   179     for( i=0; i<len; i++ ) {
   180         fread_string( tmp, sizeof(tmp), f );
   182         for( j=0; j<num_mem_rgns; j++ ) {
   183             if( strcasecmp( mem_rgn[j].name, tmp ) == 0 ) {
   184                 fread( &base, sizeof(base), 1, f );
   185                 fread( &flags, sizeof(flags), 1, f );
   186                 fread( &size, sizeof(size), 1, f );
   187                 if( base != mem_rgn[j].base ||
   188                     flags != mem_rgn[j].flags ||
   189                     size != mem_rgn[j].size ) {
   190                     ERROR( "Bad memory block %d %s (not mapped to expected region)", i, tmp );
   191                     return -1;
   192                 }
   193                 if( flags != MEM_FLAG_RAM ) {
   194                     ERROR( "Unexpected memory block %d %s (Not a RAM region)", i, tmp );
   195                     return -1;
   196                 }
   197                 fread_gzip( mem_rgn[j].mem, size, 1, f );
   198                 mem_region_loaded[j] = 1;
   199             }
   200         }
   201     }
   202     /* Make sure we got all the memory regions we expected */
   203     for( i=0; i<num_mem_rgns; i++ ) {
   204         if( mem_rgn[i].flags == MEM_FLAG_RAM &&
   205             mem_region_loaded[i] == 0 ) {
   206             ERROR( "Missing memory block %s (not found in save state)", mem_rgn[i].name );
   207             return -1;
   208         }
   209     }
   211     /* All MMIO regions */
   212     fread( &len, sizeof(len), 1, f );
   213     if( len != num_io_rgns ) {
   214         ERROR( "Unexpected IO region count %d (expected %d)", len, num_io_rgns );
   215         return -1;
   216     }
   218     for( i=0; i<len; i++ ) {
   219         fread_string( tmp, sizeof(tmp), f );
   220         fread( &base, sizeof(base), 1, f );
   221         fread( &size, sizeof(size), 1, f );
   222         if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   223                 base != io_rgn[i]->base ||
   224                 size != 4096 ) {
   225             ERROR( "Bad MMIO region %d %s", i, tmp );
   226             return -1;
   227         }
   228         fread_gzip( io_rgn[i]->mem, size, 1, f );
   229     }
   230     return 0;
   231 }
   233 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   234 {
   235     sh4ptr_t region;
   236     int len = 4096, total = 0;
   237     uint32_t addr = start;
   238     FILE *f = fopen(file,"w");
   240     if( f == NULL )
   241         return errno;
   243     while( total < length ) {
   244         region = mem_get_region(addr);
   245         len = 4096 - (addr & 0x0FFF);
   246         if( len > (length-total) ) 
   247             len = (length-total);
   248         if( fwrite( region, len, 1, f ) != 1 ) {
   249             ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
   250             break;
   251         }
   253         addr += len;
   254         total += len;
   255     }
   256     fclose( f );
   257     INFO( "Saved %d of %d bytes to %08X", total, length, start );
   258     return 0;
   259 }
   261 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   262 {
   263     sh4ptr_t region;
   264     int len = 4096, total = 0;
   265     uint32_t addr = start;
   266     struct stat st;
   267     FILE *f = fopen(file,"r");
   269     if( f == NULL ) {
   270         WARN( "Unable to load file '%s': %s", file, strerror(errno) );
   271         return -1;
   272     }
   273     fstat( fileno(f), &st );
   274     if( length == 0 || length == -1 || length > st.st_size )
   275         length = st.st_size;
   277     while( total < length ) {
   278         region = mem_get_region(addr);
   279         len = 4096 - (addr & 0x0FFF);
   280         if( len > (length-total) ) 
   281             len = (length-total);
   282         if( fread( region, len, 1, f ) != 1 ) {
   283             ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
   284             break;
   285         }
   287         addr += len;
   288         total += len;
   289     }
   290     fclose( f );
   291     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   292     return 0;
   293 }
   295 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   296                                    const char *name, mem_region_fn_t fn, int flags, 
   297                                    uint32_t repeat_offset, uint32_t repeat_until )
   298 {
   299     int i;
   300     mem_rgn[num_mem_rgns].base = base;
   301     mem_rgn[num_mem_rgns].size = size;
   302     mem_rgn[num_mem_rgns].flags = flags;
   303     mem_rgn[num_mem_rgns].name = name;
   304     mem_rgn[num_mem_rgns].mem = mem;
   305     mem_rgn[num_mem_rgns].fn = fn;
   306     num_mem_rgns++;
   308     do {
   309         for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
   310             if( mem != NULL ) {
   311                 page_map[(base>>LXDREAM_PAGE_BITS)+i] = mem + (i<<LXDREAM_PAGE_BITS);
   312             }
   313             ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
   314             mem_page_remapped( base + (i<<LXDREAM_PAGE_BITS), fn );
   315         }
   316         base += repeat_offset;	
   317     } while( base <= repeat_until );
   319     return &mem_rgn[num_mem_rgns-1];
   320 }
   322 gboolean mem_load_rom( void *output, const gchar *file, uint32_t size, uint32_t crc )
   323 {
   324     if( file != NULL && file[0] != '\0' ) {
   325         FILE *f = fopen(file,"r");
   326         struct stat st;
   327         uint32_t calc_crc;
   329         if( f == NULL ) {
   330             ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
   331             return FALSE;
   332         }
   334         fstat( fileno(f), &st );
   335         if( st.st_size != size ) {
   336             ERROR( "File '%s' is invalid, expected %d bytes but was %d bytes long.", file, size, st.st_size );
   337             fclose(f);
   338             return FALSE;
   339         }
   341         if( fread( output, 1, size, f ) != size ) { 
   342             ERROR( "Failed to load file '%s': %s", file, strerror(errno) );
   343             fclose(f);
   344             return FALSE;
   345         }
   347         /* CRC check only if we loaded something */
   348         calc_crc = crc32(0L, output, size);
   349         if( calc_crc != crc ) {
   350             WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   351                    file, calc_crc, crc);
   352         }
   353         /* Even if the CRC fails, continue normally */
   354         return TRUE;
   355     }
   356     return FALSE;
   357 }
   359 sh4ptr_t mem_get_region_by_name( const char *name )
   360 {
   361     int i;
   362     for( i=0; i<num_mem_rgns; i++ ) {
   363         if( strcmp( mem_rgn[i].name, name ) == 0 )
   364             return mem_rgn[i].mem;
   365     }
   366     return NULL;
   367 }
   369 void register_io_region( struct mmio_region *io )
   370 {
   371     int i;
   373     assert(io);
   374     io->mem = mem_alloc_pages(2);
   375     io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
   376     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   377     io->trace_flag = 0;
   378     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   379     for( i=0; io->ports[i].id != NULL; i++ ) {
   380         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   381         *io->ports[i].val = io->ports[i].def_val;
   382         io->index[io->ports[i].offset>>2] = &io->ports[i];
   383     }
   384     memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
   385     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   386         /* P4 area (on-chip I/O channels */
   387         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   388     } else {
   389         page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
   390         ext_address_space[io->base>>12] = &io->fn;
   391         mem_page_remapped( io->base, &io->fn );
   392     }
   393     io_rgn[num_io_rgns] = io;
   394     num_io_rgns++;
   395 }
   397 void register_io_regions( struct mmio_region **io )
   398 {
   399     while( *io ) register_io_region( *io++ );
   400 }
   402 gboolean mem_has_page( uint32_t addr )
   403 {
   404     return ext_address_space[ (addr&0x1FFFFFFF)>>12 ] != &mem_region_unmapped;
   405 }
   407 sh4ptr_t mem_get_region( uint32_t addr )
   408 {
   409     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   410     if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   411         return NULL;
   412     } else {
   413         return page+(addr&0xFFF);
   414     }
   415 }
   417 void mem_write_long( sh4addr_t addr, uint32_t value )
   418 {
   419     ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long(addr, value);
   420 }
   422 struct mmio_region *mem_get_io_region_by_name( const gchar *name )
   423 {
   424     int i;
   425     for( i=0; i<num_io_rgns; i++ ) {
   426         if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
   427             return io_rgn[i];
   428         }
   429     }
   430     return NULL;
   431 }
   433 void mem_set_trace( const gchar *tracelist, gboolean flag )
   434 {
   435     if( tracelist != NULL ) {
   436         gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
   437         int i;
   438         for( i=0; tracev[i] != NULL; i++ ) {
   439             // Special case "all" - trace everything
   440             if( strcasecmp(tracev[i], "all") == 0 ) {
   441                 int j;
   442                 for( j=0; j<num_io_rgns; j++ ) {
   443                     io_rgn[j]->trace_flag = flag ? 1 : 0;
   444                 }
   445                 break;
   446             }
   447             struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
   448             if( region == NULL ) {
   449                 WARN( "Unknown IO region '%s'", tracev[i] );
   450             } else {
   451                 region->trace_flag = flag ? 1 : 0;
   452             }
   453         }
   454         g_strfreev( tracev );
   455     }
   456 }
.