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