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.
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
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 )
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) );
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) );
64 memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
67 void mem_reset( void )
69 /* Restore all mmio registers to their initial settings */
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 );
82 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
83 char *name, int flags )
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;
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];
99 void *mem_create_ram_region( uint32_t base, uint32_t size, char *name )
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 );
114 void *mem_load_rom( char *file, uint32_t base, uint32_t size, uint32_t crc )
119 snprintf( buf, 512, "%s/%s",BIOS_PATH, file );
120 fd = open( buf, O_RDONLY );
122 ERROR( "Bios file not found: %s", buf );
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) );
131 mem_map_region( mem, base, size, file, 4 );
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);
142 char *mem_get_region_by_name( char *name )
145 for( i=0; i<num_mem_rgns; i++ ) {
146 if( strcmp( mem_rgn[i].name, name ) == 0 )
147 return mem_rgn[i].mem;
152 void register_io_region( struct mmio_region *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 *));
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];
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;
172 page_map[io->base>>12] = (char *)num_io_rgns;
174 io_rgn[num_io_rgns] = io;
178 void register_io_regions( struct mmio_region **io )
180 while( *io ) register_io_region( *io++ );
183 int mem_has_page( uint32_t addr )
185 char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
189 char *mem_get_page( uint32_t addr )
191 char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
195 char *mem_get_region( uint32_t addr )
197 char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
198 if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
201 return page+(addr&0xFFF);
.