Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 1167:2cc6c50d0dd2
prev1075:1a21750d300c
next1296:30ecee61f811
author nkeynes
date Fri Mar 02 23:49:10 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android WIP:
* Rename gui_jni.c to gui_android.c - now quite android specific.
* Implement generic EGL driver with very minimal Java wrapper
* Run emulation in separate thread, and implement simple queue for
inter-thread communication.
* Add menu/action-bar items for start + reset
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 void FASTCALL default_write_burst( sh4addr_t addr, unsigned char *src )
    87 {
    88     mem_write_fn_t writefn = ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long;
    89     uint32_t *p = (uint32_t *)src;
    90     sh4addr_t end = addr + 32;
    91     while( addr < end ) {
    92         writefn(addr, *p);
    93         addr += 4;
    94         p += 4;
    95     }
    96 }
    98 void FASTCALL default_read_burst( unsigned char *dest, sh4addr_t addr )
    99 {
   100     mem_read_fn_t readfn = ext_address_space[(addr&0x1FFFFFFF)>>12]->read_long;
   101     uint32_t *p = (uint32_t *)dest;
   102     sh4addr_t end = addr + 32;
   103     while( addr < end ) {
   104         *p = readfn(addr);
   105         addr += 4;
   106         p += 4;
   107     }
   108 }
   110 struct mem_region_fn mem_region_unmapped = { 
   111         unmapped_read_long, unmapped_write_long, 
   112         unmapped_read_long, unmapped_write_long, 
   113         unmapped_read_long, unmapped_write_long, 
   114         unmapped_read_burst, unmapped_write_burst,
   115         unmapped_prefetch, unmapped_read_long }; 
   117 void *mem_alloc_pages( int n )
   118 {
   119     void *mem = mmap( NULL, n * 4096,
   120             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   121     if( mem == MAP_FAILED ) {
   122         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
   123         return NULL;
   124     }
   125     return mem;
   126 }
   128 void mem_unprotect( void *region, uint32_t size )
   129 {
   130     /* Force page alignment */
   131     uintptr_t i = (uintptr_t)region;
   132     uintptr_t mask = ~(PAGE_SIZE-1);
   133     void *ptr = (void *)(i & mask);
   134     size_t len = (i & (PAGE_SIZE-1)) + size;
   135     len = (len + (PAGE_SIZE-1)) & mask;
   137     int status = mprotect( ptr, len, PROT_READ|PROT_WRITE|PROT_EXEC );
   138     assert( status == 0 );
   139 }
   141 void mem_init( void )
   142 {
   143     int i;
   144     mem_region_fn_t *ptr;
   145     page_map = (sh4ptr_t *)mmap( NULL, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES,
   146             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   147     if( page_map == MAP_FAILED ) {
   148         FATAL( "Unable to allocate page map! (%s)", strerror(errno) );
   149     }
   150     memset( page_map, 0, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES );
   152     ext_address_space = (mem_region_fn_t *) mmap( NULL, sizeof(mem_region_fn_t) * LXDREAM_PAGE_TABLE_ENTRIES,
   153             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
   154     if( ext_address_space == MAP_FAILED ) {
   155         FATAL( "Unable to allocate external memory map (%s)", strerror(errno) );
   156     }
   158     for( ptr = ext_address_space, i = LXDREAM_PAGE_TABLE_ENTRIES; i > 0; ptr++, i-- ) {
   159         *ptr = &mem_region_unmapped;
   160     }
   161 }
   163 void mem_reset( void )
   164 {
   165     /* Restore all mmio registers to their initial settings */
   166     int i, j;
   167     for( i=1; i<num_io_rgns; i++ ) {
   168         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
   169             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
   170                     io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
   171                 io_rgn[i]->fn.write_long( io_rgn[i]->ports[j].offset,
   172                         io_rgn[i]->ports[j].def_val );
   173             }
   174         }
   175     }
   176 }
   178 static void mem_save( FILE *f ) 
   179 {
   180     int i, num_ram_regions = 0;
   181     uint32_t len;
   183     /* All RAM regions (ROM and non-memory regions don't need to be saved)
   184      * Flash is questionable - for now we save it too */
   185     for( i=0; i<num_mem_rgns; i++ ) {
   186         if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
   187             num_ram_regions++;
   188         }
   189     }
   190     fwrite( &num_ram_regions, sizeof(num_ram_regions), 1, f );
   192     for( i=0; i<num_mem_rgns; i++ ) {
   193         if( mem_rgn[i].flags == MEM_FLAG_RAM ) {
   194             fwrite_string( mem_rgn[i].name, f );
   195             fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
   196             fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
   197             fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   198             fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   199         }
   200     }
   202     /* All MMIO regions */
   203     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   204     for( i=0; i<num_io_rgns; i++ ) {
   205         fwrite_string( io_rgn[i]->id, f );
   206         fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   207         len = 4096;
   208         fwrite( &len, sizeof(len), 1, f );
   209         fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   210     }
   211 }
   213 static int mem_load( FILE *f )
   214 {
   215     char tmp[64];
   216     uint32_t len;
   217     uint32_t base, size;
   218     uint32_t flags;
   219     int i, j;
   220     int mem_region_loaded[MAX_MEM_REGIONS];
   222     /* All RAM regions */
   223     memset( mem_region_loaded, 0, sizeof(mem_region_loaded) );
   224     fread( &len, sizeof(len), 1, f );
   225     for( i=0; i<len; i++ ) {
   226         fread_string( tmp, sizeof(tmp), f );
   228         for( j=0; j<num_mem_rgns; j++ ) {
   229             if( strcasecmp( mem_rgn[j].name, tmp ) == 0 ) {
   230                 fread( &base, sizeof(base), 1, f );
   231                 fread( &flags, sizeof(flags), 1, f );
   232                 fread( &size, sizeof(size), 1, f );
   233                 if( base != mem_rgn[j].base ||
   234                     flags != mem_rgn[j].flags ||
   235                     size != mem_rgn[j].size ) {
   236                     ERROR( "Bad memory block %d %s (not mapped to expected region)", i, tmp );
   237                     return -1;
   238                 }
   239                 if( flags != MEM_FLAG_RAM ) {
   240                     ERROR( "Unexpected memory block %d %s (Not a RAM region)", i, tmp );
   241                     return -1;
   242                 }
   243                 fread_gzip( mem_rgn[j].mem, size, 1, f );
   244                 mem_region_loaded[j] = 1;
   245                 break;
   246             }
   247         }
   248     }
   249     /* Make sure we got all the memory regions we expected */
   250     for( i=0; i<num_mem_rgns; i++ ) {
   251         if( mem_rgn[i].flags == MEM_FLAG_RAM &&
   252             mem_region_loaded[i] == 0 ) {
   253             ERROR( "Missing memory block %s (not found in save state)", mem_rgn[i].name );
   254             return -1;
   255         }
   256     }
   258     /* All MMIO regions */
   259     fread( &len, sizeof(len), 1, f );
   260     if( len != num_io_rgns ) {
   261         ERROR( "Unexpected IO region count %d (expected %d)", len, num_io_rgns );
   262         return -1;
   263     }
   265     for( i=0; i<len; i++ ) {
   266         fread_string( tmp, sizeof(tmp), f );
   267         fread( &base, sizeof(base), 1, f );
   268         fread( &size, sizeof(size), 1, f );
   269         if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   270                 base != io_rgn[i]->base ||
   271                 size != 4096 ) {
   272             ERROR( "Bad MMIO region %d %s", i, tmp );
   273             return -1;
   274         }
   275         fread_gzip( io_rgn[i]->mem, size, 1, f );
   276     }
   277     return 0;
   278 }
   280 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   281 {
   282     sh4ptr_t region;
   283     int len = 4096, total = 0;
   284     uint32_t addr = start;
   285     FILE *f = fopen(file,"w");
   287     if( f == NULL )
   288         return errno;
   290     while( total < length ) {
   291         region = mem_get_region(addr);
   292         len = 4096 - (addr & 0x0FFF);
   293         if( len > (length-total) ) 
   294             len = (length-total);
   295         if( fwrite( region, len, 1, f ) != 1 ) {
   296             ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
   297             break;
   298         }
   300         addr += len;
   301         total += len;
   302     }
   303     fclose( f );
   304     DEBUG( "Saved %d of %d bytes to %08X", total, length, start );
   305     return 0;
   306 }
   308 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   309 {
   310     sh4ptr_t region;
   311     int len = 4096, total = 0;
   312     uint32_t addr = start;
   313     struct stat st;
   314     FILE *f = fopen(file,"r");
   316     if( f == NULL ) {
   317         WARN( "Unable to load file '%s': %s", file, strerror(errno) );
   318         return -1;
   319     }
   320     fstat( fileno(f), &st );
   321     if( length == 0 || length == -1 || length > st.st_size )
   322         length = st.st_size;
   324     while( total < length ) {
   325         region = mem_get_region(addr);
   326         len = 4096 - (addr & 0x0FFF);
   327         if( len > (length-total) ) 
   328             len = (length-total);
   329         if( fread( region, len, 1, f ) != 1 ) {
   330             ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
   331             break;
   332         }
   334         addr += len;
   335         total += len;
   336     }
   337     fclose( f );
   338     DEBUG( "Loaded %d of %d bytes to %08X", total, length, start );
   339     return 0;
   340 }
   342 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   343                                    const char *name, mem_region_fn_t fn, int flags, 
   344                                    uint32_t repeat_offset, uint32_t repeat_until )
   345 {
   346     int i;
   347     mem_rgn[num_mem_rgns].base = base;
   348     mem_rgn[num_mem_rgns].size = size;
   349     mem_rgn[num_mem_rgns].flags = flags;
   350     mem_rgn[num_mem_rgns].name = name;
   351     mem_rgn[num_mem_rgns].mem = mem;
   352     mem_rgn[num_mem_rgns].fn = fn;
   353     fn->prefetch = unmapped_prefetch;
   354     fn->read_byte_for_write = fn->read_byte;
   355     num_mem_rgns++;
   357     do {
   358         for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
   359             if( mem != NULL ) {
   360                 page_map[(base>>LXDREAM_PAGE_BITS)+i] = ((unsigned char *)mem) + (i<<LXDREAM_PAGE_BITS);
   361             }
   362             ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
   363             mem_page_remapped( base + (i<<LXDREAM_PAGE_BITS), fn );
   364         }
   365         base += repeat_offset;	
   366     } while( base <= repeat_until );
   368     return &mem_rgn[num_mem_rgns-1];
   369 }
   371 gboolean mem_load_rom( void *output, const gchar *file, uint32_t size, uint32_t crc )
   372 {
   373     if( file != NULL && file[0] != '\0' ) {
   374         FILE *f = fopen(file,"r");
   375         struct stat st;
   376         uint32_t calc_crc;
   378         if( f == NULL ) {
   379             ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
   380             return FALSE;
   381         }
   383         fstat( fileno(f), &st );
   384         if( st.st_size != size ) {
   385             ERROR( "File '%s' is invalid, expected %d bytes but was %d bytes long.", file, size, st.st_size );
   386             fclose(f);
   387             return FALSE;
   388         }
   390         if( fread( output, 1, size, f ) != size ) { 
   391             ERROR( "Failed to load file '%s': %s", file, strerror(errno) );
   392             fclose(f);
   393             return FALSE;
   394         }
   396         /* CRC check only if we loaded something */
   397         calc_crc = crc32(0L, output, size);
   398         if( calc_crc != crc ) {
   399             WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   400                    file, calc_crc, crc);
   401         }
   402         /* Even if the CRC fails, continue normally */
   403         return TRUE;
   404     }
   405     return FALSE;
   406 }
   408 sh4ptr_t mem_get_region_by_name( const char *name )
   409 {
   410     int i;
   411     for( i=0; i<num_mem_rgns; i++ ) {
   412         if( strcmp( mem_rgn[i].name, name ) == 0 )
   413             return mem_rgn[i].mem;
   414     }
   415     return NULL;
   416 }
   418 void register_io_region( struct mmio_region *io )
   419 {
   420     int i;
   422     assert(io);
   423     io->mem = mem_alloc_pages(2);
   424     io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
   425     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   426     io->trace_flag = 0;
   427     if( io->fn.write_burst == NULL )
   428         io->fn.write_burst = default_write_burst;
   429     if( io->fn.read_burst == NULL )
   430         io->fn.read_burst = default_read_burst;
   431     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   432     for( i=0; io->ports[i].id != NULL; i++ ) {
   433         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   434         *io->ports[i].val = io->ports[i].def_val;
   435         io->index[io->ports[i].offset>>2] = &io->ports[i];
   436     }
   437     memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
   438     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   439         /* P4 area (on-chip I/O channels */
   440         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   441     } else {
   442         page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
   443         ext_address_space[io->base>>12] = &io->fn;
   444         mem_page_remapped( io->base, &io->fn );
   445     }
   446     io_rgn[num_io_rgns] = io;
   447     num_io_rgns++;
   448 }
   450 void register_io_regions( struct mmio_region **io )
   451 {
   452     while( *io ) register_io_region( *io++ );
   453 }
   455 gboolean mem_has_page( uint32_t addr )
   456 {
   457     return ext_address_space[ (addr&0x1FFFFFFF)>>12 ] != &mem_region_unmapped;
   458 }
   460 sh4ptr_t mem_get_region( uint32_t addr )
   461 {
   462     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   463     if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   464         return NULL;
   465     } else {
   466         return page+(addr&0xFFF);
   467     }
   468 }
   470 void mem_write_long( sh4addr_t addr, uint32_t value )
   471 {
   472     ext_address_space[(addr&0x1FFFFFFF)>>12]->write_long(addr, value);
   473 }
   475 static struct mmio_region *mem_get_io_region_by_name( const gchar *name )
   476 {
   477     int i;
   478     for( i=0; i<num_io_rgns; i++ ) {
   479         if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
   480             return io_rgn[i];
   481         }
   482     }
   483     return NULL;
   484 }
   486 void mem_set_trace( const gchar *tracelist, gboolean flag )
   487 {
   488     if( tracelist != NULL ) {
   489         gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
   490         int i;
   491         for( i=0; tracev[i] != NULL; i++ ) {
   492             // Special case "all" - trace everything
   493             if( strcasecmp(tracev[i], "all") == 0 ) {
   494                 int j;
   495                 for( j=0; j<num_io_rgns; j++ ) {
   496                     io_rgn[j]->trace_flag = flag ? 1 : 0;
   497                 }
   498                 break;
   499             }
   500             struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
   501             if( region == NULL ) {
   502                 WARN( "Unknown IO region '%s'", tracev[i] );
   503             } else {
   504                 region->trace_flag = flag ? 1 : 0;
   505             }
   506         }
   507         g_strfreev( tracev );
   508     }
   509 }
.