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