1 #ifndef MYENDIAN_H
2 #define MYENDIAN_H
3
4
5 #include "Config.h"
6 #include "MyTypes.h"
7
8 // This should never be true.
9 #if defined(LO) || defined(HI) || defined(LOLO) || defined(LOHI) || defined(HILO) || defined(HIHI)
10 #error Redefinition of these values can cause trouble.
11 #endif
12
13 // For optional direct memory access.
14 // First value in memory/array = index 0.
15 // Second value in memory/array = index 1.
16
17 // For a pair of bytes/words/longwords.
18 #undef LO
19 #undef HI
20
21 // For two pairs of bytes/words/longwords.
22 #undef LOLO
23 #undef LOHI
24 #undef HILO
25 #undef HIHI
26
27 #if defined(WORDS_BIGENDIAN)
28 // byte-order: HI..3210..LO
29 #define LO 1
30 #define HI 0
31 #define LOLO 3
32 #define LOHI 2
33 #define HILO 1
34 #define HIHI 0
35 #else
36 // byte-order: LO..0123..HI
37 #define WORDS_LITTLEENDIAN
38 #define LO 0
39 #define HI 1
40 #define LOLO 0
41 #define LOHI 1
42 #define HILO 2
43 #define HIHI 3
44 #endif
45
46 union cpuLword
47 {
48 uword w[2]; // single 16-bit low and high word
49 udword l; // complete 32-bit longword
50 };
51
52 union cpuWord
53 {
54 ubyte b[2]; // single 8-bit low and high byte
55 uword w; // complete 16-bit word
56 };
57
58 union cpuLBword
59 {
60 ubyte b[4]; // single 8-bit bytes
61 udword l; // complete 32-bit longword
62 };
63
64 // Convert high-byte and low-byte to 16-bit word.
65 // Used to read 16-bit words in little-endian order.
readEndian(ubyte hi,ubyte lo)66 inline uword readEndian(ubyte hi, ubyte lo)
67 {
68 return(( (uword)hi << 8 ) + (uword)lo );
69 }
70
71 // Convert high bytes and low bytes of MSW and LSW to 32-bit word.
72 // Used to read 32-bit words in little-endian order.
readEndian(ubyte hihi,ubyte hilo,ubyte hi,ubyte lo)73 inline udword readEndian(ubyte hihi, ubyte hilo, ubyte hi, ubyte lo)
74 {
75 return(( (udword)hihi << 24 ) + ( (udword)hilo << 16 ) +
76 ( (udword)hi << 8 ) + (udword)lo );
77 }
78
79 // Read a little-endian 16-bit word from two bytes in memory.
readLEword(const ubyte ptr[2])80 inline uword readLEword(const ubyte ptr[2])
81 {
82 #if defined(WORDS_LITTLEENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
83 return *((uword*)ptr);
84 #else
85 return readEndian(ptr[1],ptr[0]);
86 #endif
87 }
88
89 // Write a big-endian 16-bit word to two bytes in memory.
writeLEword(ubyte ptr[2],uword someWord)90 inline void writeLEword(ubyte ptr[2], uword someWord)
91 {
92 #if defined(WORDS_LITTLEENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
93 *((uword*)ptr) = someWord;
94 #else
95 ptr[0] = (someWord & 0xFF);
96 ptr[1] = (someWord >> 8);
97 #endif
98 }
99
100
101
102 // Read a big-endian 16-bit word from two bytes in memory.
readBEword(const ubyte ptr[2])103 inline uword readBEword(const ubyte ptr[2])
104 {
105 #if defined(WORDS_BIGENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
106 return *((uword*)ptr);
107 #else
108 return ( (((uword)ptr[0])<<8) + ((uword)ptr[1]) );
109 #endif
110 }
111
112 // Read a big-endian 32-bit word from four bytes in memory.
readBEdword(const ubyte ptr[4])113 inline udword readBEdword(const ubyte ptr[4])
114 {
115 #if defined(WORDS_BIGENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
116 return *((udword*)ptr);
117 #else
118 return ( (((udword)ptr[0])<<24) + (((udword)ptr[1])<<16)
119 + (((udword)ptr[2])<<8) + ((udword)ptr[3]) );
120 #endif
121 }
122
123 // Write a big-endian 16-bit word to two bytes in memory.
writeBEword(ubyte ptr[2],uword someWord)124 inline void writeBEword(ubyte ptr[2], uword someWord)
125 {
126 #if defined(WORDS_BIGENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
127 *((uword*)ptr) = someWord;
128 #else
129 ptr[0] = someWord >> 8;
130 ptr[1] = someWord & 0xFF;
131 #endif
132 }
133
134 // Write a big-endian 32-bit word to four bytes in memory.
writeBEdword(ubyte ptr[4],udword someDword)135 inline void writeBEdword(ubyte ptr[4], udword someDword)
136 {
137 #if defined(WORDS_BIGENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
138 *((udword*)ptr) = someDword;
139 #else
140 ptr[0] = someDword >> 24;
141 ptr[1] = (someDword>>16) & 0xFF;
142 ptr[2] = (someDword>>8) & 0xFF;
143 ptr[3] = someDword & 0xFF;
144 #endif
145 }
146
147
148
149 // Convert 16-bit little-endian word to big-endian order or vice versa.
convertEndianess(uword intelword)150 inline uword convertEndianess( uword intelword )
151 {
152 uword hi = intelword >> 8;
153 uword lo = intelword & 255;
154 return(( lo << 8 ) + hi );
155 }
156
157 // Convert 32-bit little-endian word to big-endian order or vice versa.
convertEndianess(udword inteldword)158 inline udword convertEndianess( udword inteldword )
159 {
160 udword hihi = inteldword >> 24;
161 udword hilo = ( inteldword >> 16 ) & 0xFF;
162 udword hi = ( inteldword >> 8 ) & 0xFF;
163 udword lo = inteldword & 0xFF;
164 return(( lo << 24 ) + ( hi << 16 ) + ( hilo << 8 ) + hihi );
165 }
166
167
168 #endif // MYENDIAN_H
169