Add support for 16 byte quantities to sim-endian macro H2T.
[external/binutils.git] / sim / common / sim-n-core.h
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 N
23 #error "N must be #defined"
24 #endif
25
26 #include "sim-xcat.h"
27
28 /* NOTE: see end of file for #undef of these macros */
29 #define unsigned_N XCONCAT2(unsigned_,N)
30 #define T2H_N XCONCAT2(T2H_,N)
31 #define H2T_N XCONCAT2(H2T_,N)
32
33 #define sim_core_read_aligned_N XCONCAT2(sim_core_read_aligned_,N)
34 #define sim_core_write_aligned_N XCONCAT2(sim_core_write_aligned_,N)
35 #define sim_core_read_unaligned_N XCONCAT2(sim_core_read_unaligned_,N)
36 #define sim_core_write_unaligned_N XCONCAT2(sim_core_write_unaligned_,N)
37 #define sim_core_trace_N XCONCAT2(sim_core_trace_,N)
38
39
40 /* TAGS: sim_core_trace_1 sim_core_trace_2 */
41 /* TAGS: sim_core_trace_4 sim_core_trace_8 */
42 /* TAGS: sim_core_trace_16 sim_core_trace_word */
43
44 STATIC_SIM_CORE(void)
45 sim_core_trace_N (sim_cpu *cpu,
46                   sim_cia cia,
47                   int line_nr,
48                   char *transfer,
49                   sim_core_maps map,
50                   address_word addr,
51                   unsigned_N val)
52 {
53 #if (N == 16)
54   trace_printf (CPU_STATE (cpu), cpu,
55                 "sim-n-core.h:%d: %s-%d %s:0x%08lx -> 0x%08lx%08lx%08lx%08lx\n",
56                 line_nr,
57                 transfer, sizeof (unsigned_N),
58                 sim_core_map_to_str (map),
59                 (unsigned long) addr,
60                 (unsigned long) V4_16 (val, 0),
61                 (unsigned long) V4_16 (val, 1),
62                 (unsigned long) V4_16 (val, 2),
63                 (unsigned long) V4_16 (val, 3));
64 #endif
65 #if (N == 8)
66   trace_printf (CPU_STATE (cpu), cpu,
67                 "sim-n-core.h:%d: %s-%d %s:0x%08lx -> 0x%08lx%08lx\n",
68                 line_nr,
69                 transfer, sizeof (unsigned_N),
70                 sim_core_map_to_str (map),
71                 (unsigned long) addr,
72                 (unsigned long) V4_8 (val, 0),
73                 (unsigned long) V4_8 (val, 1));
74 #endif
75 #if (N == 4)
76   trace_printf (CPU_STATE (cpu), cpu,
77                 "sim-n-core.h:%d: %s-%d %s:0x%08lx -> 0x%0*lx\n",
78                 line_nr,
79                 transfer, sizeof (unsigned_N),
80                 sim_core_map_to_str (map),
81                 (unsigned long) addr,
82                 sizeof (unsigned_N) * 2,
83                 (unsigned long) val);
84 #endif
85 }
86
87   
88 /* TAGS: sim_core_read_aligned_1 sim_core_read_aligned_2 */
89 /* TAGS: sim_core_read_aligned_4 sim_core_read_aligned_8 */
90 /* TAGS: sim_core_read_aligned_16 sim_core_read_aligned_word */
91
92 INLINE_SIM_CORE(unsigned_N)
93 sim_core_read_aligned_N(sim_cpu *cpu,
94                         sim_cia cia,
95                         sim_core_maps map,
96                         address_word xaddr)
97 {
98   sim_cpu_core *cpu_core = CPU_CORE (cpu);
99   sim_core_common *core = &cpu_core->common;
100   unsigned_N val;
101   sim_core_mapping *mapping;
102   address_word addr;
103 #if WITH_XOR_ENDIAN != 0
104   if (WITH_XOR_ENDIAN)
105     addr = xaddr ^ cpu_core->xor[(sizeof(unsigned_N) - 1) % WITH_XOR_ENDIAN];
106   else
107 #endif
108     addr = xaddr;
109   mapping = sim_core_find_mapping (core, map,
110                                    addr,
111                                    sizeof (unsigned_N),
112                                    read_transfer,
113                                    1 /*abort*/, cpu, cia);
114 #if (WITH_DEVICES)
115   if (WITH_CALLBACK_MEMORY && mapping->device != NULL) {
116     unsigned_N data;
117     if (device_io_read_buffer (mapping->device,
118                                &data,
119                                mapping->space,
120                                addr,
121                                sizeof (unsigned_N)) != sizeof (unsigned_N))
122       device_error (mapping->device, "internal error - %s - io_read_buffer should not fail",
123                     XSTRING (sim_core_read_aligned_N));
124     val = T2H_N (data);
125   }
126   else
127 #endif
128     val = T2H_N (*(unsigned_N*) sim_core_translate (mapping, addr));
129   PROFILE_COUNT_CORE (cpu, addr, sizeof (unsigned_N), map);
130   if (TRACE_P (cpu, TRACE_CORE_IDX))
131     sim_core_trace_N (cpu, cia, __LINE__, "read", map, addr, val);
132   return val;
133 }
134
135 /* TAGS: sim_core_read_unaligned_1 sim_core_read_unaligned_2 */
136 /* TAGS: sim_core_read_unaligned_4 sim_core_read_unaligned_8 */
137 /* TAGS: sim_core_read_unaligned_16 sim_core_read_unaligned_word */
138
139 INLINE_SIM_CORE(unsigned_N)
140 sim_core_read_unaligned_N(sim_cpu *cpu,
141                           sim_cia cia,
142                           sim_core_maps map,
143                           address_word addr)
144 {
145   int alignment = sizeof (unsigned_N) - 1;
146   /* if hardwired to forced alignment just do it */
147   if (WITH_ALIGNMENT == FORCED_ALIGNMENT)
148     return sim_core_read_aligned_N (cpu, cia, map, addr & ~alignment);
149   else if ((addr & alignment) == 0)
150     return sim_core_read_aligned_N (cpu, cia, map, addr);
151   else
152     switch (CURRENT_ALIGNMENT)
153       {
154       case STRICT_ALIGNMENT:
155         SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map,
156                          sizeof (unsigned_N), addr,
157                          read_transfer, sim_core_unaligned_signal);
158       case NONSTRICT_ALIGNMENT:
159         {
160           unsigned_N val;
161           if (sim_core_xor_read_buffer (CPU_STATE (cpu), cpu, map, &val, addr,
162                                         sizeof(unsigned_N))
163               != sizeof(unsigned_N))
164             SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map,
165                              sizeof (unsigned_N), addr,
166                              read_transfer, sim_core_unaligned_signal);
167           val = T2H_N(val);
168           PROFILE_COUNT_CORE (cpu, addr, sizeof (unsigned_N), map);
169           return val;
170         }
171       case FORCED_ALIGNMENT:
172         return sim_core_read_aligned_N (cpu, cia, map, addr & ~alignment);
173       case MIXED_ALIGNMENT:
174         sim_engine_abort (CPU_STATE (cpu), cpu, cia,
175                           "internal error - %s - mixed alignment",
176                           XSTRING (sim_core_read_unaligned_N));
177       default:
178         sim_engine_abort (CPU_STATE (cpu), cpu, cia,
179                           "internal error - %s - bad switch",
180                           XSTRING (sim_core_read_unaligned_N));
181         /* to keep some compilers happy, we return a dummy */
182         {
183           unsigned_N val[1] = { };
184           return val[0];
185         }
186       }
187 }
188
189 /* TAGS: sim_core_write_aligned_1 sim_core_write_aligned_2 */
190 /* TAGS: sim_core_write_aligned_4 sim_core_write_aligned_8 */
191 /* TAGS: sim_core_write_aligned_16 sim_core_write_aligned_word */
192
193 INLINE_SIM_CORE(void)
194 sim_core_write_aligned_N(sim_cpu *cpu,
195                          sim_cia cia,
196                          sim_core_maps map,
197                          address_word xaddr,
198                          unsigned_N val)
199 {
200   sim_cpu_core *cpu_core = CPU_CORE (cpu);
201   sim_core_common *core = &cpu_core->common;
202   sim_core_mapping *mapping;
203   address_word addr;
204 #if WITH_XOR_ENDIAN != 0
205   if (WITH_XOR_ENDIAN)
206     addr = xaddr ^ cpu_core->xor[(sizeof(unsigned_N) - 1) % WITH_XOR_ENDIAN];
207   else
208 #endif
209     addr = xaddr;
210   mapping = sim_core_find_mapping(core, map,
211                                   addr,
212                                   sizeof (unsigned_N),
213                                   write_transfer,
214                                   1 /*abort*/, cpu, cia);
215 #if (WITH_DEVICES)
216   if (WITH_CALLBACK_MEMORY && mapping->device != NULL) {
217     unsigned_N data = H2T_N (val);
218     if (device_io_write_buffer (mapping->device,
219                                 &data,
220                                 mapping->space,
221                                 addr,
222                                 sizeof (unsigned_N), /* nr_bytes */
223                                 cpu,
224                                 cia) != sizeof (unsigned_N))
225       device_error (mapping->device, "internal error - %s - io_write_buffer should not fail",
226                     XSTRING (sim_core_write_aligned_N));
227   }
228   else
229 #endif
230     *(unsigned_N*) sim_core_translate (mapping, addr) = H2T_N (val);
231   PROFILE_COUNT_CORE (cpu, addr, sizeof (unsigned_N), map);
232   if (TRACE_P (cpu, TRACE_CORE_IDX))
233     sim_core_trace_N (cpu, cia, __LINE__, "write", map, addr, val);
234 }
235
236 /* TAGS: sim_core_write_unaligned_1 sim_core_write_unaligned_2 */
237 /* TAGS: sim_core_write_unaligned_4 sim_core_write_unaligned_8 */
238 /* TAGS: sim_core_write_unaligned_16 sim_core_write_unaligned_word */
239
240 INLINE_SIM_CORE(void)
241 sim_core_write_unaligned_N(sim_cpu *cpu,
242                            sim_cia cia,
243                            sim_core_maps map,
244                            address_word addr,
245                            unsigned_N val)
246 {
247   int alignment = sizeof (unsigned_N) - 1;
248   /* if hardwired to forced alignment just do it */
249   if (WITH_ALIGNMENT == FORCED_ALIGNMENT)
250     sim_core_write_aligned_N (cpu, cia, map, addr & ~alignment, val);
251   else if ((addr & alignment) == 0)
252     sim_core_write_aligned_N (cpu, cia, map, addr, val);
253   else
254     switch (CURRENT_ALIGNMENT)
255       {
256       case STRICT_ALIGNMENT:
257         SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map,
258                          sizeof (unsigned_N), addr,
259                          write_transfer, sim_core_unaligned_signal);
260         break;
261       case NONSTRICT_ALIGNMENT:
262         {
263           unsigned_N val = H2T_N (val);
264           if (sim_core_xor_write_buffer (CPU_STATE (cpu), cpu, map, &val, addr,
265                                          sizeof(unsigned_N))
266               != sizeof(unsigned_N))
267             SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map,
268                              sizeof (unsigned_N), addr,
269                              write_transfer, sim_core_unaligned_signal);
270           PROFILE_COUNT_CORE (cpu, addr, sizeof (unsigned_N), map);
271           break;
272         }
273       case FORCED_ALIGNMENT:
274         sim_core_write_aligned_N (cpu, cia, map, addr & ~alignment, val);
275         break;
276       case MIXED_ALIGNMENT:
277         sim_engine_abort (CPU_STATE (cpu), cpu, cia,
278                           "internal error - %s - mixed alignment",
279                           XSTRING (sim_core_write_unaligned_N));
280         break;
281       default:
282         sim_engine_abort (CPU_STATE (cpu), cpu, cia,
283                           "internal error - %s - bad switch",
284                           XSTRING (sim_core_write_unaligned_N));
285         break;
286       }
287 }
288
289
290 /* NOTE: see start of file for #define of these macros */
291 #undef unsigned_N
292 #undef T2H_N
293 #undef H2T_N
294 #undef sim_core_read_aligned_N
295 #undef sim_core_write_aligned_N
296 #undef sim_core_read_unaligned_N
297 #undef sim_core_write_unaligned_N
298 #undef sim_core_trace_N