Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 15:5194dd0fdb60
prev11:0a82ef380c45
next17:944f75eea496
author nkeynes
date Mon Dec 12 13:11:11 2005 +0000 (15 years ago)
permissions -rw-r--r--
last change Add dreamcast_module module structure
view annotate diff log raw
     1 /**
     2  * $Id: mem.c,v 1.3 2005-12-12 13:11:07 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 struct dreamcast_module mem_module =
    38     { "MEM", mem_init, mem_reset, NULL, NULL, NULL, NULL };
    40 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    41 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    42 struct mmio_region *P4_io[4096];
    44 int num_io_rgns = 1, num_mem_rgns = 0;
    46 void *mem_alloc_pages( int n )
    47 {
    48     void *mem = mmap( NULL, n * 4096,
    49                       PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
    50     if( mem == MAP_FAILED ) {
    51         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    52         return NULL;
    53     }
    54     return mem;
    55 }
    58 void mem_init( void )
    59 {
    60     page_map = mmap( NULL, sizeof(char *) * PAGE_TABLE_ENTRIES,
    61                      PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
    62     if( page_map == MAP_FAILED ) {
    63         ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
    64         page_map = NULL;
    65         return;
    66     }
    68     memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
    69 }
    71 void mem_reset( void )
    72 {
    73     /* Restore all mmio registers to their initial settings */
    74     int i, j;
    75     for( i=1; i<num_io_rgns; i++ ) {
    76         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
    77             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
    78                 io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
    79                 io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
    80                                     io_rgn[i]->ports[j].def_val );
    81             }
    82         }
    83     }
    84 }
    86 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
    87                                    char *name, int flags )
    88 {
    89     int i;
    90     mem_rgn[num_mem_rgns].base = base;
    91     mem_rgn[num_mem_rgns].size = size;
    92     mem_rgn[num_mem_rgns].flags = flags;
    93     mem_rgn[num_mem_rgns].name = name;
    94     mem_rgn[num_mem_rgns].mem = mem;
    95     num_mem_rgns++;
    97     for( i=0; i<size>>PAGE_BITS; i++ )
    98         page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
   100     return &mem_rgn[num_mem_rgns-1];
   101 }
   103 void *mem_create_ram_region( uint32_t base, uint32_t size, char *name )
   104 {
   105     char *mem;
   107     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   108     assert( (size&0x00000FFF) == 0 );
   109     assert( num_mem_rgns < MAX_MEM_REGIONS );
   110     assert( page_map != NULL );
   112     mem = mem_alloc_pages( size>>PAGE_BITS );
   114     mem_map_region( mem, base, size, name, 6 );
   115     return mem;
   116 }
   118 void *mem_load_rom( char *file, uint32_t base, uint32_t size, uint32_t crc )
   119 {
   120     char buf[512], *mem;
   121     int fd;
   122     uint32_t calc_crc;
   123     snprintf( buf, 512, "%s/%s",BIOS_PATH, file );
   124     fd = open( buf, O_RDONLY );
   125     if( fd == -1 ) {
   126         ERROR( "Bios file not found: %s", buf );
   127         return NULL;
   128     }
   129     mem = mmap( NULL, size, PROT_READ, MAP_PRIVATE, fd, 0 );
   130     if( mem == MAP_FAILED ) {
   131         ERROR( "Unable to map bios file: %s (%s)", file, strerror(errno) );
   132         close(fd);
   133         return NULL;
   134     }
   135     mem_map_region( mem, base, size, file, 4 );
   137     /* CRC check */
   138     calc_crc = crc32(0L, mem, size);
   139     if( calc_crc != crc ) {
   140         WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   141               file, calc_crc, crc);
   142     }
   143     return mem;
   144 }
   146 char *mem_get_region_by_name( char *name )
   147 {
   148     int i;
   149     for( i=0; i<num_mem_rgns; i++ ) {
   150         if( strcmp( mem_rgn[i].name, name ) == 0 )
   151             return mem_rgn[i].mem;
   152     }
   153     return NULL;
   154 }
   156 void register_io_region( struct mmio_region *io )
   157 {
   158     int i;
   160     assert(io);
   161     io->mem = mem_alloc_pages(2);
   162     io->save_mem = io->mem + PAGE_SIZE;
   163     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   164     io->trace_flag = 1;
   165     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   166     for( i=0; io->ports[i].id != NULL; i++ ) {
   167         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   168         *io->ports[i].val = io->ports[i].def_val;
   169         io->index[io->ports[i].offset>>2] = &io->ports[i];
   170     }
   171     memcpy( io->save_mem, io->mem, PAGE_SIZE );
   172     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   173         /* P4 area (on-chip I/O channels */
   174         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   175     } else {
   176         page_map[io->base>>12] = (char *)num_io_rgns;
   177     }
   178     io_rgn[num_io_rgns] = io;
   179     num_io_rgns++;
   180 }
   182 void register_io_regions( struct mmio_region **io )
   183 {
   184     while( *io ) register_io_region( *io++ );
   185 }
   187 int mem_has_page( uint32_t addr )
   188 {
   189     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   190     return page != NULL;
   191 }
   193 char *mem_get_page( uint32_t addr )
   194 {
   195     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   196     return page;
   197 }
   199 char *mem_get_region( uint32_t addr )
   200 {
   201     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   202     if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   203         return NULL;
   204     } else {
   205         return page+(addr&0xFFF);
   206     }
   207 }
.