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