a5e3cdbb280a1baefdd3e5bb8df631be26f88253
[external/binutils.git] / sim / common / sim-core.c
1 /* The common simulator framework for GDB, the GNU Debugger.
2
3    Copyright 2002-2015 Free Software Foundation, Inc.
4
5    Contributed by Andrew Cagney and Red Hat.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22
23 #ifndef SIM_CORE_C
24 #define SIM_CORE_C
25
26 #include "sim-main.h"
27 #include "sim-assert.h"
28
29 #if (WITH_HW)
30 #include "sim-hw.h"
31 #define device_error(client, ...) device_error ((device *)(client), __VA_ARGS__)
32 #define device_io_read_buffer(client, ...) device_io_read_buffer ((device *)(client), __VA_ARGS__)
33 #define device_io_write_buffer(client, ...) device_io_write_buffer ((device *)(client), __VA_ARGS__)
34 #endif
35
36 /* "core" module install handler.
37
38    This is called via sim_module_install to install the "core"
39    subsystem into the simulator.  */
40
41 #if EXTERN_SIM_CORE_P
42 static MODULE_INIT_FN sim_core_init;
43 static MODULE_UNINSTALL_FN sim_core_uninstall;
44 #endif
45
46 #if EXTERN_SIM_CORE_P
47 SIM_RC
48 sim_core_install (SIM_DESC sd)
49 {
50   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
51
52   /* establish the other handlers */
53   sim_module_add_uninstall_fn (sd, sim_core_uninstall);
54   sim_module_add_init_fn (sd, sim_core_init);
55
56   /* establish any initial data structures - none */
57   return SIM_RC_OK;
58 }
59 #endif
60
61
62 /* Uninstall the "core" subsystem from the simulator.  */
63
64 #if EXTERN_SIM_CORE_P
65 static void
66 sim_core_uninstall (SIM_DESC sd)
67 {
68   sim_core *core = STATE_CORE (sd);
69   unsigned map;
70   /* blow away any mappings */
71   for (map = 0; map < nr_maps; map++) {
72     sim_core_mapping *curr = core->common.map[map].first;
73     while (curr != NULL) {
74       sim_core_mapping *tbd = curr;
75       curr = curr->next;
76       if (tbd->free_buffer != NULL) {
77         SIM_ASSERT (tbd->buffer != NULL);
78         free (tbd->free_buffer);
79       }
80       free (tbd);
81     }
82     core->common.map[map].first = NULL;
83   }
84 }
85 #endif
86
87
88 #if EXTERN_SIM_CORE_P
89 static SIM_RC
90 sim_core_init (SIM_DESC sd)
91 {
92   /* Nothing to do */
93   return SIM_RC_OK;
94 }
95 #endif
96
97
98
99 #ifndef SIM_CORE_SIGNAL
100 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
101 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
102 #endif
103
104 #if EXTERN_SIM_CORE_P
105 void
106 sim_core_signal (SIM_DESC sd,
107                  sim_cpu *cpu,
108                  sim_cia cia,
109                  unsigned map,
110                  int nr_bytes,
111                  address_word addr,
112                  transfer_type transfer,
113                  sim_core_signals sig)
114 {
115   const char *copy = (transfer == read_transfer ? "read" : "write");
116   address_word ip = CIA_ADDR (cia);
117   switch (sig)
118     {
119     case sim_core_unmapped_signal:
120       sim_io_eprintf (sd, "core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
121                       nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
122       sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGSEGV);
123       break;
124     case sim_core_unaligned_signal:
125       sim_io_eprintf (sd, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
126                       nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
127       sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGBUS);
128       break;
129     default:
130       sim_engine_abort (sd, cpu, cia,
131                         "sim_core_signal - internal error - bad switch");
132     }
133 }
134 #endif
135
136
137 #if EXTERN_SIM_CORE_P
138 static sim_core_mapping *
139 new_sim_core_mapping (SIM_DESC sd,
140                       int level,
141                       int space,
142                       address_word addr,
143                       address_word nr_bytes,
144                       unsigned modulo,
145 #if WITH_HW
146                       struct hw *device,
147 #else
148                       device *device,
149 #endif
150                       void *buffer,
151                       void *free_buffer)
152 {
153   sim_core_mapping *new_mapping = ZALLOC (sim_core_mapping);
154   /* common */
155   new_mapping->level = level;
156   new_mapping->space = space;
157   new_mapping->base = addr;
158   new_mapping->nr_bytes = nr_bytes;
159   new_mapping->bound = addr + (nr_bytes - 1);
160   new_mapping->mask = modulo - 1;
161   new_mapping->buffer = buffer;
162   new_mapping->free_buffer = free_buffer;
163   new_mapping->device = device;
164   return new_mapping;
165 }
166 #endif
167
168
169 #if EXTERN_SIM_CORE_P
170 static void
171 sim_core_map_attach (SIM_DESC sd,
172                      sim_core_map *access_map,
173                      int level,
174                      int space,
175                      address_word addr,
176                      address_word nr_bytes,
177                      unsigned modulo,
178 #if WITH_HW
179                      struct hw *client, /*callback/default*/
180 #else
181                      device *client, /*callback/default*/
182 #endif
183                      void *buffer, /*raw_memory*/
184                      void *free_buffer) /*raw_memory*/
185 {
186   /* find the insertion point for this additional mapping and then
187      insert */
188   sim_core_mapping *next_mapping;
189   sim_core_mapping **last_mapping;
190
191   SIM_ASSERT ((client == NULL) != (buffer == NULL));
192   SIM_ASSERT ((client == NULL) >= (free_buffer != NULL));
193
194   /* actually do occasionally get a zero size map */
195   if (nr_bytes == 0)
196     {
197 #if (WITH_DEVICES)
198       device_error (client, "called on sim_core_map_attach with size zero");
199 #endif
200 #if (WITH_HW)
201       sim_hw_abort (sd, client, "called on sim_core_map_attach with size zero");
202 #endif
203       sim_io_error (sd, "called on sim_core_map_attach with size zero");
204     }
205
206   /* find the insertion point (between last/next) */
207   next_mapping = access_map->first;
208   last_mapping = &access_map->first;
209   while (next_mapping != NULL
210         && (next_mapping->level < level
211             || (next_mapping->level == level
212                 && next_mapping->bound < addr)))
213     {
214       /* provided levels are the same */
215       /* assert: next_mapping->base > all bases before next_mapping */
216       /* assert: next_mapping->bound >= all bounds before next_mapping */
217       last_mapping = &next_mapping->next;
218       next_mapping = next_mapping->next;
219     }
220
221   /* check insertion point correct */
222   SIM_ASSERT (next_mapping == NULL || next_mapping->level >= level);
223   if (next_mapping != NULL && next_mapping->level == level
224       && next_mapping->base < (addr + (nr_bytes - 1)))
225     {
226 #if (WITH_DEVICES)
227       device_error (client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
228                     space,
229                     (long) addr,
230                     (long) (addr + nr_bytes - 1),
231                     (long) nr_bytes,
232                     next_mapping->space,
233                     (long) next_mapping->base,
234                     (long) next_mapping->bound,
235                     (long) next_mapping->nr_bytes);
236 #endif
237 #if WITH_HW
238       sim_hw_abort (sd, client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
239                     space,
240                     (long) addr,
241                     (long) (addr + (nr_bytes - 1)),
242                     (long) nr_bytes,
243                     next_mapping->space,
244                     (long) next_mapping->base,
245                     (long) next_mapping->bound,
246                     (long) next_mapping->nr_bytes);
247 #endif
248       sim_io_error (sd, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
249                     space,
250                     (long) addr,
251                     (long) (addr + (nr_bytes - 1)),
252                     (long) nr_bytes,
253                     next_mapping->space,
254                     (long) next_mapping->base,
255                     (long) next_mapping->bound,
256                     (long) next_mapping->nr_bytes);
257   }
258
259   /* create/insert the new mapping */
260   *last_mapping = new_sim_core_mapping (sd,
261                                         level,
262                                         space, addr, nr_bytes, modulo,
263                                         client, buffer, free_buffer);
264   (*last_mapping)->next = next_mapping;
265 }
266 #endif
267
268
269 /* Attach memory or a memory mapped device to the simulator.
270    See sim-core.h for a full description.  */
271
272 #if EXTERN_SIM_CORE_P
273 void
274 sim_core_attach (SIM_DESC sd,
275                  sim_cpu *cpu,
276                  int level,
277                  unsigned mapmask,
278                  int space,
279                  address_word addr,
280                  address_word nr_bytes,
281                  unsigned modulo,
282 #if WITH_HW
283                  struct hw *client,
284 #else
285                  device *client,
286 #endif
287                  void *optional_buffer)
288 {
289   sim_core *memory = STATE_CORE (sd);
290   unsigned map;
291   void *buffer;
292   void *free_buffer;
293
294   /* check for for attempt to use unimplemented per-processor core map */
295   if (cpu != NULL)
296     sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
297
298   if (client != NULL && modulo != 0)
299     {
300 #if (WITH_DEVICES)
301       device_error (client, "sim_core_attach - internal error - modulo and callback memory conflict");
302 #endif
303 #if (WITH_HW)
304       sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo and callback memory conflict");
305 #endif
306       sim_io_error (sd, "sim_core_attach - internal error - modulo and callback memory conflict");
307     }
308   if (modulo != 0)
309     {
310       unsigned mask = modulo - 1;
311       /* any zero bits */
312       while (mask >= sizeof (unsigned64)) /* minimum modulo */
313         {
314           if ((mask & 1) == 0)
315             mask = 0;
316           else
317             mask >>= 1;
318         }
319       if (mask != sizeof (unsigned64) - 1)
320         {
321 #if (WITH_DEVICES)
322           device_error (client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
323 #endif
324 #if (WITH_HW)
325           sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
326 #endif
327           sim_io_error (sd, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
328         }
329     }
330
331   /* verify consistency between device and buffer */
332   if (client != NULL && optional_buffer != NULL)
333     {
334 #if (WITH_DEVICES)
335       device_error (client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
336 #endif
337 #if (WITH_HW)
338       sim_hw_abort (sd, client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
339 #endif
340       sim_io_error (sd, "sim_core_attach - internal error - conflicting buffer and attach arguments");
341     }
342   if (client == NULL)
343     {
344       if (optional_buffer == NULL)
345         {
346           int padding = (addr % sizeof (unsigned64));
347           unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
348           free_buffer = zalloc (bytes);
349           buffer = (char*) free_buffer + padding;
350         }
351       else
352         {
353           buffer = optional_buffer;
354           free_buffer = NULL;
355         }
356     }
357   else
358     {
359       /* a device */
360       buffer = NULL;
361       free_buffer = NULL;
362     }
363
364   /* attach the region to all applicable access maps */
365   for (map = 0;
366        map < nr_maps;
367        map++)
368     {
369       if (mapmask & (1 << map))
370         {
371           sim_core_map_attach (sd, &memory->common.map[map],
372                                level, space, addr, nr_bytes, modulo,
373                                client, buffer, free_buffer);
374           free_buffer = NULL;
375         }
376     }
377
378   /* Just copy this map to each of the processor specific data structures.
379      FIXME - later this will be replaced by true processor specific
380      maps. */
381   {
382     int i;
383     for (i = 0; i < MAX_NR_PROCESSORS; i++)
384       {
385         CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
386       }
387   }
388 }
389 #endif
390
391
392 /* Remove any memory reference related to this address */
393 #if EXTERN_SIM_CORE_P
394 static void
395 sim_core_map_detach (SIM_DESC sd,
396                      sim_core_map *access_map,
397                      int level,
398                      int space,
399                      address_word addr)
400 {
401   sim_core_mapping **entry;
402   for (entry = &access_map->first;
403        (*entry) != NULL;
404        entry = &(*entry)->next)
405     {
406       if ((*entry)->base == addr
407           && (*entry)->level == level
408           && (*entry)->space == space)
409         {
410           sim_core_mapping *dead = (*entry);
411           (*entry) = dead->next;
412           if (dead->free_buffer != NULL)
413             free (dead->free_buffer);
414           free (dead);
415           return;
416         }
417     }
418 }
419 #endif
420
421 #if EXTERN_SIM_CORE_P
422 void
423 sim_core_detach (SIM_DESC sd,
424                  sim_cpu *cpu,
425                  int level,
426                  int address_space,
427                  address_word addr)
428 {
429   sim_core *memory = STATE_CORE (sd);
430   unsigned map;
431   for (map = 0; map < nr_maps; map++)
432     {
433       sim_core_map_detach (sd, &memory->common.map[map],
434                            level, address_space, addr);
435     }
436   /* Just copy this update to each of the processor specific data
437      structures.  FIXME - later this will be replaced by true
438      processor specific maps. */
439   {
440     int i;
441     for (i = 0; i < MAX_NR_PROCESSORS; i++)
442       {
443         CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
444       }
445   }
446 }
447 #endif
448
449
450 STATIC_INLINE_SIM_CORE\
451 (sim_core_mapping *)
452 sim_core_find_mapping (sim_core_common *core,
453                        unsigned map,
454                        address_word addr,
455                        unsigned nr_bytes,
456                        transfer_type transfer,
457                        int abort, /*either 0 or 1 - hint to inline/-O */
458                        sim_cpu *cpu, /* abort => cpu != NULL */
459                        sim_cia cia)
460 {
461   sim_core_mapping *mapping = core->map[map].first;
462   ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
463   ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
464   ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
465   while (mapping != NULL)
466     {
467       if (addr >= mapping->base
468           && (addr + (nr_bytes - 1)) <= mapping->bound)
469         return mapping;
470       mapping = mapping->next;
471     }
472   if (abort)
473     {
474       SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map, nr_bytes, addr, transfer,
475                        sim_core_unmapped_signal);
476     }
477   return NULL;
478 }
479
480
481 STATIC_INLINE_SIM_CORE\
482 (void *)
483 sim_core_translate (sim_core_mapping *mapping,
484                     address_word addr)
485 {
486   return (void *)((unsigned8 *) mapping->buffer
487                   + ((addr - mapping->base) & mapping->mask));
488 }
489
490
491 #if EXTERN_SIM_CORE_P
492 unsigned
493 sim_core_read_buffer (SIM_DESC sd,
494                       sim_cpu *cpu,
495                       unsigned map,
496                       void *buffer,
497                       address_word addr,
498                       unsigned len)
499 {
500   sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
501   unsigned count = 0;
502   while (count < len)
503  {
504     address_word raddr = addr + count;
505     sim_core_mapping *mapping =
506       sim_core_find_mapping (core, map,
507                             raddr, /*nr-bytes*/1,
508                             read_transfer,
509                             0 /*dont-abort*/, NULL, NULL_CIA);
510     if (mapping == NULL)
511       break;
512 #if (WITH_DEVICES)
513     if (mapping->device != NULL)
514       {
515         int nr_bytes = len - count;
516         sim_cia cia = cpu ? CPU_PC_GET (cpu) : NULL_CIA;
517         if (raddr + nr_bytes - 1> mapping->bound)
518           nr_bytes = mapping->bound - raddr + 1;
519         if (device_io_read_buffer (mapping->device,
520                                    (unsigned_1*)buffer + count,
521                                    mapping->space,
522                                    raddr,
523                                    nr_bytes,
524                                    sd,
525                                    cpu,
526                                    cia) != nr_bytes)
527           break;
528         count += nr_bytes;
529         continue;
530       }
531 #endif
532 #if (WITH_HW)
533     if (mapping->device != NULL)
534       {
535         int nr_bytes = len - count;
536         if (raddr + nr_bytes - 1> mapping->bound)
537           nr_bytes = mapping->bound - raddr + 1;
538         if (sim_hw_io_read_buffer (sd, mapping->device,
539                                    (unsigned_1*)buffer + count,
540                                    mapping->space,
541                                    raddr,
542                                    nr_bytes) != nr_bytes)
543           break;
544         count += nr_bytes;
545         continue;
546       }
547 #endif
548     ((unsigned_1*)buffer)[count] =
549       *(unsigned_1*)sim_core_translate (mapping, raddr);
550     count += 1;
551  }
552   return count;
553 }
554 #endif
555
556
557 #if EXTERN_SIM_CORE_P
558 unsigned
559 sim_core_write_buffer (SIM_DESC sd,
560                        sim_cpu *cpu,
561                        unsigned map,
562                        const void *buffer,
563                        address_word addr,
564                        unsigned len)
565 {
566   sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
567   unsigned count = 0;
568   while (count < len)
569     {
570       address_word raddr = addr + count;
571       sim_core_mapping *mapping =
572         sim_core_find_mapping (core, map,
573                                raddr, /*nr-bytes*/1,
574                                write_transfer,
575                                0 /*dont-abort*/, NULL, NULL_CIA);
576       if (mapping == NULL)
577         break;
578 #if (WITH_DEVICES)
579       if (mapping->device != NULL)
580         {
581           int nr_bytes = len - count;
582           sim_cia cia = cpu ? CPU_PC_GET (cpu) : NULL_CIA;
583           if (raddr + nr_bytes - 1 > mapping->bound)
584             nr_bytes = mapping->bound - raddr + 1;
585           if (device_io_write_buffer (mapping->device,
586                                       (unsigned_1*)buffer + count,
587                                       mapping->space,
588                                       raddr,
589                                       nr_bytes,
590                                       sd,
591                                       cpu,
592                                       cia) != nr_bytes)
593             break;
594           count += nr_bytes;
595           continue;
596         }
597 #endif
598 #if (WITH_HW)
599       if (mapping->device != NULL)
600         {
601           int nr_bytes = len - count;
602           if (raddr + nr_bytes - 1 > mapping->bound)
603             nr_bytes = mapping->bound - raddr + 1;
604           if (sim_hw_io_write_buffer (sd, mapping->device,
605                                       (unsigned_1*)buffer + count,
606                                       mapping->space,
607                                       raddr,
608                                       nr_bytes) != nr_bytes)
609             break;
610           count += nr_bytes;
611           continue;
612         }
613 #endif
614       *(unsigned_1*)sim_core_translate (mapping, raddr) =
615         ((unsigned_1*)buffer)[count];
616       count += 1;
617     }
618   return count;
619 }
620 #endif
621
622
623 #if EXTERN_SIM_CORE_P
624 void
625 sim_core_set_xor (SIM_DESC sd,
626                   sim_cpu *cpu,
627                   int is_xor)
628 {
629   /* set up the XOR map if required. */
630   if (WITH_XOR_ENDIAN) {
631     {
632       sim_core *core = STATE_CORE (sd);
633       sim_cpu_core *cpu_core = (cpu != NULL ? CPU_CORE (cpu) : NULL);
634       if (cpu_core != NULL)
635         {
636           int i = 1;
637           unsigned mask;
638           if (is_xor)
639             mask = WITH_XOR_ENDIAN - 1;
640           else
641             mask = 0;
642           while (i - 1 < WITH_XOR_ENDIAN)
643             {
644               cpu_core->xor[i-1] = mask;
645               mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
646               i = (i << 1);
647             }
648         }
649       else
650         {
651           if (is_xor)
652             core->byte_xor = WITH_XOR_ENDIAN - 1;
653           else
654             core->byte_xor = 0;
655         }
656     }
657   }
658   else {
659     if (is_xor)
660       sim_engine_abort (sd, NULL, NULL_CIA,
661                         "Attempted to enable xor-endian mode when permenantly disabled.");
662   }
663 }
664 #endif
665
666
667 #if EXTERN_SIM_CORE_P
668 static void
669 reverse_n (unsigned_1 *dest,
670            const unsigned_1 *src,
671            int nr_bytes)
672 {
673   int i;
674   for (i = 0; i < nr_bytes; i++)
675     {
676       dest [nr_bytes - i - 1] = src [i];
677     }
678 }
679 #endif
680
681
682 #if EXTERN_SIM_CORE_P
683 unsigned
684 sim_core_xor_read_buffer (SIM_DESC sd,
685                           sim_cpu *cpu,
686                           unsigned map,
687                           void *buffer,
688                           address_word addr,
689                           unsigned nr_bytes)
690 {
691   address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
692   if (!WITH_XOR_ENDIAN || !byte_xor)
693     return sim_core_read_buffer (sd, cpu, map, buffer, addr, nr_bytes);
694   else
695     /* only break up transfers when xor-endian is both selected and enabled */
696     {
697       unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero-sized array */
698       unsigned nr_transfered = 0;
699       address_word start = addr;
700       unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
701       address_word stop;
702       /* initial and intermediate transfers are broken when they cross
703          an XOR endian boundary */
704       while (nr_transfered + nr_this_transfer < nr_bytes)
705         /* initial/intermediate transfers */
706         {
707           /* since xor-endian is enabled stop^xor defines the start
708              address of the transfer */
709           stop = start + nr_this_transfer - 1;
710           SIM_ASSERT (start <= stop);
711           SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
712           if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
713               != nr_this_transfer)
714             return nr_transfered;
715           reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
716           nr_transfered += nr_this_transfer;
717           nr_this_transfer = WITH_XOR_ENDIAN;
718           start = stop + 1;
719         }
720       /* final transfer */
721       nr_this_transfer = nr_bytes - nr_transfered;
722       stop = start + nr_this_transfer - 1;
723       SIM_ASSERT (stop == (addr + nr_bytes - 1));
724       if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
725           != nr_this_transfer)
726         return nr_transfered;
727       reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
728       return nr_bytes;
729     }
730 }
731 #endif
732
733
734 #if EXTERN_SIM_CORE_P
735 unsigned
736 sim_core_xor_write_buffer (SIM_DESC sd,
737                            sim_cpu *cpu,
738                            unsigned map,
739                            const void *buffer,
740                            address_word addr,
741                            unsigned nr_bytes)
742 {
743   address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
744   if (!WITH_XOR_ENDIAN || !byte_xor)
745     return sim_core_write_buffer (sd, cpu, map, buffer, addr, nr_bytes);
746   else
747     /* only break up transfers when xor-endian is both selected and enabled */
748     {
749       unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero sized array */
750       unsigned nr_transfered = 0;
751       address_word start = addr;
752       unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
753       address_word stop;
754       /* initial and intermediate transfers are broken when they cross
755          an XOR endian boundary */
756       while (nr_transfered + nr_this_transfer < nr_bytes)
757         /* initial/intermediate transfers */
758         {
759           /* since xor-endian is enabled stop^xor defines the start
760              address of the transfer */
761           stop = start + nr_this_transfer - 1;
762           SIM_ASSERT (start <= stop);
763           SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
764           reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
765           if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
766               != nr_this_transfer)
767             return nr_transfered;
768           nr_transfered += nr_this_transfer;
769           nr_this_transfer = WITH_XOR_ENDIAN;
770           start = stop + 1;
771         }
772       /* final transfer */
773       nr_this_transfer = nr_bytes - nr_transfered;
774       stop = start + nr_this_transfer - 1;
775       SIM_ASSERT (stop == (addr + nr_bytes - 1));
776       reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
777       if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
778           != nr_this_transfer)
779         return nr_transfered;
780       return nr_bytes;
781     }
782 }
783 #endif
784
785 #if EXTERN_SIM_CORE_P
786 void *
787 sim_core_trans_addr (SIM_DESC sd,
788                      sim_cpu *cpu,
789                      unsigned map,
790                      address_word addr)
791 {
792   sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
793   sim_core_mapping *mapping =
794     sim_core_find_mapping (core, map,
795                            addr, /*nr-bytes*/1,
796                            write_transfer,
797                            0 /*dont-abort*/, NULL, NULL_CIA);
798   if (mapping == NULL)
799     return NULL;
800   return sim_core_translate (mapping, addr);
801 }
802 #endif
803
804
805
806 /* define the read/write 1/2/4/8/16/word functions */
807
808 #define N 16
809 #include "sim-n-core.h"
810
811 #define N 8
812 #include "sim-n-core.h"
813
814 #define N 7
815 #define M 8
816 #include "sim-n-core.h"
817
818 #define N 6
819 #define M 8
820 #include "sim-n-core.h"
821
822 #define N 5
823 #define M 8
824 #include "sim-n-core.h"
825
826 #define N 4
827 #include "sim-n-core.h"
828
829 #define N 3
830 #define M 4
831 #include "sim-n-core.h"
832
833 #define N 2
834 #include "sim-n-core.h"
835
836 #define N 1
837 #include "sim-n-core.h"
838
839 #endif