source: trunk/IPs/systemC/hierarchy_memory/endianness/endianness.h @ 2

Last change on this file since 2 was 2, checked in by kane, 17 years ago

Import Morpheo

File size: 4.9 KB
Line 
1#ifndef _ENDIANNESS_H
2#define _ENDIANNESS_H
3//=====[ endianness ]==========================================================
4
5/*
6 * file              : endianness.h
7 * date (yyyy/mm/dd) : 2006/05/02
8 * authors           : Rosiere Mathieu
9 */
10
11#include <byteswap.h>
12
13#define LittleEndian false
14#define BigEndian    true
15
16union TestEndian
17{
18  unsigned long total;
19  struct 
20  {
21    unsigned char octet1;
22    unsigned char octet2;
23    unsigned char octet3;
24    unsigned char octet4;
25  } octets;
26};
27
28//-----[ hostEndianness ]------------------------------------------------------
29/*
30 * false : LitteEndian
31 * true  : BigEndian
32 */
33inline bool hostEndianness()
34{
35  union TestEndian test_endian;
36  test_endian.total = 0xFF;
37 
38  return (test_endian.octets.octet1 == 0xFF)?LittleEndian:BigEndian;
39}
40
41//-----[ cpuEndianness ]-------------------------------------------------------
42/*
43 * false : LitteEndian
44 * true  : BigEndian
45 */
46inline bool cpuEndianness(unsigned int cpu_id)
47{
48  return(BigEndian);
49
50  /*
51   * Comment connaitre l'endianness ?
52   * static    : table generee dans le top level
53   * dynamique : table generee par un l'equivalent de hostEndianness et ecrivant dans un segment precis de la ram
54   */
55}
56
57//-----[ isSameEndianness ]----------------------------------------------------
58
59/*
60 * The host machine and the simulate processeur have the same endianness
61 */
62inline bool isSameEndianness(unsigned int cpu_id)
63{
64  return ((hostEndianness() ^ cpuEndianness(cpu_id)) == false);
65}
66
67//-----[ swapBytes ]-----------------------------------------------------------
68/*
69 * swapBytes -> change the endiannes
70 * exemple for 16 and size_access = 4, [0123456789abcdef] -> [32107654ba98fedc]
71 */
72inline char * swapBytes (char * string_src , unsigned int size_string, unsigned int size_access)
73{
74  char string_tmp [size_string];
75
76  if (size_access == 1)
77    return string_src;
78
79  if (size_string % size_access != 0)
80    {
81      cerr << "<swapBytes> : Size of string must be a multiple of size_access." << endl;
82      exit(1);
83    }
84
85  // size_access :
86  // 1 -> bytes
87  // 2 -> half
88  // 4 -> word
89  // 8 -> doubleword
90
91  memcpy (string_tmp,string_src,size_string);
92
93  // Loop to reorganize the string
94  for (unsigned int it_size_string = 0 ; it_size_string < size_string; it_size_string += size_access)
95    {
96      // Permutation
97//       for (unsigned int it_size_access = 0 ; it_size_access < size_access; it_size_access ++)
98//      string_src [it_size_string+it_size_access] = string_tmp [it_size_string + (size_access-1) - it_size_access];
99      switch (size_access)
100        {
101        case 2  : 
102          {
103            string_src[it_size_string+0] = string_tmp[it_size_string+1];
104            string_src[it_size_string+1] = string_tmp[it_size_string+0];
105            break;
106          }
107        case 4  :
108          {
109            string_src[it_size_string+0] = string_tmp[it_size_string+3];
110            string_src[it_size_string+1] = string_tmp[it_size_string+2];
111            string_src[it_size_string+2] = string_tmp[it_size_string+1];
112            string_src[it_size_string+3] = string_tmp[it_size_string+0];
113            break;
114          }
115        case 8  :
116          {
117            string_src[it_size_string+0] = string_tmp[it_size_string+7];
118            string_src[it_size_string+1] = string_tmp[it_size_string+6];
119            string_src[it_size_string+2] = string_tmp[it_size_string+5];
120            string_src[it_size_string+3] = string_tmp[it_size_string+4];
121            string_src[it_size_string+4] = string_tmp[it_size_string+3];
122            string_src[it_size_string+5] = string_tmp[it_size_string+2];
123            string_src[it_size_string+6] = string_tmp[it_size_string+1];
124            string_src[it_size_string+7] = string_tmp[it_size_string+0];
125            break;
126          }
127        default : 
128          {
129            cerr << "<swapBytes> : Illegal size of access (size_access = " << size_access << ")" << endl;
130            exit(1);
131            break;
132          }
133        }//end switch(size_access)
134    }//end it_size_string
135 
136
137//   cout << "size_access : " << size_access << endl;
138//   cout << "size_string : " << size_string << endl;
139
140//   for (unsigned int it_size_string = 0 ; it_size_string < size_string; it_size_string ++)
141//     printf("%.2x",(unsigned int)string_src[it_size_string]);
142//   printf("\n");
143
144//   // Loop to reorganize the string
145//   for (unsigned int it_size_string = 0 ; it_size_string < size_string; it_size_string += size_access)
146//     {
147//       switch (size_access)
148//      {
149//      case 2  : string_src[it_size_string] = bswap_16((unsigned short)   string_src[it_size_string]); break;
150//      case 4  : string_src[it_size_string] = bswap_32((unsigned int  )   string_src[it_size_string]); break;
151//      case 8  : string_src[it_size_string] = bswap_64((unsigned long int)string_src[it_size_string]); break;
152//      default : 
153//        {
154//          cerr << "<swapBytes> : Illegal size of access (size_access = " << size_access << ")" << endl;
155//          exit(1);
156//          break;
157//        }
158//      }//end switch(size_access)
159
160//   for (unsigned int it_size_string = 0 ; it_size_string < size_string; it_size_string ++)
161//     printf("%.2x",(unsigned int)string_src[it_size_string]);
162//   printf("\n");
163
164  return string_src;
165}
166#endif //_ENDIANNESS_H
Note: See TracBrowser for help on using the repository browser.