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_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
38 sim_module_add_uninstall_fn (sd
, sim_core_uninstall
);
39 sim_module_add_init_fn (sd
, sim_core_init
);
44 /* Uninstall the "core" subsystem from the simulator. */
48 sim_core_uninstall (SIM_DESC sd
)
50 /* FIXME: free buffers, etc. */
56 sim_core_init (SIM_DESC sd
)
58 sim_core
*memory
= STATE_CORE(sd
);
61 map
< nr_sim_core_maps
;
63 /* blow away old mappings */
64 sim_core_mapping
*curr
= memory
->map
[map
].first
;
65 while (curr
!= NULL
) {
66 sim_core_mapping
*tbd
= curr
;
68 if (tbd
->free_buffer
) {
69 SIM_ASSERT(tbd
->buffer
!= NULL
);
74 memory
->map
[map
].first
= NULL
;
81 #ifndef SIM_CORE_SIGNAL
82 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
83 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
86 sim_core_signal (SIM_DESC sd
,
92 transfer_type transfer
,
95 const char *copy
= (transfer
== read_transfer
? "read" : "write");
98 case sim_core_unmapped_signal
:
99 sim_engine_abort (sd
, cpu
, cia
, "sim-core: %d byte %s to unmaped address 0x%lx",
100 nr_bytes
, copy
, (unsigned long) addr
);
102 case sim_core_unaligned_signal
:
103 sim_engine_abort (sd
, cpu
, cia
, "sim-core: %d byte misaligned %s to address 0x%lx",
104 nr_bytes
, copy
, (unsigned long) addr
);
107 sim_engine_abort (sd
, cpu
, cia
, "sim_core_signal - internal error - bad switch");
115 STATIC_INLINE_SIM_CORE\
117 sim_core_map_to_str (sim_core_maps map
)
121 case sim_core_read_map
: return "read";
122 case sim_core_write_map
: return "write";
123 case sim_core_execute_map
: return "exec";
124 default: return "(invalid-map)";
129 STATIC_INLINE_SIM_CORE\
131 new_sim_core_mapping(SIM_DESC sd
,
140 sim_core_mapping
*new_mapping
= ZALLOC(sim_core_mapping
);
142 new_mapping
->level
= attach
;
143 new_mapping
->space
= space
;
144 new_mapping
->base
= addr
;
145 new_mapping
->nr_bytes
= nr_bytes
;
146 new_mapping
->bound
= addr
+ (nr_bytes
- 1);
147 if (attach
== attach_raw_memory
) {
148 new_mapping
->buffer
= buffer
;
149 new_mapping
->free_buffer
= free_buffer
;
151 else if (attach
>= attach_callback
) {
152 new_mapping
->device
= device
;
155 sim_io_error (sd
, "new_sim_core_mapping - internal error - unknown attach type %d\n",
162 STATIC_INLINE_SIM_CORE\
164 sim_core_map_attach(SIM_DESC sd
,
165 sim_core_map
*access_map
,
169 unsigned nr_bytes
, /* host limited */
170 device
*client
, /*callback/default*/
171 void *buffer
, /*raw_memory*/
172 int free_buffer
) /*raw_memory*/
174 /* find the insertion point for this additional mapping and then
176 sim_core_mapping
*next_mapping
;
177 sim_core_mapping
**last_mapping
;
179 SIM_ASSERT((attach
>= attach_callback
&& client
!= NULL
&& buffer
== NULL
&& !free_buffer
)
180 || (attach
== attach_raw_memory
&& client
== NULL
&& buffer
!= NULL
));
182 /* actually do occasionally get a zero size map */
185 device_error(client
, "called on sim_core_map_attach with size zero");
187 sim_io_error (sd
, "called on sim_core_map_attach with size zero");
191 /* find the insertion point (between last/next) */
192 next_mapping
= access_map
->first
;
193 last_mapping
= &access_map
->first
;
194 while(next_mapping
!= NULL
195 && (next_mapping
->level
< attach
196 || (next_mapping
->level
== attach
197 && next_mapping
->bound
< addr
))) {
198 /* provided levels are the same */
199 /* assert: next_mapping->base > all bases before next_mapping */
200 /* assert: next_mapping->bound >= all bounds before next_mapping */
201 last_mapping
= &next_mapping
->next
;
202 next_mapping
= next_mapping
->next
;
205 /* check insertion point correct */
206 SIM_ASSERT(next_mapping
== NULL
|| next_mapping
->level
>= attach
);
207 if (next_mapping
!= NULL
&& next_mapping
->level
== attach
208 && next_mapping
->base
< (addr
+ (nr_bytes
- 1))) {
210 device_error(client
, "map overlap when attaching %d:0x%lx (%ld)",
211 space
, (long)addr
, (long)nr_bytes
);
213 sim_io_error (sd
, "map overlap when attaching %d:0x%lx (%ld)",
214 space
, (long)addr
, (long)nr_bytes
);
218 /* create/insert the new mapping */
219 *last_mapping
= new_sim_core_mapping(sd
,
221 space
, addr
, nr_bytes
,
222 client
, buffer
, free_buffer
);
223 (*last_mapping
)->next
= next_mapping
;
229 sim_core_attach(SIM_DESC sd
,
235 unsigned nr_bytes
, /* host limited */
237 void *optional_buffer
)
239 sim_core
*memory
= STATE_CORE(sd
);
245 /* check for for attempt to use unimplemented per-processor core map */
247 sim_io_error (sd
, "sim_core_map_attach - processor specific memory map not yet supported");
249 if ((access
& access_read_write_exec
) == 0
250 || (access
& ~access_read_write_exec
) != 0) {
252 device_error(client
, "invalid access for core attach");
254 sim_io_error (sd
, "invalid access for core attach");
257 /* verify the attach type */
258 if (attach
== attach_raw_memory
) {
259 if (optional_buffer
== NULL
) {
260 buffer
= zalloc(nr_bytes
);
264 buffer
= optional_buffer
;
268 else if (attach
>= attach_callback
) {
274 device_error(client
, "sim_core_attach - conflicting buffer and attach arguments");
276 sim_io_error (sd
, "sim_core_attach - conflicting buffer and attach arguments");
281 /* attach the region to all applicable access maps */
283 map
< nr_sim_core_maps
;
286 case sim_core_read_map
:
287 if (access
& access_read
)
288 sim_core_map_attach(sd
, &memory
->map
[map
],
290 space
, addr
, nr_bytes
,
291 client
, buffer
, !buffer_freed
);
294 case sim_core_write_map
:
295 if (access
& access_write
)
296 sim_core_map_attach(sd
, &memory
->map
[map
],
298 space
, addr
, nr_bytes
,
299 client
, buffer
, !buffer_freed
);
302 case sim_core_execute_map
:
303 if (access
& access_exec
)
304 sim_core_map_attach(sd
, &memory
->map
[map
],
306 space
, addr
, nr_bytes
,
307 client
, buffer
, !buffer_freed
);
310 case nr_sim_core_maps
:
311 sim_io_error (sd
, "sim_core_attach - internal error - bad switch");
316 /* Just copy this map to each of the processor specific data structures.
317 FIXME - later this will be replaced by true processor specific
319 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
320 *CPU_CORE (STATE_CPU (sd
, i
)) = *STATE_CORE (sd
);
324 STATIC_INLINE_SIM_CORE\
326 sim_core_find_mapping(sim_core
*core
,
330 transfer_type transfer
,
331 int abort
, /*either 0 or 1 - hint to inline/-O */
332 sim_cpu
*cpu
, /* abort => cpu != NULL */
335 sim_core_mapping
*mapping
= core
->map
[map
].first
;
336 ASSERT ((addr
& (nr_bytes
- 1)) == 0); /* must be aligned */
337 ASSERT ((addr
+ (nr_bytes
- 1)) >= addr
); /* must not wrap */
338 ASSERT (!abort
|| cpu
!= NULL
); /* abort needs a non null CPU */
339 while (mapping
!= NULL
)
341 if (addr
>= mapping
->base
342 && (addr
+ (nr_bytes
- 1)) <= mapping
->bound
)
344 mapping
= mapping
->next
;
348 SIM_CORE_SIGNAL (CPU_STATE (cpu
), cpu
, cia
, map
, nr_bytes
, addr
, transfer
,
349 sim_core_unmapped_signal
);
355 STATIC_INLINE_SIM_CORE\
357 sim_core_translate(sim_core_mapping
*mapping
,
360 return (void *)(((char *)mapping
->buffer
) + addr
- mapping
->base
);
366 sim_core_read_buffer(SIM_DESC sd
,
373 while (count
< len
) {
374 unsigned_word raddr
= addr
+ count
;
375 sim_core_mapping
*mapping
=
376 sim_core_find_mapping(STATE_CORE (sd
), map
,
377 raddr
, /*nr-bytes*/1,
379 0, NULL
, NULL_CIA
); /*dont-abort*/
383 if (mapping
->device
!= NULL
) {
384 int nr_bytes
= len
- count
;
385 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
386 nr_bytes
= mapping
->bound
- raddr
+ 1;
387 if (device_io_read_buffer(mapping
->device
,
388 (unsigned_1
*)buffer
+ count
,
391 nr_bytes
) != nr_bytes
)
398 ((unsigned_1
*)buffer
)[count
] =
399 *(unsigned_1
*)sim_core_translate(mapping
, raddr
);
409 sim_core_write_buffer(SIM_DESC sd
,
416 while (count
< len
) {
417 unsigned_word raddr
= addr
+ count
;
418 sim_core_mapping
*mapping
= sim_core_find_mapping(STATE_CORE (sd
), map
,
419 raddr
, /*nr-bytes*/1,
421 0, NULL
, NULL_CIA
); /*dont-abort*/
425 if (WITH_CALLBACK_MEMORY
426 && mapping
->device
!= NULL
) {
427 int nr_bytes
= len
- count
;
428 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
429 nr_bytes
= mapping
->bound
- raddr
+ 1;
430 if (device_io_write_buffer(mapping
->device
,
431 (unsigned_1
*)buffer
+ count
,
434 nr_bytes
) != nr_bytes
)
441 *(unsigned_1
*)sim_core_translate(mapping
, raddr
) =
442 ((unsigned_1
*)buffer
)[count
];
450 /* define the read/write 1/2/4/8/word functions */
453 #include "sim-n-core.h"
457 #include "sim-n-core.h"
461 #include "sim-n-core.h"
465 #include "sim-n-core.h"
469 #include "sim-n-core.h"