3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 /*****************************************************************************
26 * abstract: This file contains the function, xsvfExecute(),
27 * call for interpreting the XSVF commands.
28 * Usage: Call xsvfExecute() to process XSVF data.
29 * The XSVF data is retrieved by readByte() in ports.c
30 * Remove the main function if you already have one.
31 * Options: XSVF_SUPPORT_COMPRESSION
32 * This define supports the XC9500/XL compression scheme.
33 * This define adds support for XSDRINC and XSETSDRMASKS.
34 * XSVF_SUPPORT_ERRORCODES
35 * This define causes the xsvfExecute function to return
36 * an error code for specific errors. See error codes below.
37 * If this is not defined, the return value defaults to the
38 * legacy values for backward compatibility:
39 * 1 = success; 0 = failure.
40 * Debugging: DEBUG_MODE (Legacy name)
41 * Define DEBUG_MODE to compile with debugging features.
42 * Both micro.c and ports.c must be compiled with the DEBUG_MODE
43 * defined to enable the standalone main implementation in
44 * micro.c that reads XSVF from a file.
45 * History: v2.00 - Original XSVF implementation.
46 * v4.04 - Added delay at end of XSIR for XC18v00 support.
47 * Added new commands for CoolRunner support:
48 * XSTATE, XENDIR, XENDDR
49 * v4.05 - Cleanup micro.c but leave ports.c intact.
50 * v4.06 - Fix xsvfGotoTapState for retry transition.
51 * v4.07 - Update example waitTime implementations for
52 * compatibility with Virtex-II.
53 * v4.10 - Add new XSIR2 command that supports a 2-byte
54 * IR-length parameter for IR shifts > 255 bits.
55 * v4.11 - No change. Update version to match SVF2XSVF xlator.
56 * v4.14 - Added XCOMMENT.
57 * v5.00 - Improve XSTATE support.
59 *****************************************************************************/
63 #include <asm/processor.h>
70 extern const unsigned char fpgadata[];
74 /*============================================================================
76 ============================================================================*/
78 #define XSVF_VERSION "5.00"
80 /*****************************************************************************
81 * Define: XSVF_SUPPORT_COMPRESSION
82 * Description: Define this to support the XC9500/XL XSVF data compression
84 * Code size can be reduced by NOT supporting this feature.
85 * However, you must use the -nc (no compress) option when
86 * translating SVF to XSVF using the SVF2XSVF translator.
87 * Corresponding, uncompressed XSVF may be larger.
88 *****************************************************************************/
89 #ifndef XSVF_SUPPORT_COMPRESSION
90 #define XSVF_SUPPORT_COMPRESSION 1
93 /*****************************************************************************
94 * Define: XSVF_SUPPORT_ERRORCODES
95 * Description: Define this to support the new XSVF error codes.
96 * (The original XSVF player just returned 1 for success and
97 * 0 for an unspecified failure.)
98 *****************************************************************************/
99 #ifndef XSVF_SUPPORT_ERRORCODES
100 #define XSVF_SUPPORT_ERRORCODES 1
103 #ifdef XSVF_SUPPORT_ERRORCODES
104 #define XSVF_ERRORCODE(errorCode) errorCode
105 #else /* Use legacy error code */
106 #define XSVF_ERRORCODE(errorCode) ((errorCode==XSVF_ERROR_NONE)?1:0)
107 #endif /* XSVF_SUPPORT_ERRORCODES */
110 /*============================================================================
112 ============================================================================*/
116 #define XSVFDBG_PRINTF(iDebugLevel,pzFormat) \
117 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
118 printf( pzFormat ); }
119 #define XSVFDBG_PRINTF1(iDebugLevel,pzFormat,arg1) \
120 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
121 printf( pzFormat, arg1 ); }
122 #define XSVFDBG_PRINTF2(iDebugLevel,pzFormat,arg1,arg2) \
123 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
124 printf( pzFormat, arg1, arg2 ); }
125 #define XSVFDBG_PRINTF3(iDebugLevel,pzFormat,arg1,arg2,arg3) \
126 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
127 printf( pzFormat, arg1, arg2, arg3 ); }
128 #define XSVFDBG_PRINTLENVAL(iDebugLevel,plenVal) \
129 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
130 xsvfPrintLenVal(plenVal); }
131 #else /* !DEBUG_MODE */
132 #define XSVFDBG_PRINTF(iDebugLevel,pzFormat)
133 #define XSVFDBG_PRINTF1(iDebugLevel,pzFormat,arg1)
134 #define XSVFDBG_PRINTF2(iDebugLevel,pzFormat,arg1,arg2)
135 #define XSVFDBG_PRINTF3(iDebugLevel,pzFormat,arg1,arg2,arg3)
136 #define XSVFDBG_PRINTLENVAL(iDebugLevel,plenVal)
137 #endif /* DEBUG_MODE */
140 /*============================================================================
141 * XSVF Type Declarations
142 ============================================================================*/
144 /*****************************************************************************
146 * Description: This structure contains all of the data used during the
147 * execution of the XSVF. Some data is persistent, predefined
148 * information (e.g. lRunTestTime). The bulk of this struct's
149 * size is due to the lenVal structs (defined in lenval.h)
150 * which contain buffers for the active shift data. The MAX_LEN
151 * #define in lenval.h defines the size of these buffers.
152 * These buffers must be large enough to store the longest
153 * shift data in your XSVF file. For example:
154 * MAX_LEN >= ( longest_shift_data_in_bits / 8 )
155 * Because the lenVal struct dominates the space usage of this
156 * struct, the rough size of this struct is:
157 * sizeof( SXsvfInfo ) ~= MAX_LEN * 7 (number of lenVals)
158 * xsvfInitialize() contains initialization code for the data
160 * xsvfCleanup() contains cleanup code for the data in this
162 *****************************************************************************/
163 typedef struct tagSXsvfInfo
165 /* XSVF status information */
166 unsigned char ucComplete; /* 0 = running; 1 = complete */
167 unsigned char ucCommand; /* Current XSVF command byte */
168 long lCommandCount; /* Number of commands processed */
169 int iErrorCode; /* An error code. 0 = no error. */
171 /* TAP state/sequencing information */
172 unsigned char ucTapState; /* Current TAP state */
173 unsigned char ucEndIR; /* ENDIR TAP state (See SVF) */
174 unsigned char ucEndDR; /* ENDDR TAP state (See SVF) */
176 /* RUNTEST information */
177 unsigned char ucMaxRepeat; /* Max repeat loops (for xc9500/xl) */
178 long lRunTestTime; /* Pre-specified RUNTEST time (usec) */
180 /* Shift Data Info and Buffers */
181 long lShiftLengthBits; /* Len. current shift data in bits */
182 short sShiftLengthBytes; /* Len. current shift data in bytes */
184 lenVal lvTdi; /* Current TDI shift data */
185 lenVal lvTdoExpected; /* Expected TDO shift data */
186 lenVal lvTdoCaptured; /* Captured TDO shift data */
187 lenVal lvTdoMask; /* TDO mask: 0=dontcare; 1=compare */
189 #ifdef XSVF_SUPPORT_COMPRESSION
190 /* XSDRINC Data Buffers */
191 lenVal lvAddressMask; /* Address mask for XSDRINC */
192 lenVal lvDataMask; /* Data mask for XSDRINC */
193 lenVal lvNextData; /* Next data for XSDRINC */
194 #endif /* XSVF_SUPPORT_COMPRESSION */
197 /* Declare pointer to functions that perform XSVF commands */
198 typedef int (*TXsvfDoCmdFuncPtr)( SXsvfInfo* );
200 /*============================================================================
202 ============================================================================*/
204 /* encodings of xsvf instructions */
215 #define XSETSDRMASKS 10
223 #define XSTATE 18 /* 4.00 */
224 #define XENDIR 19 /* 4.04 */
225 #define XENDDR 20 /* 4.04 */
226 #define XSIR2 21 /* 4.10 */
227 #define XCOMMENT 22 /* 4.14 */
228 #define XWAIT 23 /* 5.00 */
229 /* Insert new commands here */
230 /* and add corresponding xsvfDoCmd function to xsvf_pfDoCmd below. */
231 #define XLASTCMD 24 /* Last command marker */
234 /*============================================================================
235 * XSVF Command Parameter Values
236 ============================================================================*/
238 #define XSTATE_RESET 0 /* 4.00 parameter for XSTATE */
239 #define XSTATE_RUNTEST 1 /* 4.00 parameter for XSTATE */
241 #define XENDXR_RUNTEST 0 /* 4.04 parameter for XENDIR/DR */
242 #define XENDXR_PAUSE 1 /* 4.04 parameter for XENDIR/DR */
245 #define XTAPSTATE_RESET 0x00
246 #define XTAPSTATE_RUNTEST 0x01 /* a.k.a. IDLE */
247 #define XTAPSTATE_SELECTDR 0x02
248 #define XTAPSTATE_CAPTUREDR 0x03
249 #define XTAPSTATE_SHIFTDR 0x04
250 #define XTAPSTATE_EXIT1DR 0x05
251 #define XTAPSTATE_PAUSEDR 0x06
252 #define XTAPSTATE_EXIT2DR 0x07
253 #define XTAPSTATE_UPDATEDR 0x08
254 #define XTAPSTATE_IRSTATES 0x09 /* All IR states begin here */
255 #define XTAPSTATE_SELECTIR 0x09
256 #define XTAPSTATE_CAPTUREIR 0x0A
257 #define XTAPSTATE_SHIFTIR 0x0B
258 #define XTAPSTATE_EXIT1IR 0x0C
259 #define XTAPSTATE_PAUSEIR 0x0D
260 #define XTAPSTATE_EXIT2IR 0x0E
261 #define XTAPSTATE_UPDATEIR 0x0F
263 /*============================================================================
264 * XSVF Function Prototypes
265 ============================================================================*/
267 int xsvfDoIllegalCmd( SXsvfInfo* pXsvfInfo ); /* Illegal command function */
268 int xsvfDoXCOMPLETE( SXsvfInfo* pXsvfInfo );
269 int xsvfDoXTDOMASK( SXsvfInfo* pXsvfInfo );
270 int xsvfDoXSIR( SXsvfInfo* pXsvfInfo );
271 int xsvfDoXSIR2( SXsvfInfo* pXsvfInfo );
272 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo );
273 int xsvfDoXRUNTEST( SXsvfInfo* pXsvfInfo );
274 int xsvfDoXREPEAT( SXsvfInfo* pXsvfInfo );
275 int xsvfDoXSDRSIZE( SXsvfInfo* pXsvfInfo );
276 int xsvfDoXSDRTDO( SXsvfInfo* pXsvfInfo );
277 int xsvfDoXSETSDRMASKS( SXsvfInfo* pXsvfInfo );
278 int xsvfDoXSDRINC( SXsvfInfo* pXsvfInfo );
279 int xsvfDoXSDRBCE( SXsvfInfo* pXsvfInfo );
280 int xsvfDoXSDRTDOBCE( SXsvfInfo* pXsvfInfo );
281 int xsvfDoXSTATE( SXsvfInfo* pXsvfInfo );
282 int xsvfDoXENDXR( SXsvfInfo* pXsvfInfo );
283 int xsvfDoXCOMMENT( SXsvfInfo* pXsvfInfo );
284 int xsvfDoXWAIT( SXsvfInfo* pXsvfInfo );
285 /* Insert new command functions here */
287 /*============================================================================
288 * XSVF Global Variables
289 ============================================================================*/
291 /* Array of XSVF command functions. Must follow command byte value order! */
292 /* If your compiler cannot take this form, then convert to a switch statement*/
293 TXsvfDoCmdFuncPtr xsvf_pfDoCmd[] =
295 xsvfDoXCOMPLETE, /* 0 */
296 xsvfDoXTDOMASK, /* 1 */
299 xsvfDoXRUNTEST, /* 4 */
300 xsvfDoIllegalCmd, /* 5 */
301 xsvfDoIllegalCmd, /* 6 */
302 xsvfDoXREPEAT, /* 7 */
303 xsvfDoXSDRSIZE, /* 8 */
304 xsvfDoXSDRTDO, /* 9 */
305 #ifdef XSVF_SUPPORT_COMPRESSION
306 xsvfDoXSETSDRMASKS, /* 10 */
307 xsvfDoXSDRINC, /* 11 */
309 xsvfDoIllegalCmd, /* 10 */
310 xsvfDoIllegalCmd, /* 11 */
311 #endif /* XSVF_SUPPORT_COMPRESSION */
312 xsvfDoXSDRBCE, /* 12 */
313 xsvfDoXSDRBCE, /* 13 */
314 xsvfDoXSDRBCE, /* 14 */
315 xsvfDoXSDRTDOBCE, /* 15 */
316 xsvfDoXSDRTDOBCE, /* 16 */
317 xsvfDoXSDRTDOBCE, /* 17 */
318 xsvfDoXSTATE, /* 18 */
319 xsvfDoXENDXR, /* 19 */
320 xsvfDoXENDXR, /* 20 */
321 xsvfDoXSIR2, /* 21 */
322 xsvfDoXCOMMENT, /* 22 */
324 /* Insert new command functions here */
328 char* xsvf_pzCommandName[] =
356 char* xsvf_pzErrorName[] =
360 "ERROR: TDO mismatch",
361 "ERROR: TDO mismatch and exceeded max retries",
362 "ERROR: Unsupported XSVF command",
363 "ERROR: Illegal state specification",
364 "ERROR: Data overflows allocated MAX_LEN buffer size"
367 char* xsvf_pzTapState[] =
370 "RUNTEST/IDLE", /* 0x01 */
371 "DRSELECT", /* 0x02 */
372 "DRCAPTURE", /* 0x03 */
373 "DRSHIFT", /* 0x04 */
374 "DREXIT1", /* 0x05 */
375 "DRPAUSE", /* 0x06 */
376 "DREXIT2", /* 0x07 */
377 "DRUPDATE", /* 0x08 */
378 "IRSELECT", /* 0x09 */
379 "IRCAPTURE", /* 0x0A */
380 "IRSHIFT", /* 0x0B */
381 "IREXIT1", /* 0x0C */
382 "IRPAUSE", /* 0x0D */
383 "IREXIT2", /* 0x0E */
384 "IRUPDATE" /* 0x0F */
386 #endif /* DEBUG_MODE */
388 /*#ifdef DEBUG_MODE */
389 /* FILE* in; /XXX* Legacy DEBUG_MODE file pointer */
390 int xsvf_iDebugLevel;
391 /*#endif /XXX* DEBUG_MODE */
393 /*============================================================================
395 ============================================================================*/
397 /*****************************************************************************
398 * Function: xsvfPrintLenVal
399 * Description: Print the lenval value in hex.
400 * Parameters: plv - ptr to lenval.
402 *****************************************************************************/
404 void xsvfPrintLenVal( lenVal *plv )
411 for ( i = 0; i < plv->len; ++i )
413 printf( "%02x", ((unsigned int)(plv->val[ i ])) );
417 #endif /* DEBUG_MODE */
420 /*****************************************************************************
421 * Function: xsvfInfoInit
422 * Description: Initialize the xsvfInfo data.
423 * Parameters: pXsvfInfo - ptr to the XSVF info structure.
424 * Returns: int - 0 = success; otherwise error.
425 *****************************************************************************/
426 int xsvfInfoInit( SXsvfInfo* pXsvfInfo )
428 XSVFDBG_PRINTF1( 4, " sizeof( SXsvfInfo ) = %d bytes\n",
429 sizeof( SXsvfInfo ) );
431 pXsvfInfo->ucComplete = 0;
432 pXsvfInfo->ucCommand = XCOMPLETE;
433 pXsvfInfo->lCommandCount = 0;
434 pXsvfInfo->iErrorCode = XSVF_ERROR_NONE;
435 pXsvfInfo->ucMaxRepeat = 0;
436 pXsvfInfo->ucTapState = XTAPSTATE_RESET;
437 pXsvfInfo->ucEndIR = XTAPSTATE_RUNTEST;
438 pXsvfInfo->ucEndDR = XTAPSTATE_RUNTEST;
439 pXsvfInfo->lShiftLengthBits = 0L;
440 pXsvfInfo->sShiftLengthBytes= 0;
441 pXsvfInfo->lRunTestTime = 0L;
446 /*****************************************************************************
447 * Function: xsvfInfoCleanup
448 * Description: Cleanup the xsvfInfo data.
449 * Parameters: pXsvfInfo - ptr to the XSVF info structure.
451 *****************************************************************************/
452 void xsvfInfoCleanup( SXsvfInfo* pXsvfInfo )
456 /*****************************************************************************
457 * Function: xsvfGetAsNumBytes
458 * Description: Calculate the number of bytes the given number of bits
460 * Parameters: lNumBits - the number of bits.
461 * Returns: short - the number of bytes to store the number of bits.
462 *****************************************************************************/
463 short xsvfGetAsNumBytes( long lNumBits )
465 return( (short)( ( lNumBits + 7L ) / 8L ) );
468 /*****************************************************************************
469 * Function: xsvfTmsTransition
470 * Description: Apply TMS and transition TAP controller by applying one TCK
472 * Parameters: sTms - new TMS value.
474 *****************************************************************************/
475 void xsvfTmsTransition( short sTms )
477 setPort( TMS, sTms );
482 /*****************************************************************************
483 * Function: xsvfGotoTapState
484 * Description: From the current TAP state, go to the named TAP state.
485 * A target state of RESET ALWAYS causes TMS reset sequence.
486 * All SVF standard stable state paths are supported.
487 * All state transitions are supported except for the following
488 * which cause an XSVF_ERROR_ILLEGALSTATE:
489 * - Target==DREXIT2; Start!=DRPAUSE
490 * - Target==IREXIT2; Start!=IRPAUSE
491 * Parameters: pucTapState - Current TAP state; returns final TAP state.
492 * ucTargetState - New target TAP state.
493 * Returns: int - 0 = success; otherwise error.
494 *****************************************************************************/
495 int xsvfGotoTapState( unsigned char* pucTapState,
496 unsigned char ucTargetState )
501 iErrorCode = XSVF_ERROR_NONE;
502 if ( ucTargetState == XTAPSTATE_RESET )
504 /* If RESET, always perform TMS reset sequence to reset/sync TAPs */
505 xsvfTmsTransition( 1 );
506 for ( i = 0; i < 5; ++i )
511 *pucTapState = XTAPSTATE_RESET;
512 XSVFDBG_PRINTF( 3, " TMS Reset Sequence -> Test-Logic-Reset\n" );
513 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
514 xsvf_pzTapState[ *pucTapState ] );
515 } else if ( ( ucTargetState != *pucTapState ) &&
516 ( ( ( ucTargetState == XTAPSTATE_EXIT2DR ) && ( *pucTapState != XTAPSTATE_PAUSEDR ) ) ||
517 ( ( ucTargetState == XTAPSTATE_EXIT2IR ) && ( *pucTapState != XTAPSTATE_PAUSEIR ) ) ) )
519 /* Trap illegal TAP state path specification */
520 iErrorCode = XSVF_ERROR_ILLEGALSTATE;
522 if ( ucTargetState == *pucTapState )
524 /* Already in target state. Do nothing except when in DRPAUSE
525 or in IRPAUSE to comply with SVF standard */
526 if ( ucTargetState == XTAPSTATE_PAUSEDR )
528 xsvfTmsTransition( 1 );
529 *pucTapState = XTAPSTATE_EXIT2DR;
530 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
531 xsvf_pzTapState[ *pucTapState ] );
533 else if ( ucTargetState == XTAPSTATE_PAUSEIR )
535 xsvfTmsTransition( 1 );
536 *pucTapState = XTAPSTATE_EXIT2IR;
537 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
538 xsvf_pzTapState[ *pucTapState ] );
542 /* Perform TAP state transitions to get to the target state */
543 while ( ucTargetState != *pucTapState )
545 switch ( *pucTapState )
547 case XTAPSTATE_RESET:
548 xsvfTmsTransition( 0 );
549 *pucTapState = XTAPSTATE_RUNTEST;
551 case XTAPSTATE_RUNTEST:
552 xsvfTmsTransition( 1 );
553 *pucTapState = XTAPSTATE_SELECTDR;
555 case XTAPSTATE_SELECTDR:
556 if ( ucTargetState >= XTAPSTATE_IRSTATES )
558 xsvfTmsTransition( 1 );
559 *pucTapState = XTAPSTATE_SELECTIR;
563 xsvfTmsTransition( 0 );
564 *pucTapState = XTAPSTATE_CAPTUREDR;
567 case XTAPSTATE_CAPTUREDR:
568 if ( ucTargetState == XTAPSTATE_SHIFTDR )
570 xsvfTmsTransition( 0 );
571 *pucTapState = XTAPSTATE_SHIFTDR;
575 xsvfTmsTransition( 1 );
576 *pucTapState = XTAPSTATE_EXIT1DR;
579 case XTAPSTATE_SHIFTDR:
580 xsvfTmsTransition( 1 );
581 *pucTapState = XTAPSTATE_EXIT1DR;
583 case XTAPSTATE_EXIT1DR:
584 if ( ucTargetState == XTAPSTATE_PAUSEDR )
586 xsvfTmsTransition( 0 );
587 *pucTapState = XTAPSTATE_PAUSEDR;
591 xsvfTmsTransition( 1 );
592 *pucTapState = XTAPSTATE_UPDATEDR;
595 case XTAPSTATE_PAUSEDR:
596 xsvfTmsTransition( 1 );
597 *pucTapState = XTAPSTATE_EXIT2DR;
599 case XTAPSTATE_EXIT2DR:
600 if ( ucTargetState == XTAPSTATE_SHIFTDR )
602 xsvfTmsTransition( 0 );
603 *pucTapState = XTAPSTATE_SHIFTDR;
607 xsvfTmsTransition( 1 );
608 *pucTapState = XTAPSTATE_UPDATEDR;
611 case XTAPSTATE_UPDATEDR:
612 if ( ucTargetState == XTAPSTATE_RUNTEST )
614 xsvfTmsTransition( 0 );
615 *pucTapState = XTAPSTATE_RUNTEST;
619 xsvfTmsTransition( 1 );
620 *pucTapState = XTAPSTATE_SELECTDR;
623 case XTAPSTATE_SELECTIR:
624 xsvfTmsTransition( 0 );
625 *pucTapState = XTAPSTATE_CAPTUREIR;
627 case XTAPSTATE_CAPTUREIR:
628 if ( ucTargetState == XTAPSTATE_SHIFTIR )
630 xsvfTmsTransition( 0 );
631 *pucTapState = XTAPSTATE_SHIFTIR;
635 xsvfTmsTransition( 1 );
636 *pucTapState = XTAPSTATE_EXIT1IR;
639 case XTAPSTATE_SHIFTIR:
640 xsvfTmsTransition( 1 );
641 *pucTapState = XTAPSTATE_EXIT1IR;
643 case XTAPSTATE_EXIT1IR:
644 if ( ucTargetState == XTAPSTATE_PAUSEIR )
646 xsvfTmsTransition( 0 );
647 *pucTapState = XTAPSTATE_PAUSEIR;
651 xsvfTmsTransition( 1 );
652 *pucTapState = XTAPSTATE_UPDATEIR;
655 case XTAPSTATE_PAUSEIR:
656 xsvfTmsTransition( 1 );
657 *pucTapState = XTAPSTATE_EXIT2IR;
659 case XTAPSTATE_EXIT2IR:
660 if ( ucTargetState == XTAPSTATE_SHIFTIR )
662 xsvfTmsTransition( 0 );
663 *pucTapState = XTAPSTATE_SHIFTIR;
667 xsvfTmsTransition( 1 );
668 *pucTapState = XTAPSTATE_UPDATEIR;
671 case XTAPSTATE_UPDATEIR:
672 if ( ucTargetState == XTAPSTATE_RUNTEST )
674 xsvfTmsTransition( 0 );
675 *pucTapState = XTAPSTATE_RUNTEST;
679 xsvfTmsTransition( 1 );
680 *pucTapState = XTAPSTATE_SELECTDR;
684 iErrorCode = XSVF_ERROR_ILLEGALSTATE;
685 *pucTapState = ucTargetState; /* Exit while loop */
688 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
689 xsvf_pzTapState[ *pucTapState ] );
693 return( iErrorCode );
696 /*****************************************************************************
697 * Function: xsvfShiftOnly
698 * Description: Assumes that starting TAP state is SHIFT-DR or SHIFT-IR.
699 * Shift the given TDI data into the JTAG scan chain.
700 * Optionally, save the TDO data shifted out of the scan chain.
701 * Last shift cycle is special: capture last TDO, set last TDI,
702 * but does not pulse TCK. Caller must pulse TCK and optionally
703 * set TMS=1 to exit shift state.
704 * Parameters: lNumBits - number of bits to shift.
705 * plvTdi - ptr to lenval for TDI data.
706 * plvTdoCaptured - ptr to lenval for storing captured TDO data.
707 * iExitShift - 1=exit at end of shift; 0=stay in Shift-DR.
709 *****************************************************************************/
710 void xsvfShiftOnly( long lNumBits,
712 lenVal* plvTdoCaptured,
715 unsigned char* pucTdi;
716 unsigned char* pucTdo;
717 unsigned char ucTdiByte;
718 unsigned char ucTdoByte;
719 unsigned char ucTdoBit;
722 /* assert( ( ( lNumBits + 7 ) / 8 ) == plvTdi->len ); */
724 /* Initialize TDO storage len == TDI len */
726 if ( plvTdoCaptured )
728 plvTdoCaptured->len = plvTdi->len;
729 pucTdo = plvTdoCaptured->val + plvTdi->len;
732 /* Shift LSB first. val[N-1] == LSB. val[0] == MSB. */
733 pucTdi = plvTdi->val + plvTdi->len;
736 /* Process on a byte-basis */
737 ucTdiByte = (*(--pucTdi));
739 for ( i = 0; ( lNumBits && ( i < 8 ) ); ++i )
742 if ( iExitShift && !lNumBits )
744 /* Exit Shift-DR state */
748 /* Set the new TDI value */
749 setPort( TDI, (short)(ucTdiByte & 1) );
757 /* Save the TDO value */
758 ucTdoBit = readTDOBit();
759 ucTdoByte |= ( ucTdoBit << i );
766 /* Save the TDO byte value */
769 (*(--pucTdo)) = ucTdoByte;
774 /*****************************************************************************
775 * Function: xsvfShift
776 * Description: Goes to the given starting TAP state.
777 * Calls xsvfShiftOnly to shift in the given TDI data and
778 * optionally capture the TDO data.
779 * Compares the TDO captured data against the TDO expected
781 * If a data mismatch occurs, then executes the exception
782 * handling loop upto ucMaxRepeat times.
783 * Parameters: pucTapState - Ptr to current TAP state.
784 * ucStartState - Starting shift state: Shift-DR or Shift-IR.
785 * lNumBits - number of bits to shift.
786 * plvTdi - ptr to lenval for TDI data.
787 * plvTdoCaptured - ptr to lenval for storing TDO data.
788 * plvTdoExpected - ptr to expected TDO data.
789 * plvTdoMask - ptr to TDO mask.
790 * ucEndState - state in which to end the shift.
791 * lRunTestTime - amount of time to wait after the shift.
792 * ucMaxRepeat - Maximum number of retries on TDO mismatch.
793 * Returns: int - 0 = success; otherwise TDO mismatch.
794 * Notes: XC9500XL-only Optimization:
795 * Skip the waitTime() if plvTdoMask->val[0:plvTdoMask->len-1]
796 * is NOT all zeros and sMatch==1.
797 *****************************************************************************/
798 int xsvfShift( unsigned char* pucTapState,
799 unsigned char ucStartState,
802 lenVal* plvTdoCaptured,
803 lenVal* plvTdoExpected,
805 unsigned char ucEndState,
807 unsigned char ucMaxRepeat )
811 unsigned char ucRepeat;
814 iErrorCode = XSVF_ERROR_NONE;
817 iExitShift = ( ucStartState != ucEndState );
819 XSVFDBG_PRINTF1( 3, " Shift Length = %ld\n", lNumBits );
820 XSVFDBG_PRINTF( 4, " TDI = ");
821 XSVFDBG_PRINTLENVAL( 4, plvTdi );
822 XSVFDBG_PRINTF( 4, "\n");
823 XSVFDBG_PRINTF( 4, " TDO Expected = ");
824 XSVFDBG_PRINTLENVAL( 4, plvTdoExpected );
825 XSVFDBG_PRINTF( 4, "\n");
829 /* Compatibility with XSVF2.00: XSDR 0 = no shift, but wait in RTI */
832 /* Wait for prespecified XRUNTEST time */
833 xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
834 XSVFDBG_PRINTF1( 3, " Wait = %ld usec\n", lRunTestTime );
835 waitTime( lRunTestTime );
842 /* Goto Shift-DR or Shift-IR */
843 xsvfGotoTapState( pucTapState, ucStartState );
845 /* Shift TDI and capture TDO */
846 xsvfShiftOnly( lNumBits, plvTdi, plvTdoCaptured, iExitShift );
848 if ( plvTdoExpected )
850 /* Compare TDO data to expected TDO data */
851 iMismatch = !EqualLenVal( plvTdoExpected,
858 /* Update TAP state: Shift->Exit */
860 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
861 xsvf_pzTapState[ *pucTapState ] );
863 if ( iMismatch && lRunTestTime && ( ucRepeat < ucMaxRepeat ) )
865 XSVFDBG_PRINTF( 4, " TDO Expected = ");
866 XSVFDBG_PRINTLENVAL( 4, plvTdoExpected );
867 XSVFDBG_PRINTF( 4, "\n");
868 XSVFDBG_PRINTF( 4, " TDO Captured = ");
869 XSVFDBG_PRINTLENVAL( 4, plvTdoCaptured );
870 XSVFDBG_PRINTF( 4, "\n");
871 XSVFDBG_PRINTF( 4, " TDO Mask = ");
872 XSVFDBG_PRINTLENVAL( 4, plvTdoMask );
873 XSVFDBG_PRINTF( 4, "\n");
874 XSVFDBG_PRINTF1( 3, " Retry #%d\n", ( ucRepeat + 1 ) );
875 /* Do exception handling retry - ShiftDR only */
876 xsvfGotoTapState( pucTapState, XTAPSTATE_PAUSEDR );
877 /* Shift 1 extra bit */
878 xsvfGotoTapState( pucTapState, XTAPSTATE_SHIFTDR );
879 /* Increment RUNTEST time by an additional 25% */
880 lRunTestTime += ( lRunTestTime >> 2 );
884 /* Do normal exit from Shift-XR */
885 xsvfGotoTapState( pucTapState, ucEndState );
890 /* Wait for prespecified XRUNTEST time */
891 xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
892 XSVFDBG_PRINTF1( 3, " Wait = %ld usec\n", lRunTestTime );
893 waitTime( lRunTestTime );
896 } while ( iMismatch && ( ucRepeat++ < ucMaxRepeat ) );
901 XSVFDBG_PRINTF( 1, " TDO Expected = ");
902 XSVFDBG_PRINTLENVAL( 1, plvTdoExpected );
903 XSVFDBG_PRINTF( 1, "\n");
904 XSVFDBG_PRINTF( 1, " TDO Captured = ");
905 XSVFDBG_PRINTLENVAL( 1, plvTdoCaptured );
906 XSVFDBG_PRINTF( 1, "\n");
907 XSVFDBG_PRINTF( 1, " TDO Mask = ");
908 XSVFDBG_PRINTLENVAL( 1, plvTdoMask );
909 XSVFDBG_PRINTF( 1, "\n");
910 if ( ucMaxRepeat && ( ucRepeat > ucMaxRepeat ) )
912 iErrorCode = XSVF_ERROR_MAXRETRIES;
916 iErrorCode = XSVF_ERROR_TDOMISMATCH;
920 return( iErrorCode );
923 /*****************************************************************************
924 * Function: xsvfBasicXSDRTDO
925 * Description: Get the XSDRTDO parameters and execute the XSDRTDO command.
926 * This is the common function for all XSDRTDO commands.
927 * Parameters: pucTapState - Current TAP state.
928 * lShiftLengthBits - number of bits to shift.
929 * sShiftLengthBytes - number of bytes to read.
930 * plvTdi - ptr to lenval for TDI data.
931 * lvTdoCaptured - ptr to lenval for storing TDO data.
932 * iEndState - state in which to end the shift.
933 * lRunTestTime - amount of time to wait after the shift.
934 * ucMaxRepeat - maximum xc9500/xl retries.
935 * Returns: int - 0 = success; otherwise TDO mismatch.
936 *****************************************************************************/
937 int xsvfBasicXSDRTDO( unsigned char* pucTapState,
938 long lShiftLengthBits,
939 short sShiftLengthBytes,
941 lenVal* plvTdoCaptured,
942 lenVal* plvTdoExpected,
944 unsigned char ucEndState,
946 unsigned char ucMaxRepeat )
948 readVal( plvTdi, sShiftLengthBytes );
949 if ( plvTdoExpected )
951 readVal( plvTdoExpected, sShiftLengthBytes );
953 return( xsvfShift( pucTapState, XTAPSTATE_SHIFTDR, lShiftLengthBits,
954 plvTdi, plvTdoCaptured, plvTdoExpected, plvTdoMask,
955 ucEndState, lRunTestTime, ucMaxRepeat ) );
958 /*****************************************************************************
959 * Function: xsvfDoSDRMasking
960 * Description: Update the data value with the next XSDRINC data and address.
961 * Example: dataVal=0x01ff, nextData=0xab, addressMask=0x0100,
962 * dataMask=0x00ff, should set dataVal to 0x02ab
963 * Parameters: plvTdi - The current TDI value.
964 * plvNextData - the next data value.
965 * plvAddressMask - the address mask.
966 * plvDataMask - the data mask.
968 *****************************************************************************/
969 #ifdef XSVF_SUPPORT_COMPRESSION
970 void xsvfDoSDRMasking( lenVal* plvTdi,
972 lenVal* plvAddressMask,
973 lenVal* plvDataMask )
977 unsigned char ucTdiMask;
978 unsigned char ucDataMask;
979 unsigned char ucNextData;
980 unsigned char ucNextMask;
983 /* add the address Mask to dataVal and return as a new dataVal */
984 addVal( plvTdi, plvTdi, plvAddressMask );
988 sNextData = plvNextData->len;
989 for ( i = plvDataMask->len - 1; i >= 0; --i )
991 /* Go through data mask in reverse order looking for mask (1) bits */
992 ucDataMask = plvDataMask->val[ i ];
995 /* Retrieve the corresponding TDI byte value */
996 ucTdi = plvTdi->val[ i ];
998 /* For each bit in the data mask byte, look for 1's */
1000 while ( ucDataMask )
1002 if ( ucDataMask & 1 )
1006 /* Get the next data byte */
1007 ucNextData = plvNextData->val[ --sNextData ];
1011 /* Set or clear the data bit according to the next data */
1012 if ( ucNextData & ucNextMask )
1014 ucTdi |= ucTdiMask; /* Set bit */
1018 ucTdi &= ( ~ucTdiMask ); /* Clear bit */
1021 /* Update the next data */
1028 /* Update the TDI value */
1029 plvTdi->val[ i ] = ucTdi;
1033 #endif /* XSVF_SUPPORT_COMPRESSION */
1035 /*============================================================================
1036 * XSVF Command Functions (type = TXsvfDoCmdFuncPtr)
1037 * These functions update pXsvfInfo->iErrorCode only on an error.
1038 * Otherwise, the error code is left alone.
1039 * The function returns the error code from the function.
1040 ============================================================================*/
1042 /*****************************************************************************
1043 * Function: xsvfDoIllegalCmd
1044 * Description: Function place holder for illegal/unsupported commands.
1045 * Parameters: pXsvfInfo - XSVF information pointer.
1046 * Returns: int - 0 = success; non-zero = error.
1047 *****************************************************************************/
1048 int xsvfDoIllegalCmd( SXsvfInfo* pXsvfInfo )
1050 XSVFDBG_PRINTF2( 0, "ERROR: Encountered unsupported command #%d (%s)\n",
1051 ((unsigned int)(pXsvfInfo->ucCommand)),
1052 ((pXsvfInfo->ucCommand < XLASTCMD)
1053 ? (xsvf_pzCommandName[pXsvfInfo->ucCommand])
1055 pXsvfInfo->iErrorCode = XSVF_ERROR_ILLEGALCMD;
1056 return( pXsvfInfo->iErrorCode );
1059 /*****************************************************************************
1060 * Function: xsvfDoXCOMPLETE
1061 * Description: XCOMPLETE (no parameters)
1062 * Update complete status for XSVF player.
1063 * Parameters: pXsvfInfo - XSVF information pointer.
1064 * Returns: int - 0 = success; non-zero = error.
1065 *****************************************************************************/
1066 int xsvfDoXCOMPLETE( SXsvfInfo* pXsvfInfo )
1068 pXsvfInfo->ucComplete = 1;
1069 return( XSVF_ERROR_NONE );
1072 /*****************************************************************************
1073 * Function: xsvfDoXTDOMASK
1074 * Description: XTDOMASK <lenVal.TdoMask[XSDRSIZE]>
1075 * Prespecify the TDO compare mask.
1076 * Parameters: pXsvfInfo - XSVF information pointer.
1077 * Returns: int - 0 = success; non-zero = error.
1078 *****************************************************************************/
1079 int xsvfDoXTDOMASK( SXsvfInfo* pXsvfInfo )
1081 readVal( &(pXsvfInfo->lvTdoMask), pXsvfInfo->sShiftLengthBytes );
1082 XSVFDBG_PRINTF( 4, " TDO Mask = ");
1083 XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvTdoMask) );
1084 XSVFDBG_PRINTF( 4, "\n");
1085 return( XSVF_ERROR_NONE );
1088 /*****************************************************************************
1089 * Function: xsvfDoXSIR
1090 * Description: XSIR <(byte)shiftlen> <lenVal.TDI[shiftlen]>
1091 * Get the instruction and shift the instruction into the TAP.
1092 * If prespecified XRUNTEST!=0, goto RUNTEST and wait after
1093 * the shift for XRUNTEST usec.
1094 * Parameters: pXsvfInfo - XSVF information pointer.
1095 * Returns: int - 0 = success; non-zero = error.
1096 *****************************************************************************/
1097 int xsvfDoXSIR( SXsvfInfo* pXsvfInfo )
1099 unsigned char ucShiftIrBits;
1100 short sShiftIrBytes;
1103 /* Get the shift length and store */
1104 readByte( &ucShiftIrBits );
1105 sShiftIrBytes = xsvfGetAsNumBytes( ucShiftIrBits );
1106 XSVFDBG_PRINTF1( 3, " XSIR length = %d\n",
1107 ((unsigned int)ucShiftIrBits) );
1109 if ( sShiftIrBytes > MAX_LEN )
1111 iErrorCode = XSVF_ERROR_DATAOVERFLOW;
1115 /* Get and store instruction to shift in */
1116 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( ucShiftIrBits ) );
1118 /* Shift the data */
1119 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTIR,
1120 ucShiftIrBits, &(pXsvfInfo->lvTdi),
1121 /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1122 /*plvTdoMask*/0, pXsvfInfo->ucEndIR,
1123 pXsvfInfo->lRunTestTime, /*ucMaxRepeat*/0 );
1126 if ( iErrorCode != XSVF_ERROR_NONE )
1128 pXsvfInfo->iErrorCode = iErrorCode;
1130 return( iErrorCode );
1133 /*****************************************************************************
1134 * Function: xsvfDoXSIR2
1135 * Description: XSIR <(2-byte)shiftlen> <lenVal.TDI[shiftlen]>
1136 * Get the instruction and shift the instruction into the TAP.
1137 * If prespecified XRUNTEST!=0, goto RUNTEST and wait after
1138 * the shift for XRUNTEST usec.
1139 * Parameters: pXsvfInfo - XSVF information pointer.
1140 * Returns: int - 0 = success; non-zero = error.
1141 *****************************************************************************/
1142 int xsvfDoXSIR2( SXsvfInfo* pXsvfInfo )
1145 short sShiftIrBytes;
1148 /* Get the shift length and store */
1149 readVal( &(pXsvfInfo->lvTdi), 2 );
1150 lShiftIrBits = value( &(pXsvfInfo->lvTdi) );
1151 sShiftIrBytes = xsvfGetAsNumBytes( lShiftIrBits );
1152 XSVFDBG_PRINTF1( 3, " XSIR2 length = %d\n", (int)lShiftIrBits);
1154 if ( sShiftIrBytes > MAX_LEN )
1156 iErrorCode = XSVF_ERROR_DATAOVERFLOW;
1160 /* Get and store instruction to shift in */
1161 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( lShiftIrBits ) );
1163 /* Shift the data */
1164 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTIR,
1165 lShiftIrBits, &(pXsvfInfo->lvTdi),
1166 /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1167 /*plvTdoMask*/0, pXsvfInfo->ucEndIR,
1168 pXsvfInfo->lRunTestTime, /*ucMaxRepeat*/0 );
1171 if ( iErrorCode != XSVF_ERROR_NONE )
1173 pXsvfInfo->iErrorCode = iErrorCode;
1175 return( iErrorCode );
1178 /*****************************************************************************
1179 * Function: xsvfDoXSDR
1180 * Description: XSDR <lenVal.TDI[XSDRSIZE]>
1181 * Shift the given TDI data into the JTAG scan chain.
1182 * Compare the captured TDO with the expected TDO from the
1183 * previous XSDRTDO command using the previously specified
1185 * Parameters: pXsvfInfo - XSVF information pointer.
1186 * Returns: int - 0 = success; non-zero = error.
1187 *****************************************************************************/
1188 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo )
1191 readVal( &(pXsvfInfo->lvTdi), pXsvfInfo->sShiftLengthBytes );
1192 /* use TDOExpected from last XSDRTDO instruction */
1193 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTDR,
1194 pXsvfInfo->lShiftLengthBits, &(pXsvfInfo->lvTdi),
1195 &(pXsvfInfo->lvTdoCaptured),
1196 &(pXsvfInfo->lvTdoExpected),
1197 &(pXsvfInfo->lvTdoMask), pXsvfInfo->ucEndDR,
1198 pXsvfInfo->lRunTestTime, pXsvfInfo->ucMaxRepeat );
1199 if ( iErrorCode != XSVF_ERROR_NONE )
1201 pXsvfInfo->iErrorCode = iErrorCode;
1203 return( iErrorCode );
1206 /*****************************************************************************
1207 * Function: xsvfDoXRUNTEST
1208 * Description: XRUNTEST <uint32>
1209 * Prespecify the XRUNTEST wait time for shift operations.
1210 * Parameters: pXsvfInfo - XSVF information pointer.
1211 * Returns: int - 0 = success; non-zero = error.
1212 *****************************************************************************/
1213 int xsvfDoXRUNTEST( SXsvfInfo* pXsvfInfo )
1215 readVal( &(pXsvfInfo->lvTdi), 4 );
1216 pXsvfInfo->lRunTestTime = value( &(pXsvfInfo->lvTdi) );
1217 XSVFDBG_PRINTF1( 3, " XRUNTEST = %ld\n", pXsvfInfo->lRunTestTime );
1218 return( XSVF_ERROR_NONE );
1221 /*****************************************************************************
1222 * Function: xsvfDoXREPEAT
1223 * Description: XREPEAT <byte>
1224 * Prespecify the maximum number of XC9500/XL retries.
1225 * Parameters: pXsvfInfo - XSVF information pointer.
1226 * Returns: int - 0 = success; non-zero = error.
1227 *****************************************************************************/
1228 int xsvfDoXREPEAT( SXsvfInfo* pXsvfInfo )
1230 readByte( &(pXsvfInfo->ucMaxRepeat) );
1231 XSVFDBG_PRINTF1( 3, " XREPEAT = %d\n",
1232 ((unsigned int)(pXsvfInfo->ucMaxRepeat)) );
1233 return( XSVF_ERROR_NONE );
1236 /*****************************************************************************
1237 * Function: xsvfDoXSDRSIZE
1238 * Description: XSDRSIZE <uint32>
1239 * Prespecify the XRUNTEST wait time for shift operations.
1240 * Parameters: pXsvfInfo - XSVF information pointer.
1241 * Returns: int - 0 = success; non-zero = error.
1242 *****************************************************************************/
1243 int xsvfDoXSDRSIZE( SXsvfInfo* pXsvfInfo )
1246 iErrorCode = XSVF_ERROR_NONE;
1247 readVal( &(pXsvfInfo->lvTdi), 4 );
1248 pXsvfInfo->lShiftLengthBits = value( &(pXsvfInfo->lvTdi) );
1249 pXsvfInfo->sShiftLengthBytes= xsvfGetAsNumBytes( pXsvfInfo->lShiftLengthBits );
1250 XSVFDBG_PRINTF1( 3, " XSDRSIZE = %ld\n", pXsvfInfo->lShiftLengthBits );
1251 if ( pXsvfInfo->sShiftLengthBytes > MAX_LEN )
1253 iErrorCode = XSVF_ERROR_DATAOVERFLOW;
1254 pXsvfInfo->iErrorCode = iErrorCode;
1256 return( iErrorCode );
1259 /*****************************************************************************
1260 * Function: xsvfDoXSDRTDO
1261 * Description: XSDRTDO <lenVal.TDI[XSDRSIZE]> <lenVal.TDO[XSDRSIZE]>
1262 * Get the TDI and expected TDO values. Then, shift.
1263 * Compare the expected TDO with the captured TDO using the
1264 * prespecified XTDOMASK.
1265 * Parameters: pXsvfInfo - XSVF information pointer.
1266 * Returns: int - 0 = success; non-zero = error.
1267 *****************************************************************************/
1268 int xsvfDoXSDRTDO( SXsvfInfo* pXsvfInfo )
1271 iErrorCode = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1272 pXsvfInfo->lShiftLengthBits,
1273 pXsvfInfo->sShiftLengthBytes,
1274 &(pXsvfInfo->lvTdi),
1275 &(pXsvfInfo->lvTdoCaptured),
1276 &(pXsvfInfo->lvTdoExpected),
1277 &(pXsvfInfo->lvTdoMask),
1279 pXsvfInfo->lRunTestTime,
1280 pXsvfInfo->ucMaxRepeat );
1281 if ( iErrorCode != XSVF_ERROR_NONE )
1283 pXsvfInfo->iErrorCode = iErrorCode;
1285 return( iErrorCode );
1288 /*****************************************************************************
1289 * Function: xsvfDoXSETSDRMASKS
1290 * Description: XSETSDRMASKS <lenVal.AddressMask[XSDRSIZE]>
1291 * <lenVal.DataMask[XSDRSIZE]>
1292 * Get the prespecified address and data mask for the XSDRINC
1294 * Used for xc9500/xl compressed XSVF data.
1295 * Parameters: pXsvfInfo - XSVF information pointer.
1296 * Returns: int - 0 = success; non-zero = error.
1297 *****************************************************************************/
1298 #ifdef XSVF_SUPPORT_COMPRESSION
1299 int xsvfDoXSETSDRMASKS( SXsvfInfo* pXsvfInfo )
1301 /* read the addressMask */
1302 readVal( &(pXsvfInfo->lvAddressMask), pXsvfInfo->sShiftLengthBytes );
1303 /* read the dataMask */
1304 readVal( &(pXsvfInfo->lvDataMask), pXsvfInfo->sShiftLengthBytes );
1306 XSVFDBG_PRINTF( 4, " Address Mask = " );
1307 XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvAddressMask) );
1308 XSVFDBG_PRINTF( 4, "\n" );
1309 XSVFDBG_PRINTF( 4, " Data Mask = " );
1310 XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvDataMask) );
1311 XSVFDBG_PRINTF( 4, "\n" );
1313 return( XSVF_ERROR_NONE );
1315 #endif /* XSVF_SUPPORT_COMPRESSION */
1317 /*****************************************************************************
1318 * Function: xsvfDoXSDRINC
1319 * Description: XSDRINC <lenVal.firstTDI[XSDRSIZE]> <byte(numTimes)>
1320 * <lenVal.data[XSETSDRMASKS.dataMask.len]> ...
1321 * Get the XSDRINC parameters and execute the XSDRINC command.
1322 * XSDRINC starts by loading the first TDI shift value.
1323 * Then, for numTimes, XSDRINC gets the next piece of data,
1324 * replaces the bits from the starting TDI as defined by the
1325 * XSETSDRMASKS.dataMask, adds the address mask from
1326 * XSETSDRMASKS.addressMask, shifts the new TDI value,
1327 * and compares the TDO to the expected TDO from the previous
1328 * XSDRTDO command using the XTDOMASK.
1329 * Used for xc9500/xl compressed XSVF data.
1330 * Parameters: pXsvfInfo - XSVF information pointer.
1331 * Returns: int - 0 = success; non-zero = error.
1332 *****************************************************************************/
1333 #ifdef XSVF_SUPPORT_COMPRESSION
1334 int xsvfDoXSDRINC( SXsvfInfo* pXsvfInfo )
1338 unsigned char ucDataMask;
1339 unsigned char ucNumTimes;
1342 readVal( &(pXsvfInfo->lvTdi), pXsvfInfo->sShiftLengthBytes );
1343 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTDR,
1344 pXsvfInfo->lShiftLengthBits,
1345 &(pXsvfInfo->lvTdi), &(pXsvfInfo->lvTdoCaptured),
1346 &(pXsvfInfo->lvTdoExpected),
1347 &(pXsvfInfo->lvTdoMask), pXsvfInfo->ucEndDR,
1348 pXsvfInfo->lRunTestTime, pXsvfInfo->ucMaxRepeat );
1351 /* Calculate number of data mask bits */
1353 for ( i = 0; i < pXsvfInfo->lvDataMask.len; ++i )
1355 ucDataMask = pXsvfInfo->lvDataMask.val[ i ];
1356 while ( ucDataMask )
1358 iDataMaskLen += ( ucDataMask & 1 );
1363 /* Get the number of data pieces, i.e. number of times to shift */
1364 readByte( &ucNumTimes );
1366 /* For numTimes, get data, fix TDI, and shift */
1367 for ( i = 0; !iErrorCode && ( i < ucNumTimes ); ++i )
1369 readVal( &(pXsvfInfo->lvNextData),
1370 xsvfGetAsNumBytes( iDataMaskLen ) );
1371 xsvfDoSDRMasking( &(pXsvfInfo->lvTdi),
1372 &(pXsvfInfo->lvNextData),
1373 &(pXsvfInfo->lvAddressMask),
1374 &(pXsvfInfo->lvDataMask) );
1375 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState),
1377 pXsvfInfo->lShiftLengthBits,
1378 &(pXsvfInfo->lvTdi),
1379 &(pXsvfInfo->lvTdoCaptured),
1380 &(pXsvfInfo->lvTdoExpected),
1381 &(pXsvfInfo->lvTdoMask),
1383 pXsvfInfo->lRunTestTime,
1384 pXsvfInfo->ucMaxRepeat );
1387 if ( iErrorCode != XSVF_ERROR_NONE )
1389 pXsvfInfo->iErrorCode = iErrorCode;
1391 return( iErrorCode );
1393 #endif /* XSVF_SUPPORT_COMPRESSION */
1395 /*****************************************************************************
1396 * Function: xsvfDoXSDRBCE
1397 * Description: XSDRB/XSDRC/XSDRE <lenVal.TDI[XSDRSIZE]>
1398 * If not already in SHIFTDR, goto SHIFTDR.
1399 * Shift the given TDI data into the JTAG scan chain.
1401 * If cmd==XSDRE, then goto ENDDR. Otherwise, stay in ShiftDR.
1402 * XSDRB, XSDRC, and XSDRE are the same implementation.
1403 * Parameters: pXsvfInfo - XSVF information pointer.
1404 * Returns: int - 0 = success; non-zero = error.
1405 *****************************************************************************/
1406 int xsvfDoXSDRBCE( SXsvfInfo* pXsvfInfo )
1408 unsigned char ucEndDR;
1410 ucEndDR = (unsigned char)(( pXsvfInfo->ucCommand == XSDRE ) ?
1411 pXsvfInfo->ucEndDR : XTAPSTATE_SHIFTDR);
1412 iErrorCode = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1413 pXsvfInfo->lShiftLengthBits,
1414 pXsvfInfo->sShiftLengthBytes,
1415 &(pXsvfInfo->lvTdi),
1416 /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1417 /*plvTdoMask*/0, ucEndDR,
1418 /*lRunTestTime*/0, /*ucMaxRepeat*/0 );
1419 if ( iErrorCode != XSVF_ERROR_NONE )
1421 pXsvfInfo->iErrorCode = iErrorCode;
1423 return( iErrorCode );
1426 /*****************************************************************************
1427 * Function: xsvfDoXSDRTDOBCE
1428 * Description: XSDRB/XSDRC/XSDRE <lenVal.TDI[XSDRSIZE]> <lenVal.TDO[XSDRSIZE]>
1429 * If not already in SHIFTDR, goto SHIFTDR.
1430 * Shift the given TDI data into the JTAG scan chain.
1431 * Compare TDO, but do NOT use XTDOMASK.
1432 * If cmd==XSDRTDOE, then goto ENDDR. Otherwise, stay in ShiftDR.
1433 * XSDRTDOB, XSDRTDOC, and XSDRTDOE are the same implementation.
1434 * Parameters: pXsvfInfo - XSVF information pointer.
1435 * Returns: int - 0 = success; non-zero = error.
1436 *****************************************************************************/
1437 int xsvfDoXSDRTDOBCE( SXsvfInfo* pXsvfInfo )
1439 unsigned char ucEndDR;
1441 ucEndDR = (unsigned char)(( pXsvfInfo->ucCommand == XSDRTDOE ) ?
1442 pXsvfInfo->ucEndDR : XTAPSTATE_SHIFTDR);
1443 iErrorCode = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1444 pXsvfInfo->lShiftLengthBits,
1445 pXsvfInfo->sShiftLengthBytes,
1446 &(pXsvfInfo->lvTdi),
1447 &(pXsvfInfo->lvTdoCaptured),
1448 &(pXsvfInfo->lvTdoExpected),
1449 /*plvTdoMask*/0, ucEndDR,
1450 /*lRunTestTime*/0, /*ucMaxRepeat*/0 );
1451 if ( iErrorCode != XSVF_ERROR_NONE )
1453 pXsvfInfo->iErrorCode = iErrorCode;
1455 return( iErrorCode );
1458 /*****************************************************************************
1459 * Function: xsvfDoXSTATE
1460 * Description: XSTATE <byte>
1461 * <byte> == XTAPSTATE;
1462 * Get the state parameter and transition the TAP to that state.
1463 * Parameters: pXsvfInfo - XSVF information pointer.
1464 * Returns: int - 0 = success; non-zero = error.
1465 *****************************************************************************/
1466 int xsvfDoXSTATE( SXsvfInfo* pXsvfInfo )
1468 unsigned char ucNextState;
1470 readByte( &ucNextState );
1471 iErrorCode = xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucNextState );
1472 if ( iErrorCode != XSVF_ERROR_NONE )
1474 pXsvfInfo->iErrorCode = iErrorCode;
1476 return( iErrorCode );
1479 /*****************************************************************************
1480 * Function: xsvfDoXENDXR
1481 * Description: XENDIR/XENDDR <byte>
1482 * <byte>: 0 = RUNTEST; 1 = PAUSE.
1483 * Get the prespecified XENDIR or XENDDR.
1484 * Both XENDIR and XENDDR use the same implementation.
1485 * Parameters: pXsvfInfo - XSVF information pointer.
1486 * Returns: int - 0 = success; non-zero = error.
1487 *****************************************************************************/
1488 int xsvfDoXENDXR( SXsvfInfo* pXsvfInfo )
1491 unsigned char ucEndState;
1493 iErrorCode = XSVF_ERROR_NONE;
1494 readByte( &ucEndState );
1495 if ( ( ucEndState != XENDXR_RUNTEST ) && ( ucEndState != XENDXR_PAUSE ) )
1497 iErrorCode = XSVF_ERROR_ILLEGALSTATE;
1502 if ( pXsvfInfo->ucCommand == XENDIR )
1504 if ( ucEndState == XENDXR_RUNTEST )
1506 pXsvfInfo->ucEndIR = XTAPSTATE_RUNTEST;
1510 pXsvfInfo->ucEndIR = XTAPSTATE_PAUSEIR;
1512 XSVFDBG_PRINTF1( 3, " ENDIR State = %s\n",
1513 xsvf_pzTapState[ pXsvfInfo->ucEndIR ] );
1517 if ( ucEndState == XENDXR_RUNTEST )
1519 pXsvfInfo->ucEndDR = XTAPSTATE_RUNTEST;
1523 pXsvfInfo->ucEndDR = XTAPSTATE_PAUSEDR;
1525 XSVFDBG_PRINTF1( 3, " ENDDR State = %s\n",
1526 xsvf_pzTapState[ pXsvfInfo->ucEndDR ] );
1530 if ( iErrorCode != XSVF_ERROR_NONE )
1532 pXsvfInfo->iErrorCode = iErrorCode;
1534 return( iErrorCode );
1537 /*****************************************************************************
1538 * Function: xsvfDoXCOMMENT
1539 * Description: XCOMMENT <text string ending in \0>
1540 * <text string ending in \0> == text comment;
1541 * Arbitrary comment embedded in the XSVF.
1542 * Parameters: pXsvfInfo - XSVF information pointer.
1543 * Returns: int - 0 = success; non-zero = error.
1544 *****************************************************************************/
1545 int xsvfDoXCOMMENT( SXsvfInfo* pXsvfInfo )
1547 /* Use the comment for debugging */
1548 /* Otherwise, read through the comment to the end '\0' and ignore */
1549 unsigned char ucText;
1551 if ( xsvf_iDebugLevel > 0 )
1558 readByte( &ucText );
1559 if ( xsvf_iDebugLevel > 0 )
1561 putc( ucText ? ucText : '\n' );
1565 pXsvfInfo->iErrorCode = XSVF_ERROR_NONE;
1567 return( pXsvfInfo->iErrorCode );
1570 /*****************************************************************************
1571 * Function: xsvfDoXWAIT
1572 * Description: XWAIT <wait_state> <end_state> <wait_time>
1573 * If not already in <wait_state>, then go to <wait_state>.
1574 * Wait in <wait_state> for <wait_time> microseconds.
1575 * Finally, if not already in <end_state>, then goto <end_state>.
1576 * Parameters: pXsvfInfo - XSVF information pointer.
1577 * Returns: int - 0 = success; non-zero = error.
1578 *****************************************************************************/
1579 int xsvfDoXWAIT( SXsvfInfo* pXsvfInfo )
1581 unsigned char ucWaitState;
1582 unsigned char ucEndState;
1585 /* Get Parameters */
1587 readVal( &(pXsvfInfo->lvTdi), 1 );
1588 ucWaitState = pXsvfInfo->lvTdi.val[0];
1591 readVal( &(pXsvfInfo->lvTdi), 1 );
1592 ucEndState = pXsvfInfo->lvTdi.val[0];
1595 readVal( &(pXsvfInfo->lvTdi), 4 );
1596 lWaitTime = value( &(pXsvfInfo->lvTdi) );
1597 XSVFDBG_PRINTF2( 3, " XWAIT: state = %s; time = %ld\n",
1598 xsvf_pzTapState[ ucWaitState ], lWaitTime );
1600 /* If not already in <wait_state>, go to <wait_state> */
1601 if ( pXsvfInfo->ucTapState != ucWaitState )
1603 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucWaitState );
1606 /* Wait for <wait_time> microseconds */
1607 waitTime( lWaitTime );
1609 /* If not already in <end_state>, go to <end_state> */
1610 if ( pXsvfInfo->ucTapState != ucEndState )
1612 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucEndState );
1615 return( XSVF_ERROR_NONE );
1619 /*============================================================================
1620 * Execution Control Functions
1621 ============================================================================*/
1623 /*****************************************************************************
1624 * Function: xsvfInitialize
1625 * Description: Initialize the xsvf player.
1626 * Call this before running the player to initialize the data
1627 * in the SXsvfInfo struct.
1628 * xsvfCleanup is called to clean up the data in SXsvfInfo
1629 * after the XSVF is played.
1630 * Parameters: pXsvfInfo - ptr to the XSVF information.
1631 * Returns: int - 0 = success; otherwise error.
1632 *****************************************************************************/
1633 int xsvfInitialize( SXsvfInfo* pXsvfInfo )
1635 /* Initialize values */
1636 pXsvfInfo->iErrorCode = xsvfInfoInit( pXsvfInfo );
1638 if ( !pXsvfInfo->iErrorCode )
1640 /* Initialize the TAPs */
1641 pXsvfInfo->iErrorCode = xsvfGotoTapState( &(pXsvfInfo->ucTapState),
1645 return( pXsvfInfo->iErrorCode );
1648 /*****************************************************************************
1650 * Description: Run the xsvf player for a single command and return.
1651 * First, call xsvfInitialize.
1652 * Then, repeatedly call this function until an error is detected
1653 * or until the pXsvfInfo->ucComplete variable is non-zero.
1654 * Finally, call xsvfCleanup to cleanup any remnants.
1655 * Parameters: pXsvfInfo - ptr to the XSVF information.
1656 * Returns: int - 0 = success; otherwise error.
1657 *****************************************************************************/
1658 int xsvfRun( SXsvfInfo* pXsvfInfo )
1660 /* Process the XSVF commands */
1661 if ( (!pXsvfInfo->iErrorCode) && (!pXsvfInfo->ucComplete) )
1663 /* read 1 byte for the instruction */
1664 readByte( &(pXsvfInfo->ucCommand) );
1665 ++(pXsvfInfo->lCommandCount);
1667 if ( pXsvfInfo->ucCommand < XLASTCMD )
1669 /* Execute the command. Func sets error code. */
1670 XSVFDBG_PRINTF1( 2, " %s\n",
1671 xsvf_pzCommandName[pXsvfInfo->ucCommand] );
1672 /* If your compiler cannot take this form,
1673 then convert to a switch statement */
1674 #if 0 /* test-only */
1675 xsvf_pfDoCmd[ pXsvfInfo->ucCommand ]( pXsvfInfo );
1677 switch (pXsvfInfo->ucCommand) {
1679 xsvfDoXCOMPLETE(pXsvfInfo); /* 0 */
1682 xsvfDoXTDOMASK(pXsvfInfo); /* 1 */
1685 xsvfDoXSIR(pXsvfInfo); /* 2 */
1688 xsvfDoXSDR(pXsvfInfo); /* 3 */
1691 xsvfDoXRUNTEST(pXsvfInfo); /* 4 */
1694 xsvfDoIllegalCmd(pXsvfInfo); /* 5 */
1697 xsvfDoIllegalCmd(pXsvfInfo); /* 6 */
1700 xsvfDoXREPEAT(pXsvfInfo); /* 7 */
1703 xsvfDoXSDRSIZE(pXsvfInfo); /* 8 */
1706 xsvfDoXSDRTDO(pXsvfInfo); /* 9 */
1708 #ifdef XSVF_SUPPORT_COMPRESSION
1710 xsvfDoXSETSDRMASKS(pXsvfInfo); /* 10 */
1713 xsvfDoXSDRINC(pXsvfInfo); /* 11 */
1717 xsvfDoIllegalCmd(pXsvfInfo); /* 10 */
1720 xsvfDoIllegalCmd(pXsvfInfo); /* 11 */
1722 #endif /* XSVF_SUPPORT_COMPRESSION */
1724 xsvfDoXSDRBCE(pXsvfInfo); /* 12 */
1727 xsvfDoXSDRBCE(pXsvfInfo); /* 13 */
1730 xsvfDoXSDRBCE(pXsvfInfo); /* 14 */
1733 xsvfDoXSDRTDOBCE(pXsvfInfo); /* 15 */
1736 xsvfDoXSDRTDOBCE(pXsvfInfo); /* 16 */
1739 xsvfDoXSDRTDOBCE(pXsvfInfo); /* 17 */
1742 xsvfDoXSTATE(pXsvfInfo); /* 18 */
1745 xsvfDoXENDXR(pXsvfInfo); /* 19 */
1748 xsvfDoXENDXR(pXsvfInfo); /* 20 */
1751 xsvfDoXSIR2(pXsvfInfo); /* 21 */
1754 xsvfDoXCOMMENT(pXsvfInfo); /* 22 */
1757 xsvfDoXWAIT(pXsvfInfo); /* 23 */
1764 /* Illegal command value. Func sets error code. */
1765 xsvfDoIllegalCmd( pXsvfInfo );
1769 return( pXsvfInfo->iErrorCode );
1772 /*****************************************************************************
1773 * Function: xsvfCleanup
1774 * Description: cleanup remnants of the xsvf player.
1775 * Parameters: pXsvfInfo - ptr to the XSVF information.
1777 *****************************************************************************/
1778 void xsvfCleanup( SXsvfInfo* pXsvfInfo )
1780 xsvfInfoCleanup( pXsvfInfo );
1784 /*============================================================================
1785 * xsvfExecute() - The primary entry point to the XSVF player
1786 ============================================================================*/
1788 /*****************************************************************************
1789 * Function: xsvfExecute
1790 * Description: Process, interpret, and apply the XSVF commands.
1791 * See port.c:readByte for source of XSVF data.
1793 * Returns: int - Legacy result values: 1 == success; 0 == failed.
1794 *****************************************************************************/
1795 int xsvfExecute(void)
1799 xsvfInitialize( &xsvfInfo );
1801 while ( !xsvfInfo.iErrorCode && (!xsvfInfo.ucComplete) )
1803 xsvfRun( &xsvfInfo );
1806 if ( xsvfInfo.iErrorCode )
1808 XSVFDBG_PRINTF1( 0, "%s\n", xsvf_pzErrorName[
1809 ( xsvfInfo.iErrorCode < XSVF_ERROR_LAST )
1810 ? xsvfInfo.iErrorCode : XSVF_ERROR_UNKNOWN ] );
1811 XSVFDBG_PRINTF2( 0, "ERROR at or near XSVF command #%ld. See line #%ld in the XSVF ASCII file.\n",
1812 xsvfInfo.lCommandCount, xsvfInfo.lCommandCount );
1816 XSVFDBG_PRINTF( 0, "SUCCESS - Completed XSVF execution.\n" );
1819 xsvfCleanup( &xsvfInfo );
1821 return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) );
1825 /*****************************************************************************
1827 * Description: main function.
1828 * Specified here for creating stand-alone debug executable.
1829 * Embedded users should call xsvfExecute() directly.
1830 * Parameters: iArgc - number of command-line arguments.
1831 * ppzArgv - array of ptrs to strings (command-line arguments).
1832 * Returns: int - Legacy return value: 1 = success; 0 = error.
1833 *****************************************************************************/
1834 int do_cpld(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1837 char* pzXsvfFileName;
1838 unsigned long duration;
1839 unsigned long long startClock, endClock;
1841 iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE );
1843 xsvf_iDebugLevel = 0;
1845 printf("XSVF Player v%s, Xilinx, Inc.\n", XSVF_VERSION);
1846 printf("XSVF Filesize = %d bytes\n", filesize);
1848 /* Initialize the I/O. SetPort initializes I/O on first call */
1851 /* Execute the XSVF in the file */
1852 startClock = get_ticks();
1853 iErrorCode = xsvfExecute();
1854 endClock = get_ticks();
1855 duration = (unsigned long)(endClock - startClock);
1856 printf("\nExecution Time = %d seconds\n", (int)(duration/get_tbclk()));
1858 return( iErrorCode );
1861 cpld, 1, 1, do_cpld,
1862 "cpld - Program onboard CPLD\n",