1 |
/* |
/* |
2 |
* Copyright (C) 2004-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2004-2006 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: dev_ram.c,v 1.13 2005/02/11 09:53:48 debug Exp $ |
* $Id: dev_ram.c,v 1.21 2006/01/01 13:17:17 debug Exp $ |
29 |
* |
* |
30 |
* A generic RAM (memory) device. Can also be used to mirror/alias another |
* A generic RAM (memory) device. Can also be used to mirror/alias another |
31 |
* part of RAM. |
* part of RAM. |
32 |
*/ |
*/ |
33 |
|
|
37 |
#include <sys/types.h> |
#include <sys/types.h> |
38 |
#include <sys/mman.h> |
#include <sys/mman.h> |
39 |
|
|
|
#include "console.h" |
|
40 |
#include "cpu.h" |
#include "cpu.h" |
41 |
#include "devices.h" |
#include "devices.h" |
42 |
|
#include "machine.h" |
43 |
#include "memory.h" |
#include "memory.h" |
44 |
#include "misc.h" |
#include "misc.h" |
45 |
|
|
50 |
int mode; |
int mode; |
51 |
uint64_t otheraddress; |
uint64_t otheraddress; |
52 |
|
|
53 |
|
/* If mode = DEV_RAM_MIRROR: */ |
54 |
|
uint64_t offset; |
55 |
|
|
56 |
/* If mode = DEV_RAM_RAM: */ |
/* If mode = DEV_RAM_RAM: */ |
57 |
unsigned char *data; |
unsigned char *data; |
58 |
uint64_t length; |
uint64_t length; |
62 |
/* |
/* |
63 |
* dev_ram_access(): |
* dev_ram_access(): |
64 |
*/ |
*/ |
65 |
int dev_ram_access(struct cpu *cpu, struct memory *mem, |
DEVICE_ACCESS(ram) |
|
uint64_t relative_addr, unsigned char *data, size_t len, |
|
|
int writeflag, void *extra) |
|
66 |
{ |
{ |
67 |
struct ram_data *d = extra; |
struct ram_data *d = extra; |
68 |
|
|
102 |
|
|
103 |
/* |
/* |
104 |
* dev_ram_init(): |
* dev_ram_init(): |
105 |
|
* |
106 |
|
* Initializes a RAM or mirror device. Things get a bit complicated because |
107 |
|
* of dyntrans (i.e. mirrored memory ranges should be entered into the |
108 |
|
* translation arrays just as normal memory and other devices are). |
109 |
*/ |
*/ |
110 |
void dev_ram_init(struct memory *mem, uint64_t baseaddr, uint64_t length, |
void dev_ram_init(struct machine *machine, uint64_t baseaddr, uint64_t length, |
111 |
int mode, uint64_t otheraddress) |
int mode, uint64_t otheraddress) |
112 |
{ |
{ |
113 |
struct ram_data *d; |
struct ram_data *d; |
114 |
|
int flags = DM_DEFAULT, points_to_ram = 1; |
115 |
|
|
116 |
d = malloc(sizeof(struct ram_data)); |
d = malloc(sizeof(struct ram_data)); |
117 |
if (d == NULL) { |
if (d == NULL) { |
121 |
|
|
122 |
memset(d, 0, sizeof(struct ram_data)); |
memset(d, 0, sizeof(struct ram_data)); |
123 |
|
|
124 |
|
if (mode & DEV_RAM_MIGHT_POINT_TO_DEVICES) { |
125 |
|
mode &= ~DEV_RAM_MIGHT_POINT_TO_DEVICES; |
126 |
|
points_to_ram = 0; |
127 |
|
} |
128 |
|
|
129 |
d->mode = mode; |
d->mode = mode; |
130 |
d->otheraddress = otheraddress; |
d->otheraddress = otheraddress; |
131 |
|
|
132 |
switch (d->mode) { |
switch (d->mode) { |
133 |
|
|
134 |
case DEV_RAM_MIRROR: |
case DEV_RAM_MIRROR: |
135 |
|
/* |
136 |
|
* Calculate the amount that the mirror memory is offset from |
137 |
|
* the real (physical) memory. This is used in src/memory_rw.c |
138 |
|
* with dyntrans accesses if DM_EMULATED_RAM is set. |
139 |
|
*/ |
140 |
|
d->offset = baseaddr - otheraddress; |
141 |
|
|
142 |
|
/* Aligned RAM? Then it works with dyntrans. */ |
143 |
|
if (points_to_ram && |
144 |
|
(baseaddr & (machine->arch_pagesize-1)) == 0 && |
145 |
|
(otheraddress & (machine->arch_pagesize - 1)) == 0 && |
146 |
|
(length & (machine->arch_pagesize - 1)) == 0) |
147 |
|
flags |= DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK |
148 |
|
| DM_EMULATED_RAM; |
149 |
|
|
150 |
|
memory_device_register(machine->memory, "ram [mirror]", |
151 |
|
baseaddr, length, dev_ram_access, d, flags |
152 |
|
| DM_READS_HAVE_NO_SIDE_EFFECTS, (void *) &d->offset); |
153 |
break; |
break; |
154 |
|
|
155 |
case DEV_RAM_RAM: |
case DEV_RAM_RAM: |
156 |
/* |
/* |
157 |
* Allocate zero-filled RAM using mmap(). If mmap() failed, |
* Allocate zero-filled RAM using mmap(). If mmap() failed, |
169 |
} |
} |
170 |
memset(d->data, 0, length); |
memset(d->data, 0, length); |
171 |
} |
} |
172 |
|
|
173 |
|
/* Aligned memory? Then it works with dyntrans. */ |
174 |
|
if ((baseaddr & (machine->arch_pagesize - 1)) == 0 && |
175 |
|
(length & (machine->arch_pagesize - 1)) == 0) |
176 |
|
flags |= DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK; |
177 |
|
|
178 |
|
memory_device_register(machine->memory, "ram", baseaddr, |
179 |
|
d->length, dev_ram_access, d, flags |
180 |
|
| DM_READS_HAVE_NO_SIDE_EFFECTS, d->data); |
181 |
break; |
break; |
182 |
|
|
183 |
default: |
default: |
184 |
fatal("dev_ram_access(): unknown mode %i\n", d->mode); |
fatal("dev_ram_access(): unknown mode %i\n", d->mode); |
185 |
exit(1); |
exit(1); |
186 |
} |
} |
|
|
|
|
memory_device_register(mem, "ram", baseaddr, length, |
|
|
dev_ram_access, d, MEM_DEFAULT, NULL); |
|
187 |
} |
} |
188 |
|
|