Passify GCC. Convert 0x0LL to something more portable in the FP code.
[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
29 /* "core" module install handler.
30
31    This is called via sim_module_install to install the "core" subsystem
32    into the simulator.  */
33
34 static MODULE_INIT_FN sim_core_init;
35 static MODULE_UNINSTALL_FN sim_core_uninstall;
36
37 EXTERN_SIM_CORE\
38 (SIM_RC)
39 sim_core_install (SIM_DESC sd)
40 {
41   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
42   sim_module_add_uninstall_fn (sd, sim_core_uninstall);
43   sim_module_add_init_fn (sd, sim_core_init);
44   return SIM_RC_OK;
45 }
46
47
48 /* Uninstall the "core" subsystem from the simulator.  */
49
50 STATIC_SIM_CORE\
51 (void)
52 sim_core_uninstall (SIM_DESC sd)
53 {
54   /* FIXME: free buffers, etc. */
55 }
56
57
58 STATIC_SIM_CORE\
59 (SIM_RC)
60 sim_core_init (SIM_DESC sd)
61 {
62   sim_core *core = STATE_CORE(sd);
63   sim_core_maps map;
64   for (map = 0; map < nr_sim_core_maps; map++) {
65     /* blow away old mappings */
66     sim_core_mapping *curr = core->common.map[map].first;
67     while (curr != NULL) {
68       sim_core_mapping *tbd = curr;
69       curr = curr->next;
70       if (tbd->free_buffer) {
71         SIM_ASSERT(tbd->buffer != NULL);
72         zfree(tbd->buffer);
73       }
74       zfree(tbd);
75     }
76     core->common.map[map].first = NULL;
77   }
78   core->byte_xor = 0;
79   /* Just copy this map to each of the processor specific data structures.
80      FIXME - later this will be replaced by true processor specific
81      maps. */
82   {
83     int i;
84     for (i = 0; i < MAX_NR_PROCESSORS; i++)
85       {
86         int j;
87         CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
88         for (j = 0; j < WITH_XOR_ENDIAN; j++)
89           CPU_CORE (STATE_CPU (sd, i))->xor [j] = 0;
90       }
91   }
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
101 STATIC_SIM_CORE\
102 (void)
103 sim_core_signal (SIM_DESC sd,
104                  sim_cpu *cpu,
105                  sim_cia cia,
106                  sim_core_maps map,
107                  int nr_bytes,
108                  address_word addr,
109                  transfer_type transfer,
110                  sim_core_signals sig)
111 {
112   const char *copy = (transfer == read_transfer ? "read" : "write");
113   switch (sig)
114     {
115     case sim_core_unmapped_signal:
116       sim_engine_abort (sd, cpu, cia, "sim-core: %d byte %s to unmaped address 0x%lx",
117                         nr_bytes, copy, (unsigned long) addr);
118       break;
119     case sim_core_unaligned_signal:
120       sim_engine_abort (sd, cpu, cia, "sim-core: %d byte misaligned %s to address 0x%lx",
121                         nr_bytes, copy, (unsigned long) addr);
122       break;
123     default:
124       sim_engine_abort (sd, cpu, cia, "sim_core_signal - internal error - bad switch");
125     }
126 }
127 #endif
128
129
130 STATIC_INLINE_SIM_CORE\
131 (const char *)
132 sim_core_map_to_str (sim_core_maps map)
133 {
134   switch (map)
135     {
136     case sim_core_read_map: return "read";
137     case sim_core_write_map: return "write";
138     case sim_core_execute_map: return "exec";
139     default: return "(invalid-map)";
140     }
141 }
142
143
144 STATIC_SIM_CORE\
145 (sim_core_mapping *)
146 new_sim_core_mapping(SIM_DESC sd,
147                      attach_type attach,
148                      int space,
149                      address_word addr,
150                      unsigned nr_bytes,
151                      device *device,
152                      void *buffer,
153                      int free_buffer)
154 {
155   sim_core_mapping *new_mapping = ZALLOC(sim_core_mapping);
156   /* common */
157   new_mapping->level = attach;
158   new_mapping->space = space;
159   new_mapping->base = addr;
160   new_mapping->nr_bytes = nr_bytes;
161   new_mapping->bound = addr + (nr_bytes - 1);
162   if (attach == attach_raw_memory) {
163     new_mapping->buffer = buffer;
164     new_mapping->free_buffer = free_buffer;
165   }
166   else if (attach >= attach_callback) {
167     new_mapping->device = device;
168   }
169   else {
170     sim_io_error (sd, "new_sim_core_mapping - internal error - unknown attach type %d\n",
171                  attach);
172   }
173   return new_mapping;
174 }
175
176
177 STATIC_SIM_CORE\
178 (void)
179 sim_core_map_attach(SIM_DESC sd,
180                     sim_core_map *access_map,
181                     attach_type attach,
182                     int space,
183                     address_word addr,
184                     unsigned nr_bytes, /* host limited */
185                     device *client, /*callback/default*/
186                     void *buffer, /*raw_memory*/
187                     int free_buffer) /*raw_memory*/
188 {
189   /* find the insertion point for this additional mapping and then
190      insert */
191   sim_core_mapping *next_mapping;
192   sim_core_mapping **last_mapping;
193
194   SIM_ASSERT((attach >= attach_callback && client != NULL && buffer == NULL && !free_buffer)
195          || (attach == attach_raw_memory && client == NULL && buffer != NULL));
196
197   /* actually do occasionally get a zero size map */
198   if (nr_bytes == 0) {
199 #if (WITH_DEVICES)
200     device_error(client, "called on sim_core_map_attach with size zero");
201 #else
202     sim_io_error (sd, "called on sim_core_map_attach with size zero");
203 #endif
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 < (int) attach
211             || (next_mapping->level == (int) attach
212                 && 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;
218   }
219
220   /* check insertion point correct */
221   SIM_ASSERT (next_mapping == NULL || next_mapping->level >= (int) attach);
222   if (next_mapping != NULL && next_mapping->level == (int) attach
223       && next_mapping->base < (addr + (nr_bytes - 1))) {
224 #if (WITH_DEVICES)
225     device_error(client, "map overlap when attaching %d:0x%lx (%ld)",
226                  space, (long)addr, (long)nr_bytes);
227 #else
228     sim_io_error (sd, "map overlap when attaching %d:0x%lx (%ld)",
229                  space, (long)addr, (long)nr_bytes);
230 #endif
231   }
232
233   /* create/insert the new mapping */
234   *last_mapping = new_sim_core_mapping(sd,
235                                    attach,
236                                    space, addr, nr_bytes,
237                                    client, buffer, free_buffer);
238   (*last_mapping)->next = next_mapping;
239 }
240
241
242 EXTERN_SIM_CORE\
243 (void)
244 sim_core_attach(SIM_DESC sd,
245                 sim_cpu *cpu,
246                 attach_type attach,
247                 access_type access,
248                 int space,
249                 address_word addr,
250                 unsigned nr_bytes, /* host limited */
251                 device *client,
252                 void *optional_buffer)
253 {
254   sim_core *memory = STATE_CORE(sd);
255   sim_core_maps map;
256   void *buffer;
257   int buffer_freed;
258
259   /* check for for attempt to use unimplemented per-processor core map */
260   if (cpu != NULL)
261     sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
262
263   if ((access & access_read_write_exec) == 0
264       || (access & ~access_read_write_exec) != 0) {
265 #if (WITH_DEVICES)
266     device_error(client, "invalid access for core attach");
267 #else
268     sim_io_error (sd, "invalid access for core attach");
269 #endif
270   }
271   /* verify the attach type */
272   if (attach == attach_raw_memory) {
273     if (optional_buffer == NULL) {
274       buffer = zalloc(nr_bytes);
275       buffer_freed = 0;
276     }
277     else {
278       buffer = optional_buffer;
279       buffer_freed = 1;
280     }
281   }
282   else if (attach >= attach_callback) {
283     buffer = NULL;
284     buffer_freed = 1;
285   }
286   else {
287 #if (WITH_DEVICES)
288     device_error(client, "sim_core_attach - conflicting buffer and attach arguments");
289 #else
290     sim_io_error (sd, "sim_core_attach - conflicting buffer and attach arguments");
291 #endif
292     buffer = NULL;
293     buffer_freed = 1;
294   }
295   /* attach the region to all applicable access maps */
296   for (map = 0; 
297        map < nr_sim_core_maps;
298        map++) {
299     switch (map) {
300     case sim_core_read_map:
301       if (access & access_read)
302         sim_core_map_attach(sd, &memory->common.map[map],
303                             attach,
304                             space, addr, nr_bytes,
305                             client, buffer, !buffer_freed);
306       buffer_freed ++;
307       break;
308     case sim_core_write_map:
309       if (access & access_write)
310         sim_core_map_attach(sd, &memory->common.map[map],
311                         attach,
312                         space, addr, nr_bytes,
313                         client, buffer, !buffer_freed);
314       buffer_freed ++;
315       break;
316     case sim_core_execute_map:
317       if (access & access_exec)
318         sim_core_map_attach(sd, &memory->common.map[map],
319                         attach,
320                         space, addr, nr_bytes,
321                         client, buffer, !buffer_freed);
322       buffer_freed ++;
323       break;
324     case nr_sim_core_maps:
325       sim_io_error (sd, "sim_core_attach - internal error - bad switch");
326       break;
327     }
328   }
329
330   /* Just copy this map to each of the processor specific data structures.
331      FIXME - later this will be replaced by true processor specific
332      maps. */
333   {
334     int i;
335     for (i = 0; i < MAX_NR_PROCESSORS; i++)
336       {
337         CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
338       }
339   }
340 }
341
342
343 STATIC_INLINE_SIM_CORE\
344 (sim_core_mapping *)
345 sim_core_find_mapping(sim_core_common *core,
346                       sim_core_maps map,
347                       address_word addr,
348                       unsigned nr_bytes,
349                       transfer_type transfer,
350                       int abort, /*either 0 or 1 - hint to inline/-O */
351                       sim_cpu *cpu, /* abort => cpu != NULL */
352                       sim_cia cia)
353 {
354   sim_core_mapping *mapping = core->map[map].first;
355   ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
356   ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
357   ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
358   while (mapping != NULL)
359     {
360       if (addr >= mapping->base
361           && (addr + (nr_bytes - 1)) <= mapping->bound)
362         return mapping;
363       mapping = mapping->next;
364     }
365   if (abort)
366     {
367       SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map, nr_bytes, addr, transfer,
368                        sim_core_unmapped_signal);
369     }
370   return NULL;
371 }
372
373
374 STATIC_INLINE_SIM_CORE\
375 (void *)
376 sim_core_translate (sim_core_mapping *mapping,
377                     address_word addr)
378 {
379   return (void *)(((char *)mapping->buffer) + addr - mapping->base);
380 }
381
382
383 EXTERN_SIM_CORE\
384 (unsigned)
385 sim_core_read_buffer (SIM_DESC sd,
386                       sim_cpu *cpu,
387                       sim_core_maps map,
388                       void *buffer,
389                       address_word addr,
390                       unsigned len)
391 {
392   sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
393   unsigned count = 0;
394   while (count < len) {
395     unsigned_word raddr = addr + count;
396     sim_core_mapping *mapping =
397       sim_core_find_mapping(core, map,
398                             raddr, /*nr-bytes*/1,
399                             read_transfer,
400                             0 /*dont-abort*/, NULL, NULL_CIA);
401     if (mapping == NULL)
402       break;
403 #if (WITH_DEVICES)
404     if (mapping->device != NULL) {
405       int nr_bytes = len - count;
406       if (raddr + nr_bytes - 1> mapping->bound)
407         nr_bytes = mapping->bound - raddr + 1;
408       if (device_io_read_buffer(mapping->device,
409                                 (unsigned_1*)buffer + count,
410                                 mapping->space,
411                                 raddr,
412                                 nr_bytes) != nr_bytes)
413         break;
414       count += nr_bytes;
415     }
416     else
417 #endif
418       {
419         ((unsigned_1*)buffer)[count] =
420           *(unsigned_1*)sim_core_translate(mapping, raddr);
421         count += 1;
422       }
423   }
424   return count;
425 }
426
427
428 EXTERN_SIM_CORE\
429 (unsigned)
430 sim_core_write_buffer (SIM_DESC sd,
431                        sim_cpu *cpu,
432                        sim_core_maps map,
433                        const void *buffer,
434                        address_word addr,
435                        unsigned len)
436 {
437   sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
438   unsigned count = 0;
439   while (count < len) {
440     unsigned_word raddr = addr + count;
441     sim_core_mapping *mapping =
442       sim_core_find_mapping(core, map,
443                             raddr, /*nr-bytes*/1,
444                             write_transfer,
445                             0 /*dont-abort*/, NULL, NULL_CIA);
446     if (mapping == NULL)
447       break;
448 #if (WITH_DEVICES)
449     if (WITH_CALLBACK_MEMORY
450         && mapping->device != NULL) {
451       int nr_bytes = len - count;
452       if (raddr + nr_bytes - 1 > mapping->bound)
453         nr_bytes = mapping->bound - raddr + 1;
454       if (device_io_write_buffer(mapping->device,
455                                  (unsigned_1*)buffer + count,
456                                  mapping->space,
457                                  raddr,
458                                  nr_bytes) != nr_bytes)
459         break;
460       count += nr_bytes;
461     }
462     else
463 #endif
464       {
465         *(unsigned_1*)sim_core_translate(mapping, raddr) =
466           ((unsigned_1*)buffer)[count];
467         count += 1;
468       }
469   }
470   return count;
471 }
472
473
474 EXTERN_SIM_CORE\
475 (void)
476 sim_core_set_xor (SIM_DESC sd,
477                   sim_cpu *cpu,
478                   int is_xor)
479 {
480   /* set up the XOR map if required. */
481   if (WITH_XOR_ENDIAN) {
482     {
483       sim_core *core = STATE_CORE (sd);
484       sim_cpu_core *cpu_core = (cpu != NULL ? CPU_CORE (cpu) : NULL);
485       if (cpu_core != NULL)
486         {
487           int i = 1;
488           unsigned mask;
489           if (is_xor)
490             mask = WITH_XOR_ENDIAN - 1;
491           else
492             mask = 0;
493           while (i - 1 < WITH_XOR_ENDIAN)
494             {
495               cpu_core->xor[i-1] = mask;
496               mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
497               i = (i << 1);
498             }
499         }
500       else
501         {
502           if (is_xor)
503             core->byte_xor = WITH_XOR_ENDIAN - 1;
504           else
505             core->byte_xor = 0;
506         }         
507     }
508   }
509   else {
510     if (is_xor)
511       sim_engine_abort (sd, cpu, NULL_CIA,
512                         "Attempted to enable xor-endian mode when permenantly disabled.");
513   }
514 }
515
516 STATIC_INLINE_SIM_CORE\
517 (void)
518 reverse_n (unsigned_1 *dest,
519            const unsigned_1 *src,
520            int nr_bytes)
521 {
522   int i;
523   for (i = 0; i < nr_bytes; i++)
524     {
525       dest [nr_bytes - i - 1] = src [i];
526     }
527 }
528
529
530 EXTERN_SIM_CORE\
531 (unsigned)
532 sim_core_xor_read_buffer (SIM_DESC sd,
533                           sim_cpu *cpu,
534                           sim_core_maps map,
535                           void *buffer,
536                           address_word addr,
537                           unsigned nr_bytes)
538 {
539   address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
540   if (!WITH_XOR_ENDIAN || !byte_xor)
541     return sim_core_read_buffer (sd, cpu, map, buffer, addr, nr_bytes);
542   else
543     /* only break up transfers when xor-endian is both selected and enabled */
544     {
545       unsigned_1 x[WITH_XOR_ENDIAN];
546       unsigned nr_transfered = 0;
547       address_word start = addr;
548       unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
549       address_word stop;
550       /* initial and intermediate transfers are broken when they cross
551          an XOR endian boundary */
552       while (nr_transfered + nr_this_transfer < nr_bytes)
553         /* initial/intermediate transfers */
554         {
555           /* since xor-endian is enabled stop^xor defines the start
556              address of the transfer */
557           stop = start + nr_this_transfer - 1;
558           SIM_ASSERT (start <= stop);
559           SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
560           if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
561               != nr_this_transfer)
562             return nr_transfered;
563           reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
564           nr_transfered += nr_this_transfer;
565           nr_this_transfer = WITH_XOR_ENDIAN;
566           start = stop + 1;
567         }
568       /* final transfer */
569       nr_this_transfer = nr_bytes - nr_transfered;
570       stop = start + nr_this_transfer - 1;
571       SIM_ASSERT (stop == (addr + nr_bytes - 1));
572       if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
573           != nr_this_transfer)
574         return nr_transfered;
575       reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
576       return nr_bytes;
577     }
578 }
579   
580   
581 EXTERN_SIM_CORE\
582 (unsigned)
583 sim_core_xor_write_buffer (SIM_DESC sd,
584                            sim_cpu *cpu,
585                            sim_core_maps map,
586                            const void *buffer,
587                            address_word addr,
588                            unsigned nr_bytes)
589 {
590   address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
591   if (!WITH_XOR_ENDIAN || !byte_xor)
592     return sim_core_write_buffer (sd, cpu, map, buffer, addr, nr_bytes);
593   else
594     /* only break up transfers when xor-endian is both selected and enabled */
595     {
596       unsigned_1 x[WITH_XOR_ENDIAN];
597       unsigned nr_transfered = 0;
598       address_word start = addr;
599       unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
600       address_word stop;
601       /* initial and intermediate transfers are broken when they cross
602          an XOR endian boundary */
603       while (nr_transfered + nr_this_transfer < nr_bytes)
604         /* initial/intermediate transfers */
605         {
606           /* since xor-endian is enabled stop^xor defines the start
607              address of the transfer */
608           stop = start + nr_this_transfer - 1;
609           SIM_ASSERT (start <= stop);
610           SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
611           reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
612           if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
613               != nr_this_transfer)
614             return nr_transfered;
615           nr_transfered += nr_this_transfer;
616           nr_this_transfer = WITH_XOR_ENDIAN;
617           start = stop + 1;
618         }
619       /* final transfer */
620       nr_this_transfer = nr_bytes - nr_transfered;
621       stop = start + nr_this_transfer - 1;
622       SIM_ASSERT (stop == (addr + nr_bytes - 1));
623       reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
624       if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
625           != nr_this_transfer)
626         return nr_transfered;
627       return nr_bytes;
628     }
629 }
630
631
632
633 /* define the read/write 1/2/4/8/word functions */
634
635 #define N 1
636 #include "sim-n-core.h"
637 #undef N
638
639 #define N 2
640 #include "sim-n-core.h"
641 #undef N
642
643 #define N 4
644 #include "sim-n-core.h"
645 #undef N
646
647 #define N 8
648 #include "sim-n-core.h"
649 #undef N
650
651 #define N word
652 #include "sim-n-core.h"
653 #undef N
654
655 #endif