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