Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* This file is part of the program psim. |
2 | ||
3 | Copyright (C) 1994-1995, 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_ENDIAN_H_ | |
23 | #define _SIM_ENDIAN_H_ | |
24 | ||
25 | ||
26 | /* C byte conversion functions */ | |
27 | ||
d29d5195 MG |
28 | INLINE_PSIM_ENDIAN(unsigned_1) endian_h2t_1(unsigned_1 x); |
29 | INLINE_PSIM_ENDIAN(unsigned_2) endian_h2t_2(unsigned_2 x); | |
30 | INLINE_PSIM_ENDIAN(unsigned_4) endian_h2t_4(unsigned_4 x); | |
31 | INLINE_PSIM_ENDIAN(unsigned_8) endian_h2t_8(unsigned_8 x); | |
32 | ||
33 | INLINE_PSIM_ENDIAN(unsigned_1) endian_t2h_1(unsigned_1 x); | |
34 | INLINE_PSIM_ENDIAN(unsigned_2) endian_t2h_2(unsigned_2 x); | |
35 | INLINE_PSIM_ENDIAN(unsigned_4) endian_t2h_4(unsigned_4 x); | |
36 | INLINE_PSIM_ENDIAN(unsigned_8) endian_t2h_8(unsigned_8 x); | |
37 | ||
38 | INLINE_PSIM_ENDIAN(unsigned_1) swap_1(unsigned_1 x); | |
39 | INLINE_PSIM_ENDIAN(unsigned_2) swap_2(unsigned_2 x); | |
40 | INLINE_PSIM_ENDIAN(unsigned_4) swap_4(unsigned_4 x); | |
41 | INLINE_PSIM_ENDIAN(unsigned_8) swap_8(unsigned_8 x); | |
42 | ||
43 | INLINE_PSIM_ENDIAN(unsigned_1) endian_h2be_1(unsigned_1 x); | |
44 | INLINE_PSIM_ENDIAN(unsigned_2) endian_h2be_2(unsigned_2 x); | |
45 | INLINE_PSIM_ENDIAN(unsigned_4) endian_h2be_4(unsigned_4 x); | |
46 | INLINE_PSIM_ENDIAN(unsigned_8) endian_h2be_8(unsigned_8 x); | |
47 | ||
48 | INLINE_PSIM_ENDIAN(unsigned_1) endian_be2h_1(unsigned_1 x); | |
49 | INLINE_PSIM_ENDIAN(unsigned_2) endian_be2h_2(unsigned_2 x); | |
50 | INLINE_PSIM_ENDIAN(unsigned_4) endian_be2h_4(unsigned_4 x); | |
51 | INLINE_PSIM_ENDIAN(unsigned_8) endian_be2h_8(unsigned_8 x); | |
52 | ||
53 | INLINE_PSIM_ENDIAN(unsigned_1) endian_h2le_1(unsigned_1 x); | |
54 | INLINE_PSIM_ENDIAN(unsigned_2) endian_h2le_2(unsigned_2 x); | |
55 | INLINE_PSIM_ENDIAN(unsigned_4) endian_h2le_4(unsigned_4 x); | |
56 | INLINE_PSIM_ENDIAN(unsigned_8) endian_h2le_8(unsigned_8 x); | |
57 | ||
58 | INLINE_PSIM_ENDIAN(unsigned_1) endian_le2h_1(unsigned_1 x); | |
59 | INLINE_PSIM_ENDIAN(unsigned_2) endian_le2h_2(unsigned_2 x); | |
60 | INLINE_PSIM_ENDIAN(unsigned_4) endian_le2h_4(unsigned_4 x); | |
61 | INLINE_PSIM_ENDIAN(unsigned_8) endian_le2h_8(unsigned_8 x); | |
c906108c SS |
62 | |
63 | ||
64 | /* Host dependant: | |
65 | ||
66 | The CPP below defines information about the compilation host. In | |
67 | particular it defines the macro's: | |
68 | ||
69 | WITH_HOST_BYTE_ORDER The byte order of the host. Could | |
70 | be any of LITTLE_ENDIAN, BIG_ENDIAN | |
71 | or 0 (unknown). Those macro's also | |
72 | need to be defined. | |
73 | ||
74 | */ | |
75 | ||
76 | ||
77 | /* NetBSD: | |
78 | ||
79 | NetBSD is easy, everything you could ever want is in a header file | |
80 | (well almost :-) */ | |
81 | ||
82 | #if defined(__NetBSD__) | |
83 | # include <machine/endian.h> | |
84 | # if (WITH_HOST_BYTE_ORDER == 0) | |
85 | # undef WITH_HOST_BYTE_ORDER | |
86 | # define WITH_HOST_BYTE_ORDER BYTE_ORDER | |
87 | # endif | |
88 | # if (BYTE_ORDER != WITH_HOST_BYTE_ORDER) | |
89 | # error "host endian incorrectly configured, check config.h" | |
90 | # endif | |
91 | #endif | |
92 | ||
93 | /* Linux is similarly easy. */ | |
94 | ||
95 | #if defined(__linux__) | |
96 | # include <endian.h> | |
97 | # include <asm/byteorder.h> | |
98 | # if defined(__LITTLE_ENDIAN) && !defined(LITTLE_ENDIAN) | |
99 | # define LITTLE_ENDIAN __LITTLE_ENDIAN | |
100 | # endif | |
101 | # if defined(__BIG_ENDIAN) && !defined(BIG_ENDIAN) | |
102 | # define BIG_ENDIAN __BIG_ENDIAN | |
103 | # endif | |
104 | # if defined(__BYTE_ORDER) && !defined(BYTE_ORDER) | |
105 | # define BYTE_ORDER __BYTE_ORDER | |
106 | # endif | |
107 | # if (WITH_HOST_BYTE_ORDER == 0) | |
108 | # undef WITH_HOST_BYTE_ORDER | |
109 | # define WITH_HOST_BYTE_ORDER BYTE_ORDER | |
110 | # endif | |
111 | # if (BYTE_ORDER != WITH_HOST_BYTE_ORDER) | |
112 | # error "host endian incorrectly configured, check config.h" | |
113 | # endif | |
114 | #endif | |
115 | ||
116 | /* INSERT HERE - hosts that have available LITTLE_ENDIAN and | |
117 | BIG_ENDIAN macro's */ | |
118 | ||
119 | ||
120 | /* Some hosts don't define LITTLE_ENDIAN or BIG_ENDIAN, help them out */ | |
121 | ||
122 | #ifndef LITTLE_ENDIAN | |
123 | #define LITTLE_ENDIAN 1234 | |
124 | #endif | |
125 | #ifndef BIG_ENDIAN | |
126 | #define BIG_ENDIAN 4321 | |
127 | #endif | |
128 | ||
129 | ||
130 | /* SunOS on SPARC: | |
131 | ||
132 | Big endian last time I looked */ | |
133 | ||
134 | #if defined(sparc) || defined(__sparc__) | |
135 | # if (WITH_HOST_BYTE_ORDER == 0) | |
136 | # undef WITH_HOST_BYTE_ORDER | |
137 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
138 | # endif | |
139 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
140 | # error "sun was big endian last time I looked ..." | |
141 | # endif | |
142 | #endif | |
143 | ||
144 | ||
145 | /* Random x86 | |
146 | ||
147 | Little endian last time I looked */ | |
148 | ||
149 | #if defined(i386) || defined(i486) || defined(i586) || defined (i686) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined (__i686__) | |
150 | # if (WITH_HOST_BYTE_ORDER == 0) | |
151 | # undef WITH_HOST_BYTE_ORDER | |
152 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
153 | # endif | |
154 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
155 | # error "x86 was little endian last time I looked ..." | |
156 | # endif | |
157 | #endif | |
158 | ||
159 | #if (defined (__i486__) || defined (__i586__) || defined (__i686__)) && defined(__GNUC__) && WITH_BSWAP | |
160 | #undef htonl | |
161 | #undef ntohl | |
162 | #define htonl(IN) __extension__ ({ int _out; __asm__ ("bswap %0" : "=r" (_out) : "0" (IN)); _out; }) | |
163 | #define ntohl(IN) __extension__ ({ int _out; __asm__ ("bswap %0" : "=r" (_out) : "0" (IN)); _out; }) | |
164 | #endif | |
165 | ||
166 | /* Power or PowerPC running AIX */ | |
167 | #if defined(_POWER) && defined(_AIX) | |
168 | # if (WITH_HOST_BYTE_ORDER == 0) | |
169 | # undef WITH_HOST_BYTE_ORDER | |
170 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
171 | # endif | |
172 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
173 | # error "Power/PowerPC AIX was big endian last time I looked ..." | |
174 | # endif | |
175 | #endif | |
176 | ||
177 | /* Solaris running PowerPC */ | |
178 | #if defined(__PPC) && defined(__sun__) | |
179 | # if (WITH_HOST_BYTE_ORDER == 0) | |
180 | # undef WITH_HOST_BYTE_ORDER | |
181 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
182 | # endif | |
183 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
184 | # error "Solaris on PowerPCs was little endian last time I looked ..." | |
185 | # endif | |
186 | #endif | |
187 | ||
188 | /* HP/PA */ | |
189 | #if defined(__hppa__) | |
190 | # if (WITH_HOST_BYTE_ORDER == 0) | |
191 | # undef WITH_HOST_BYTE_ORDER | |
192 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
193 | # endif | |
194 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
195 | # error "HP/PA was big endian last time I looked ..." | |
196 | # endif | |
197 | #endif | |
198 | ||
199 | /* Big endian MIPS */ | |
200 | #if defined(__MIPSEB__) | |
201 | # if (WITH_HOST_BYTE_ORDER == 0) | |
202 | # undef WITH_HOST_BYTE_ORDER | |
203 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
204 | # endif | |
205 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
206 | # error "MIPSEB was big endian last time I looked ..." | |
207 | # endif | |
208 | #endif | |
209 | ||
210 | /* Little endian MIPS */ | |
211 | #if defined(__MIPSEL__) | |
212 | # if (WITH_HOST_BYTE_ORDER == 0) | |
213 | # undef WITH_HOST_BYTE_ORDER | |
214 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
215 | # endif | |
216 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
217 | # error "MIPSEL was little endian last time I looked ..." | |
218 | # endif | |
219 | #endif | |
220 | ||
221 | /* Windows NT */ | |
222 | #if defined(__WIN32__) | |
223 | # if (WITH_HOST_BYTE_ORDER == 0) | |
224 | # undef WITH_HOST_BYTE_ORDER | |
225 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
226 | # endif | |
227 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
228 | # error "Windows NT was little endian last time I looked ..." | |
229 | # endif | |
230 | #endif | |
231 | ||
232 | /* Alpha running DEC unix */ | |
233 | #if defined(__osf__) && defined(__alpha__) | |
234 | # if (WITH_HOST_BYTE_ORDER == 0) | |
235 | # undef WITH_HOST_BYTE_ORDER | |
236 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
237 | # endif | |
238 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
239 | # error "AXP running DEC unix was little endian last time I looked ..." | |
240 | # endif | |
241 | #endif | |
242 | ||
243 | ||
244 | /* INSERT HERE - additional hosts that do not have LITTLE_ENDIAN and | |
245 | BIG_ENDIAN definitions available. */ | |
246 | ||
247 | /* SWAP */ | |
248 | ||
4c15ccf7 GK |
249 | #define SWAP_1 swap_1 |
250 | #define SWAP_2 swap_2 | |
251 | #define SWAP_4 swap_4 | |
252 | #define SWAP_8 swap_8 | |
c906108c SS |
253 | |
254 | ||
255 | /* HOST to BE */ | |
256 | ||
4c15ccf7 GK |
257 | #define H2BE_1 endian_h2be_1 |
258 | #define H2BE_2 endian_h2be_2 | |
259 | #define H2BE_4 endian_h2be_4 | |
260 | #define H2BE_8 endian_h2be_8 | |
261 | #define BE2H_1 endian_be2h_1 | |
262 | #define BE2H_2 endian_be2h_2 | |
263 | #define BE2H_4 endian_be2h_4 | |
264 | #define BE2H_8 endian_be2h_8 | |
c906108c SS |
265 | |
266 | ||
267 | /* HOST to LE */ | |
268 | ||
4c15ccf7 GK |
269 | #define H2LE_1 endian_h2le_1 |
270 | #define H2LE_2 endian_h2le_2 | |
271 | #define H2LE_4 endian_h2le_4 | |
272 | #define H2LE_8 endian_h2le_8 | |
273 | #define LE2H_1 endian_le2h_1 | |
274 | #define LE2H_2 endian_le2h_2 | |
275 | #define LE2H_4 endian_le2h_4 | |
276 | #define LE2H_8 endian_le2h_8 | |
c906108c SS |
277 | |
278 | ||
279 | /* HOST to TARGET */ | |
280 | ||
4c15ccf7 GK |
281 | #define H2T_1 endian_h2t_1 |
282 | #define H2T_2 endian_h2t_2 | |
283 | #define H2T_4 endian_h2t_4 | |
284 | #define H2T_8 endian_h2t_8 | |
285 | #define T2H_1 endian_t2h_1 | |
286 | #define T2H_2 endian_t2h_2 | |
287 | #define T2H_4 endian_t2h_4 | |
288 | #define T2H_8 endian_t2h_8 | |
c906108c SS |
289 | |
290 | ||
291 | /* CONVERT IN PLACE | |
292 | ||
293 | These macros, given an argument of unknown size, swap its value in | |
294 | place if a host/target conversion is required. */ | |
295 | ||
296 | #define H2T(VARIABLE) \ | |
297 | do { \ | |
298 | switch (sizeof(VARIABLE)) { \ | |
299 | case 1: VARIABLE = H2T_1(VARIABLE); break; \ | |
300 | case 2: VARIABLE = H2T_2(VARIABLE); break; \ | |
301 | case 4: VARIABLE = H2T_4(VARIABLE); break; \ | |
302 | case 8: VARIABLE = H2T_8(VARIABLE); break; \ | |
303 | } \ | |
304 | } while (0) | |
305 | ||
306 | #define T2H(VARIABLE) \ | |
307 | do { \ | |
308 | switch (sizeof(VARIABLE)) { \ | |
309 | case 1: VARIABLE = T2H_1(VARIABLE); break; \ | |
310 | case 2: VARIABLE = T2H_2(VARIABLE); break; \ | |
311 | case 4: VARIABLE = T2H_4(VARIABLE); break; \ | |
312 | case 8: VARIABLE = T2H_8(VARIABLE); break; \ | |
313 | } \ | |
314 | } while (0) | |
315 | ||
316 | #define SWAP(VARIABLE) \ | |
317 | do { \ | |
318 | switch (sizeof(VARIABLE)) { \ | |
319 | case 1: VARIABLE = SWAP_1(VARIABLE); break; \ | |
320 | case 2: VARIABLE = SWAP_2(VARIABLE); break; \ | |
321 | case 4: VARIABLE = SWAP_4(VARIABLE); break; \ | |
322 | case 8: VARIABLE = SWAP_8(VARIABLE); break; \ | |
323 | } \ | |
324 | } while (0) | |
325 | ||
326 | #define H2BE(VARIABLE) \ | |
327 | do { \ | |
328 | switch (sizeof(VARIABLE)) { \ | |
329 | case 1: VARIABLE = H2BE_1(VARIABLE); break; \ | |
330 | case 2: VARIABLE = H2BE_2(VARIABLE); break; \ | |
331 | case 4: VARIABLE = H2BE_4(VARIABLE); break; \ | |
332 | case 8: VARIABLE = H2BE_8(VARIABLE); break; \ | |
333 | } \ | |
334 | } while (0) | |
335 | ||
336 | #define BE2H(VARIABLE) \ | |
337 | do { \ | |
338 | switch (sizeof(VARIABLE)) { \ | |
339 | case 1: VARIABLE = BE2H_1(VARIABLE); break; \ | |
340 | case 2: VARIABLE = BE2H_2(VARIABLE); break; \ | |
341 | case 4: VARIABLE = BE2H_4(VARIABLE); break; \ | |
342 | case 8: VARIABLE = BE2H_8(VARIABLE); break; \ | |
343 | } \ | |
344 | } while (0) | |
345 | ||
346 | #define H2LE(VARIABLE) \ | |
347 | do { \ | |
348 | switch (sizeof(VARIABLE)) { \ | |
349 | case 1: VARIABLE = H2LE_1(VARIABLE); break; \ | |
350 | case 2: VARIABLE = H2LE_2(VARIABLE); break; \ | |
351 | case 4: VARIABLE = H2LE_4(VARIABLE); break; \ | |
352 | case 8: VARIABLE = H2LE_8(VARIABLE); break; \ | |
353 | } \ | |
354 | } while (0) | |
355 | ||
356 | #define LE2H(VARIABLE) \ | |
357 | do { \ | |
358 | switch (sizeof(VARIABLE)) { \ | |
359 | case 1: VARIABLE = LE2H_1(VARIABLE); break; \ | |
360 | case 2: VARIABLE = LE2H_2(VARIABLE); break; \ | |
361 | case 4: VARIABLE = LE2H_4(VARIABLE); break; \ | |
362 | case 8: VARIABLE = LE2H_8(VARIABLE); break; \ | |
363 | } \ | |
364 | } while (0) | |
365 | ||
366 | ||
367 | ||
368 | /* TARGET WORD: | |
369 | ||
370 | Byte swap a quantity the size of the targets word */ | |
371 | ||
372 | #if (WITH_TARGET_WORD_BITSIZE == 64) | |
4c15ccf7 GK |
373 | #define H2T_word H2T_8 |
374 | #define T2H_word T2H_8 | |
375 | #define H2BE_word H2BE_8 | |
376 | #define BE2H_word BE2H_8 | |
377 | #define H2LE_word H2LE_8 | |
378 | #define LE2H_word LE2H_8 | |
379 | #define SWAP_word SWAP_8 | |
c906108c SS |
380 | #endif |
381 | #if (WITH_TARGET_WORD_BITSIZE == 32) | |
4c15ccf7 GK |
382 | #define H2T_word H2T_4 |
383 | #define T2H_word T2H_4 | |
384 | #define H2BE_word H2BE_4 | |
385 | #define BE2H_word BE2H_4 | |
386 | #define H2LE_word H2LE_4 | |
387 | #define LE2H_word LE2H_4 | |
388 | #define SWAP_word SWAP_4 | |
c906108c SS |
389 | #endif |
390 | ||
391 | ||
392 | /* TARGET CELL: | |
393 | ||
394 | Byte swap a quantity the size of the targets IEEE 1275 memory cell */ | |
395 | ||
4c15ccf7 GK |
396 | #define H2T_cell H2T_4 |
397 | #define T2H_cell T2H_4 | |
398 | #define H2BE_cell H2BE_4 | |
399 | #define BE2H_cell BE2H_4 | |
400 | #define H2LE_cell H2LE_4 | |
401 | #define LE2H_cell LE2H_4 | |
402 | #define SWAP_cell SWAP_4 | |
c906108c SS |
403 | |
404 | ||
405 | #if (SIM_ENDIAN_INLINE & INCLUDE_MODULE) | |
406 | # include "sim-endian.c" | |
407 | #endif | |
408 | ||
409 | #endif /* _SIM_ENDIAN_H_ */ |