-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitfieldMacros.h
219 lines (176 loc) · 9.02 KB
/
BitfieldMacros.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef BITFIELD_MACROS_H
#define BITFIELD_MACROS_H
#include <stdio.h>
#include <arpa/inet.h>
////////////////////////////////////////////////////////////////////////////////
//
// This file includes macros that are designed to only work with the
// MemoryMappedHardware classes, they were just put in this separate file
// rather than that file for readability purposes
//
////////////////////////////////////////////////////////////////////////////////
#if defined(FORCE_BIG_ENDIAN)
// we force a big endian byte interpretation ourselves based on the compiler flag
// big endian, no need to do any byte swapping
#define HTONS(n) (n)
#define NTOHS(n) (n)
#define HTONL(n) (n)
#define NTOHL(n) (n)
// simple endianess checker, were forcing big endian at compile time, hardcode to return true
inline bool isBigEndian(void)
{
return(true);
}
#elif defined(FORCE_LITTLE_ENDIAN)
// we force a little endian byte interpretation ourselves based on the compiler flag
// little endian, swap bytes to put in bit endian order
#define HTONS(n) (((((uint16_t)(n) & 0xFF)) << 8) | (((uint16_t)(n) & 0xFF00) >> 8))
#define NTOHS(n) (((((uint16_t)(n) & 0xFF)) << 8) | (((uint16_t)(n) & 0xFF00) >> 8))
#define HTONL(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
((((uint32_t)(n) & 0xFF00)) << 8) | \
((((uint32_t)(n) & 0xFF0000)) >> 8) | \
((((uint32_t)(n) & 0xFF000000)) >> 24))
#define NTOHL(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
((((uint32_t)(n) & 0xFF00)) << 8) | \
((((uint32_t)(n) & 0xFF0000)) >> 8) | \
((((uint32_t)(n) & 0xFF000000)) >> 24))
// simple endianess checker, were forcing little endian at compile time, hardcode to return false
inline bool isBigEndian(void)
{
return(false);
}
#else
// no compiler flag for byte order, let the system decide
#define HTONS(n) htons(n)
#define NTOHS(n) ntohs(n)
#define HTONL(n) htonl(n)
#define NTOHL(n) ntohl(n)
// simple endianess checker, we auto detect the endianess
inline bool isBigEndian(void)
{
static uint8_t endian[2] = {0,1};
return(*(uint16_t *)endian == 1);
}
#endif
// all these macros are designed to only work with the MemoryMappedHardware classes,
// they were just put in a separate file rather than that file for readability purposes
#define MAX_BITFIELD_VALUE(lowOrderBit, highOrderBit) (unsigned)((1<<(highOrderBit-lowOrderBit+1))-1)
#define BITMASK(lowOrderBit, highOrderBit) (MAX_BITFIELD_VALUE(lowOrderBit, highOrderBit)<<lowOrderBit)
#define SET_BITFIELD8(fullValue, lowOrderBit, highOrderBit, bitfieldValue) (fullValue = ((fullValue & ~BITMASK(lowOrderBit, highOrderBit)) | (bitfieldValue << lowOrderBit)))
#define GET_BITFIELD8(fullValue, lowOrderBit, highOrderBit) return(((fullValue & BITMASK(lowOrderBit, highOrderBit)) >> lowOrderBit));
#define SET_BITFIELD16(fullValue, lowOrderBit, highOrderBit, bitfieldValue) (fullValue = NTOHS(((HTONS(fullValue) & ~BITMASK(lowOrderBit, highOrderBit)) | (bitfieldValue << lowOrderBit))))
#define GET_BITFIELD16(fullValue, lowOrderBit, highOrderBit) return(((HTONS(fullValue) & BITMASK(lowOrderBit, highOrderBit)) >> lowOrderBit));
#define SET_BITFIELD32(fullValue, lowOrderBit, highOrderBit, bitfieldValue) (fullValue = NTOHL(((HTONL(fullValue) & ~BITMASK(lowOrderBit, highOrderBit)) | (bitfieldValue << lowOrderBit))))
#define GET_BITFIELD32(fullValue, lowOrderBit, highOrderBit) return(((HTONL(fullValue) & BITMASK(lowOrderBit, highOrderBit)) >> lowOrderBit));
// undefine this for performance
#if defined(ERROR_CHECKING)
// real error checking macros for debug/diagnostics
#define SET_REGISTER_ERROR_CHECKING(register_) \
if (_address == NULL) \
{ \
printf("ERROR: device: %s, REGISTER: address is NULL\n", getName()); \
return; \
} \
else if (register_ > _size) \
{ \
printf("ERROR: device: %s, REGISTER: requested register: %d, exceeds memory mapped size: %d\n", getName(), register_, _size); \
return; \
}
#define GET_REGISTER_ERROR_CHECKING(register_) \
if (_address == NULL) \
{ \
printf("ERROR: device: %s, REGISTER: address is NULL\n", getName()); \
return (0); \
} \
else if (register_ > _size) \
{ \
printf("ERROR: device: %s, REGISTER: requested register: %d, exceeds memory mapped size: %d\n", getName(), register_, _size); \
return (0); \
}
#define SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, numBits_, value_) \
if (value_ > MAX_BITFIELD_VALUE(lowOrderBit_, highOrderBit_)) \
{ \
printf("ERROR: BITFIELD: value: %d, exceeds max bitfield value: %d\n", value_, MAX_BITFIELD_VALUE(lowOrderBit_, highOrderBit_)); \
return; \
} \
else if (lowOrderBit_ > highOrderBit_) \
{ \
printf("ERROR: BITFIELD: lowOrderBit: %d, is greater than highOrderBit: %d\n", lowOrderBit_, highOrderBit_); \
return; \
} \
else if (highOrderBit_ > (numBits_-1)) \
{ \
printf("ERROR: BITFIELD: highOrderBit: %d, exceeds range: 0-%d, for %d-bit value\n", highOrderBit_, (numBits_-1), numBits_); \
return; \
}
#define GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, numBits_) \
if (lowOrderBit_ > highOrderBit_) \
{ \
printf("ERROR: BITFIELD: lowOrderBit: %d, is greater than highOrderBit: %d\n", lowOrderBit_, highOrderBit_); \
return (0); \
} \
else if (highOrderBit_ > (numBits_-1)) \
{ \
printf("ERROR: BITFIELD: highOrderBit: %d, exceeds range: 0-%d, for %d-bit value\n", highOrderBit_, (numBits_-1), numBits_); \
return (0); \
}
#else
// dummy macros when compiling for performance
#define SET_REGISTER_ERROR_CHECKING(register_)
#define GET_REGISTER_ERROR_CHECKING(register_)
#define SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, numBits_, value_)
#define GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, numBits_)
#endif
// thes macros are used by the MemoryMappedHardware classes and
// assume a base memory mapped address of a given HW device
#define SET_REGISTER_BITFIELD8(register_, lowOrderBit_, highOrderBit_, value_) \
SET_REGISTER_ERROR_CHECKING(register_) \
SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 8, value_) \
SET_BITFIELD8(_address[register_], lowOrderBit_, highOrderBit_, value_)
#define GET_REGISTER_BITFIELD8(register_, lowOrderBit_, highOrderBit_) \
GET_REGISTER_ERROR_CHECKING(register_) \
GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 8) \
GET_BITFIELD8(_address[register_], lowOrderBit_, highOrderBit_)
#define SET_REGISTER_BITFIELD16(register_, lowOrderBit_, highOrderBit_, value_) \
SET_REGISTER_ERROR_CHECKING(register_) \
SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 16, value_) \
SET_BITFIELD16(_address[register_], lowOrderBit_, highOrderBit_, value_)
#define GET_REGISTER_BITFIELD16(register_, lowOrderBit_, highOrderBit_) \
GET_REGISTER_ERROR_CHECKING(register_) \
GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 16) \
GET_BITFIELD16(_address[register_], lowOrderBit_, highOrderBit_)
#define SET_REGISTER_BITFIELD32(register_, lowOrderBit_, highOrderBit_, value_) \
SET_REGISTER_ERROR_CHECKING(register_) \
SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 32, value_) \
SET_BITFIELD32(_address[register_], lowOrderBit_, highOrderBit_, value_)
#define GET_REGISTER_BITFIELD32(register_, lowOrderBit_, highOrderBit_) \
GET_REGISTER_ERROR_CHECKING(register_) \
GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 32) \
GET_BITFIELD32(_address[register_], lowOrderBit_, highOrderBit_)
#define SET_REGISTER_VALUE(register_, value_) \
SET_REGISTER_ERROR_CHECKING(register_) \
_address[register_] = value_;
#define GET_REGISTER_VALUE(register_) \
GET_REGISTER_ERROR_CHECKING(register_) \
return(_address[register_]);
// thes macros are used by the BitBanger classes and use the passed in values as-is,
// without assuming a base memory mapped address of a given HW device
#define SET_VALUE_BITFIELD8(fullValue_, lowOrderBit_, highOrderBit_, bitfieldValue_) \
SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 8, bitfieldValue_) \
SET_BITFIELD8(fullValue_, lowOrderBit_, highOrderBit_, bitfieldValue_)
#define GET_VALUE_BITFIELD8(fullValue_, lowOrderBit_, highOrderBit_) \
GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 8) \
GET_BITFIELD8(fullValue_, lowOrderBit_, highOrderBit_)
#define SET_VALUE_BITFIELD16(fullValue_, lowOrderBit_, highOrderBit_, bitfieldValue_) \
SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 16, bitfieldValue_) \
SET_BITFIELD16(fullValue_, lowOrderBit_, highOrderBit_, bitfieldValue_)
#define GET_VALUE_BITFIELD16(fullValue_, lowOrderBit_, highOrderBit_) \
GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 16) \
GET_BITFIELD16(fullValue_, lowOrderBit_, highOrderBit_)
#define SET_VALUE_BITFIELD32(fullValue_, lowOrderBit_, highOrderBit_, bitfieldValue_) \
SET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 32, bitfieldValue_) \
SET_BITFIELD32(fullValue_, lowOrderBit_, highOrderBit_, bitfieldValue_)
#define GET_VALUE_BITFIELD32(fullValue_, lowOrderBit_, highOrderBit_) \
GET_BITFIELD_ERROR_CHECKING(lowOrderBit_, highOrderBit_, 32) \
GET_BITFIELD32(fullValue_, lowOrderBit_, highOrderBit_)
#endif