Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 691:ad3356543392
prev674:377d987db8f2
next736:a02d1475ccfd
author nkeynes
date Thu Jun 19 04:40:37 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Refactor the gd-rom list management out of the GUI (devices, recent files, etc).
Add gd-rom list to the cocoa UI.
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;
    40 int mem_load(FILE *f);
    41 void mem_save(FILE *f);
    42 struct dreamcast_module mem_module =
    43     { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
    45 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    46 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    47 struct mmio_region *P4_io[4096];
    49 uint32_t num_io_rgns = 0, num_mem_rgns = 0;
    51 void *mem_alloc_pages( int n )
    52 {
    53     void *mem = mmap( NULL, n * 4096,
    54                       PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    55     if( mem == MAP_FAILED ) {
    56         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    57         return NULL;
    58     }
    59     return mem;
    60 }
    63 void mem_init( void )
    64 {
    65     page_map = mmap( NULL, sizeof(sh4ptr_t) * PAGE_TABLE_ENTRIES,
    66                      PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    67     if( page_map == MAP_FAILED ) {
    68         ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
    69         page_map = NULL;
    70         return;
    71     }
    73     memset( page_map, 0, sizeof(sh4ptr_t) * PAGE_TABLE_ENTRIES );
    74 }
    76 void mem_reset( void )
    77 {
    78     /* Restore all mmio registers to their initial settings */
    79     int i, j;
    80     for( i=1; i<num_io_rgns; i++ ) {
    81         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
    82             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
    83                 io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
    84                 io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
    85                                     io_rgn[i]->ports[j].def_val );
    86             }
    87         }
    88     }
    89 }
    91 void mem_save( FILE *f ) 
    92 {
    93     int i;
    94     uint32_t len;
    96     /* All memory regions */
    97     fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
    98     for( i=0; i<num_mem_rgns; i++ ) {
    99 	fwrite_string( mem_rgn[i].name, f );
   100 	fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
   101 	fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
   102 	fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   103 	if( mem_rgn[i].flags != MEM_FLAG_ROM )
   104 	    fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   105     }
   107     /* All MMIO regions */
   108     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   109     for( i=0; i<num_io_rgns; i++ ) {
   110 	fwrite_string( io_rgn[i]->id, f );
   111 	fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   112 	len = 4096;
   113 	fwrite( &len, sizeof(len), 1, f );
   114 	fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   115     }
   116 }
   118 int mem_load( FILE *f )
   119 {
   120     char tmp[64];
   121     uint32_t len;
   122     uint32_t base, size;
   123     uint32_t flags;
   124     int i;
   126     /* All memory regions */
   127     fread( &len, sizeof(len), 1, f );
   128     if( len != num_mem_rgns )
   129 	return -1;
   130     for( i=0; i<len; i++ ) {
   131 	fread_string( tmp, sizeof(tmp), f );
   132 	fread( &base, sizeof(base), 1, f );
   133 	fread( &flags, sizeof(flags), 1, f );
   134 	fread( &size, sizeof(size), 1, f );
   135 	if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
   136 	    base != mem_rgn[i].base ||
   137 	    flags != mem_rgn[i].flags ||
   138 	    size != mem_rgn[i].size ) {
   139 	    ERROR( "Bad memory region %d %s", i, tmp );
   140 	    return -1;
   141 	}
   142 	if( flags != MEM_FLAG_ROM )
   143 	    fread_gzip( mem_rgn[i].mem, size, 1, f );
   144     }
   146     /* All MMIO regions */
   147     fread( &len, sizeof(len), 1, f );
   148     if( len != num_io_rgns ) 
   149 	return -1;
   150     for( i=0; i<len; i++ ) {
   151 	fread_string( tmp, sizeof(tmp), f );
   152 	fread( &base, sizeof(base), 1, f );
   153 	fread( &size, sizeof(size), 1, f );
   154 	if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   155 	    base != io_rgn[i]->base ||
   156 	    size != 4096 ) {
   157 	    ERROR( "Bad MMIO region %d %s", i, tmp );
   158 	    return -1;
   159 	}
   160 	fread_gzip( io_rgn[i]->mem, size, 1, f );
   161     }
   162     return 0;
   163 }
   165 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   166 {
   167     sh4ptr_t region;
   168     int len = 4096, total = 0;
   169     uint32_t addr = start;
   170     FILE *f = fopen(file,"w");
   172     if( f == NULL )
   173 	return errno;
   175     while( total < length ) {
   176 	region = mem_get_region(addr);
   177 	len = 4096 - (addr & 0x0FFF);
   178 	if( len > (length-total) ) 
   179 	    len = (length-total);
   180 	if( fwrite( region, len, 1, f ) != 1 ) {
   181 	    ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
   182 	    break;
   183 	}
   185 	addr += len;
   186 	total += len;
   187     }
   188     fclose( f );
   189     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   190     return 0;
   191 }
   193 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   194 {
   195     sh4ptr_t region;
   196     int len = 4096, total = 0;
   197     uint32_t addr = start;
   198     struct stat st;
   199     FILE *f = fopen(file,"r");
   201     if( f == NULL ) {
   202 	WARN( "Unable to load file '%s': %s", file, strerror(errno) );
   203 	return -1;
   204     }
   205     fstat( fileno(f), &st );
   206     if( length == 0 || length == -1 || length > st.st_size )
   207 	length = st.st_size;
   209     while( total < length ) {
   210 	region = mem_get_region(addr);
   211 	len = 4096 - (addr & 0x0FFF);
   212 	if( len > (length-total) ) 
   213 	    len = (length-total);
   214 	if( fread( region, len, 1, f ) != 1 ) {
   215 	    ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
   216 	    break;
   217 	}
   219 	addr += len;
   220 	total += len;
   221     }
   222     fclose( f );
   223     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   224     return 0;
   225 }
   227 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   228                                    const char *name, int flags, uint32_t repeat_offset,
   229 				   uint32_t repeat_until )
   230 {
   231     int i;
   232     mem_rgn[num_mem_rgns].base = base;
   233     mem_rgn[num_mem_rgns].size = size;
   234     mem_rgn[num_mem_rgns].flags = flags;
   235     mem_rgn[num_mem_rgns].name = name;
   236     mem_rgn[num_mem_rgns].mem = mem;
   237     num_mem_rgns++;
   239     do {
   240 	for( i=0; i<size>>PAGE_BITS; i++ )
   241 	    page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
   242 	base += repeat_offset;	
   243     } while( base <= repeat_until );
   245     return &mem_rgn[num_mem_rgns-1];
   246 }
   248 void *mem_create_ram_region( uint32_t base, uint32_t size, const char *name )
   249 {
   250     return mem_create_repeating_ram_region( base, size, name, size, base );
   251 }
   253 void *mem_create_repeating_ram_region( uint32_t base, uint32_t size, const char *name,
   254 				       uint32_t repeat_offset, uint32_t repeat_until )
   255 {
   256     char *mem;
   258     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   259     assert( (size&0x00000FFF) == 0 );
   260     assert( num_mem_rgns < MAX_MEM_REGIONS );
   261     assert( page_map != NULL );
   263     mem = mem_alloc_pages( size>>PAGE_BITS );
   265     mem_map_region( mem, base, size, name, MEM_FLAG_RAM, repeat_offset, repeat_until );
   267     return mem;
   268 }
   270 gboolean mem_load_rom( const gchar *file, uint32_t base, uint32_t size, uint32_t crc,
   271 		       const gchar *region_name )
   272 {
   273     sh4ptr_t mem;
   274     uint32_t calc_crc;
   275     int status;
   277     mem = mem_get_region(base);
   278     if( mem == NULL ) {
   279 	mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0 );
   280 	if( mem == MAP_FAILED ) {
   281 	    ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
   282 	    return FALSE;
   283 	}
   284 	mem_map_region( mem, base, size, region_name, MEM_FLAG_ROM, size, base );
   285     } else {
   286 	mprotect( mem, size, PROT_READ|PROT_WRITE );
   287     }
   289     status = mem_load_block( file, base, size );
   290     mprotect( mem, size, PROT_READ );
   292     if( status == 0 ) {
   293 	/* CRC check only if we loaded something */
   294 	calc_crc = crc32(0L, (sh4ptr_t)mem, size);
   295 	if( calc_crc != crc ) {
   296 	    WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   297 		  file, calc_crc, crc);
   298 	}
   299 	return TRUE;
   300     }
   302     return FALSE;
   303 }
   305 sh4ptr_t mem_get_region_by_name( const char *name )
   306 {
   307     int i;
   308     for( i=0; i<num_mem_rgns; i++ ) {
   309         if( strcmp( mem_rgn[i].name, name ) == 0 )
   310             return mem_rgn[i].mem;
   311     }
   312     return NULL;
   313 }
   315 void register_io_region( struct mmio_region *io )
   316 {
   317     int i;
   319     assert(io);
   320     io->mem = mem_alloc_pages(2);
   321     io->save_mem = io->mem + PAGE_SIZE;
   322     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   323     io->trace_flag = 0;
   324     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   325     for( i=0; io->ports[i].id != NULL; i++ ) {
   326         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   327         *io->ports[i].val = io->ports[i].def_val;
   328         io->index[io->ports[i].offset>>2] = &io->ports[i];
   329     }
   330     memcpy( io->save_mem, io->mem, PAGE_SIZE );
   331     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   332         /* P4 area (on-chip I/O channels */
   333         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   334     } else {
   335         page_map[io->base>>12] = (sh4ptr_t)(uintptr_t)num_io_rgns;
   336     }
   337     io_rgn[num_io_rgns] = io;
   338     num_io_rgns++;
   339 }
   341 void register_io_regions( struct mmio_region **io )
   342 {
   343     while( *io ) register_io_region( *io++ );
   344 }
   346 int mem_has_page( uint32_t addr )
   347 {
   348     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   349     return page != NULL;
   350 }
   352 sh4ptr_t mem_get_page( uint32_t addr )
   353 {
   354     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   355     return page;
   356 }
   358 sh4ptr_t mem_get_region( uint32_t addr )
   359 {
   360     sh4ptr_t page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   361     if( ((uintptr_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   362         return NULL;
   363     } else {
   364         return page+(addr&0xFFF);
   365     }
   366 }
   368 void mem_write_long( sh4addr_t addr, uint32_t value )
   369 {
   370     sh4ptr_t ptr = mem_get_region(addr);
   371     assert(ptr != NULL);
   372     *((uint32_t *)ptr) = value;
   373 }
   375 struct mmio_region *mem_get_io_region( uint32_t addr )
   376 {
   377     if( addr > 0xFF000000 ) {
   378 	return P4_io[(addr&0x00FFFFFF)>>12];
   379     }
   380     sh4ptr_t page = page_map[(addr&0x1FFFFFFF)>>12];
   381     if( ((uintptr_t)page) < MAX_IO_REGIONS ) {
   382 	return io_rgn[(uintptr_t)page];
   383     } else {
   384 	return NULL;
   385     }
   386 }
   388 struct mmio_region *mem_get_io_region_by_name( const gchar *name )
   389 {
   390     int i;
   391     for( i=0; i<num_io_rgns; i++ ) {
   392 	if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
   393 	    return io_rgn[i];
   394 	}
   395     }
   396     return NULL;
   397 }
   399 void mem_set_trace( const gchar *tracelist, gboolean flag )
   400 {
   401     if( tracelist != NULL ) {
   402 	gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
   403 	int i;
   404 	for( i=0; tracev[i] != NULL; i++ ) {
   405 	    // Special case "all" - trace everything
   406 	    if( strcasecmp(tracev[i], "all") == 0 ) {
   407 		int j;
   408 		for( j=0; j<num_io_rgns; j++ ) {
   409 		    io_rgn[j]->trace_flag = flag ? 1 : 0;
   410 		}
   411 		break;
   412 	    }
   413 	    struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
   414 	    if( region == NULL ) {
   415 		WARN( "Unknown IO region '%s'", tracev[i] );
   416 	    } else {
   417 		region->trace_flag = flag ? 1 : 0;
   418 	    }
   419 	}
   420 	g_strfreev( tracev );
   421     }
   422 }
.