Search
lxdream.org :: lxdream/src/mem.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 736:a02d1475ccfd
prev691:ad3356543392
next743:6f0f79fae449
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
file annotate diff log raw
1.1 --- a/src/mem.c Thu Jun 19 04:40:37 2008 +0000
1.2 +++ b/src/mem.c Mon Jul 14 07:44:42 2008 +0000
1.3 @@ -40,7 +40,7 @@
1.4 int mem_load(FILE *f);
1.5 void mem_save(FILE *f);
1.6 struct dreamcast_module mem_module =
1.7 - { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
1.8 +{ "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
1.9
1.10 struct mem_region mem_rgn[MAX_MEM_REGIONS];
1.11 struct mmio_region *io_rgn[MAX_IO_REGIONS];
1.12 @@ -51,7 +51,7 @@
1.13 void *mem_alloc_pages( int n )
1.14 {
1.15 void *mem = mmap( NULL, n * 4096,
1.16 - PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
1.17 + PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
1.18 if( mem == MAP_FAILED ) {
1.19 ERROR( "Memory allocation failure! (%s)", strerror(errno) );
1.20 return NULL;
1.21 @@ -63,7 +63,7 @@
1.22 void mem_init( void )
1.23 {
1.24 page_map = mmap( NULL, sizeof(sh4ptr_t) * PAGE_TABLE_ENTRIES,
1.25 - PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
1.26 + PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
1.27 if( page_map == MAP_FAILED ) {
1.28 ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
1.29 page_map = NULL;
1.30 @@ -80,9 +80,9 @@
1.31 for( i=1; i<num_io_rgns; i++ ) {
1.32 for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
1.33 if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
1.34 - io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
1.35 + io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
1.36 io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
1.37 - io_rgn[i]->ports[j].def_val );
1.38 + io_rgn[i]->ports[j].def_val );
1.39 }
1.40 }
1.41 }
1.42 @@ -92,26 +92,26 @@
1.43 {
1.44 int i;
1.45 uint32_t len;
1.46 -
1.47 +
1.48 /* All memory regions */
1.49 fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
1.50 for( i=0; i<num_mem_rgns; i++ ) {
1.51 - fwrite_string( mem_rgn[i].name, f );
1.52 - fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
1.53 - fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
1.54 - fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
1.55 - if( mem_rgn[i].flags != MEM_FLAG_ROM )
1.56 - fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
1.57 + fwrite_string( mem_rgn[i].name, f );
1.58 + fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
1.59 + fwrite( &mem_rgn[i].flags, sizeof(uint32_t), 1, f );
1.60 + fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
1.61 + if( mem_rgn[i].flags != MEM_FLAG_ROM )
1.62 + fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
1.63 }
1.64
1.65 /* All MMIO regions */
1.66 fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
1.67 for( i=0; i<num_io_rgns; i++ ) {
1.68 - fwrite_string( io_rgn[i]->id, f );
1.69 - fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
1.70 - len = 4096;
1.71 - fwrite( &len, sizeof(len), 1, f );
1.72 - fwrite_gzip( io_rgn[i]->mem, len, 1, f );
1.73 + fwrite_string( io_rgn[i]->id, f );
1.74 + fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
1.75 + len = 4096;
1.76 + fwrite( &len, sizeof(len), 1, f );
1.77 + fwrite_gzip( io_rgn[i]->mem, len, 1, f );
1.78 }
1.79 }
1.80
1.81 @@ -126,38 +126,38 @@
1.82 /* All memory regions */
1.83 fread( &len, sizeof(len), 1, f );
1.84 if( len != num_mem_rgns )
1.85 - return -1;
1.86 + return -1;
1.87 for( i=0; i<len; i++ ) {
1.88 - fread_string( tmp, sizeof(tmp), f );
1.89 - fread( &base, sizeof(base), 1, f );
1.90 - fread( &flags, sizeof(flags), 1, f );
1.91 - fread( &size, sizeof(size), 1, f );
1.92 - if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
1.93 - base != mem_rgn[i].base ||
1.94 - flags != mem_rgn[i].flags ||
1.95 - size != mem_rgn[i].size ) {
1.96 - ERROR( "Bad memory region %d %s", i, tmp );
1.97 - return -1;
1.98 - }
1.99 - if( flags != MEM_FLAG_ROM )
1.100 - fread_gzip( mem_rgn[i].mem, size, 1, f );
1.101 + fread_string( tmp, sizeof(tmp), f );
1.102 + fread( &base, sizeof(base), 1, f );
1.103 + fread( &flags, sizeof(flags), 1, f );
1.104 + fread( &size, sizeof(size), 1, f );
1.105 + if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
1.106 + base != mem_rgn[i].base ||
1.107 + flags != mem_rgn[i].flags ||
1.108 + size != mem_rgn[i].size ) {
1.109 + ERROR( "Bad memory region %d %s", i, tmp );
1.110 + return -1;
1.111 + }
1.112 + if( flags != MEM_FLAG_ROM )
1.113 + fread_gzip( mem_rgn[i].mem, size, 1, f );
1.114 }
1.115
1.116 /* All MMIO regions */
1.117 fread( &len, sizeof(len), 1, f );
1.118 if( len != num_io_rgns )
1.119 - return -1;
1.120 + return -1;
1.121 for( i=0; i<len; i++ ) {
1.122 - fread_string( tmp, sizeof(tmp), f );
1.123 - fread( &base, sizeof(base), 1, f );
1.124 - fread( &size, sizeof(size), 1, f );
1.125 - if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
1.126 - base != io_rgn[i]->base ||
1.127 - size != 4096 ) {
1.128 - ERROR( "Bad MMIO region %d %s", i, tmp );
1.129 - return -1;
1.130 - }
1.131 - fread_gzip( io_rgn[i]->mem, size, 1, f );
1.132 + fread_string( tmp, sizeof(tmp), f );
1.133 + fread( &base, sizeof(base), 1, f );
1.134 + fread( &size, sizeof(size), 1, f );
1.135 + if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
1.136 + base != io_rgn[i]->base ||
1.137 + size != 4096 ) {
1.138 + ERROR( "Bad MMIO region %d %s", i, tmp );
1.139 + return -1;
1.140 + }
1.141 + fread_gzip( io_rgn[i]->mem, size, 1, f );
1.142 }
1.143 return 0;
1.144 }
1.145 @@ -170,20 +170,20 @@
1.146 FILE *f = fopen(file,"w");
1.147
1.148 if( f == NULL )
1.149 - return errno;
1.150 -
1.151 + return errno;
1.152 +
1.153 while( total < length ) {
1.154 - region = mem_get_region(addr);
1.155 - len = 4096 - (addr & 0x0FFF);
1.156 - if( len > (length-total) )
1.157 - len = (length-total);
1.158 - if( fwrite( region, len, 1, f ) != 1 ) {
1.159 - ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
1.160 - break;
1.161 - }
1.162 -
1.163 - addr += len;
1.164 - total += len;
1.165 + region = mem_get_region(addr);
1.166 + len = 4096 - (addr & 0x0FFF);
1.167 + if( len > (length-total) )
1.168 + len = (length-total);
1.169 + if( fwrite( region, len, 1, f ) != 1 ) {
1.170 + ERROR( "Unexpected error writing blocks: %d (%s)", len, strerror(errno) );
1.171 + break;
1.172 + }
1.173 +
1.174 + addr += len;
1.175 + total += len;
1.176 }
1.177 fclose( f );
1.178 INFO( "Loaded %d of %d bytes to %08X", total, length, start );
1.179 @@ -199,25 +199,25 @@
1.180 FILE *f = fopen(file,"r");
1.181
1.182 if( f == NULL ) {
1.183 - WARN( "Unable to load file '%s': %s", file, strerror(errno) );
1.184 - return -1;
1.185 + WARN( "Unable to load file '%s': %s", file, strerror(errno) );
1.186 + return -1;
1.187 }
1.188 fstat( fileno(f), &st );
1.189 if( length == 0 || length == -1 || length > st.st_size )
1.190 - length = st.st_size;
1.191 -
1.192 + length = st.st_size;
1.193 +
1.194 while( total < length ) {
1.195 - region = mem_get_region(addr);
1.196 - len = 4096 - (addr & 0x0FFF);
1.197 - if( len > (length-total) )
1.198 - len = (length-total);
1.199 - if( fread( region, len, 1, f ) != 1 ) {
1.200 - ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
1.201 - break;
1.202 - }
1.203 -
1.204 - addr += len;
1.205 - total += len;
1.206 + region = mem_get_region(addr);
1.207 + len = 4096 - (addr & 0x0FFF);
1.208 + if( len > (length-total) )
1.209 + len = (length-total);
1.210 + if( fread( region, len, 1, f ) != 1 ) {
1.211 + ERROR( "Unexpected error reading: %d (%s)", len, strerror(errno) );
1.212 + break;
1.213 + }
1.214 +
1.215 + addr += len;
1.216 + total += len;
1.217 }
1.218 fclose( f );
1.219 INFO( "Loaded %d of %d bytes to %08X", total, length, start );
1.220 @@ -226,7 +226,7 @@
1.221
1.222 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
1.223 const char *name, int flags, uint32_t repeat_offset,
1.224 - uint32_t repeat_until )
1.225 + uint32_t repeat_until )
1.226 {
1.227 int i;
1.228 mem_rgn[num_mem_rgns].base = base;
1.229 @@ -237,9 +237,9 @@
1.230 num_mem_rgns++;
1.231
1.232 do {
1.233 - for( i=0; i<size>>PAGE_BITS; i++ )
1.234 - page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
1.235 - base += repeat_offset;
1.236 + for( i=0; i<size>>PAGE_BITS; i++ )
1.237 + page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
1.238 + base += repeat_offset;
1.239 } while( base <= repeat_until );
1.240
1.241 return &mem_rgn[num_mem_rgns-1];
1.242 @@ -251,10 +251,10 @@
1.243 }
1.244
1.245 void *mem_create_repeating_ram_region( uint32_t base, uint32_t size, const char *name,
1.246 - uint32_t repeat_offset, uint32_t repeat_until )
1.247 + uint32_t repeat_offset, uint32_t repeat_until )
1.248 {
1.249 char *mem;
1.250 -
1.251 +
1.252 assert( (base&0xFFFFF000) == base ); /* must be page aligned */
1.253 assert( (size&0x00000FFF) == 0 );
1.254 assert( num_mem_rgns < MAX_MEM_REGIONS );
1.255 @@ -263,12 +263,12 @@
1.256 mem = mem_alloc_pages( size>>PAGE_BITS );
1.257
1.258 mem_map_region( mem, base, size, name, MEM_FLAG_RAM, repeat_offset, repeat_until );
1.259 -
1.260 +
1.261 return mem;
1.262 }
1.263
1.264 gboolean mem_load_rom( const gchar *file, uint32_t base, uint32_t size, uint32_t crc,
1.265 - const gchar *region_name )
1.266 + const gchar *region_name )
1.267 {
1.268 sh4ptr_t mem;
1.269 uint32_t calc_crc;
1.270 @@ -276,29 +276,29 @@
1.271
1.272 mem = mem_get_region(base);
1.273 if( mem == NULL ) {
1.274 - mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0 );
1.275 - if( mem == MAP_FAILED ) {
1.276 - ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
1.277 - return FALSE;
1.278 - }
1.279 - mem_map_region( mem, base, size, region_name, MEM_FLAG_ROM, size, base );
1.280 + mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0 );
1.281 + if( mem == MAP_FAILED ) {
1.282 + ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
1.283 + return FALSE;
1.284 + }
1.285 + mem_map_region( mem, base, size, region_name, MEM_FLAG_ROM, size, base );
1.286 } else {
1.287 - mprotect( mem, size, PROT_READ|PROT_WRITE );
1.288 + mprotect( mem, size, PROT_READ|PROT_WRITE );
1.289 }
1.290
1.291 status = mem_load_block( file, base, size );
1.292 mprotect( mem, size, PROT_READ );
1.293
1.294 if( status == 0 ) {
1.295 - /* CRC check only if we loaded something */
1.296 - calc_crc = crc32(0L, (sh4ptr_t)mem, size);
1.297 - if( calc_crc != crc ) {
1.298 - WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
1.299 - file, calc_crc, crc);
1.300 - }
1.301 - return TRUE;
1.302 + /* CRC check only if we loaded something */
1.303 + calc_crc = crc32(0L, (sh4ptr_t)mem, size);
1.304 + if( calc_crc != crc ) {
1.305 + WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
1.306 + file, calc_crc, crc);
1.307 + }
1.308 + return TRUE;
1.309 }
1.310 -
1.311 +
1.312 return FALSE;
1.313 }
1.314
1.315 @@ -315,7 +315,7 @@
1.316 void register_io_region( struct mmio_region *io )
1.317 {
1.318 int i;
1.319 -
1.320 +
1.321 assert(io);
1.322 io->mem = mem_alloc_pages(2);
1.323 io->save_mem = io->mem + PAGE_SIZE;
1.324 @@ -375,13 +375,13 @@
1.325 struct mmio_region *mem_get_io_region( uint32_t addr )
1.326 {
1.327 if( addr > 0xFF000000 ) {
1.328 - return P4_io[(addr&0x00FFFFFF)>>12];
1.329 + return P4_io[(addr&0x00FFFFFF)>>12];
1.330 }
1.331 sh4ptr_t page = page_map[(addr&0x1FFFFFFF)>>12];
1.332 if( ((uintptr_t)page) < MAX_IO_REGIONS ) {
1.333 - return io_rgn[(uintptr_t)page];
1.334 + return io_rgn[(uintptr_t)page];
1.335 } else {
1.336 - return NULL;
1.337 + return NULL;
1.338 }
1.339 }
1.340
1.341 @@ -389,9 +389,9 @@
1.342 {
1.343 int i;
1.344 for( i=0; i<num_io_rgns; i++ ) {
1.345 - if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
1.346 - return io_rgn[i];
1.347 - }
1.348 + if( strcasecmp(io_rgn[i]->id, name) == 0 ) {
1.349 + return io_rgn[i];
1.350 + }
1.351 }
1.352 return NULL;
1.353 }
1.354 @@ -399,25 +399,25 @@
1.355 void mem_set_trace( const gchar *tracelist, gboolean flag )
1.356 {
1.357 if( tracelist != NULL ) {
1.358 - gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
1.359 - int i;
1.360 - for( i=0; tracev[i] != NULL; i++ ) {
1.361 - // Special case "all" - trace everything
1.362 - if( strcasecmp(tracev[i], "all") == 0 ) {
1.363 - int j;
1.364 - for( j=0; j<num_io_rgns; j++ ) {
1.365 - io_rgn[j]->trace_flag = flag ? 1 : 0;
1.366 - }
1.367 - break;
1.368 - }
1.369 - struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
1.370 - if( region == NULL ) {
1.371 - WARN( "Unknown IO region '%s'", tracev[i] );
1.372 - } else {
1.373 - region->trace_flag = flag ? 1 : 0;
1.374 - }
1.375 - }
1.376 - g_strfreev( tracev );
1.377 + gchar ** tracev = g_strsplit_set( tracelist, ",:; \t\r\n", 0 );
1.378 + int i;
1.379 + for( i=0; tracev[i] != NULL; i++ ) {
1.380 + // Special case "all" - trace everything
1.381 + if( strcasecmp(tracev[i], "all") == 0 ) {
1.382 + int j;
1.383 + for( j=0; j<num_io_rgns; j++ ) {
1.384 + io_rgn[j]->trace_flag = flag ? 1 : 0;
1.385 + }
1.386 + break;
1.387 + }
1.388 + struct mmio_region *region = mem_get_io_region_by_name( tracev[i] );
1.389 + if( region == NULL ) {
1.390 + WARN( "Unknown IO region '%s'", tracev[i] );
1.391 + } else {
1.392 + region->trace_flag = flag ? 1 : 0;
1.393 + }
1.394 + }
1.395 + g_strfreev( tracev );
1.396 }
1.397 }
1.398
.