Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 18:9a1b5d75703f
prev17:944f75eea496
next19:9da7a8e38f9d
author nkeynes
date Thu Dec 15 13:33:14 2005 +0000 (15 years ago)
permissions -rw-r--r--
last change Implement save states
view annotate diff log raw
     1 /**
     2  * $Id: mem.c,v 1.5 2005-12-15 13:33:14 nkeynes Exp $
     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  */
    19 #include <sys/mman.h>
    20 #include <assert.h>
    21 #include <stdint.h>
    22 #include <stdlib.h>
    23 #include <stdio.h>
    24 #include <unistd.h>
    25 #include <fcntl.h>
    26 #include <errno.h>
    27 #include <string.h>
    28 #include <zlib.h>
    29 #include "dream.h"
    30 #include "mem.h"
    31 #include "mmio.h"
    32 #include "modules.h"
    33 #include "dreamcast.h"
    35 char **page_map = NULL;
    37 int mem_load(FILE *f);
    38 void mem_save(FILE *f);
    39 struct dreamcast_module mem_module =
    40     { "MEM", mem_init, mem_reset, NULL, NULL, mem_save, mem_load };
    42 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    43 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    44 struct mmio_region *P4_io[4096];
    46 int num_io_rgns = 0, num_mem_rgns = 0;
    48 void *mem_alloc_pages( int n )
    49 {
    50     void *mem = mmap( NULL, n * 4096,
    51                       PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
    52     if( mem == MAP_FAILED ) {
    53         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    54         return NULL;
    55     }
    56     return mem;
    57 }
    60 void mem_init( void )
    61 {
    62     page_map = mmap( NULL, sizeof(char *) * PAGE_TABLE_ENTRIES,
    63                      PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
    64     if( page_map == MAP_FAILED ) {
    65         ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
    66         page_map = NULL;
    67         return;
    68     }
    70     memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
    71 }
    73 void mem_reset( void )
    74 {
    75     /* Restore all mmio registers to their initial settings */
    76     int i, j;
    77     for( i=1; i<num_io_rgns; i++ ) {
    78         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
    79             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
    80                 io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
    81                 io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
    82                                     io_rgn[i]->ports[j].def_val );
    83             }
    84         }
    85     }
    86 }
    88 void mem_save( FILE *f ) 
    89 {
    90     int i;
    91     uint32_t len;
    93     /* All memory regions */
    94     fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
    95     for( i=0; i<num_mem_rgns; i++ ) {
    96 	fwrite_string( mem_rgn[i].name, f );
    97 	fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
    98 	fwrite( &mem_rgn[i].flags, sizeof(int), 1, f );
    99 	fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   100 	if( mem_rgn[i].flags != MEM_FLAG_ROM )
   101 	    fwrite( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   102     }
   104     /* All MMIO regions */
   105     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   106     for( i=0; i<num_io_rgns; i++ ) {
   107 	fwrite_string( io_rgn[i]->id, f );
   108 	fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   109 	len = 4096;
   110 	fwrite( &len, sizeof(len), 1, f );
   111 	fwrite( io_rgn[i]->mem, len, 1, f );
   112     }
   113 }
   115 int mem_load( FILE *f )
   116 {
   117     char tmp[64];
   118     uint32_t len;
   119     uint32_t base, size;
   120     int flags;
   121     int i;
   123     /* All memory regions */
   124     fread( &len, sizeof(len), 1, f );
   125     if( len != num_mem_rgns )
   126 	return -1;
   127     for( i=0; i<len; i++ ) {
   128 	fread_string( tmp, sizeof(tmp), f );
   129 	fread( &base, sizeof(base), 1, f );
   130 	fread( &flags, sizeof(flags), 1, f );
   131 	fread( &size, sizeof(size), 1, f );
   132 	if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
   133 	    base != mem_rgn[i].base ||
   134 	    flags != mem_rgn[i].flags ||
   135 	    size != mem_rgn[i].size ) {
   136 	    ERROR( "Bad memory region %d %s", i, tmp );
   137 	    return -1;
   138 	}
   139 	if( flags != MEM_FLAG_ROM )
   140 	    fread( mem_rgn[i].mem, size, 1, f );
   141     }
   143     /* All MMIO regions */
   144     fread( &len, sizeof(len), 1, f );
   145     if( len != num_io_rgns ) 
   146 	return -1;
   147     for( i=0; i<len; i++ ) {
   148 	fread_string( tmp, sizeof(tmp), f );
   149 	fread( &base, sizeof(base), 1, f );
   150 	fread( &size, sizeof(size), 1, f );
   151 	if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   152 	    base != io_rgn[i]->base ||
   153 	    size != 4096 ) {
   154 	    ERROR( "Bad MMIO region %d %s", i, tmp );
   155 	    return -1;
   156 	}
   157 	fread( io_rgn[i]->mem, size, 1, f );
   158     }
   159     return 0;
   160 }
   162 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   163                                    char *name, int flags )
   164 {
   165     int i;
   166     mem_rgn[num_mem_rgns].base = base;
   167     mem_rgn[num_mem_rgns].size = size;
   168     mem_rgn[num_mem_rgns].flags = flags;
   169     mem_rgn[num_mem_rgns].name = name;
   170     mem_rgn[num_mem_rgns].mem = mem;
   171     num_mem_rgns++;
   173     for( i=0; i<size>>PAGE_BITS; i++ )
   174         page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
   176     return &mem_rgn[num_mem_rgns-1];
   177 }
   179 void *mem_create_ram_region( uint32_t base, uint32_t size, char *name )
   180 {
   181     char *mem;
   183     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   184     assert( (size&0x00000FFF) == 0 );
   185     assert( num_mem_rgns < MAX_MEM_REGIONS );
   186     assert( page_map != NULL );
   188     mem = mem_alloc_pages( size>>PAGE_BITS );
   190     mem_map_region( mem, base, size, name, MEM_FLAG_RAM );
   191     return mem;
   192 }
   194 void *mem_load_rom( char *file, uint32_t base, uint32_t size, uint32_t crc )
   195 {
   196     char buf[512], *mem;
   197     int fd;
   198     uint32_t calc_crc;
   199     snprintf( buf, 512, "%s/%s",BIOS_PATH, file );
   200     fd = open( buf, O_RDONLY );
   201     if( fd == -1 ) {
   202         ERROR( "Bios file not found: %s", buf );
   203         return NULL;
   204     }
   205     mem = mmap( NULL, size, PROT_READ, MAP_PRIVATE, fd, 0 );
   206     if( mem == MAP_FAILED ) {
   207         ERROR( "Unable to map bios file: %s (%s)", file, strerror(errno) );
   208         close(fd);
   209         return NULL;
   210     }
   211     mem_map_region( mem, base, size, file, MEM_FLAG_ROM );
   213     /* CRC check */
   214     calc_crc = crc32(0L, mem, size);
   215     if( calc_crc != crc ) {
   216         WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   217               file, calc_crc, crc);
   218     }
   220     return mem;
   221 }
   223 char *mem_get_region_by_name( char *name )
   224 {
   225     int i;
   226     for( i=0; i<num_mem_rgns; i++ ) {
   227         if( strcmp( mem_rgn[i].name, name ) == 0 )
   228             return mem_rgn[i].mem;
   229     }
   230     return NULL;
   231 }
   233 void register_io_region( struct mmio_region *io )
   234 {
   235     int i;
   237     assert(io);
   238     io->mem = mem_alloc_pages(2);
   239     io->save_mem = io->mem + PAGE_SIZE;
   240     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   241     io->trace_flag = 1;
   242     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   243     for( i=0; io->ports[i].id != NULL; i++ ) {
   244         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   245         *io->ports[i].val = io->ports[i].def_val;
   246         io->index[io->ports[i].offset>>2] = &io->ports[i];
   247     }
   248     memcpy( io->save_mem, io->mem, PAGE_SIZE );
   249     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   250         /* P4 area (on-chip I/O channels */
   251         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   252     } else {
   253         page_map[io->base>>12] = (char *)num_io_rgns;
   254     }
   255     io_rgn[num_io_rgns] = io;
   256     num_io_rgns++;
   257 }
   259 void register_io_regions( struct mmio_region **io )
   260 {
   261     while( *io ) register_io_region( *io++ );
   262 }
   264 int mem_has_page( uint32_t addr )
   265 {
   266     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   267     return page != NULL;
   268 }
   270 char *mem_get_page( uint32_t addr )
   271 {
   272     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   273     return page;
   274 }
   276 char *mem_get_region( uint32_t addr )
   277 {
   278     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   279     if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   280         return NULL;
   281     } else {
   282         return page+(addr&0xFFF);
   283     }
   284 }
.