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 (13 years ago)
permissions -rw-r--r--
last change Add dreamcast_module module structure
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@15
     2
 * $Id: mem.c,v 1.3 2005-12-12 13:11:07 nkeynes Exp $
nkeynes@10
     3
 * mem.c is responsible for creating and maintaining the overall system memory
nkeynes@10
     4
 * map, as visible from the SH4 processor. 
nkeynes@10
     5
 *
nkeynes@10
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@10
     7
 *
nkeynes@10
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@10
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@10
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@10
    11
 * (at your option) any later version.
nkeynes@10
    12
 *
nkeynes@10
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@10
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@10
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@10
    16
 * GNU General Public License for more details.
nkeynes@10
    17
 */
nkeynes@10
    18
nkeynes@10
    19
#include <sys/mman.h>
nkeynes@10
    20
#include <assert.h>
nkeynes@10
    21
#include <stdint.h>
nkeynes@10
    22
#include <stdlib.h>
nkeynes@10
    23
#include <stdio.h>
nkeynes@10
    24
#include <unistd.h>
nkeynes@10
    25
#include <fcntl.h>
nkeynes@10
    26
#include <errno.h>
nkeynes@10
    27
#include <string.h>
nkeynes@10
    28
#include <zlib.h>
nkeynes@10
    29
#include "dream.h"
nkeynes@10
    30
#include "mem.h"
nkeynes@10
    31
#include "mmio.h"
nkeynes@15
    32
#include "modules.h"
nkeynes@10
    33
#include "dreamcast.h"
nkeynes@10
    34
nkeynes@10
    35
char **page_map = NULL;
nkeynes@10
    36
nkeynes@15
    37
struct dreamcast_module mem_module =
nkeynes@15
    38
    { "MEM", mem_init, mem_reset, NULL, NULL, NULL, NULL };
nkeynes@15
    39
nkeynes@10
    40
struct mem_region mem_rgn[MAX_MEM_REGIONS];
nkeynes@10
    41
struct mmio_region *io_rgn[MAX_IO_REGIONS];
nkeynes@10
    42
struct mmio_region *P4_io[4096];
nkeynes@10
    43
nkeynes@10
    44
int num_io_rgns = 1, num_mem_rgns = 0;
nkeynes@10
    45
nkeynes@10
    46
void *mem_alloc_pages( int n )
nkeynes@10
    47
{
nkeynes@10
    48
    void *mem = mmap( NULL, n * 4096,
nkeynes@10
    49
                      PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
nkeynes@10
    50
    if( mem == MAP_FAILED ) {
nkeynes@10
    51
        ERROR( "Memory allocation failure! (%s)", strerror(errno) );
nkeynes@10
    52
        return NULL;
nkeynes@10
    53
    }
nkeynes@10
    54
    return mem;
nkeynes@10
    55
}
nkeynes@10
    56
nkeynes@10
    57
nkeynes@10
    58
void mem_init( void )
nkeynes@10
    59
{
nkeynes@10
    60
    page_map = mmap( NULL, sizeof(char *) * PAGE_TABLE_ENTRIES,
nkeynes@10
    61
                     PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0 );
nkeynes@10
    62
    if( page_map == MAP_FAILED ) {
nkeynes@10
    63
        ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
nkeynes@10
    64
        page_map = NULL;
nkeynes@10
    65
        return;
nkeynes@10
    66
    }
nkeynes@10
    67
nkeynes@10
    68
    memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
nkeynes@10
    69
}
nkeynes@10
    70
nkeynes@10
    71
void mem_reset( void )
nkeynes@10
    72
{
nkeynes@10
    73
    /* Restore all mmio registers to their initial settings */
nkeynes@10
    74
    int i, j;
nkeynes@10
    75
    for( i=1; i<num_io_rgns; i++ ) {
nkeynes@10
    76
        for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
nkeynes@10
    77
            if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
nkeynes@10
    78
                io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
nkeynes@10
    79
                io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
nkeynes@10
    80
                                    io_rgn[i]->ports[j].def_val );
nkeynes@10
    81
            }
nkeynes@10
    82
        }
nkeynes@10
    83
    }
nkeynes@10
    84
}
nkeynes@10
    85
nkeynes@10
    86
struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
nkeynes@10
    87
                                   char *name, int flags )
nkeynes@10
    88
{
nkeynes@10
    89
    int i;
nkeynes@10
    90
    mem_rgn[num_mem_rgns].base = base;
nkeynes@10
    91
    mem_rgn[num_mem_rgns].size = size;
nkeynes@10
    92
    mem_rgn[num_mem_rgns].flags = flags;
nkeynes@10
    93
    mem_rgn[num_mem_rgns].name = name;
nkeynes@10
    94
    mem_rgn[num_mem_rgns].mem = mem;
nkeynes@10
    95
    num_mem_rgns++;
nkeynes@10
    96
nkeynes@10
    97
    for( i=0; i<size>>PAGE_BITS; i++ )
nkeynes@10
    98
        page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
nkeynes@10
    99
nkeynes@10
   100
    return &mem_rgn[num_mem_rgns-1];
nkeynes@10
   101
}
nkeynes@10
   102
nkeynes@10
   103
void *mem_create_ram_region( uint32_t base, uint32_t size, char *name )
nkeynes@10
   104
{
nkeynes@10
   105
    char *mem;
nkeynes@10
   106
    
nkeynes@10
   107
    assert( (base&0xFFFFF000) == base ); /* must be page aligned */
nkeynes@10
   108
    assert( (size&0x00000FFF) == 0 );
nkeynes@10
   109
    assert( num_mem_rgns < MAX_MEM_REGIONS );
nkeynes@10
   110
    assert( page_map != NULL );
nkeynes@10
   111
nkeynes@10
   112
    mem = mem_alloc_pages( size>>PAGE_BITS );
nkeynes@10
   113
nkeynes@10
   114
    mem_map_region( mem, base, size, name, 6 );
nkeynes@10
   115
    return mem;
nkeynes@10
   116
}
nkeynes@10
   117
nkeynes@10
   118
void *mem_load_rom( char *file, uint32_t base, uint32_t size, uint32_t crc )
nkeynes@10
   119
{
nkeynes@10
   120
    char buf[512], *mem;
nkeynes@10
   121
    int fd;
nkeynes@10
   122
    uint32_t calc_crc;
nkeynes@10
   123
    snprintf( buf, 512, "%s/%s",BIOS_PATH, file );
nkeynes@10
   124
    fd = open( buf, O_RDONLY );
nkeynes@10
   125
    if( fd == -1 ) {
nkeynes@10
   126
        ERROR( "Bios file not found: %s", buf );
nkeynes@10
   127
        return NULL;
nkeynes@10
   128
    }
nkeynes@10
   129
    mem = mmap( NULL, size, PROT_READ, MAP_PRIVATE, fd, 0 );
nkeynes@10
   130
    if( mem == MAP_FAILED ) {
nkeynes@10
   131
        ERROR( "Unable to map bios file: %s (%s)", file, strerror(errno) );
nkeynes@10
   132
        close(fd);
nkeynes@10
   133
        return NULL;
nkeynes@10
   134
    }
nkeynes@10
   135
    mem_map_region( mem, base, size, file, 4 );
nkeynes@10
   136
nkeynes@10
   137
    /* CRC check */
nkeynes@10
   138
    calc_crc = crc32(0L, mem, size);
nkeynes@10
   139
    if( calc_crc != crc ) {
nkeynes@10
   140
        WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
nkeynes@10
   141
              file, calc_crc, crc);
nkeynes@10
   142
    }
nkeynes@10
   143
    return mem;
nkeynes@10
   144
}
nkeynes@10
   145
nkeynes@10
   146
char *mem_get_region_by_name( char *name )
nkeynes@10
   147
{
nkeynes@10
   148
    int i;
nkeynes@10
   149
    for( i=0; i<num_mem_rgns; i++ ) {
nkeynes@10
   150
        if( strcmp( mem_rgn[i].name, name ) == 0 )
nkeynes@10
   151
            return mem_rgn[i].mem;
nkeynes@10
   152
    }
nkeynes@10
   153
    return NULL;
nkeynes@10
   154
}
nkeynes@10
   155
nkeynes@10
   156
void register_io_region( struct mmio_region *io )
nkeynes@10
   157
{
nkeynes@10
   158
    int i;
nkeynes@10
   159
    
nkeynes@10
   160
    assert(io);
nkeynes@10
   161
    io->mem = mem_alloc_pages(2);
nkeynes@10
   162
    io->save_mem = io->mem + PAGE_SIZE;
nkeynes@10
   163
    io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
nkeynes@10
   164
    io->trace_flag = 1;
nkeynes@10
   165
    memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
nkeynes@10
   166
    for( i=0; io->ports[i].id != NULL; i++ ) {
nkeynes@10
   167
        io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
nkeynes@10
   168
        *io->ports[i].val = io->ports[i].def_val;
nkeynes@10
   169
        io->index[io->ports[i].offset>>2] = &io->ports[i];
nkeynes@10
   170
    }
nkeynes@10
   171
    memcpy( io->save_mem, io->mem, PAGE_SIZE );
nkeynes@10
   172
    if( (io->base & 0xFF000000) == 0xFF000000 ) {
nkeynes@10
   173
        /* P4 area (on-chip I/O channels */
nkeynes@10
   174
        P4_io[(io->base&0x1FFFFFFF)>>19] = io;
nkeynes@10
   175
    } else {
nkeynes@10
   176
        page_map[io->base>>12] = (char *)num_io_rgns;
nkeynes@10
   177
    }
nkeynes@10
   178
    io_rgn[num_io_rgns] = io;
nkeynes@10
   179
    num_io_rgns++;
nkeynes@10
   180
}
nkeynes@10
   181
nkeynes@10
   182
void register_io_regions( struct mmio_region **io )
nkeynes@10
   183
{
nkeynes@10
   184
    while( *io ) register_io_region( *io++ );
nkeynes@10
   185
}
nkeynes@10
   186
nkeynes@10
   187
int mem_has_page( uint32_t addr )
nkeynes@10
   188
{
nkeynes@10
   189
    char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   190
    return page != NULL;
nkeynes@10
   191
}
nkeynes@10
   192
nkeynes@10
   193
char *mem_get_page( uint32_t addr )
nkeynes@10
   194
{
nkeynes@10
   195
    char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   196
    return page;
nkeynes@10
   197
}
nkeynes@10
   198
nkeynes@10
   199
char *mem_get_region( uint32_t addr )
nkeynes@10
   200
{
nkeynes@10
   201
    char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
nkeynes@10
   202
    if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
nkeynes@10
   203
        return NULL;
nkeynes@10
   204
    } else {
nkeynes@10
   205
        return page+(addr&0xFFF);
nkeynes@10
   206
    }
nkeynes@10
   207
}
nkeynes@11
   208
.