-
Notifications
You must be signed in to change notification settings - Fork 7
/
jtaghal.cpp
286 lines (234 loc) · 9.01 KB
/
jtaghal.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/***********************************************************************************************************************
* *
* ANTIKERNEL v0.1 *
* *
* Copyright (c) 2012-2018 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/
/**
@file
@author Andrew D. Zonenberg
@brief Implementation of global functions
*/
#include "jtaghal.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Byte manipulation
/**
@brief Extracts a bit from a bit string
(data[0] & 1) is considered to be the LSB.
@param data The bit string
@param nbit Index (zero based) of the bit to extract
@return Value of the bit
\ingroup libjtaghal
*/
bool PeekBit(const unsigned char* data, int nbit)
{
unsigned char w = data[nbit/8];
w >>= (nbit & 7);
w &= 1;
return w;
}
/**
@brief Writes a bit to a bit string.
(data[0] & 1) is considered to be the LSB.
@param data The bit string
@param nbit Index (zero based) of the bit to write
@param val The value to write at that bit
\ingroup libjtaghal
*/
void PokeBit(unsigned char* data, int nbit, bool val)
{
unsigned char w = data[nbit/8];
int bitpos = nbit & 7;
unsigned char mask = 1 << bitpos;
w &= ~mask;
w |= (val << bitpos);
data[nbit/8] = w;
}
/**
@brief Flips the bits in a byte
@param c Input byte
@return Output byte
\ingroup libjtaghal
*/
unsigned char FlipByte(unsigned char c)
{
return
( ( (c >> 0) & 1) << 7 ) |
( ( (c >> 1) & 1) << 6 ) |
( ( (c >> 2) & 1) << 5 ) |
( ( (c >> 3) & 1) << 4 ) |
( ( (c >> 4) & 1) << 3 ) |
( ( (c >> 5) & 1) << 2 ) |
( ( (c >> 6) & 1) << 1 ) |
( ( (c >> 7) & 1) << 0 );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Array manipulation
/**
@brief Reverses an array of bytes in place without changing bit ordering
@param data The buffer to manipulate
@param len Length, in bytes, of the buffer
\ingroup libjtaghal
*/
void FlipByteArray(unsigned char* data, int len)
{
unsigned char* temp = new unsigned char[len];
memcpy(temp, data, len);
for(int i=0; i<len; i++)
data[i] = temp[len-i-1];
delete[] temp;
}
/**
@brief Reverses the bit ordering in an array of bytes, but does not change byte ordering
@param data The buffer to manipulate
@param len Length, in bytes, of the buffer
\ingroup libjtaghal
*/
void FlipBitArray(unsigned char* data, int len)
{
for(int i=0; i<len; i++)
data[i] = FlipByte(data[i]);
}
/**
@brief Reverses the bit ordering in an array of bits (need not be integer byte size)
@param data The buffer to manipulate
@param bitlen Length, in bits, of the buffer
\ingroup libjtaghal
*/
void MirrorBitArray(unsigned char* data, int bitlen)
{
int bytesize = ceil(bitlen / 8.0f);
unsigned char* temp = new unsigned char[bytesize];
memcpy(temp, data, bytesize);
for(int i=0; i<bitlen; i++)
PokeBit(data, i, PeekBit(temp, bitlen-(i+1)));
delete[] temp;
}
/**
@brief Swaps endianness in an array of 16-bit values
@param data The buffer to manipulate
@param len Length, in bytes, of the buffer (must be even)
\ingroup libjtaghal
*/
void FlipEndianArray(unsigned char* data, int len)
{
//make sure len is even
len &= ~1;
for(int i=0; i<len; i+= 2)
{
unsigned char temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
}
}
/**
@brief Swaps endianness in an array of 32-bit values
@param data The buffer to manipulate
@param len Length, in bytes, of the buffer (must be a multiple of 4)
\ingroup libjtaghal
*/
void FlipEndian32Array(unsigned char* data, int len)
{
//make sure len is even
len &= ~3;
for(int i=0; i<len; i+= 4)
{
unsigned char temp[4] = { data[i], data[i+1], data[i+2], data[i+3] };
data[i] = temp[3];
data[i+1] = temp[2];
data[i+2] = temp[1];
data[i+3] = temp[0];
}
}
/**
@brief Reverses the bit ordering in an array of bytes, as well as 16-bit endianness
@param data The buffer to manipulate
@param len Length, in bytes, of the buffer
\ingroup libjtaghal
*/
void FlipBitAndEndianArray(unsigned char* data, int len)
{
FlipEndianArray(data, len);
FlipBitArray(data, len);
}
/**
@brief Reverses the bit ordering in an array of bytes, as well as 32-bit endianness
@param data The buffer to manipulate
@param len Length, in bytes, of the buffer
\ingroup libjtaghal
*/
void FlipBitAndEndian32Array(unsigned char* data, int len)
{
FlipEndian32Array(data, len);
FlipBitArray(data, len);
}
/**
@brief Casts (data+offset) to a uint16_t and dereferences it with big-endian ordering.
Byte-level accesses are used to ensure safety for machines requiring aligned access to words.
*/
uint16_t GetBigEndianUint16FromByteArray(const unsigned char* data, size_t offset)
{
return
(static_cast<uint16_t>(data[offset]) << 8) |
static_cast<uint16_t>(data[offset+1]);
}
/**
@brief Casts (data+offset) to a uint32_t and dereferences it with big-endian ordering.
Byte-level accesses are used to ensure safety for machines requiring aligned access to words.
*/
uint32_t GetBigEndianUint32FromByteArray(const unsigned char* data, size_t offset)
{
return
(static_cast<uint16_t>(data[offset]) << 24) |
(static_cast<uint16_t>(data[offset+1]) << 16) |
(static_cast<uint16_t>(data[offset+2]) << 8) |
static_cast<uint16_t>(data[offset+3]);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Performance measurement
/**
@brief Returns a timestamp suitable for performance measurement.
The base unit is seconds.
@return The timestamp.
\ingroup libjtaghal
*/
double GetTime()
{
#ifdef _WIN32
uint64_t tm;
static uint64_t freq = 0;
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&tm));
double ret = tm;
if(freq == 0)
QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq));
return ret / freq;
#else
timespec t;
clock_gettime(CLOCK_REALTIME,&t);
double d = static_cast<double>(t.tv_nsec) / 1E9f;
d += t.tv_sec;
return d;
#endif
}