1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "device_table.h"
28 typedef struct _core_mapping core_mapping;
29 struct _core_mapping {
57 core_map map[nr_core_map_types];
71 core_from_device(device *root)
73 root = device_root(root);
74 ASSERT(strcmp(device_name(root), "core") == 0);
75 return device_data(root);
81 core_init(core *memory)
83 core_map_types access_type;
85 access_type < nr_core_map_types;
87 core_map *map = memory->map + access_type;
88 /* blow away old mappings */
89 core_mapping *curr = map->first;
90 while (curr != NULL) {
91 core_mapping *tbd = curr;
93 if (tbd->free_buffer != NULL) {
94 ASSERT(tbd->buffer != NULL);
95 free(tbd->free_buffer);
105 /* the core has three sub mappings that the more efficient
106 read/write fixed quantity functions use */
110 core_readable(core *memory)
112 return memory->map + core_read_map;
117 core_writeable(core *memory)
119 return memory->map + core_write_map;
124 core_executable(core *memory)
126 return memory->map + core_execute_map;
133 new_core_mapping(attach_type attach,
141 core_mapping *new_mapping = ZALLOC(core_mapping);
143 new_mapping->level = attach;
144 new_mapping->space = space;
145 new_mapping->base = addr;
146 new_mapping->nr_bytes = nr_bytes;
147 new_mapping->bound = addr + (nr_bytes - 1);
148 if (attach == attach_raw_memory) {
149 new_mapping->buffer = buffer;
150 new_mapping->free_buffer = free_buffer;
152 else if (attach >= attach_callback) {
153 new_mapping->device = device;
156 error("new_core_mapping() - internal error - unknown attach type %d\n",
165 core_map_attach(core_map *access_map,
169 unsigned nr_bytes, /* host limited */
170 device *client, /*callback/default*/
171 void *buffer, /*raw_memory*/
172 void *free_buffer) /*raw_memory*/
174 /* find the insertion point for this additional mapping and insert */
175 core_mapping *next_mapping;
176 core_mapping **last_mapping;
178 /* actually do occasionally get a zero size map */
180 device_error(client, "called on core_map_attach() with size zero");
183 /* find the insertion point (between last/next) */
184 next_mapping = access_map->first;
185 last_mapping = &access_map->first;
186 while(next_mapping != NULL
187 && (next_mapping->level < attach
188 || (next_mapping->level == attach
189 && next_mapping->bound < addr))) {
190 /* provided levels are the same */
191 /* assert: next_mapping->base > all bases before next_mapping */
192 /* assert: next_mapping->bound >= all bounds before next_mapping */
193 last_mapping = &next_mapping->next;
194 next_mapping = next_mapping->next;
197 /* check insertion point correct */
198 ASSERT(next_mapping == NULL || next_mapping->level >= attach);
199 if (next_mapping != NULL && next_mapping->level == attach
200 && next_mapping->base < (addr + (nr_bytes - 1))) {
201 device_error(client, "map overlap when attaching %d:0x%lx (%ld)",
202 space, (long)addr, (long)nr_bytes);
205 /* create/insert the new mapping */
206 *last_mapping = new_core_mapping(attach,
207 space, addr, nr_bytes,
208 client, buffer, free_buffer);
209 (*last_mapping)->next = next_mapping;
215 core_attach(core *memory,
220 unsigned nr_bytes, /* host limited */
221 device *client) /*callback/default*/
223 core_map_types access_map;
226 if (attach == attach_raw_memory) {
227 /* Padd out the raw buffer to ensure that ADDR starts on a
228 correctly aligned boundary */
229 int padding = (addr % sizeof (unsigned64));
230 free_buffer = zalloc(nr_bytes + padding);
231 buffer = (char*)free_buffer + padding;
235 free_buffer = &buffer; /* marker for assertion */
238 access_map < nr_core_map_types;
240 switch (access_map) {
242 if (access & access_read)
243 core_map_attach(memory->map + access_map,
245 space, addr, nr_bytes,
246 client, buffer, free_buffer);
250 if (access & access_write)
251 core_map_attach(memory->map + access_map,
253 space, addr, nr_bytes,
254 client, buffer, free_buffer);
257 case core_execute_map:
258 if (access & access_exec)
259 core_map_attach(memory->map + access_map,
261 space, addr, nr_bytes,
262 client, buffer, free_buffer);
266 error("core_attach() internal error\n");
270 /* allocated buffer must attach to at least one thing */
271 ASSERT(free_buffer == NULL);
277 core_map_find_mapping(core_map *map,
282 int abort) /*either 0 or 1 - helps inline */
284 core_mapping *mapping = map->first;
285 ASSERT((addr & (nr_bytes - 1)) == 0); /* must be aligned */
286 ASSERT((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
287 while (mapping != NULL) {
288 if (addr >= mapping->base
289 && (addr + (nr_bytes - 1)) <= mapping->bound)
291 mapping = mapping->next;
294 error("core_find_mapping() - access to unmaped address, attach a default map to handle this - addr=0x%x nr_bytes=0x%x processor=0x%x cia=0x%x\n",
295 addr, nr_bytes, processor, cia);
302 core_translate(core_mapping *mapping,
305 return (void *)(((char *)mapping->buffer) + addr - mapping->base);
311 core_map_read_buffer(core_map *map,
317 while (count < len) {
318 unsigned_word raddr = addr + count;
319 core_mapping *mapping =
320 core_map_find_mapping(map,
327 if (mapping->device != NULL) {
328 int nr_bytes = len - count;
329 if (raddr + nr_bytes - 1> mapping->bound)
330 nr_bytes = mapping->bound - raddr + 1;
331 if (device_io_read_buffer(mapping->device,
332 (unsigned_1*)buffer + count,
337 0 /*cpu*/) != nr_bytes)
342 ((unsigned_1*)buffer)[count] =
343 *(unsigned_1*)core_translate(mapping, raddr);
353 core_map_write_buffer(core_map *map,
359 while (count < len) {
360 unsigned_word raddr = addr + count;
361 core_mapping *mapping = core_map_find_mapping(map,
368 if (mapping->device != NULL) {
369 int nr_bytes = len - count;
370 if (raddr + nr_bytes - 1 > mapping->bound)
371 nr_bytes = mapping->bound - raddr + 1;
372 if (device_io_write_buffer(mapping->device,
373 (unsigned_1*)buffer + count,
378 0 /*cpu*/) != nr_bytes)
383 *(unsigned_1*)core_translate(mapping, raddr) =
384 ((unsigned_1*)buffer)[count];
392 /* define the read/write 1/2/4/8/word functions */
395 #include "corefile-n.h"
399 #include "corefile-n.h"
403 #include "corefile-n.h"
407 #include "corefile-n.h"
411 #include "corefile-n.h"
414 #endif /* _CORE_C_ */