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.
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.
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 )
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) );
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) );
68 memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
71 void mem_reset( void )
73 /* Restore all mmio registers to their initial settings */
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 );
86 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
87 char *name, int flags )
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;
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];
103 void *mem_create_ram_region( uint32_t base, uint32_t size, char *name )
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 );
118 void *mem_load_rom( char *file, uint32_t base, uint32_t size, uint32_t crc )
123 snprintf( buf, 512, "%s/%s",BIOS_PATH, file );
124 fd = open( buf, O_RDONLY );
126 ERROR( "Bios file not found: %s", buf );
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) );
135 mem_map_region( mem, base, size, file, 4 );
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);
146 char *mem_get_region_by_name( char *name )
149 for( i=0; i<num_mem_rgns; i++ ) {
150 if( strcmp( mem_rgn[i].name, name ) == 0 )
151 return mem_rgn[i].mem;
156 void register_io_region( struct mmio_region *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 *));
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];
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;
176 page_map[io->base>>12] = (char *)num_io_rgns;
178 io_rgn[num_io_rgns] = io;
182 void register_io_regions( struct mmio_region **io )
184 while( *io ) register_io_region( *io++ );
187 int mem_has_page( uint32_t addr )
189 char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
193 char *mem_get_page( uint32_t addr )
195 char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
199 char *mem_get_region( uint32_t addr )
201 char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
202 if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
205 return page+(addr&0xFFF);
.