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.
31 This is called via sim_module_install to install the "core" subsystem
32 into the simulator. */
34 static MODULE_INIT_FN sim_core_init
;
35 static MODULE_UNINSTALL_FN sim_core_uninstall
;
39 sim_core_install (SIM_DESC sd
)
41 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
43 /* establish the other handlers */
44 sim_module_add_uninstall_fn (sd
, sim_core_uninstall
);
45 sim_module_add_init_fn (sd
, sim_core_init
);
47 /* establish any initial data structures - none */
52 /* Uninstall the "core" subsystem from the simulator. */
56 sim_core_uninstall (SIM_DESC sd
)
58 sim_core
*core
= STATE_CORE(sd
);
60 /* blow away any mappings */
61 for (map
= 0; map
< nr_sim_core_maps
; map
++) {
62 sim_core_mapping
*curr
= core
->common
.map
[map
].first
;
63 while (curr
!= NULL
) {
64 sim_core_mapping
*tbd
= curr
;
66 if (tbd
->free_buffer
) {
67 SIM_ASSERT(tbd
->buffer
!= NULL
);
72 core
->common
.map
[map
].first
= NULL
;
79 sim_core_init (SIM_DESC sd
)
87 #ifndef SIM_CORE_SIGNAL
88 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
89 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
93 sim_core_signal (SIM_DESC sd
,
99 transfer_type transfer
,
100 sim_core_signals sig
)
102 const char *copy
= (transfer
== read_transfer
? "read" : "write");
105 case sim_core_unmapped_signal
:
106 sim_engine_abort (sd
, cpu
, cia
, "sim-core: %d byte %s to unmaped address 0x%lx",
107 nr_bytes
, copy
, (unsigned long) addr
);
109 case sim_core_unaligned_signal
:
110 sim_engine_abort (sd
, cpu
, cia
, "sim-core: %d byte misaligned %s to address 0x%lx",
111 nr_bytes
, copy
, (unsigned long) addr
);
114 sim_engine_abort (sd
, cpu
, cia
, "sim_core_signal - internal error - bad switch");
120 STATIC_INLINE_SIM_CORE\
122 sim_core_map_to_str (sim_core_maps map
)
126 case sim_core_read_map
: return "read";
127 case sim_core_write_map
: return "write";
128 case sim_core_execute_map
: return "exec";
129 default: return "(invalid-map)";
136 new_sim_core_mapping (SIM_DESC sd
,
140 address_word nr_bytes
,
146 sim_core_mapping
*new_mapping
= ZALLOC(sim_core_mapping
);
148 new_mapping
->level
= attach
;
149 new_mapping
->space
= space
;
150 new_mapping
->base
= addr
;
151 new_mapping
->nr_bytes
= nr_bytes
;
152 new_mapping
->bound
= addr
+ (nr_bytes
- 1);
154 new_mapping
->mask
= (unsigned) 0 - 1;
156 new_mapping
->mask
= modulo
- 1;
157 if (attach
== attach_raw_memory
)
159 new_mapping
->buffer
= buffer
;
160 new_mapping
->free_buffer
= free_buffer
;
162 else if (attach
>= attach_callback
)
164 new_mapping
->device
= device
;
167 sim_io_error (sd
, "new_sim_core_mapping - internal error - unknown attach type %d\n",
176 sim_core_map_attach (SIM_DESC sd
,
177 sim_core_map
*access_map
,
181 address_word nr_bytes
,
183 device
*client
, /*callback/default*/
184 void *buffer
, /*raw_memory*/
185 int free_buffer
) /*raw_memory*/
187 /* find the insertion point for this additional mapping and then
189 sim_core_mapping
*next_mapping
;
190 sim_core_mapping
**last_mapping
;
192 SIM_ASSERT ((attach
>= attach_callback
)
193 <= (client
!= NULL
&& buffer
== NULL
&& !free_buffer
));
194 SIM_ASSERT ((attach
== attach_raw_memory
)
195 <= (client
== NULL
&& buffer
!= NULL
));
197 /* actually do occasionally get a zero size map */
201 device_error(client
, "called on sim_core_map_attach with size zero");
203 sim_io_error (sd
, "called on sim_core_map_attach with size zero");
207 /* find the insertion point (between last/next) */
208 next_mapping
= access_map
->first
;
209 last_mapping
= &access_map
->first
;
210 while(next_mapping
!= NULL
211 && (next_mapping
->level
< (int) attach
212 || (next_mapping
->level
== (int) attach
213 && next_mapping
->bound
< addr
)))
215 /* provided levels are the same */
216 /* assert: next_mapping->base > all bases before next_mapping */
217 /* assert: next_mapping->bound >= all bounds before next_mapping */
218 last_mapping
= &next_mapping
->next
;
219 next_mapping
= next_mapping
->next
;
222 /* check insertion point correct */
223 SIM_ASSERT (next_mapping
== NULL
|| next_mapping
->level
>= (int) attach
);
224 if (next_mapping
!= NULL
&& next_mapping
->level
== (int) attach
225 && next_mapping
->base
< (addr
+ (nr_bytes
- 1)))
228 device_error (client
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
232 (long) (addr
+ (nr_bytes
- 1)),
234 (long) next_mapping
->base
,
235 (long) next_mapping
->bound
,
236 (long) next_mapping
->nr_bytes
);
238 sim_io_error (sd
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
242 (long) (addr
+ (nr_bytes
- 1)),
244 (long) next_mapping
->base
,
245 (long) next_mapping
->bound
,
246 (long) next_mapping
->nr_bytes
);
250 /* create/insert the new mapping */
251 *last_mapping
= new_sim_core_mapping(sd
,
253 space
, addr
, nr_bytes
, modulo
,
254 client
, buffer
, free_buffer
);
255 (*last_mapping
)->next
= next_mapping
;
261 sim_core_attach (SIM_DESC sd
,
267 address_word nr_bytes
,
270 void *optional_buffer
)
272 sim_core
*memory
= STATE_CORE(sd
);
277 /* check for for attempt to use unimplemented per-processor core map */
279 sim_io_error (sd
, "sim_core_map_attach - processor specific memory map not yet supported");
281 if ((access
& access_read_write_exec
) == 0
282 || (access
& ~access_read_write_exec
) != 0)
285 device_error(client
, "invalid access for core attach");
287 sim_io_error (sd
, "invalid access for core attach");
291 /* verify the attach type */
292 if (attach
== attach_raw_memory
)
294 if (WITH_MODULO_MEMORY
&& modulo
!= 0)
296 unsigned mask
= modulo
- 1;
297 if (mask
< 7) /* 8 is minimum modulo */
299 while (mask
> 1) /* no zero bits */
309 device_error (client
, "sim_core_attach - internal error - modulo not power of two");
311 sim_io_error (sd
, "sim_core_attach - internal error - modulo not power of two");
315 else if (!WITH_MODULO_MEMORY
&& modulo
!= 0)
318 device_error (client
, "sim_core_attach - internal error - modulo memory disabled");
320 sim_io_error (sd
, "sim_core_attach - internal error - modulo memory disabled");
323 if (optional_buffer
== NULL
)
325 buffer
= zalloc (modulo
== 0 ? nr_bytes
: modulo
);
330 buffer
= optional_buffer
;
334 else if (attach
>= attach_callback
)
342 device_error (client
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
344 sim_io_error (sd
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
350 /* attach the region to all applicable access maps */
352 map
< nr_sim_core_maps
;
357 case sim_core_read_map
:
358 if (access
& access_read
)
359 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
361 space
, addr
, nr_bytes
, modulo
,
362 client
, buffer
, !buffer_freed
);
365 case sim_core_write_map
:
366 if (access
& access_write
)
367 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
369 space
, addr
, nr_bytes
, modulo
,
370 client
, buffer
, !buffer_freed
);
373 case sim_core_execute_map
:
374 if (access
& access_exec
)
375 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
377 space
, addr
, nr_bytes
, modulo
,
378 client
, buffer
, !buffer_freed
);
381 case nr_sim_core_maps
:
382 sim_io_error (sd
, "sim_core_attach - internal error - bad switch");
387 /* Just copy this map to each of the processor specific data structures.
388 FIXME - later this will be replaced by true processor specific
392 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
394 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
400 /* Remove any memory reference related to this address */
401 STATIC_INLINE_SIM_CORE\
403 sim_core_map_detach (SIM_DESC sd
,
404 sim_core_map
*access_map
,
409 sim_core_mapping
**entry
;
410 for (entry
= &access_map
->first
;
412 entry
= &(*entry
)->next
)
414 if ((*entry
)->base
== addr
415 && (*entry
)->level
== (int) attach
416 && (*entry
)->space
== space
)
418 sim_core_mapping
*dead
= (*entry
);
419 (*entry
) = dead
->next
;
420 if (dead
->free_buffer
)
421 zfree (dead
->buffer
);
430 sim_core_detach (SIM_DESC sd
,
436 sim_core
*memory
= STATE_CORE (sd
);
438 for (map
= 0; map
< nr_sim_core_maps
; map
++)
440 sim_core_map_detach (sd
, &memory
->common
.map
[map
],
441 attach
, address_space
, addr
);
443 /* Just copy this update to each of the processor specific data
444 structures. FIXME - later this will be replaced by true
445 processor specific maps. */
448 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
450 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
456 STATIC_INLINE_SIM_CORE\
458 sim_core_find_mapping(sim_core_common
*core
,
462 transfer_type transfer
,
463 int abort
, /*either 0 or 1 - hint to inline/-O */
464 sim_cpu
*cpu
, /* abort => cpu != NULL */
467 sim_core_mapping
*mapping
= core
->map
[map
].first
;
468 ASSERT ((addr
& (nr_bytes
- 1)) == 0); /* must be aligned */
469 ASSERT ((addr
+ (nr_bytes
- 1)) >= addr
); /* must not wrap */
470 ASSERT (!abort
|| cpu
!= NULL
); /* abort needs a non null CPU */
471 while (mapping
!= NULL
)
473 if (addr
>= mapping
->base
474 && (addr
+ (nr_bytes
- 1)) <= mapping
->bound
)
476 mapping
= mapping
->next
;
480 SIM_CORE_SIGNAL (CPU_STATE (cpu
), cpu
, cia
, map
, nr_bytes
, addr
, transfer
,
481 sim_core_unmapped_signal
);
487 STATIC_INLINE_SIM_CORE\
489 sim_core_translate (sim_core_mapping
*mapping
,
492 if (WITH_MODULO_MEMORY
)
493 return (void *)((unsigned8
*) mapping
->buffer
494 + ((addr
- mapping
->base
) & mapping
->mask
));
496 return (void *)((unsigned8
*) mapping
->buffer
497 + addr
- mapping
->base
);
503 sim_core_read_buffer (SIM_DESC sd
,
510 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
512 while (count
< len
) {
513 unsigned_word raddr
= addr
+ count
;
514 sim_core_mapping
*mapping
=
515 sim_core_find_mapping(core
, map
,
516 raddr
, /*nr-bytes*/1,
518 0 /*dont-abort*/, NULL
, NULL_CIA
);
522 if (mapping
->device
!= NULL
) {
523 int nr_bytes
= len
- count
;
524 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
525 nr_bytes
= mapping
->bound
- raddr
+ 1;
526 if (device_io_read_buffer(mapping
->device
,
527 (unsigned_1
*)buffer
+ count
,
530 nr_bytes
) != nr_bytes
)
537 ((unsigned_1
*)buffer
)[count
] =
538 *(unsigned_1
*)sim_core_translate(mapping
, raddr
);
548 sim_core_write_buffer (SIM_DESC sd
,
555 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
557 while (count
< len
) {
558 unsigned_word raddr
= addr
+ count
;
559 sim_core_mapping
*mapping
=
560 sim_core_find_mapping(core
, map
,
561 raddr
, /*nr-bytes*/1,
563 0 /*dont-abort*/, NULL
, NULL_CIA
);
567 if (WITH_CALLBACK_MEMORY
568 && mapping
->device
!= NULL
) {
569 int nr_bytes
= len
- count
;
570 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
571 nr_bytes
= mapping
->bound
- raddr
+ 1;
572 if (device_io_write_buffer(mapping
->device
,
573 (unsigned_1
*)buffer
+ count
,
576 nr_bytes
) != nr_bytes
)
583 *(unsigned_1
*)sim_core_translate(mapping
, raddr
) =
584 ((unsigned_1
*)buffer
)[count
];
594 sim_core_set_xor (SIM_DESC sd
,
598 /* set up the XOR map if required. */
599 if (WITH_XOR_ENDIAN
) {
601 sim_core
*core
= STATE_CORE (sd
);
602 sim_cpu_core
*cpu_core
= (cpu
!= NULL
? CPU_CORE (cpu
) : NULL
);
603 if (cpu_core
!= NULL
)
608 mask
= WITH_XOR_ENDIAN
- 1;
611 while (i
- 1 < WITH_XOR_ENDIAN
)
613 cpu_core
->xor[i
-1] = mask
;
614 mask
= (mask
<< 1) & (WITH_XOR_ENDIAN
- 1);
621 core
->byte_xor
= WITH_XOR_ENDIAN
- 1;
629 sim_engine_abort (sd
, cpu
, NULL_CIA
,
630 "Attempted to enable xor-endian mode when permenantly disabled.");
634 STATIC_INLINE_SIM_CORE\
636 reverse_n (unsigned_1
*dest
,
637 const unsigned_1
*src
,
641 for (i
= 0; i
< nr_bytes
; i
++)
643 dest
[nr_bytes
- i
- 1] = src
[i
];
650 sim_core_xor_read_buffer (SIM_DESC sd
,
657 address_word byte_xor
= (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->xor[0]);
658 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
659 return sim_core_read_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
661 /* only break up transfers when xor-endian is both selected and enabled */
663 unsigned_1 x
[WITH_XOR_ENDIAN
];
664 unsigned nr_transfered
= 0;
665 address_word start
= addr
;
666 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
668 /* initial and intermediate transfers are broken when they cross
669 an XOR endian boundary */
670 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
671 /* initial/intermediate transfers */
673 /* since xor-endian is enabled stop^xor defines the start
674 address of the transfer */
675 stop
= start
+ nr_this_transfer
- 1;
676 SIM_ASSERT (start
<= stop
);
677 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
678 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
680 return nr_transfered
;
681 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
682 nr_transfered
+= nr_this_transfer
;
683 nr_this_transfer
= WITH_XOR_ENDIAN
;
687 nr_this_transfer
= nr_bytes
- nr_transfered
;
688 stop
= start
+ nr_this_transfer
- 1;
689 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
690 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
692 return nr_transfered
;
693 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
701 sim_core_xor_write_buffer (SIM_DESC sd
,
708 address_word byte_xor
= (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->xor[0]);
709 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
710 return sim_core_write_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
712 /* only break up transfers when xor-endian is both selected and enabled */
714 unsigned_1 x
[WITH_XOR_ENDIAN
];
715 unsigned nr_transfered
= 0;
716 address_word start
= addr
;
717 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
719 /* initial and intermediate transfers are broken when they cross
720 an XOR endian boundary */
721 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
722 /* initial/intermediate transfers */
724 /* since xor-endian is enabled stop^xor defines the start
725 address of the transfer */
726 stop
= start
+ nr_this_transfer
- 1;
727 SIM_ASSERT (start
<= stop
);
728 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
729 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
730 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
732 return nr_transfered
;
733 nr_transfered
+= nr_this_transfer
;
734 nr_this_transfer
= WITH_XOR_ENDIAN
;
738 nr_this_transfer
= nr_bytes
- nr_transfered
;
739 stop
= start
+ nr_this_transfer
- 1;
740 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
741 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
742 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
744 return nr_transfered
;
751 /* define the read/write 1/2/4/8/word functions */
754 #include "sim-n-core.h"
758 #include "sim-n-core.h"
762 #include "sim-n-core.h"
766 #include "sim-n-core.h"
770 #include "sim-n-core.h"