25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: dev_ram.c,v 1.24 2007/03/08 11:43:44 debug Exp $ |
* $Id: dev_ram.c,v 1.25 2007/06/15 19:57:33 debug Exp $ |
29 |
* |
* |
30 |
* A generic RAM (memory) device. Can also be used to mirror/alias another |
* COMMENT: A generic RAM (memory) device |
31 |
* part of RAM. |
* |
32 |
|
* Note: This device can also be used to mirror/alias another part of RAM. |
33 |
*/ |
*/ |
34 |
|
|
35 |
#include <stdio.h> |
#include <stdio.h> |
80 |
#endif |
#endif |
81 |
|
|
82 |
switch (d->mode) { |
switch (d->mode) { |
83 |
|
|
84 |
case DEV_RAM_MIRROR: |
case DEV_RAM_MIRROR: |
85 |
/* TODO: how about caches? */ |
/* TODO: how about caches? */ |
86 |
return cpu->memory_rw(cpu, mem, |
return cpu->memory_rw(cpu, mem, |
87 |
d->otheraddress + relative_addr, data, len, |
d->otheraddress + relative_addr, data, len, |
88 |
writeflag, PHYSICAL); |
writeflag, PHYSICAL); |
89 |
|
|
90 |
case DEV_RAM_RAM: |
case DEV_RAM_RAM: |
91 |
if (writeflag == MEM_WRITE) { |
if (writeflag == MEM_WRITE) { |
92 |
memcpy(&d->data[relative_addr], data, len); |
memcpy(&d->data[relative_addr], data, len); |
101 |
memcpy(data, &d->data[relative_addr], len); |
memcpy(data, &d->data[relative_addr], len); |
102 |
} |
} |
103 |
break; |
break; |
104 |
|
|
105 |
default: |
default: |
106 |
fatal("dev_ram_access(): unknown mode %i\n", d->mode); |
fatal("dev_ram_access(): unknown mode %i\n", d->mode); |
107 |
exit(1); |
exit(1); |
124 |
struct ram_data *d; |
struct ram_data *d; |
125 |
int flags = DM_DEFAULT, points_to_ram = 1; |
int flags = DM_DEFAULT, points_to_ram = 1; |
126 |
|
|
127 |
d = malloc(sizeof(struct ram_data)); |
CHECK_ALLOCATION(d = malloc(sizeof(struct ram_data))); |
|
if (d == NULL) { |
|
|
fprintf(stderr, "out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
128 |
memset(d, 0, sizeof(struct ram_data)); |
memset(d, 0, sizeof(struct ram_data)); |
129 |
|
|
130 |
if (mode & DEV_RAM_MIGHT_POINT_TO_DEVICES) { |
if (mode & DEV_RAM_MIGHT_POINT_TO_DEVICES) { |
162 |
case DEV_RAM_RAM: |
case DEV_RAM_RAM: |
163 |
/* |
/* |
164 |
* Allocate zero-filled RAM using mmap(). If mmap() failed, |
* Allocate zero-filled RAM using mmap(). If mmap() failed, |
165 |
* try malloc(), but then we also have to memset(), which |
* try malloc(), but then memset() must also be called, which |
166 |
* can be slow for large chunks of memory. |
* can be slow for large chunks of memory. |
167 |
*/ |
*/ |
168 |
d->length = length; |
d->length = length; |
169 |
d->data = (unsigned char *) mmap(NULL, length, |
d->data = (unsigned char *) mmap(NULL, length, |
170 |
PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
171 |
if (d->data == NULL) { |
if (d->data == NULL) { |
172 |
d->data = malloc(length); |
CHECK_ALLOCATION(d->data = malloc(length)); |
|
if (d->data == NULL) { |
|
|
fprintf(stderr, "out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
173 |
memset(d->data, 0, length); |
memset(d->data, 0, length); |
174 |
} |
} |
175 |
|
|