1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, 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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "sim-assert.h"
29 /* "core" module install handler.
30 This is called via sim_module_install to install the "core" subsystem
31 into the simulator. */
35 sim_core_install (SIM_DESC sd)
37 sim_module_add_uninstall_fn (sd, sim_core_uninstall);
38 sim_module_add_init_fn (sd, sim_core_init);
43 /* Uninstall the "core" subsystem from the simulator. */
47 sim_core_uninstall (SIM_DESC sd)
49 /* FIXME: free buffers, etc. */
55 sim_core_init (SIM_DESC sd)
57 sim_core *memory = STATE_CORE(sd);
60 map < nr_sim_core_maps;
62 /* blow away old mappings */
63 sim_core_mapping *curr = memory->map[map].first;
64 while (curr != NULL) {
65 sim_core_mapping *tbd = curr;
67 if (tbd->free_buffer) {
68 SIM_ASSERT(tbd->buffer != NULL);
73 memory->map[map].first = NULL;
80 STATIC_INLINE_SIM_CORE\
82 sim_core_map_to_str (sim_core_maps map)
86 case sim_core_read_map: return "read";
87 case sim_core_write_map: return "write";
88 case sim_core_execute_map: return "exec";
89 default: return "(invalid-map)";
94 STATIC_INLINE_SIM_CORE\
96 new_sim_core_mapping(SIM_DESC sd,
105 sim_core_mapping *new_mapping = ZALLOC(sim_core_mapping);
107 new_mapping->level = attach;
108 new_mapping->space = space;
109 new_mapping->base = addr;
110 new_mapping->nr_bytes = nr_bytes;
111 new_mapping->bound = addr + (nr_bytes - 1);
112 if (attach == attach_raw_memory) {
113 new_mapping->buffer = buffer;
114 new_mapping->free_buffer = free_buffer;
116 else if (attach >= attach_callback) {
117 new_mapping->device = device;
120 sim_io_error (sd, "new_sim_core_mapping - internal error - unknown attach type %d\n",
127 STATIC_INLINE_SIM_CORE\
129 sim_core_map_attach(SIM_DESC sd,
130 sim_core_map *access_map,
134 unsigned nr_bytes, /* host limited */
135 device *client, /*callback/default*/
136 void *buffer, /*raw_memory*/
137 int free_buffer) /*raw_memory*/
139 /* find the insertion point for this additional mapping and then
141 sim_core_mapping *next_mapping;
142 sim_core_mapping **last_mapping;
144 SIM_ASSERT((attach >= attach_callback && client != NULL && buffer == NULL && !free_buffer)
145 || (attach == attach_raw_memory && client == NULL && buffer != NULL));
147 /* actually do occasionally get a zero size map */
150 device_error(client, "called on sim_core_map_attach with size zero");
152 sim_io_error (sd, "called on sim_core_map_attach with size zero");
156 /* find the insertion point (between last/next) */
157 next_mapping = access_map->first;
158 last_mapping = &access_map->first;
159 while(next_mapping != NULL
160 && (next_mapping->level < attach
161 || (next_mapping->level == attach
162 && next_mapping->bound < addr))) {
163 /* provided levels are the same */
164 /* assert: next_mapping->base > all bases before next_mapping */
165 /* assert: next_mapping->bound >= all bounds before next_mapping */
166 last_mapping = &next_mapping->next;
167 next_mapping = next_mapping->next;
170 /* check insertion point correct */
171 SIM_ASSERT(next_mapping == NULL || next_mapping->level >= attach);
172 if (next_mapping != NULL && next_mapping->level == attach
173 && next_mapping->base < (addr + (nr_bytes - 1))) {
175 device_error(client, "map overlap when attaching %d:0x%lx (%ld)",
176 space, (long)addr, (long)nr_bytes);
178 sim_io_error (sd, "map overlap when attaching %d:0x%lx (%ld)",
179 space, (long)addr, (long)nr_bytes);
183 /* create/insert the new mapping */
184 *last_mapping = new_sim_core_mapping(sd,
186 space, addr, nr_bytes,
187 client, buffer, free_buffer);
188 (*last_mapping)->next = next_mapping;
194 sim_core_attach(SIM_DESC sd,
200 unsigned nr_bytes, /* host limited */
202 void *optional_buffer)
204 sim_core *memory = STATE_CORE(sd);
210 /* check for for attempt to use unimplemented per-processor core map */
212 sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
214 if ((access & access_read_write_exec) == 0
215 || (access & ~access_read_write_exec) != 0) {
217 device_error(client, "invalid access for core attach");
219 sim_io_error (sd, "invalid access for core attach");
222 /* verify the attach type */
223 if (attach == attach_raw_memory) {
224 if (optional_buffer == NULL) {
225 buffer = zalloc(nr_bytes);
229 buffer = optional_buffer;
233 else if (attach >= attach_callback) {
239 device_error(client, "sim_core_attach - conflicting buffer and attach arguments");
241 sim_io_error (sd, "sim_core_attach - conflicting buffer and attach arguments");
246 /* attach the region to all applicable access maps */
248 map < nr_sim_core_maps;
251 case sim_core_read_map:
252 if (access & access_read)
253 sim_core_map_attach(sd, &memory->map[map],
255 space, addr, nr_bytes,
256 client, buffer, !buffer_freed);
259 case sim_core_write_map:
260 if (access & access_write)
261 sim_core_map_attach(sd, &memory->map[map],
263 space, addr, nr_bytes,
264 client, buffer, !buffer_freed);
267 case sim_core_execute_map:
268 if (access & access_exec)
269 sim_core_map_attach(sd, &memory->map[map],
271 space, addr, nr_bytes,
272 client, buffer, !buffer_freed);
275 case nr_sim_core_maps:
276 sim_io_error (sd, "sim_core_attach - internal error - bad switch");
281 /* Just copy this map to each of the processor specific data structures.
282 FIXME - later this will be replaced by true processor specific
284 for (i = 0; i < MAX_NR_PROCESSORS; i++)
285 *CPU_CORE (STATE_CPU (sd, i)) = *STATE_CORE (sd);
289 STATIC_INLINE_SIM_CORE\
291 sim_core_find_mapping(sim_core *core,
295 int abort, /*either 0 or 1 - hint to inline/-O */
296 sim_cpu *cpu, /* abort => cpu != NULL */
299 sim_core_mapping *mapping = core->map[map].first;
300 ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
301 ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
302 ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
303 while (mapping != NULL)
305 if (addr >= mapping->base
306 && (addr + (nr_bytes - 1)) <= mapping->bound)
308 mapping = mapping->next;
313 sim_io_error (NULL, "sim_core_find_map - internal error - can not abort without a processor");
315 engine_error (CPU_STATE (cpu), cpu, cia,
316 "access to unmaped address 0x%lx (%d bytes)\n",
317 (unsigned long) addr, nr_bytes);
323 STATIC_INLINE_SIM_CORE\
325 sim_core_translate(sim_core_mapping *mapping,
328 return (void *)(((char *)mapping->buffer) + addr - mapping->base);
334 sim_core_read_buffer(SIM_DESC sd,
341 while (count < len) {
342 unsigned_word raddr = addr + count;
343 sim_core_mapping *mapping =
344 sim_core_find_mapping(STATE_CORE (sd), map,
346 0, NULL, NULL_CIA); /*dont-abort*/
350 if (mapping->device != NULL) {
351 int nr_bytes = len - count;
352 if (raddr + nr_bytes - 1> mapping->bound)
353 nr_bytes = mapping->bound - raddr + 1;
354 if (device_io_read_buffer(mapping->device,
355 (unsigned_1*)buffer + count,
358 nr_bytes) != nr_bytes)
365 ((unsigned_1*)buffer)[count] =
366 *(unsigned_1*)sim_core_translate(mapping, raddr);
376 sim_core_write_buffer(SIM_DESC sd,
383 while (count < len) {
384 unsigned_word raddr = addr + count;
385 sim_core_mapping *mapping = sim_core_find_mapping(STATE_CORE (sd), map,
387 0, NULL, NULL_CIA); /*dont-abort*/
391 if (WITH_CALLBACK_MEMORY
392 && mapping->device != NULL) {
393 int nr_bytes = len - count;
394 if (raddr + nr_bytes - 1 > mapping->bound)
395 nr_bytes = mapping->bound - raddr + 1;
396 if (device_io_write_buffer(mapping->device,
397 (unsigned_1*)buffer + count,
400 nr_bytes) != nr_bytes)
407 *(unsigned_1*)sim_core_translate(mapping, raddr) =
408 ((unsigned_1*)buffer)[count];
416 /* define the read/write 1/2/4/8/word functions */
419 #include "sim-n-core.h"
423 #include "sim-n-core.h"
427 #include "sim-n-core.h"
431 #include "sim-n-core.h"
435 #include "sim-n-core.h"