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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/*
* Lib(X)SVF - A library for implementing SVF and XSVF JTAG players
*
* Copyright (C) 2009 RIEGL Research ForschungsGmbH
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "libxsvf.h"
/* command codes as defined in xilinx xapp503 */
enum xsvf_cmd {
XCOMPLETE = 0x00,
XTDOMASK = 0x01,
XSIR = 0x02,
XSDR = 0x03,
XRUNTEST = 0x04,
XREPEAT = 0x07,
XSDRSIZE = 0x08,
XSDRTDO = 0x09,
XSETSDRMASKS = 0x0A,
XSDRINC = 0x0B,
XSDRB = 0x0C,
XSDRC = 0x0D,
XSDRE = 0x0E,
XSDRTDOB = 0x0F,
XSDRTDOC = 0x10,
XSDRTDOE = 0x11,
XSTATE = 0x12,
XENDIR = 0x13,
XENDDR = 0x14,
XSIR2 = 0x15,
XCOMMENT = 0x16,
XWAIT = 0x17
};
// This is to not confuse the VIM syntax highlighting
#define VAL_OPEN (
#define VAL_CLOSE )
#define READ_BITS(_buf, _len) do { \
unsigned char *_p = _buf; int _i; \
for (_i=0; _i<(_len); _i+=8) { \
int tmp = LIBXSVF_HOST_GETBYTE(); \
if (tmp < 0) { \
LIBXSVF_HOST_REPORT_ERROR("Unexpected EOF."); \
goto error; \
} \
*(_p++) = tmp; \
} \
} while (0)
#define READ_LONG() VAL_OPEN{ \
long _buf = 0; int _i; \
for (_i=0; _i<4; _i++) { \
int tmp = LIBXSVF_HOST_GETBYTE(); \
if (tmp < 0) { \
LIBXSVF_HOST_REPORT_ERROR("Unexpected EOF."); \
goto error; \
} \
_buf = _buf << 8 | tmp; \
} \
_buf; \
}VAL_CLOSE
#define READ_BYTE() VAL_OPEN{ \
int _tmp = LIBXSVF_HOST_GETBYTE(); \
if (_tmp < 0) { \
LIBXSVF_HOST_REPORT_ERROR("Unexpected EOF."); \
goto error; \
} \
_tmp; \
}VAL_CLOSE
#define SHIFT_DATA(_inp, _outp, _maskp, _len, _state, _estate, _edelay, _ret) do { \
if (shift_data(h, _inp, _outp, _maskp, _len, _state, _estate, _edelay, _ret) < 0) { \
goto error; \
} \
} while (0)
#define TAP(_state) do { \
if (libxsvf_tap_walk(h, _state) < 0) \
goto error; \
} while (0)
static int bits2bytes(int bits)
{
return (bits+7) / 8;
}
static int getbit(unsigned char *data, int n)
{
return (data[n/8] & (1 << (7 - n%8))) ? 1 : 0;
}
static void setbit(unsigned char *data, int n, int v)
{
unsigned char mask = 1 << (7 - n%8);
if (v)
data[n/8] |= mask;
else
data[n/8] &= ~mask;
}
static int xilinx_tap(int state)
{
/* state codes as defined in xilinx xapp503 */
switch (state)
{
case 0x00:
return LIBXSVF_TAP_RESET;
break;
case 0x01:
return LIBXSVF_TAP_IDLE;
break;
case 0x02:
return LIBXSVF_TAP_DRSELECT;
break;
case 0x03:
return LIBXSVF_TAP_DRCAPTURE;
break;
case 0x04:
return LIBXSVF_TAP_DRSHIFT;
break;
case 0x05:
return LIBXSVF_TAP_DREXIT1;
break;
case 0x06:
return LIBXSVF_TAP_DRPAUSE;
break;
case 0x07:
return LIBXSVF_TAP_DREXIT2;
break;
case 0x08:
return LIBXSVF_TAP_DRUPDATE;
break;
case 0x09:
return LIBXSVF_TAP_IRSELECT;
break;
case 0x0A:
return LIBXSVF_TAP_IRCAPTURE;
break;
case 0x0B:
return LIBXSVF_TAP_IRSHIFT;
break;
case 0x0C:
return LIBXSVF_TAP_IREXIT1;
break;
case 0x0D:
return LIBXSVF_TAP_IRPAUSE;
break;
case 0x0E:
return LIBXSVF_TAP_IREXIT2;
break;
case 0x0F:
return LIBXSVF_TAP_IRUPDATE;
break;
}
return -1;
}
static int shift_data(struct libxsvf_host *h, unsigned char *inp, unsigned char *outp, unsigned char *maskp, int len, enum libxsvf_tap_state state, enum libxsvf_tap_state estate, int edelay, int retries)
{
int left_padding = (8 - len % 8) % 8;
int with_retries = retries > 0;
int i;
if (with_retries && LIBXSVF_HOST_SYNC() < 0) {
LIBXSVF_HOST_REPORT_ERROR("TDO mismatch.");
return -1;
}
while (1)
{
int tdo_error = 0;
int tms = 0;
TAP(state);
tms = 0;
for (i=len+left_padding-1; i>=left_padding; i--) {
if (i == left_padding && h->tap_state != estate) {
h->tap_state++;
tms = 1;
}
int tdi = getbit(inp, i);
int tdo = -1;
if (maskp && getbit(maskp, i))
tdo = outp && getbit(outp, i);
int sync = with_retries && i == left_padding;
if (LIBXSVF_HOST_PULSE_TCK(tms, tdi, tdo, 0, sync) < 0)
tdo_error = 1;
}
if (tms)
LIBXSVF_HOST_REPORT_TAPSTATE();
if (edelay) {
TAP(LIBXSVF_TAP_IDLE);
LIBXSVF_HOST_UDELAY(edelay, 0, edelay);
} else {
TAP(estate);
}
if (!tdo_error)
return 0;
if (retries <= 0) {
LIBXSVF_HOST_REPORT_ERROR("TDO mismatch.");
return -1;
}
retries--;
}
error:
return -1;
}
int libxsvf_xsvf(struct libxsvf_host *h)
{
int rc = 0;
int i, j;
unsigned char *buf_tdi_data = (void*)0;
unsigned char *buf_tdo_data = (void*)0;
unsigned char *buf_tdo_mask = (void*)0;
unsigned char *buf_addr_mask = (void*)0;
unsigned char *buf_data_mask = (void*)0;
long state_dr_size = 0;
long state_data_size = 0;
long state_runtest = 0;
unsigned char state_xendir = 0;
unsigned char state_xenddr = 0;
unsigned char state_retries = 0;
unsigned char cmd = 0;
while (1)
{
unsigned char last_cmd = cmd;
cmd = LIBXSVF_HOST_GETBYTE();
#define STATUS(_c) LIBXSVF_HOST_REPORT_STATUS("XSVF Command " #_c);
switch (cmd)
{
case XCOMPLETE: {
STATUS(XCOMPLETE);
goto got_complete_command;
}
case XTDOMASK: {
STATUS(XTDOMASK);
READ_BITS(buf_tdo_mask, state_dr_size);
break;
}
case XSIR: {
STATUS(XSIR);
int length = READ_BYTE();
unsigned char buf[bits2bytes(length)];
READ_BITS(buf, length);
SHIFT_DATA(buf, (void*)0, (void*)0, length, LIBXSVF_TAP_IRSHIFT,
state_xendir ? LIBXSVF_TAP_IRPAUSE : LIBXSVF_TAP_IDLE,
state_runtest, state_retries);
break;
}
case XSDR: {
STATUS(XSDR);
READ_BITS(buf_tdi_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, buf_tdo_data, buf_tdo_mask, state_dr_size, LIBXSVF_TAP_DRSHIFT,
state_xenddr ? LIBXSVF_TAP_DRPAUSE : LIBXSVF_TAP_IDLE,
state_runtest, state_retries);
break;
}
case XRUNTEST: {
STATUS(XRUNTEST);
state_runtest = READ_LONG();
break;
}
case XREPEAT: {
STATUS(XREPEAT);
state_retries = READ_BYTE();
break;
}
case XSDRSIZE: {
STATUS(XSDRSIZE);
state_dr_size = READ_LONG();
buf_tdi_data = LIBXSVF_HOST_REALLOC(buf_tdi_data, bits2bytes(state_dr_size), LIBXSVF_MEM_XSVF_TDI_DATA);
buf_tdo_data = LIBXSVF_HOST_REALLOC(buf_tdo_data, bits2bytes(state_dr_size), LIBXSVF_MEM_XSVF_TDO_DATA);
buf_tdo_mask = LIBXSVF_HOST_REALLOC(buf_tdo_mask, bits2bytes(state_dr_size), LIBXSVF_MEM_XSVF_TDO_MASK);
buf_addr_mask = LIBXSVF_HOST_REALLOC(buf_addr_mask, bits2bytes(state_dr_size), LIBXSVF_MEM_XSVF_ADDR_MASK);
buf_data_mask = LIBXSVF_HOST_REALLOC(buf_data_mask, bits2bytes(state_dr_size), LIBXSVF_MEM_XSVF_DATA_MASK);
if (!buf_tdi_data || !buf_tdo_data || !buf_tdo_mask || !buf_addr_mask || !buf_data_mask) {
LIBXSVF_HOST_REPORT_ERROR("Allocating memory failed.");
goto error;
}
break;
}
case XSDRTDO: {
STATUS(XSDRTDO);
READ_BITS(buf_tdi_data, state_dr_size);
READ_BITS(buf_tdo_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, buf_tdo_data, buf_tdo_mask, state_dr_size, LIBXSVF_TAP_DRSHIFT,
state_xenddr ? LIBXSVF_TAP_DRPAUSE : LIBXSVF_TAP_IDLE,
state_runtest, state_retries);
break;
}
case XSETSDRMASKS: {
STATUS(XSETSDRMASKS);
READ_BITS(buf_addr_mask, state_dr_size);
READ_BITS(buf_data_mask, state_dr_size);
state_data_size = 0;
for (i=0; i<state_dr_size; i++)
state_data_size += getbit(buf_data_mask, i);
break;
}
case XSDRINC: {
STATUS(XSDRINC);
READ_BITS(buf_tdi_data, state_dr_size);
int num = READ_BYTE();
while (1) {
SHIFT_DATA(buf_tdi_data, buf_tdo_data, buf_tdo_mask, state_dr_size, LIBXSVF_TAP_DRSHIFT,
state_xenddr ? LIBXSVF_TAP_DRPAUSE : LIBXSVF_TAP_IDLE,
state_runtest, state_retries);
if (num-- <= 0)
break;
int carry = 1;
for (i=state_dr_size-1; i>=0; i--) {
if (getbit(buf_addr_mask, i) == 0)
continue;
if (getbit(buf_tdi_data, i)) {
setbit(buf_tdi_data, i, !carry);
} else {
setbit(buf_tdi_data, i, carry);
carry = 0;
}
}
unsigned char this_byte = 0;
for (i=0, j=0; i<state_data_size; i++) {
if (i%8 == 0)
this_byte = READ_BYTE();
while (getbit(buf_data_mask, j) == 0)
j++;
setbit(buf_tdi_data, j++, getbit(&this_byte, i%8));
}
}
break;
}
case XSDRB: {
STATUS(XSDRB);
READ_BITS(buf_tdi_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, (void*)0, (void*)0, state_dr_size, LIBXSVF_TAP_DRSHIFT, LIBXSVF_TAP_DRSHIFT, 0, 0);
break;
}
case XSDRC: {
STATUS(XSDRC);
READ_BITS(buf_tdi_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, (void*)0, (void*)0, state_dr_size, LIBXSVF_TAP_DRSHIFT, LIBXSVF_TAP_DRSHIFT, 0, 0);
break;
}
case XSDRE: {
STATUS(XSDRE);
READ_BITS(buf_tdi_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, (void*)0, (void*)0, state_dr_size, LIBXSVF_TAP_DRSHIFT,
state_xenddr ? LIBXSVF_TAP_DRPAUSE : LIBXSVF_TAP_IDLE, 0, 0);
break;
}
case XSDRTDOB: {
STATUS(XSDRTDOB);
READ_BITS(buf_tdi_data, state_dr_size);
READ_BITS(buf_tdo_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, buf_tdo_data, (void*)0, state_dr_size, LIBXSVF_TAP_DRSHIFT, LIBXSVF_TAP_DRSHIFT, 0, 0);
break;
}
case XSDRTDOC: {
STATUS(XSDRTDOC);
READ_BITS(buf_tdi_data, state_dr_size);
READ_BITS(buf_tdo_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, buf_tdo_data, (void*)0, state_dr_size, LIBXSVF_TAP_DRSHIFT, LIBXSVF_TAP_DRSHIFT, 0, 0);
break;
}
case XSDRTDOE: {
STATUS(XSDRTDOE);
READ_BITS(buf_tdi_data, state_dr_size);
READ_BITS(buf_tdo_data, state_dr_size);
SHIFT_DATA(buf_tdi_data, buf_tdo_data, (void*)0, state_dr_size, LIBXSVF_TAP_DRSHIFT,
state_xenddr ? LIBXSVF_TAP_DRPAUSE : LIBXSVF_TAP_IDLE, 0, 0);
break;
}
case XSTATE: {
STATUS(XSTATE);
if (state_runtest && last_cmd == XRUNTEST) {
TAP(LIBXSVF_TAP_IDLE);
LIBXSVF_HOST_UDELAY(state_runtest, 0, state_runtest);
}
unsigned char state = READ_BYTE();
TAP(xilinx_tap(state));
break;
}
case XENDIR: {
STATUS(XENDIR);
state_xendir = READ_BYTE();
break;
}
case XENDDR: {
STATUS(XENDDR);
state_xenddr = READ_BYTE();
break;
}
case XSIR2: {
STATUS(XSIR2);
int length = READ_BYTE();
length = length << 8 | READ_BYTE();
unsigned char buf[bits2bytes(length)];
READ_BITS(buf, length);
SHIFT_DATA(buf, (void*)0, (void*)0, length, LIBXSVF_TAP_IRSHIFT,
state_xendir ? LIBXSVF_TAP_IRPAUSE : LIBXSVF_TAP_IDLE,
state_runtest, state_retries);
break;
}
case XCOMMENT: {
STATUS(XCOMMENT);
unsigned char this_byte;
do {
this_byte = READ_BYTE();
} while (this_byte);
break;
}
case XWAIT: {
STATUS(XWAIT);
unsigned char state1 = READ_BYTE();
unsigned char state2 = READ_BYTE();
long usecs = READ_LONG();
TAP(xilinx_tap(state1));
LIBXSVF_HOST_UDELAY(usecs, 0, 0);
TAP(xilinx_tap(state2));
break;
}
default:
LIBXSVF_HOST_REPORT_ERROR("Unknown XSVF command.");
goto error;
}
}
error:
rc = -1;
got_complete_command:
if (LIBXSVF_HOST_SYNC() != 0 && rc >= 0 ) {
LIBXSVF_HOST_REPORT_ERROR("TDO mismatch.");
rc = -1;
}
LIBXSVF_HOST_REALLOC(buf_tdi_data, 0, LIBXSVF_MEM_XSVF_TDI_DATA);
LIBXSVF_HOST_REALLOC(buf_tdo_data, 0, LIBXSVF_MEM_XSVF_TDO_DATA);
LIBXSVF_HOST_REALLOC(buf_tdo_mask, 0, LIBXSVF_MEM_XSVF_TDO_MASK);
LIBXSVF_HOST_REALLOC(buf_addr_mask, 0, LIBXSVF_MEM_XSVF_ADDR_MASK);
LIBXSVF_HOST_REALLOC(buf_data_mask, 0, LIBXSVF_MEM_XSVF_DATA_MASK);
return rc;
}
|