Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 930:07e5b11419db
prev929:fd8cb0c82f5f
next931:430048ea8b71
author nkeynes
date Mon Dec 22 09:51:11 2008 +0000 (13 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Remove pointer cache and add full address-space map. Much better
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 void *mem_alloc_pages( int n )
    55 {
    56     void *mem = mmap( NULL, n * 4096,
    57             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    58     if( mem == MAP_FAILED ) {
    59         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    60         return NULL;
    61     }
    62     return mem;
    63 }
    66 void mem_init( void )
    67 {
    68     int i;
    69     mem_region_fn_t *ptr;
    70     page_map = mmap( NULL, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES,
    71             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    72     if( page_map == MAP_FAILED ) {
    73         FATAL( "Unable to allocate page map! (%s)", strerror(errno) );
    74     }
    75     memset( page_map, 0, sizeof(sh4ptr_t) * LXDREAM_PAGE_TABLE_ENTRIES );
    77     ext_address_space = mmap( NULL, sizeof(mem_region_fn_t) * LXDREAM_PAGE_TABLE_ENTRIES,
    78             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    79     if( ext_address_space == MAP_FAILED ) {
    80         FATAL( "Unable to allocate external memory map (%s)", strerror(errno) );
    81     }
    83     for( ptr = ext_address_space, i = LXDREAM_PAGE_TABLE_ENTRIES; i > 0; ptr++, i-- ) {
    84         *ptr = &mem_region_unmapped;
    85     }
    86 }
    88 void mem_reset( void )
    89 {
    90     /* Restore all mmio registers to their initial settings */
    91     int i, j;
    92     for( i=1; i<num_io_rgns; i++ ) {
    93         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
    94             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
    95                     io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
    96                 io_rgn[i]->fn.write_long( io_rgn[i]->ports[j].offset,
    97                         io_rgn[i]->ports[j].def_val );
    98             }
    99         }
   100     }
   101 }
   103 void mem_save( FILE *f ) 
   104 {
   105     int i;
   106     uint32_t len;
   108     /* All memory regions */
   109     fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
   110     for( i=0; i<num_mem_rgns; i++ ) {
   111         fwrite_string( mem_rgn[i].name, f );
   112         fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
   113         fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
   114         fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   115         if( mem_rgn[i].flags != MEM_FLAG_ROM )
   116             fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   117     }
   119     /* All MMIO regions */
   120     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   121     for( i=0; i<num_io_rgns; i++ ) {
   122         fwrite_string( io_rgn[i]->id, f );
   123         fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   124         len = 4096;
   125         fwrite( &len, sizeof(len), 1, f );
   126         fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   127     }
   128 }
   130 int mem_load( FILE *f )
   131 {
   132     char tmp[64];
   133     uint32_t len;
   134     uint32_t base, size;
   135     uint32_t flags;
   136     int i;
   138     /* All memory regions */
   139     fread( &len, sizeof(len), 1, f );
   140     if( len != num_mem_rgns )
   141         return -1;
   142     for( i=0; i<len; i++ ) {
   143         fread_string( tmp, sizeof(tmp), f );
   144         fread( &base, sizeof(base), 1, f );
   145         fread( &flags, sizeof(flags), 1, f );
   146         fread( &size, sizeof(size), 1, f );
   147         if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
   148                 base != mem_rgn[i].base ||
   149                 flags != mem_rgn[i].flags ||
   150                 size != mem_rgn[i].size ) {
   151             ERROR( "Bad memory region %d %s", i, tmp );
   152             return -1;
   153         }
   154         if( flags != MEM_FLAG_ROM )
   155             fread_gzip( mem_rgn[i].mem, size, 1, f );
   156     }
   158     /* All MMIO regions */
   159     fread( &len, sizeof(len), 1, f );
   160     if( len != num_io_rgns ) 
   161         return -1;
   162     for( i=0; i<len; i++ ) {
   163         fread_string( tmp, sizeof(tmp), f );
   164         fread( &base, sizeof(base), 1, f );
   165         fread( &size, sizeof(size), 1, f );
   166         if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   167                 base != io_rgn[i]->base ||
   168                 size != 4096 ) {
   169             ERROR( "Bad MMIO region %d %s", i, tmp );
   170             return -1;
   171         }
   172         fread_gzip( io_rgn[i]->mem, size, 1, f );
   173     }
   174     return 0;
   175 }
   177 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   178 {
   179     sh4ptr_t region;
   180     int len = 4096, total = 0;
   181     uint32_t addr = start;
   182     FILE *f = fopen(file,"w");
   184     if( f == NULL )
   185         return errno;
   187     while( total < length ) {
   188         region = mem_get_region(addr);
   189         len = 4096 - (addr & 0x0FFF);
   190         if( len > (length-total) ) 
   191             len = (length-total);
   192         if( fwrite( region, len, 1, f ) != 1 ) {
   193             ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
   194             break;
   195         }
   197         addr += len;
   198         total += len;
   199     }
   200     fclose( f );
   201     INFO( "Saved %d of %d bytes to %08X", total, length, start );
   202     return 0;
   203 }
   205 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   206 {
   207     sh4ptr_t region;
   208     int len = 4096, total = 0;
   209     uint32_t addr = start;
   210     struct stat st;
   211     FILE *f = fopen(file,"r");
   213     if( f == NULL ) {
   214         WARN( "Unable to load file '%s': %s", file, strerror(errno) );
   215         return -1;
   216     }
   217     fstat( fileno(f), &st );
   218     if( length == 0 || length == -1 || length > st.st_size )
   219         length = st.st_size;
   221     while( total < length ) {
   222         region = mem_get_region(addr);
   223         len = 4096 - (addr & 0x0FFF);
   224         if( len > (length-total) ) 
   225             len = (length-total);
   226         if( fread( region, len, 1, f ) != 1 ) {
   227             ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
   228             break;
   229         }
   231         addr += len;
   232         total += len;
   233     }
   234     fclose( f );
   235     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   236     return 0;
   237 }
   239 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   240                                    const char *name, mem_region_fn_t fn, int flags, uint32_t repeat_offset,
   241                                    uint32_t repeat_until )
   242 {
   243     int i;
   244     mem_rgn[num_mem_rgns].base = base;
   245     mem_rgn[num_mem_rgns].size = size;
   246     mem_rgn[num_mem_rgns].flags = flags;
   247     mem_rgn[num_mem_rgns].name = name;
   248     mem_rgn[num_mem_rgns].mem = mem;
   249     mem_rgn[num_mem_rgns].fn = fn;
   250     num_mem_rgns++;
   252     do {
   253         for( i=0; i<size>>LXDREAM_PAGE_BITS; i++ ) {
   254             page_map[(base>>LXDREAM_PAGE_BITS)+i] = mem + (i<<LXDREAM_PAGE_BITS);
   255             ext_address_space[(base>>LXDREAM_PAGE_BITS)+i] = fn;
   256         }
   257         base += repeat_offset;	
   258     } while( base <= repeat_until );
   260     return &mem_rgn[num_mem_rgns-1];
   261 }
   263 void *mem_create_ram_region( uint32_t base, uint32_t size, const char *name, mem_region_fn_t fn )
   264 {
   265     return mem_create_repeating_ram_region( base, size, name, fn, size, base );
   266 }
   268 void *mem_create_repeating_ram_region( uint32_t base, uint32_t size, const char *name,
   269                                        mem_region_fn_t fn,
   270                                        uint32_t repeat_offset, uint32_t repeat_until )
   271 {
   272     char *mem;
   274     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   275     assert( (size&0x00000FFF) == 0 );
   276     assert( num_mem_rgns < MAX_MEM_REGIONS );
   277     assert( page_map != NULL );
   279     mem = mem_alloc_pages( size>>LXDREAM_PAGE_BITS );
   281     mem_map_region( mem, base, size, name, fn, MEM_FLAG_RAM, repeat_offset, repeat_until );
   283     return mem;
   284 }
   286 gboolean mem_load_rom( const gchar *file, uint32_t base, uint32_t size, uint32_t crc,
   287                        const gchar *region_name, mem_region_fn_t fn )
   288 {
   289     sh4ptr_t mem;
   290     uint32_t calc_crc;
   291     int status;
   293     mem = mem_get_region(base);
   294     if( mem == NULL ) {
   295         mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0 );
   296         if( mem == MAP_FAILED ) {
   297             ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
   298             return FALSE;
   299         }
   300         mem_map_region( mem, base, size, region_name, fn, MEM_FLAG_ROM, size, base );
   301     } else {
   302         mprotect( mem, size, PROT_READ|PROT_WRITE );
   303     }
   305     if( file != NULL && file[0] != '\0' ) {
   306         status = mem_load_block( file, base, size );
   307         mprotect( mem, size, PROT_READ );
   309         if( status == 0 ) {
   310             /* CRC check only if we loaded something */
   311             calc_crc = crc32(0L, (sh4ptr_t)mem, size);
   312             if( calc_crc != crc ) {
   313                 WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   314                         file, calc_crc, crc);
   315             }
   316             return TRUE;
   317         }
   318     }
   319     return FALSE;
   320 }
   322 sh4ptr_t mem_get_region_by_name( const char *name )
   323 {
   324     int i;
   325     for( i=0; i<num_mem_rgns; i++ ) {
   326         if( strcmp( mem_rgn[i].name, name ) == 0 )
   327             return mem_rgn[i].mem;
   328     }
   329     return NULL;
   330 }
   332 void register_io_region( struct mmio_region *io )
   333 {
   334     int i;
   336     assert(io);
   337     io->mem = mem_alloc_pages(2);
   338     io->save_mem = io->mem + LXDREAM_PAGE_SIZE;
   339     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   340     io->trace_flag = 0;
   341     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   342     for( i=0; io->ports[i].id != NULL; i++ ) {
   343         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   344         *io->ports[i].val = io->ports[i].def_val;
   345         io->index[io->ports[i].offset>>2] = &io->ports[i];
   346     }
   347     memcpy( io->save_mem, io->mem, LXDREAM_PAGE_SIZE );
   348     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   349         /* P4 area (on-chip I/O channels */
   350         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   351     } else {
   352         page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
   353         ext_address_space[io->base>>12] = &io->fn;
   354     }
   355     io_rgn[num_io_rgns] = io;
   356     num_io_rgns++;
   357 }
   359 void register_io_regions( struct mmio_region **io )
   360 {
   361     while( *io ) register_io_region( *io++ );
   362 }
   364 int mem_has_page( uint32_t addr )
   365 {
   366     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   367     return page != NULL;
   368 }
   370 sh4ptr_t mem_get_page( uint32_t addr )
   371 {
   372     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   373     return page;
   374 }
   376 sh4ptr_t mem_get_region( uint32_t addr )
   377 {
   378     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   379     if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   380         return NULL;
   381     } else {
   382         return page+(addr&0xFFF);
   383     }
   384 }
   386 void mem_write_long( sh4addr_t addr, uint32_t value )
   387 {
   388     sh4ptr_t ptr = mem_get_region(addr);
   389     assert(ptr != NULL);
   390     *((uint32_t *)ptr) = value;
   391 }
   393 struct mmio_region *mem_get_io_region( uint32_t addr )
   394 {
   395     if( addr > 0xFF000000 ) {
   396         return P4_io[(addr&0x00FFFFFF)>>12];
   397     }
   398     sh4ptr_t page = page_map[(addr&0x1FFFFFFF)>>12];
   399     if( ((uintptr_t)page) < MAX_IO_REGIONS ) {
   400         return io_rgn[(uintptr_t)page];
   401     } else {
   402         return NULL;
   403     }
   404 }
   406 struct mmio_region *mem_get_io_region_by_name( const gchar *name )
   407 {
   408     int i;
   409     for( i=0; i<num_io_rgns; i++ ) {
   410         if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
   411             return io_rgn[i];
   412         }
   413     }
   414     return NULL;
   415 }
   417 void mem_set_trace( const gchar *tracelist, gboolean flag )
   418 {
   419     if( tracelist != NULL ) {
   420         gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
   421         int i;
   422         for( i=0; tracev[i] != NULL; i++ ) {
   423             // Special case "all" - trace everything
   424             if( strcasecmp(tracev[i], "all") == 0 ) {
   425                 int j;
   426                 for( j=0; j<num_io_rgns; j++ ) {
   427                     io_rgn[j]->trace_flag = flag ? 1 : 0;
   428                 }
   429                 break;
   430             }
   431             struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
   432             if( region == NULL ) {
   433                 WARN( "Unknown IO region '%s'", tracev[i] );
   434             } else {
   435                 region->trace_flag = flag ? 1 : 0;
   436             }
   437         }
   438         g_strfreev( tracev );
   439     }
   440 }
.