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"
32 /* "core" module install handler.
34 This is called via sim_module_install to install the "core"
35 subsystem into the simulator. */
38 static MODULE_INIT_FN sim_core_init
;
39 static MODULE_UNINSTALL_FN sim_core_uninstall
;
44 sim_core_install (SIM_DESC sd
)
46 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
48 /* establish the other handlers */
49 sim_module_add_uninstall_fn (sd
, sim_core_uninstall
);
50 sim_module_add_init_fn (sd
, sim_core_init
);
52 /* establish any initial data structures - none */
58 /* Uninstall the "core" subsystem from the simulator. */
62 sim_core_uninstall (SIM_DESC sd
)
64 sim_core
*core
= STATE_CORE(sd
);
66 /* blow away any mappings */
67 for (map
= 0; map
< nr_maps
; map
++) {
68 sim_core_mapping
*curr
= core
->common
.map
[map
].first
;
69 while (curr
!= NULL
) {
70 sim_core_mapping
*tbd
= curr
;
72 if (tbd
->free_buffer
!= NULL
) {
73 SIM_ASSERT(tbd
->buffer
!= NULL
);
74 zfree(tbd
->free_buffer
);
78 core
->common
.map
[map
].first
= NULL
;
86 sim_core_init (SIM_DESC sd
)
95 #ifndef SIM_CORE_SIGNAL
96 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
97 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
100 #if EXTERN_SIM_CORE_P
102 sim_core_signal (SIM_DESC sd
,
108 transfer_type transfer
,
109 sim_core_signals sig
)
111 const char *copy
= (transfer
== read_transfer
? "read" : "write");
112 address_word ip
= CIA_ADDR (cia
);
115 case sim_core_unmapped_signal
:
116 sim_io_eprintf (sd
, "core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
117 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
118 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGSEGV
);
120 case sim_core_unaligned_signal
:
121 sim_io_eprintf (sd
, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
122 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
123 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGBUS
);
126 sim_engine_abort (sd
, cpu
, cia
,
127 "sim_core_signal - internal error - bad switch");
133 #if EXTERN_SIM_CORE_P
134 static sim_core_mapping
*
135 new_sim_core_mapping (SIM_DESC sd
,
139 address_word nr_bytes
,
149 sim_core_mapping
*new_mapping
= ZALLOC(sim_core_mapping
);
151 new_mapping
->level
= level
;
152 new_mapping
->space
= space
;
153 new_mapping
->base
= addr
;
154 new_mapping
->nr_bytes
= nr_bytes
;
155 new_mapping
->bound
= addr
+ (nr_bytes
- 1);
157 new_mapping
->mask
= (unsigned) 0 - 1;
159 new_mapping
->mask
= modulo
- 1;
160 new_mapping
->buffer
= buffer
;
161 new_mapping
->free_buffer
= free_buffer
;
162 new_mapping
->device
= device
;
168 #if EXTERN_SIM_CORE_P
170 sim_core_map_attach (SIM_DESC sd
,
171 sim_core_map
*access_map
,
175 address_word nr_bytes
,
178 struct hw
*client
, /*callback/default*/
180 device
*client
, /*callback/default*/
182 void *buffer
, /*raw_memory*/
183 void *free_buffer
) /*raw_memory*/
185 /* find the insertion point for this additional mapping and then
187 sim_core_mapping
*next_mapping
;
188 sim_core_mapping
**last_mapping
;
190 SIM_ASSERT ((client
== NULL
) != (buffer
== NULL
));
191 SIM_ASSERT ((client
== NULL
) >= (free_buffer
!= NULL
));
193 /* actually do occasionally get a zero size map */
197 device_error(client
, "called on sim_core_map_attach with size zero");
200 sim_hw_abort (sd
, client
, "called on sim_core_map_attach with size zero");
202 sim_io_error (sd
, "called on sim_core_map_attach with size zero");
205 /* find the insertion point (between last/next) */
206 next_mapping
= access_map
->first
;
207 last_mapping
= &access_map
->first
;
208 while(next_mapping
!= NULL
209 && (next_mapping
->level
< level
210 || (next_mapping
->level
== level
211 && next_mapping
->bound
< addr
)))
213 /* provided levels are the same */
214 /* assert: next_mapping->base > all bases before next_mapping */
215 /* assert: next_mapping->bound >= all bounds before next_mapping */
216 last_mapping
= &next_mapping
->next
;
217 next_mapping
= next_mapping
->next
;
220 /* check insertion point correct */
221 SIM_ASSERT (next_mapping
== NULL
|| next_mapping
->level
>= level
);
222 if (next_mapping
!= NULL
&& next_mapping
->level
== level
223 && next_mapping
->base
< (addr
+ (nr_bytes
- 1)))
226 device_error (client
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
230 (long) (addr
+ (nr_bytes
- 1)),
232 (long) next_mapping
->base
,
233 (long) next_mapping
->bound
,
234 (long) next_mapping
->nr_bytes
);
237 sim_hw_abort (sd
, client
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
241 (long) (addr
+ (nr_bytes
- 1)),
243 (long) next_mapping
->base
,
244 (long) next_mapping
->bound
,
245 (long) next_mapping
->nr_bytes
);
247 sim_io_error (sd
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
251 (long) (addr
+ (nr_bytes
- 1)),
253 (long) next_mapping
->base
,
254 (long) next_mapping
->bound
,
255 (long) next_mapping
->nr_bytes
);
258 /* create/insert the new mapping */
259 *last_mapping
= new_sim_core_mapping(sd
,
261 space
, addr
, nr_bytes
, modulo
,
262 client
, buffer
, free_buffer
);
263 (*last_mapping
)->next
= next_mapping
;
268 /* Attach memory or a memory mapped device to the simulator.
269 See sim-core.h for a full description. */
271 #if EXTERN_SIM_CORE_P
273 sim_core_attach (SIM_DESC sd
,
279 address_word nr_bytes
,
286 void *optional_buffer
)
288 sim_core
*memory
= STATE_CORE(sd
);
293 /* check for for attempt to use unimplemented per-processor core map */
295 sim_io_error (sd
, "sim_core_map_attach - processor specific memory map not yet supported");
297 /* verify modulo memory */
298 if (!WITH_MODULO_MEMORY
&& modulo
!= 0)
301 device_error (client
, "sim_core_attach - internal error - modulo memory disabled");
304 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo memory disabled");
306 sim_io_error (sd
, "sim_core_attach - internal error - modulo memory disabled");
308 if (client
!= NULL
&& modulo
!= 0)
311 device_error (client
, "sim_core_attach - internal error - modulo and callback memory conflict");
314 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo and callback memory conflict");
316 sim_io_error (sd
, "sim_core_attach - internal error - modulo and callback memory conflict");
320 unsigned mask
= modulo
- 1;
322 while (mask
>= sizeof (unsigned64
)) /* minimum modulo */
329 if (mask
!= sizeof (unsigned64
) - 1)
332 device_error (client
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
335 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
337 sim_io_error (sd
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
341 /* verify consistency between device and buffer */
342 if (client
!= NULL
&& optional_buffer
!= NULL
)
345 device_error (client
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
348 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
350 sim_io_error (sd
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
354 if (optional_buffer
== NULL
)
356 int padding
= (addr
% sizeof (unsigned64
));
357 unsigned long bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
358 free_buffer
= zalloc (bytes
);
359 buffer
= (char*) free_buffer
+ padding
;
363 buffer
= optional_buffer
;
374 /* attach the region to all applicable access maps */
379 if (mapmask
& (1 << map
))
381 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
382 level
, space
, addr
, nr_bytes
, modulo
,
383 client
, buffer
, free_buffer
);
388 /* Just copy this map to each of the processor specific data structures.
389 FIXME - later this will be replaced by true processor specific
393 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
395 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
402 /* Remove any memory reference related to this address */
403 #if EXTERN_SIM_CORE_P
405 sim_core_map_detach (SIM_DESC sd
,
406 sim_core_map
*access_map
,
411 sim_core_mapping
**entry
;
412 for (entry
= &access_map
->first
;
414 entry
= &(*entry
)->next
)
416 if ((*entry
)->base
== addr
417 && (*entry
)->level
== level
418 && (*entry
)->space
== space
)
420 sim_core_mapping
*dead
= (*entry
);
421 (*entry
) = dead
->next
;
422 if (dead
->free_buffer
!= NULL
)
423 zfree (dead
->free_buffer
);
431 #if EXTERN_SIM_CORE_P
433 sim_core_detach (SIM_DESC sd
,
439 sim_core
*memory
= STATE_CORE (sd
);
441 for (map
= 0; map
< nr_maps
; map
++)
443 sim_core_map_detach (sd
, &memory
->common
.map
[map
],
444 level
, address_space
, addr
);
446 /* Just copy this update to each of the processor specific data
447 structures. FIXME - later this will be replaced by true
448 processor specific maps. */
451 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
453 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
460 STATIC_INLINE_SIM_CORE\
462 sim_core_find_mapping(sim_core_common
*core
,
466 transfer_type transfer
,
467 int abort
, /*either 0 or 1 - hint to inline/-O */
468 sim_cpu
*cpu
, /* abort => cpu != NULL */
471 sim_core_mapping
*mapping
= core
->map
[map
].first
;
472 ASSERT ((addr
& (nr_bytes
- 1)) == 0); /* must be aligned */
473 ASSERT ((addr
+ (nr_bytes
- 1)) >= addr
); /* must not wrap */
474 ASSERT (!abort
|| cpu
!= NULL
); /* abort needs a non null CPU */
475 while (mapping
!= NULL
)
477 if (addr
>= mapping
->base
478 && (addr
+ (nr_bytes
- 1)) <= mapping
->bound
)
480 mapping
= mapping
->next
;
484 SIM_CORE_SIGNAL (CPU_STATE (cpu
), cpu
, cia
, map
, nr_bytes
, addr
, transfer
,
485 sim_core_unmapped_signal
);
491 STATIC_INLINE_SIM_CORE\
493 sim_core_translate (sim_core_mapping
*mapping
,
496 if (WITH_MODULO_MEMORY
)
497 return (void *)((unsigned8
*) mapping
->buffer
498 + ((addr
- mapping
->base
) & mapping
->mask
));
500 return (void *)((unsigned8
*) mapping
->buffer
501 + addr
- mapping
->base
);
505 #if EXTERN_SIM_CORE_P
507 sim_core_read_buffer (SIM_DESC sd
,
514 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
518 unsigned_word raddr
= addr
+ count
;
519 sim_core_mapping
*mapping
=
520 sim_core_find_mapping (core
, map
,
521 raddr
, /*nr-bytes*/1,
523 0 /*dont-abort*/, NULL
, NULL_CIA
);
527 if (mapping
->device
!= NULL
)
529 int nr_bytes
= len
- count
;
530 sim_cia cia
= cpu
? CIA_GET (cpu
) : NULL_CIA
;
531 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
532 nr_bytes
= mapping
->bound
- raddr
+ 1;
533 if (device_io_read_buffer (mapping
->device
,
534 (unsigned_1
*)buffer
+ count
,
547 if (mapping
->device
!= NULL
)
549 int nr_bytes
= len
- count
;
550 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
551 nr_bytes
= mapping
->bound
- raddr
+ 1;
552 if (sim_hw_io_read_buffer (sd
, mapping
->device
,
553 (unsigned_1
*)buffer
+ count
,
556 nr_bytes
) != nr_bytes
)
562 ((unsigned_1
*)buffer
)[count
] =
563 *(unsigned_1
*)sim_core_translate(mapping
, raddr
);
571 #if EXTERN_SIM_CORE_P
573 sim_core_write_buffer (SIM_DESC sd
,
580 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
584 unsigned_word raddr
= addr
+ count
;
585 sim_core_mapping
*mapping
=
586 sim_core_find_mapping (core
, map
,
587 raddr
, /*nr-bytes*/1,
589 0 /*dont-abort*/, NULL
, NULL_CIA
);
593 if (WITH_CALLBACK_MEMORY
594 && mapping
->device
!= NULL
)
596 int nr_bytes
= len
- count
;
597 sim_cia cia
= cpu
? CIA_GET (cpu
) : NULL_CIA
;
598 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
599 nr_bytes
= mapping
->bound
- raddr
+ 1;
600 if (device_io_write_buffer (mapping
->device
,
601 (unsigned_1
*)buffer
+ count
,
614 if (WITH_CALLBACK_MEMORY
615 && mapping
->device
!= NULL
)
617 int nr_bytes
= len
- count
;
618 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
619 nr_bytes
= mapping
->bound
- raddr
+ 1;
620 if (sim_hw_io_write_buffer (sd
, mapping
->device
,
621 (unsigned_1
*)buffer
+ count
,
624 nr_bytes
) != nr_bytes
)
630 *(unsigned_1
*)sim_core_translate(mapping
, raddr
) =
631 ((unsigned_1
*)buffer
)[count
];
639 #if EXTERN_SIM_CORE_P
641 sim_core_set_xor (SIM_DESC sd
,
645 /* set up the XOR map if required. */
646 if (WITH_XOR_ENDIAN
) {
648 sim_core
*core
= STATE_CORE (sd
);
649 sim_cpu_core
*cpu_core
= (cpu
!= NULL
? CPU_CORE (cpu
) : NULL
);
650 if (cpu_core
!= NULL
)
655 mask
= WITH_XOR_ENDIAN
- 1;
658 while (i
- 1 < WITH_XOR_ENDIAN
)
660 cpu_core
->xor[i
-1] = mask
;
661 mask
= (mask
<< 1) & (WITH_XOR_ENDIAN
- 1);
668 core
->byte_xor
= WITH_XOR_ENDIAN
- 1;
676 sim_engine_abort (sd
, NULL
, NULL_CIA
,
677 "Attempted to enable xor-endian mode when permenantly disabled.");
683 #if EXTERN_SIM_CORE_P
685 reverse_n (unsigned_1
*dest
,
686 const unsigned_1
*src
,
690 for (i
= 0; i
< nr_bytes
; i
++)
692 dest
[nr_bytes
- i
- 1] = src
[i
];
698 #if EXTERN_SIM_CORE_P
700 sim_core_xor_read_buffer (SIM_DESC sd
,
707 address_word byte_xor
= (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->xor[0]);
708 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
709 return sim_core_read_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
711 /* only break up transfers when xor-endian is both selected and enabled */
713 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero-sized array */
714 unsigned nr_transfered
= 0;
715 address_word start
= addr
;
716 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
718 /* initial and intermediate transfers are broken when they cross
719 an XOR endian boundary */
720 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
721 /* initial/intermediate transfers */
723 /* since xor-endian is enabled stop^xor defines the start
724 address of the transfer */
725 stop
= start
+ nr_this_transfer
- 1;
726 SIM_ASSERT (start
<= stop
);
727 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
728 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
730 return nr_transfered
;
731 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
732 nr_transfered
+= nr_this_transfer
;
733 nr_this_transfer
= WITH_XOR_ENDIAN
;
737 nr_this_transfer
= nr_bytes
- nr_transfered
;
738 stop
= start
+ nr_this_transfer
- 1;
739 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
740 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
742 return nr_transfered
;
743 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
750 #if EXTERN_SIM_CORE_P
752 sim_core_xor_write_buffer (SIM_DESC sd
,
759 address_word byte_xor
= (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->xor[0]);
760 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
761 return sim_core_write_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
763 /* only break up transfers when xor-endian is both selected and enabled */
765 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero sized array */
766 unsigned nr_transfered
= 0;
767 address_word start
= addr
;
768 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
770 /* initial and intermediate transfers are broken when they cross
771 an XOR endian boundary */
772 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
773 /* initial/intermediate transfers */
775 /* since xor-endian is enabled stop^xor defines the start
776 address of the transfer */
777 stop
= start
+ nr_this_transfer
- 1;
778 SIM_ASSERT (start
<= stop
);
779 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
780 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
781 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
783 return nr_transfered
;
784 nr_transfered
+= nr_this_transfer
;
785 nr_this_transfer
= WITH_XOR_ENDIAN
;
789 nr_this_transfer
= nr_bytes
- nr_transfered
;
790 stop
= start
+ nr_this_transfer
- 1;
791 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
792 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
793 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
795 return nr_transfered
;
803 /* define the read/write 1/2/4/8/16/word functions */
806 #include "sim-n-core.h"
809 #include "sim-n-core.h"
813 #include "sim-n-core.h"
817 #include "sim-n-core.h"
821 #include "sim-n-core.h"
824 #include "sim-n-core.h"
828 #include "sim-n-core.h"
831 #include "sim-n-core.h"
834 #include "sim-n-core.h"