Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 17:944f75eea496
prev15:5194dd0fdb60
next18:9a1b5d75703f
author nkeynes
date Tue Dec 13 14:47:59 2005 +0000 (15 years ago)
permissions -rw-r--r--
last change More work on load/save state - save state a little more structured now
Memory save now in
view annotate diff log raw
     1 /**
     2  * $Id: mem.c,v 1.4 2005-12-13 14:47:59 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 = 1, 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 	fwrite( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   101     }
   103     /* All MMIO regions */
   104     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   105     for( i=0; i<num_io_rgns; i++ ) {
   106 	fwrite_string( io_rgn[i]->id, f );
   107 	fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   108 	fwrite( io_rgn[i]->mem, 4096, 1, f );
   109     }
   110 }
   112 int mem_load( FILE *f )
   113 {
   114     char tmp[64];
   115     uint32_t len;
   116     int i;
   118     /* All memory regions */
   120 }
   122 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   123                                    char *name, int flags )
   124 {
   125     int i;
   126     mem_rgn[num_mem_rgns].base = base;
   127     mem_rgn[num_mem_rgns].size = size;
   128     mem_rgn[num_mem_rgns].flags = flags;
   129     mem_rgn[num_mem_rgns].name = name;
   130     mem_rgn[num_mem_rgns].mem = mem;
   131     num_mem_rgns++;
   133     for( i=0; i<size>>PAGE_BITS; i++ )
   134         page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
   136     return &mem_rgn[num_mem_rgns-1];
   137 }
   139 void *mem_create_ram_region( uint32_t base, uint32_t size, char *name )
   140 {
   141     char *mem;
   143     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   144     assert( (size&0x00000FFF) == 0 );
   145     assert( num_mem_rgns < MAX_MEM_REGIONS );
   146     assert( page_map != NULL );
   148     mem = mem_alloc_pages( size>>PAGE_BITS );
   150     mem_map_region( mem, base, size, name, 6 );
   151     return mem;
   152 }
   154 void *mem_load_rom( char *file, uint32_t base, uint32_t size, uint32_t crc )
   155 {
   156     char buf[512], *mem;
   157     int fd;
   158     uint32_t calc_crc;
   159     snprintf( buf, 512, "%s/%s",BIOS_PATH, file );
   160     fd = open( buf, O_RDONLY );
   161     if( fd == -1 ) {
   162         ERROR( "Bios file not found: %s", buf );
   163         return NULL;
   164     }
   165     mem = mmap( NULL, size, PROT_READ, MAP_PRIVATE, fd, 0 );
   166     if( mem == MAP_FAILED ) {
   167         ERROR( "Unable to map bios file: %s (%s)", file, strerror(errno) );
   168         close(fd);
   169         return NULL;
   170     }
   171     mem_map_region( mem, base, size, file, 4 );
   173     /* CRC check */
   174     calc_crc = crc32(0L, mem, size);
   175     if( calc_crc != crc ) {
   176         WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   177               file, calc_crc, crc);
   178     }
   179     return mem;
   180 }
   182 char *mem_get_region_by_name( char *name )
   183 {
   184     int i;
   185     for( i=0; i<num_mem_rgns; i++ ) {
   186         if( strcmp( mem_rgn[i].name, name ) == 0 )
   187             return mem_rgn[i].mem;
   188     }
   189     return NULL;
   190 }
   192 void register_io_region( struct mmio_region *io )
   193 {
   194     int i;
   196     assert(io);
   197     io->mem = mem_alloc_pages(2);
   198     io->save_mem = io->mem + PAGE_SIZE;
   199     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   200     io->trace_flag = 1;
   201     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   202     for( i=0; io->ports[i].id != NULL; i++ ) {
   203         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   204         *io->ports[i].val = io->ports[i].def_val;
   205         io->index[io->ports[i].offset>>2] = &io->ports[i];
   206     }
   207     memcpy( io->save_mem, io->mem, PAGE_SIZE );
   208     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   209         /* P4 area (on-chip I/O channels */
   210         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   211     } else {
   212         page_map[io->base>>12] = (char *)num_io_rgns;
   213     }
   214     io_rgn[num_io_rgns] = io;
   215     num_io_rgns++;
   216 }
   218 void register_io_regions( struct mmio_region **io )
   219 {
   220     while( *io ) register_io_region( *io++ );
   221 }
   223 int mem_has_page( uint32_t addr )
   224 {
   225     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   226     return page != NULL;
   227 }
   229 char *mem_get_page( uint32_t addr )
   230 {
   231     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   232     return page;
   233 }
   235 char *mem_get_region( uint32_t addr )
   236 {
   237     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   238     if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   239         return NULL;
   240     } else {
   241         return page+(addr&0xFFF);
   242     }
   243 }
.