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>
69 const unsigned char *xsvfdata;
71 /*============================================================================
73 ============================================================================*/
75 #define XSVF_VERSION "5.00"
77 /*****************************************************************************
78 * Define: XSVF_SUPPORT_COMPRESSION
79 * Description: Define this to support the XC9500/XL XSVF data compression
81 * Code size can be reduced by NOT supporting this feature.
82 * However, you must use the -nc (no compress) option when
83 * translating SVF to XSVF using the SVF2XSVF translator.
84 * Corresponding, uncompressed XSVF may be larger.
85 *****************************************************************************/
86 #ifndef XSVF_SUPPORT_COMPRESSION
87 #define XSVF_SUPPORT_COMPRESSION 1
90 /*****************************************************************************
91 * Define: XSVF_SUPPORT_ERRORCODES
92 * Description: Define this to support the new XSVF error codes.
93 * (The original XSVF player just returned 1 for success and
94 * 0 for an unspecified failure.)
95 *****************************************************************************/
96 #ifndef XSVF_SUPPORT_ERRORCODES
97 #define XSVF_SUPPORT_ERRORCODES 1
100 #ifdef XSVF_SUPPORT_ERRORCODES
101 #define XSVF_ERRORCODE(errorCode) errorCode
102 #else /* Use legacy error code */
103 #define XSVF_ERRORCODE(errorCode) ((errorCode==XSVF_ERROR_NONE)?1:0)
104 #endif /* XSVF_SUPPORT_ERRORCODES */
107 /*============================================================================
109 ============================================================================*/
113 #define XSVFDBG_PRINTF(iDebugLevel,pzFormat) \
114 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
115 printf( pzFormat ); }
116 #define XSVFDBG_PRINTF1(iDebugLevel,pzFormat,arg1) \
117 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
118 printf( pzFormat, arg1 ); }
119 #define XSVFDBG_PRINTF2(iDebugLevel,pzFormat,arg1,arg2) \
120 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
121 printf( pzFormat, arg1, arg2 ); }
122 #define XSVFDBG_PRINTF3(iDebugLevel,pzFormat,arg1,arg2,arg3) \
123 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
124 printf( pzFormat, arg1, arg2, arg3 ); }
125 #define XSVFDBG_PRINTLENVAL(iDebugLevel,plenVal) \
126 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
127 xsvfPrintLenVal(plenVal); }
128 #else /* !DEBUG_MODE */
129 #define XSVFDBG_PRINTF(iDebugLevel,pzFormat)
130 #define XSVFDBG_PRINTF1(iDebugLevel,pzFormat,arg1)
131 #define XSVFDBG_PRINTF2(iDebugLevel,pzFormat,arg1,arg2)
132 #define XSVFDBG_PRINTF3(iDebugLevel,pzFormat,arg1,arg2,arg3)
133 #define XSVFDBG_PRINTLENVAL(iDebugLevel,plenVal)
134 #endif /* DEBUG_MODE */
137 /*============================================================================
138 * XSVF Type Declarations
139 ============================================================================*/
141 /*****************************************************************************
143 * Description: This structure contains all of the data used during the
144 * execution of the XSVF. Some data is persistent, predefined
145 * information (e.g. lRunTestTime). The bulk of this struct's
146 * size is due to the lenVal structs (defined in lenval.h)
147 * which contain buffers for the active shift data. The MAX_LEN
148 * #define in lenval.h defines the size of these buffers.
149 * These buffers must be large enough to store the longest
150 * shift data in your XSVF file. For example:
151 * MAX_LEN >= ( longest_shift_data_in_bits / 8 )
152 * Because the lenVal struct dominates the space usage of this
153 * struct, the rough size of this struct is:
154 * sizeof( SXsvfInfo ) ~= MAX_LEN * 7 (number of lenVals)
155 * xsvfInitialize() contains initialization code for the data
157 * xsvfCleanup() contains cleanup code for the data in this
159 *****************************************************************************/
160 typedef struct tagSXsvfInfo
162 /* XSVF status information */
163 unsigned char ucComplete; /* 0 = running; 1 = complete */
164 unsigned char ucCommand; /* Current XSVF command byte */
165 long lCommandCount; /* Number of commands processed */
166 int iErrorCode; /* An error code. 0 = no error. */
168 /* TAP state/sequencing information */
169 unsigned char ucTapState; /* Current TAP state */
170 unsigned char ucEndIR; /* ENDIR TAP state (See SVF) */
171 unsigned char ucEndDR; /* ENDDR TAP state (See SVF) */
173 /* RUNTEST information */
174 unsigned char ucMaxRepeat; /* Max repeat loops (for xc9500/xl) */
175 long lRunTestTime; /* Pre-specified RUNTEST time (usec) */
177 /* Shift Data Info and Buffers */
178 long lShiftLengthBits; /* Len. current shift data in bits */
179 short sShiftLengthBytes; /* Len. current shift data in bytes */
181 lenVal lvTdi; /* Current TDI shift data */
182 lenVal lvTdoExpected; /* Expected TDO shift data */
183 lenVal lvTdoCaptured; /* Captured TDO shift data */
184 lenVal lvTdoMask; /* TDO mask: 0=dontcare; 1=compare */
186 #ifdef XSVF_SUPPORT_COMPRESSION
187 /* XSDRINC Data Buffers */
188 lenVal lvAddressMask; /* Address mask for XSDRINC */
189 lenVal lvDataMask; /* Data mask for XSDRINC */
190 lenVal lvNextData; /* Next data for XSDRINC */
191 #endif /* XSVF_SUPPORT_COMPRESSION */
194 /* Declare pointer to functions that perform XSVF commands */
195 typedef int (*TXsvfDoCmdFuncPtr)( SXsvfInfo* );
197 /*============================================================================
199 ============================================================================*/
201 /* encodings of xsvf instructions */
212 #define XSETSDRMASKS 10
220 #define XSTATE 18 /* 4.00 */
221 #define XENDIR 19 /* 4.04 */
222 #define XENDDR 20 /* 4.04 */
223 #define XSIR2 21 /* 4.10 */
224 #define XCOMMENT 22 /* 4.14 */
225 #define XWAIT 23 /* 5.00 */
226 /* Insert new commands here */
227 /* and add corresponding xsvfDoCmd function to xsvf_pfDoCmd below. */
228 #define XLASTCMD 24 /* Last command marker */
231 /*============================================================================
232 * XSVF Command Parameter Values
233 ============================================================================*/
235 #define XSTATE_RESET 0 /* 4.00 parameter for XSTATE */
236 #define XSTATE_RUNTEST 1 /* 4.00 parameter for XSTATE */
238 #define XENDXR_RUNTEST 0 /* 4.04 parameter for XENDIR/DR */
239 #define XENDXR_PAUSE 1 /* 4.04 parameter for XENDIR/DR */
242 #define XTAPSTATE_RESET 0x00
243 #define XTAPSTATE_RUNTEST 0x01 /* a.k.a. IDLE */
244 #define XTAPSTATE_SELECTDR 0x02
245 #define XTAPSTATE_CAPTUREDR 0x03
246 #define XTAPSTATE_SHIFTDR 0x04
247 #define XTAPSTATE_EXIT1DR 0x05
248 #define XTAPSTATE_PAUSEDR 0x06
249 #define XTAPSTATE_EXIT2DR 0x07
250 #define XTAPSTATE_UPDATEDR 0x08
251 #define XTAPSTATE_IRSTATES 0x09 /* All IR states begin here */
252 #define XTAPSTATE_SELECTIR 0x09
253 #define XTAPSTATE_CAPTUREIR 0x0A
254 #define XTAPSTATE_SHIFTIR 0x0B
255 #define XTAPSTATE_EXIT1IR 0x0C
256 #define XTAPSTATE_PAUSEIR 0x0D
257 #define XTAPSTATE_EXIT2IR 0x0E
258 #define XTAPSTATE_UPDATEIR 0x0F
260 /*============================================================================
261 * XSVF Function Prototypes
262 ============================================================================*/
264 int xsvfDoIllegalCmd( SXsvfInfo* pXsvfInfo ); /* Illegal command function */
265 int xsvfDoXCOMPLETE( SXsvfInfo* pXsvfInfo );
266 int xsvfDoXTDOMASK( SXsvfInfo* pXsvfInfo );
267 int xsvfDoXSIR( SXsvfInfo* pXsvfInfo );
268 int xsvfDoXSIR2( SXsvfInfo* pXsvfInfo );
269 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo );
270 int xsvfDoXRUNTEST( SXsvfInfo* pXsvfInfo );
271 int xsvfDoXREPEAT( SXsvfInfo* pXsvfInfo );
272 int xsvfDoXSDRSIZE( SXsvfInfo* pXsvfInfo );
273 int xsvfDoXSDRTDO( SXsvfInfo* pXsvfInfo );
274 int xsvfDoXSETSDRMASKS( SXsvfInfo* pXsvfInfo );
275 int xsvfDoXSDRINC( SXsvfInfo* pXsvfInfo );
276 int xsvfDoXSDRBCE( SXsvfInfo* pXsvfInfo );
277 int xsvfDoXSDRTDOBCE( SXsvfInfo* pXsvfInfo );
278 int xsvfDoXSTATE( SXsvfInfo* pXsvfInfo );
279 int xsvfDoXENDXR( SXsvfInfo* pXsvfInfo );
280 int xsvfDoXCOMMENT( SXsvfInfo* pXsvfInfo );
281 int xsvfDoXWAIT( SXsvfInfo* pXsvfInfo );
282 /* Insert new command functions here */
284 /*============================================================================
285 * XSVF Global Variables
286 ============================================================================*/
288 /* Array of XSVF command functions. Must follow command byte value order! */
289 /* If your compiler cannot take this form, then convert to a switch statement*/
290 TXsvfDoCmdFuncPtr xsvf_pfDoCmd[] =
292 xsvfDoXCOMPLETE, /* 0 */
293 xsvfDoXTDOMASK, /* 1 */
296 xsvfDoXRUNTEST, /* 4 */
297 xsvfDoIllegalCmd, /* 5 */
298 xsvfDoIllegalCmd, /* 6 */
299 xsvfDoXREPEAT, /* 7 */
300 xsvfDoXSDRSIZE, /* 8 */
301 xsvfDoXSDRTDO, /* 9 */
302 #ifdef XSVF_SUPPORT_COMPRESSION
303 xsvfDoXSETSDRMASKS, /* 10 */
304 xsvfDoXSDRINC, /* 11 */
306 xsvfDoIllegalCmd, /* 10 */
307 xsvfDoIllegalCmd, /* 11 */
308 #endif /* XSVF_SUPPORT_COMPRESSION */
309 xsvfDoXSDRBCE, /* 12 */
310 xsvfDoXSDRBCE, /* 13 */
311 xsvfDoXSDRBCE, /* 14 */
312 xsvfDoXSDRTDOBCE, /* 15 */
313 xsvfDoXSDRTDOBCE, /* 16 */
314 xsvfDoXSDRTDOBCE, /* 17 */
315 xsvfDoXSTATE, /* 18 */
316 xsvfDoXENDXR, /* 19 */
317 xsvfDoXENDXR, /* 20 */
318 xsvfDoXSIR2, /* 21 */
319 xsvfDoXCOMMENT, /* 22 */
321 /* Insert new command functions here */
325 char* xsvf_pzCommandName[] =
353 char* xsvf_pzErrorName[] =
357 "ERROR: TDO mismatch",
358 "ERROR: TDO mismatch and exceeded max retries",
359 "ERROR: Unsupported XSVF command",
360 "ERROR: Illegal state specification",
361 "ERROR: Data overflows allocated MAX_LEN buffer size"
364 char* xsvf_pzTapState[] =
367 "RUNTEST/IDLE", /* 0x01 */
368 "DRSELECT", /* 0x02 */
369 "DRCAPTURE", /* 0x03 */
370 "DRSHIFT", /* 0x04 */
371 "DREXIT1", /* 0x05 */
372 "DRPAUSE", /* 0x06 */
373 "DREXIT2", /* 0x07 */
374 "DRUPDATE", /* 0x08 */
375 "IRSELECT", /* 0x09 */
376 "IRCAPTURE", /* 0x0A */
377 "IRSHIFT", /* 0x0B */
378 "IREXIT1", /* 0x0C */
379 "IRPAUSE", /* 0x0D */
380 "IREXIT2", /* 0x0E */
381 "IRUPDATE" /* 0x0F */
383 #endif /* DEBUG_MODE */
385 /*#ifdef DEBUG_MODE */
386 /* FILE* in; /XXX* Legacy DEBUG_MODE file pointer */
387 int xsvf_iDebugLevel;
388 /*#endif /XXX* DEBUG_MODE */
390 /*============================================================================
392 ============================================================================*/
394 /*****************************************************************************
395 * Function: xsvfPrintLenVal
396 * Description: Print the lenval value in hex.
397 * Parameters: plv - ptr to lenval.
399 *****************************************************************************/
401 void xsvfPrintLenVal( lenVal *plv )
408 for ( i = 0; i < plv->len; ++i )
410 printf( "%02x", ((unsigned int)(plv->val[ i ])) );
414 #endif /* DEBUG_MODE */
417 /*****************************************************************************
418 * Function: xsvfInfoInit
419 * Description: Initialize the xsvfInfo data.
420 * Parameters: pXsvfInfo - ptr to the XSVF info structure.
421 * Returns: int - 0 = success; otherwise error.
422 *****************************************************************************/
423 int xsvfInfoInit( SXsvfInfo* pXsvfInfo )
425 XSVFDBG_PRINTF1( 4, " sizeof( SXsvfInfo ) = %d bytes\n",
426 sizeof( SXsvfInfo ) );
428 pXsvfInfo->ucComplete = 0;
429 pXsvfInfo->ucCommand = XCOMPLETE;
430 pXsvfInfo->lCommandCount = 0;
431 pXsvfInfo->iErrorCode = XSVF_ERROR_NONE;
432 pXsvfInfo->ucMaxRepeat = 0;
433 pXsvfInfo->ucTapState = XTAPSTATE_RESET;
434 pXsvfInfo->ucEndIR = XTAPSTATE_RUNTEST;
435 pXsvfInfo->ucEndDR = XTAPSTATE_RUNTEST;
436 pXsvfInfo->lShiftLengthBits = 0L;
437 pXsvfInfo->sShiftLengthBytes= 0;
438 pXsvfInfo->lRunTestTime = 0L;
443 /*****************************************************************************
444 * Function: xsvfInfoCleanup
445 * Description: Cleanup the xsvfInfo data.
446 * Parameters: pXsvfInfo - ptr to the XSVF info structure.
448 *****************************************************************************/
449 void xsvfInfoCleanup( SXsvfInfo* pXsvfInfo )
453 /*****************************************************************************
454 * Function: xsvfGetAsNumBytes
455 * Description: Calculate the number of bytes the given number of bits
457 * Parameters: lNumBits - the number of bits.
458 * Returns: short - the number of bytes to store the number of bits.
459 *****************************************************************************/
460 short xsvfGetAsNumBytes( long lNumBits )
462 return( (short)( ( lNumBits + 7L ) / 8L ) );
465 /*****************************************************************************
466 * Function: xsvfTmsTransition
467 * Description: Apply TMS and transition TAP controller by applying one TCK
469 * Parameters: sTms - new TMS value.
471 *****************************************************************************/
472 void xsvfTmsTransition( short sTms )
474 setPort( TMS, sTms );
479 /*****************************************************************************
480 * Function: xsvfGotoTapState
481 * Description: From the current TAP state, go to the named TAP state.
482 * A target state of RESET ALWAYS causes TMS reset sequence.
483 * All SVF standard stable state paths are supported.
484 * All state transitions are supported except for the following
485 * which cause an XSVF_ERROR_ILLEGALSTATE:
486 * - Target==DREXIT2; Start!=DRPAUSE
487 * - Target==IREXIT2; Start!=IRPAUSE
488 * Parameters: pucTapState - Current TAP state; returns final TAP state.
489 * ucTargetState - New target TAP state.
490 * Returns: int - 0 = success; otherwise error.
491 *****************************************************************************/
492 int xsvfGotoTapState( unsigned char* pucTapState,
493 unsigned char ucTargetState )
498 iErrorCode = XSVF_ERROR_NONE;
499 if ( ucTargetState == XTAPSTATE_RESET )
501 /* If RESET, always perform TMS reset sequence to reset/sync TAPs */
502 xsvfTmsTransition( 1 );
503 for ( i = 0; i < 5; ++i )
508 *pucTapState = XTAPSTATE_RESET;
509 XSVFDBG_PRINTF( 3, " TMS Reset Sequence -> Test-Logic-Reset\n" );
510 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
511 xsvf_pzTapState[ *pucTapState ] );
512 } else if ( ( ucTargetState != *pucTapState ) &&
513 ( ( ( ucTargetState == XTAPSTATE_EXIT2DR ) && ( *pucTapState != XTAPSTATE_PAUSEDR ) ) ||
514 ( ( ucTargetState == XTAPSTATE_EXIT2IR ) && ( *pucTapState != XTAPSTATE_PAUSEIR ) ) ) )
516 /* Trap illegal TAP state path specification */
517 iErrorCode = XSVF_ERROR_ILLEGALSTATE;
519 if ( ucTargetState == *pucTapState )
521 /* Already in target state. Do nothing except when in DRPAUSE
522 or in IRPAUSE to comply with SVF standard */
523 if ( ucTargetState == XTAPSTATE_PAUSEDR )
525 xsvfTmsTransition( 1 );
526 *pucTapState = XTAPSTATE_EXIT2DR;
527 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
528 xsvf_pzTapState[ *pucTapState ] );
530 else if ( ucTargetState == XTAPSTATE_PAUSEIR )
532 xsvfTmsTransition( 1 );
533 *pucTapState = XTAPSTATE_EXIT2IR;
534 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
535 xsvf_pzTapState[ *pucTapState ] );
539 /* Perform TAP state transitions to get to the target state */
540 while ( ucTargetState != *pucTapState )
542 switch ( *pucTapState )
544 case XTAPSTATE_RESET:
545 xsvfTmsTransition( 0 );
546 *pucTapState = XTAPSTATE_RUNTEST;
548 case XTAPSTATE_RUNTEST:
549 xsvfTmsTransition( 1 );
550 *pucTapState = XTAPSTATE_SELECTDR;
552 case XTAPSTATE_SELECTDR:
553 if ( ucTargetState >= XTAPSTATE_IRSTATES )
555 xsvfTmsTransition( 1 );
556 *pucTapState = XTAPSTATE_SELECTIR;
560 xsvfTmsTransition( 0 );
561 *pucTapState = XTAPSTATE_CAPTUREDR;
564 case XTAPSTATE_CAPTUREDR:
565 if ( ucTargetState == XTAPSTATE_SHIFTDR )
567 xsvfTmsTransition( 0 );
568 *pucTapState = XTAPSTATE_SHIFTDR;
572 xsvfTmsTransition( 1 );
573 *pucTapState = XTAPSTATE_EXIT1DR;
576 case XTAPSTATE_SHIFTDR:
577 xsvfTmsTransition( 1 );
578 *pucTapState = XTAPSTATE_EXIT1DR;
580 case XTAPSTATE_EXIT1DR:
581 if ( ucTargetState == XTAPSTATE_PAUSEDR )
583 xsvfTmsTransition( 0 );
584 *pucTapState = XTAPSTATE_PAUSEDR;
588 xsvfTmsTransition( 1 );
589 *pucTapState = XTAPSTATE_UPDATEDR;
592 case XTAPSTATE_PAUSEDR:
593 xsvfTmsTransition( 1 );
594 *pucTapState = XTAPSTATE_EXIT2DR;
596 case XTAPSTATE_EXIT2DR:
597 if ( ucTargetState == XTAPSTATE_SHIFTDR )
599 xsvfTmsTransition( 0 );
600 *pucTapState = XTAPSTATE_SHIFTDR;
604 xsvfTmsTransition( 1 );
605 *pucTapState = XTAPSTATE_UPDATEDR;
608 case XTAPSTATE_UPDATEDR:
609 if ( ucTargetState == XTAPSTATE_RUNTEST )
611 xsvfTmsTransition( 0 );
612 *pucTapState = XTAPSTATE_RUNTEST;
616 xsvfTmsTransition( 1 );
617 *pucTapState = XTAPSTATE_SELECTDR;
620 case XTAPSTATE_SELECTIR:
621 xsvfTmsTransition( 0 );
622 *pucTapState = XTAPSTATE_CAPTUREIR;
624 case XTAPSTATE_CAPTUREIR:
625 if ( ucTargetState == XTAPSTATE_SHIFTIR )
627 xsvfTmsTransition( 0 );
628 *pucTapState = XTAPSTATE_SHIFTIR;
632 xsvfTmsTransition( 1 );
633 *pucTapState = XTAPSTATE_EXIT1IR;
636 case XTAPSTATE_SHIFTIR:
637 xsvfTmsTransition( 1 );
638 *pucTapState = XTAPSTATE_EXIT1IR;
640 case XTAPSTATE_EXIT1IR:
641 if ( ucTargetState == XTAPSTATE_PAUSEIR )
643 xsvfTmsTransition( 0 );
644 *pucTapState = XTAPSTATE_PAUSEIR;
648 xsvfTmsTransition( 1 );
649 *pucTapState = XTAPSTATE_UPDATEIR;
652 case XTAPSTATE_PAUSEIR:
653 xsvfTmsTransition( 1 );
654 *pucTapState = XTAPSTATE_EXIT2IR;
656 case XTAPSTATE_EXIT2IR:
657 if ( ucTargetState == XTAPSTATE_SHIFTIR )
659 xsvfTmsTransition( 0 );
660 *pucTapState = XTAPSTATE_SHIFTIR;
664 xsvfTmsTransition( 1 );
665 *pucTapState = XTAPSTATE_UPDATEIR;
668 case XTAPSTATE_UPDATEIR:
669 if ( ucTargetState == XTAPSTATE_RUNTEST )
671 xsvfTmsTransition( 0 );
672 *pucTapState = XTAPSTATE_RUNTEST;
676 xsvfTmsTransition( 1 );
677 *pucTapState = XTAPSTATE_SELECTDR;
681 iErrorCode = XSVF_ERROR_ILLEGALSTATE;
682 *pucTapState = ucTargetState; /* Exit while loop */
685 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
686 xsvf_pzTapState[ *pucTapState ] );
690 return( iErrorCode );
693 /*****************************************************************************
694 * Function: xsvfShiftOnly
695 * Description: Assumes that starting TAP state is SHIFT-DR or SHIFT-IR.
696 * Shift the given TDI data into the JTAG scan chain.
697 * Optionally, save the TDO data shifted out of the scan chain.
698 * Last shift cycle is special: capture last TDO, set last TDI,
699 * but does not pulse TCK. Caller must pulse TCK and optionally
700 * set TMS=1 to exit shift state.
701 * Parameters: lNumBits - number of bits to shift.
702 * plvTdi - ptr to lenval for TDI data.
703 * plvTdoCaptured - ptr to lenval for storing captured TDO data.
704 * iExitShift - 1=exit at end of shift; 0=stay in Shift-DR.
706 *****************************************************************************/
707 void xsvfShiftOnly( long lNumBits,
709 lenVal* plvTdoCaptured,
712 unsigned char* pucTdi;
713 unsigned char* pucTdo;
714 unsigned char ucTdiByte;
715 unsigned char ucTdoByte;
716 unsigned char ucTdoBit;
719 /* assert( ( ( lNumBits + 7 ) / 8 ) == plvTdi->len ); */
721 /* Initialize TDO storage len == TDI len */
723 if ( plvTdoCaptured )
725 plvTdoCaptured->len = plvTdi->len;
726 pucTdo = plvTdoCaptured->val + plvTdi->len;
729 /* Shift LSB first. val[N-1] == LSB. val[0] == MSB. */
730 pucTdi = plvTdi->val + plvTdi->len;
733 /* Process on a byte-basis */
734 ucTdiByte = (*(--pucTdi));
736 for ( i = 0; ( lNumBits && ( i < 8 ) ); ++i )
739 if ( iExitShift && !lNumBits )
741 /* Exit Shift-DR state */
745 /* Set the new TDI value */
746 setPort( TDI, (short)(ucTdiByte & 1) );
754 /* Save the TDO value */
755 ucTdoBit = readTDOBit();
756 ucTdoByte |= ( ucTdoBit << i );
763 /* Save the TDO byte value */
766 (*(--pucTdo)) = ucTdoByte;
771 /*****************************************************************************
772 * Function: xsvfShift
773 * Description: Goes to the given starting TAP state.
774 * Calls xsvfShiftOnly to shift in the given TDI data and
775 * optionally capture the TDO data.
776 * Compares the TDO captured data against the TDO expected
778 * If a data mismatch occurs, then executes the exception
779 * handling loop upto ucMaxRepeat times.
780 * Parameters: pucTapState - Ptr to current TAP state.
781 * ucStartState - Starting shift state: Shift-DR or Shift-IR.
782 * lNumBits - number of bits to shift.
783 * plvTdi - ptr to lenval for TDI data.
784 * plvTdoCaptured - ptr to lenval for storing TDO data.
785 * plvTdoExpected - ptr to expected TDO data.
786 * plvTdoMask - ptr to TDO mask.
787 * ucEndState - state in which to end the shift.
788 * lRunTestTime - amount of time to wait after the shift.
789 * ucMaxRepeat - Maximum number of retries on TDO mismatch.
790 * Returns: int - 0 = success; otherwise TDO mismatch.
791 * Notes: XC9500XL-only Optimization:
792 * Skip the waitTime() if plvTdoMask->val[0:plvTdoMask->len-1]
793 * is NOT all zeros and sMatch==1.
794 *****************************************************************************/
795 int xsvfShift( unsigned char* pucTapState,
796 unsigned char ucStartState,
799 lenVal* plvTdoCaptured,
800 lenVal* plvTdoExpected,
802 unsigned char ucEndState,
804 unsigned char ucMaxRepeat )
808 unsigned char ucRepeat;
811 iErrorCode = XSVF_ERROR_NONE;
814 iExitShift = ( ucStartState != ucEndState );
816 XSVFDBG_PRINTF1( 3, " Shift Length = %ld\n", lNumBits );
817 XSVFDBG_PRINTF( 4, " TDI = ");
818 XSVFDBG_PRINTLENVAL( 4, plvTdi );
819 XSVFDBG_PRINTF( 4, "\n");
820 XSVFDBG_PRINTF( 4, " TDO Expected = ");
821 XSVFDBG_PRINTLENVAL( 4, plvTdoExpected );
822 XSVFDBG_PRINTF( 4, "\n");
826 /* Compatibility with XSVF2.00: XSDR 0 = no shift, but wait in RTI */
829 /* Wait for prespecified XRUNTEST time */
830 xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
831 XSVFDBG_PRINTF1( 3, " Wait = %ld usec\n", lRunTestTime );
832 waitTime( lRunTestTime );
839 /* Goto Shift-DR or Shift-IR */
840 xsvfGotoTapState( pucTapState, ucStartState );
842 /* Shift TDI and capture TDO */
843 xsvfShiftOnly( lNumBits, plvTdi, plvTdoCaptured, iExitShift );
845 if ( plvTdoExpected )
847 /* Compare TDO data to expected TDO data */
848 iMismatch = !EqualLenVal( plvTdoExpected,
855 /* Update TAP state: Shift->Exit */
857 XSVFDBG_PRINTF1( 3, " TAP State = %s\n",
858 xsvf_pzTapState[ *pucTapState ] );
860 if ( iMismatch && lRunTestTime && ( ucRepeat < ucMaxRepeat ) )
862 XSVFDBG_PRINTF( 4, " TDO Expected = ");
863 XSVFDBG_PRINTLENVAL( 4, plvTdoExpected );
864 XSVFDBG_PRINTF( 4, "\n");
865 XSVFDBG_PRINTF( 4, " TDO Captured = ");
866 XSVFDBG_PRINTLENVAL( 4, plvTdoCaptured );
867 XSVFDBG_PRINTF( 4, "\n");
868 XSVFDBG_PRINTF( 4, " TDO Mask = ");
869 XSVFDBG_PRINTLENVAL( 4, plvTdoMask );
870 XSVFDBG_PRINTF( 4, "\n");
871 XSVFDBG_PRINTF1( 3, " Retry #%d\n", ( ucRepeat + 1 ) );
872 /* Do exception handling retry - ShiftDR only */
873 xsvfGotoTapState( pucTapState, XTAPSTATE_PAUSEDR );
874 /* Shift 1 extra bit */
875 xsvfGotoTapState( pucTapState, XTAPSTATE_SHIFTDR );
876 /* Increment RUNTEST time by an additional 25% */
877 lRunTestTime += ( lRunTestTime >> 2 );
881 /* Do normal exit from Shift-XR */
882 xsvfGotoTapState( pucTapState, ucEndState );
887 /* Wait for prespecified XRUNTEST time */
888 xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
889 XSVFDBG_PRINTF1( 3, " Wait = %ld usec\n", lRunTestTime );
890 waitTime( lRunTestTime );
893 } while ( iMismatch && ( ucRepeat++ < ucMaxRepeat ) );
898 XSVFDBG_PRINTF( 1, " TDO Expected = ");
899 XSVFDBG_PRINTLENVAL( 1, plvTdoExpected );
900 XSVFDBG_PRINTF( 1, "\n");
901 XSVFDBG_PRINTF( 1, " TDO Captured = ");
902 XSVFDBG_PRINTLENVAL( 1, plvTdoCaptured );
903 XSVFDBG_PRINTF( 1, "\n");
904 XSVFDBG_PRINTF( 1, " TDO Mask = ");
905 XSVFDBG_PRINTLENVAL( 1, plvTdoMask );
906 XSVFDBG_PRINTF( 1, "\n");
907 if ( ucMaxRepeat && ( ucRepeat > ucMaxRepeat ) )
909 iErrorCode = XSVF_ERROR_MAXRETRIES;
913 iErrorCode = XSVF_ERROR_TDOMISMATCH;
917 return( iErrorCode );
920 /*****************************************************************************
921 * Function: xsvfBasicXSDRTDO
922 * Description: Get the XSDRTDO parameters and execute the XSDRTDO command.
923 * This is the common function for all XSDRTDO commands.
924 * Parameters: pucTapState - Current TAP state.
925 * lShiftLengthBits - number of bits to shift.
926 * sShiftLengthBytes - number of bytes to read.
927 * plvTdi - ptr to lenval for TDI data.
928 * lvTdoCaptured - ptr to lenval for storing TDO data.
929 * iEndState - state in which to end the shift.
930 * lRunTestTime - amount of time to wait after the shift.
931 * ucMaxRepeat - maximum xc9500/xl retries.
932 * Returns: int - 0 = success; otherwise TDO mismatch.
933 *****************************************************************************/
934 int xsvfBasicXSDRTDO( unsigned char* pucTapState,
935 long lShiftLengthBits,
936 short sShiftLengthBytes,
938 lenVal* plvTdoCaptured,
939 lenVal* plvTdoExpected,
941 unsigned char ucEndState,
943 unsigned char ucMaxRepeat )
945 readVal( plvTdi, sShiftLengthBytes );
946 if ( plvTdoExpected )
948 readVal( plvTdoExpected, sShiftLengthBytes );
950 return( xsvfShift( pucTapState, XTAPSTATE_SHIFTDR, lShiftLengthBits,
951 plvTdi, plvTdoCaptured, plvTdoExpected, plvTdoMask,
952 ucEndState, lRunTestTime, ucMaxRepeat ) );
955 /*****************************************************************************
956 * Function: xsvfDoSDRMasking
957 * Description: Update the data value with the next XSDRINC data and address.
958 * Example: dataVal=0x01ff, nextData=0xab, addressMask=0x0100,
959 * dataMask=0x00ff, should set dataVal to 0x02ab
960 * Parameters: plvTdi - The current TDI value.
961 * plvNextData - the next data value.
962 * plvAddressMask - the address mask.
963 * plvDataMask - the data mask.
965 *****************************************************************************/
966 #ifdef XSVF_SUPPORT_COMPRESSION
967 void xsvfDoSDRMasking( lenVal* plvTdi,
969 lenVal* plvAddressMask,
970 lenVal* plvDataMask )
974 unsigned char ucTdiMask;
975 unsigned char ucDataMask;
976 unsigned char ucNextData;
977 unsigned char ucNextMask;
980 /* add the address Mask to dataVal and return as a new dataVal */
981 addVal( plvTdi, plvTdi, plvAddressMask );
985 sNextData = plvNextData->len;
986 for ( i = plvDataMask->len - 1; i >= 0; --i )
988 /* Go through data mask in reverse order looking for mask (1) bits */
989 ucDataMask = plvDataMask->val[ i ];
992 /* Retrieve the corresponding TDI byte value */
993 ucTdi = plvTdi->val[ i ];
995 /* For each bit in the data mask byte, look for 1's */
999 if ( ucDataMask & 1 )
1003 /* Get the next data byte */
1004 ucNextData = plvNextData->val[ --sNextData ];
1008 /* Set or clear the data bit according to the next data */
1009 if ( ucNextData & ucNextMask )
1011 ucTdi |= ucTdiMask; /* Set bit */
1015 ucTdi &= ( ~ucTdiMask ); /* Clear bit */
1018 /* Update the next data */
1025 /* Update the TDI value */
1026 plvTdi->val[ i ] = ucTdi;
1030 #endif /* XSVF_SUPPORT_COMPRESSION */
1032 /*============================================================================
1033 * XSVF Command Functions (type = TXsvfDoCmdFuncPtr)
1034 * These functions update pXsvfInfo->iErrorCode only on an error.
1035 * Otherwise, the error code is left alone.
1036 * The function returns the error code from the function.
1037 ============================================================================*/
1039 /*****************************************************************************
1040 * Function: xsvfDoIllegalCmd
1041 * Description: Function place holder for illegal/unsupported commands.
1042 * Parameters: pXsvfInfo - XSVF information pointer.
1043 * Returns: int - 0 = success; non-zero = error.
1044 *****************************************************************************/
1045 int xsvfDoIllegalCmd( SXsvfInfo* pXsvfInfo )
1047 XSVFDBG_PRINTF2( 0, "ERROR: Encountered unsupported command #%d (%s)\n",
1048 ((unsigned int)(pXsvfInfo->ucCommand)),
1049 ((pXsvfInfo->ucCommand < XLASTCMD)
1050 ? (xsvf_pzCommandName[pXsvfInfo->ucCommand])
1052 pXsvfInfo->iErrorCode = XSVF_ERROR_ILLEGALCMD;
1053 return( pXsvfInfo->iErrorCode );
1056 /*****************************************************************************
1057 * Function: xsvfDoXCOMPLETE
1058 * Description: XCOMPLETE (no parameters)
1059 * Update complete status for XSVF player.
1060 * Parameters: pXsvfInfo - XSVF information pointer.
1061 * Returns: int - 0 = success; non-zero = error.
1062 *****************************************************************************/
1063 int xsvfDoXCOMPLETE( SXsvfInfo* pXsvfInfo )
1065 pXsvfInfo->ucComplete = 1;
1066 return( XSVF_ERROR_NONE );
1069 /*****************************************************************************
1070 * Function: xsvfDoXTDOMASK
1071 * Description: XTDOMASK <lenVal.TdoMask[XSDRSIZE]>
1072 * Prespecify the TDO compare mask.
1073 * Parameters: pXsvfInfo - XSVF information pointer.
1074 * Returns: int - 0 = success; non-zero = error.
1075 *****************************************************************************/
1076 int xsvfDoXTDOMASK( SXsvfInfo* pXsvfInfo )
1078 readVal( &(pXsvfInfo->lvTdoMask), pXsvfInfo->sShiftLengthBytes );
1079 XSVFDBG_PRINTF( 4, " TDO Mask = ");
1080 XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvTdoMask) );
1081 XSVFDBG_PRINTF( 4, "\n");
1082 return( XSVF_ERROR_NONE );
1085 /*****************************************************************************
1086 * Function: xsvfDoXSIR
1087 * Description: XSIR <(byte)shiftlen> <lenVal.TDI[shiftlen]>
1088 * Get the instruction and shift the instruction into the TAP.
1089 * If prespecified XRUNTEST!=0, goto RUNTEST and wait after
1090 * the shift for XRUNTEST usec.
1091 * Parameters: pXsvfInfo - XSVF information pointer.
1092 * Returns: int - 0 = success; non-zero = error.
1093 *****************************************************************************/
1094 int xsvfDoXSIR( SXsvfInfo* pXsvfInfo )
1096 unsigned char ucShiftIrBits;
1097 short sShiftIrBytes;
1100 /* Get the shift length and store */
1101 readByte( &ucShiftIrBits );
1102 sShiftIrBytes = xsvfGetAsNumBytes( ucShiftIrBits );
1103 XSVFDBG_PRINTF1( 3, " XSIR length = %d\n",
1104 ((unsigned int)ucShiftIrBits) );
1106 if ( sShiftIrBytes > MAX_LEN )
1108 iErrorCode = XSVF_ERROR_DATAOVERFLOW;
1112 /* Get and store instruction to shift in */
1113 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( ucShiftIrBits ) );
1115 /* Shift the data */
1116 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTIR,
1117 ucShiftIrBits, &(pXsvfInfo->lvTdi),
1118 /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1119 /*plvTdoMask*/0, pXsvfInfo->ucEndIR,
1120 pXsvfInfo->lRunTestTime, /*ucMaxRepeat*/0 );
1123 if ( iErrorCode != XSVF_ERROR_NONE )
1125 pXsvfInfo->iErrorCode = iErrorCode;
1127 return( iErrorCode );
1130 /*****************************************************************************
1131 * Function: xsvfDoXSIR2
1132 * Description: XSIR <(2-byte)shiftlen> <lenVal.TDI[shiftlen]>
1133 * Get the instruction and shift the instruction into the TAP.
1134 * If prespecified XRUNTEST!=0, goto RUNTEST and wait after
1135 * the shift for XRUNTEST usec.
1136 * Parameters: pXsvfInfo - XSVF information pointer.
1137 * Returns: int - 0 = success; non-zero = error.
1138 *****************************************************************************/
1139 int xsvfDoXSIR2( SXsvfInfo* pXsvfInfo )
1142 short sShiftIrBytes;
1145 /* Get the shift length and store */
1146 readVal( &(pXsvfInfo->lvTdi), 2 );
1147 lShiftIrBits = value( &(pXsvfInfo->lvTdi) );
1148 sShiftIrBytes = xsvfGetAsNumBytes( lShiftIrBits );
1149 XSVFDBG_PRINTF1( 3, " XSIR2 length = %d\n", (int)lShiftIrBits);
1151 if ( sShiftIrBytes > MAX_LEN )
1153 iErrorCode = XSVF_ERROR_DATAOVERFLOW;
1157 /* Get and store instruction to shift in */
1158 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( lShiftIrBits ) );
1160 /* Shift the data */
1161 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTIR,
1162 lShiftIrBits, &(pXsvfInfo->lvTdi),
1163 /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1164 /*plvTdoMask*/0, pXsvfInfo->ucEndIR,
1165 pXsvfInfo->lRunTestTime, /*ucMaxRepeat*/0 );
1168 if ( iErrorCode != XSVF_ERROR_NONE )
1170 pXsvfInfo->iErrorCode = iErrorCode;
1172 return( iErrorCode );
1175 /*****************************************************************************
1176 * Function: xsvfDoXSDR
1177 * Description: XSDR <lenVal.TDI[XSDRSIZE]>
1178 * Shift the given TDI data into the JTAG scan chain.
1179 * Compare the captured TDO with the expected TDO from the
1180 * previous XSDRTDO command using the previously specified
1182 * Parameters: pXsvfInfo - XSVF information pointer.
1183 * Returns: int - 0 = success; non-zero = error.
1184 *****************************************************************************/
1185 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo )
1188 readVal( &(pXsvfInfo->lvTdi), pXsvfInfo->sShiftLengthBytes );
1189 /* use TDOExpected from last XSDRTDO instruction */
1190 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTDR,
1191 pXsvfInfo->lShiftLengthBits, &(pXsvfInfo->lvTdi),
1192 &(pXsvfInfo->lvTdoCaptured),
1193 &(pXsvfInfo->lvTdoExpected),
1194 &(pXsvfInfo->lvTdoMask), pXsvfInfo->ucEndDR,
1195 pXsvfInfo->lRunTestTime, pXsvfInfo->ucMaxRepeat );
1196 if ( iErrorCode != XSVF_ERROR_NONE )
1198 pXsvfInfo->iErrorCode = iErrorCode;
1200 return( iErrorCode );
1203 /*****************************************************************************
1204 * Function: xsvfDoXRUNTEST
1205 * Description: XRUNTEST <uint32>
1206 * Prespecify the XRUNTEST wait time for shift operations.
1207 * Parameters: pXsvfInfo - XSVF information pointer.
1208 * Returns: int - 0 = success; non-zero = error.
1209 *****************************************************************************/
1210 int xsvfDoXRUNTEST( SXsvfInfo* pXsvfInfo )
1212 readVal( &(pXsvfInfo->lvTdi), 4 );
1213 pXsvfInfo->lRunTestTime = value( &(pXsvfInfo->lvTdi) );
1214 XSVFDBG_PRINTF1( 3, " XRUNTEST = %ld\n", pXsvfInfo->lRunTestTime );
1215 return( XSVF_ERROR_NONE );
1218 /*****************************************************************************
1219 * Function: xsvfDoXREPEAT
1220 * Description: XREPEAT <byte>
1221 * Prespecify the maximum number of XC9500/XL retries.
1222 * Parameters: pXsvfInfo - XSVF information pointer.
1223 * Returns: int - 0 = success; non-zero = error.
1224 *****************************************************************************/
1225 int xsvfDoXREPEAT( SXsvfInfo* pXsvfInfo )
1227 readByte( &(pXsvfInfo->ucMaxRepeat) );
1228 XSVFDBG_PRINTF1( 3, " XREPEAT = %d\n",
1229 ((unsigned int)(pXsvfInfo->ucMaxRepeat)) );
1230 return( XSVF_ERROR_NONE );
1233 /*****************************************************************************
1234 * Function: xsvfDoXSDRSIZE
1235 * Description: XSDRSIZE <uint32>
1236 * Prespecify the XRUNTEST wait time for shift operations.
1237 * Parameters: pXsvfInfo - XSVF information pointer.
1238 * Returns: int - 0 = success; non-zero = error.
1239 *****************************************************************************/
1240 int xsvfDoXSDRSIZE( SXsvfInfo* pXsvfInfo )
1243 iErrorCode = XSVF_ERROR_NONE;
1244 readVal( &(pXsvfInfo->lvTdi), 4 );
1245 pXsvfInfo->lShiftLengthBits = value( &(pXsvfInfo->lvTdi) );
1246 pXsvfInfo->sShiftLengthBytes= xsvfGetAsNumBytes( pXsvfInfo->lShiftLengthBits );
1247 XSVFDBG_PRINTF1( 3, " XSDRSIZE = %ld\n", pXsvfInfo->lShiftLengthBits );
1248 if ( pXsvfInfo->sShiftLengthBytes > MAX_LEN )
1250 iErrorCode = XSVF_ERROR_DATAOVERFLOW;
1251 pXsvfInfo->iErrorCode = iErrorCode;
1253 return( iErrorCode );
1256 /*****************************************************************************
1257 * Function: xsvfDoXSDRTDO
1258 * Description: XSDRTDO <lenVal.TDI[XSDRSIZE]> <lenVal.TDO[XSDRSIZE]>
1259 * Get the TDI and expected TDO values. Then, shift.
1260 * Compare the expected TDO with the captured TDO using the
1261 * prespecified XTDOMASK.
1262 * Parameters: pXsvfInfo - XSVF information pointer.
1263 * Returns: int - 0 = success; non-zero = error.
1264 *****************************************************************************/
1265 int xsvfDoXSDRTDO( SXsvfInfo* pXsvfInfo )
1268 iErrorCode = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1269 pXsvfInfo->lShiftLengthBits,
1270 pXsvfInfo->sShiftLengthBytes,
1271 &(pXsvfInfo->lvTdi),
1272 &(pXsvfInfo->lvTdoCaptured),
1273 &(pXsvfInfo->lvTdoExpected),
1274 &(pXsvfInfo->lvTdoMask),
1276 pXsvfInfo->lRunTestTime,
1277 pXsvfInfo->ucMaxRepeat );
1278 if ( iErrorCode != XSVF_ERROR_NONE )
1280 pXsvfInfo->iErrorCode = iErrorCode;
1282 return( iErrorCode );
1285 /*****************************************************************************
1286 * Function: xsvfDoXSETSDRMASKS
1287 * Description: XSETSDRMASKS <lenVal.AddressMask[XSDRSIZE]>
1288 * <lenVal.DataMask[XSDRSIZE]>
1289 * Get the prespecified address and data mask for the XSDRINC
1291 * Used for xc9500/xl compressed XSVF data.
1292 * Parameters: pXsvfInfo - XSVF information pointer.
1293 * Returns: int - 0 = success; non-zero = error.
1294 *****************************************************************************/
1295 #ifdef XSVF_SUPPORT_COMPRESSION
1296 int xsvfDoXSETSDRMASKS( SXsvfInfo* pXsvfInfo )
1298 /* read the addressMask */
1299 readVal( &(pXsvfInfo->lvAddressMask), pXsvfInfo->sShiftLengthBytes );
1300 /* read the dataMask */
1301 readVal( &(pXsvfInfo->lvDataMask), pXsvfInfo->sShiftLengthBytes );
1303 XSVFDBG_PRINTF( 4, " Address Mask = " );
1304 XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvAddressMask) );
1305 XSVFDBG_PRINTF( 4, "\n" );
1306 XSVFDBG_PRINTF( 4, " Data Mask = " );
1307 XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvDataMask) );
1308 XSVFDBG_PRINTF( 4, "\n" );
1310 return( XSVF_ERROR_NONE );
1312 #endif /* XSVF_SUPPORT_COMPRESSION */
1314 /*****************************************************************************
1315 * Function: xsvfDoXSDRINC
1316 * Description: XSDRINC <lenVal.firstTDI[XSDRSIZE]> <byte(numTimes)>
1317 * <lenVal.data[XSETSDRMASKS.dataMask.len]> ...
1318 * Get the XSDRINC parameters and execute the XSDRINC command.
1319 * XSDRINC starts by loading the first TDI shift value.
1320 * Then, for numTimes, XSDRINC gets the next piece of data,
1321 * replaces the bits from the starting TDI as defined by the
1322 * XSETSDRMASKS.dataMask, adds the address mask from
1323 * XSETSDRMASKS.addressMask, shifts the new TDI value,
1324 * and compares the TDO to the expected TDO from the previous
1325 * XSDRTDO command using the XTDOMASK.
1326 * Used for xc9500/xl compressed XSVF data.
1327 * Parameters: pXsvfInfo - XSVF information pointer.
1328 * Returns: int - 0 = success; non-zero = error.
1329 *****************************************************************************/
1330 #ifdef XSVF_SUPPORT_COMPRESSION
1331 int xsvfDoXSDRINC( SXsvfInfo* pXsvfInfo )
1335 unsigned char ucDataMask;
1336 unsigned char ucNumTimes;
1339 readVal( &(pXsvfInfo->lvTdi), pXsvfInfo->sShiftLengthBytes );
1340 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTDR,
1341 pXsvfInfo->lShiftLengthBits,
1342 &(pXsvfInfo->lvTdi), &(pXsvfInfo->lvTdoCaptured),
1343 &(pXsvfInfo->lvTdoExpected),
1344 &(pXsvfInfo->lvTdoMask), pXsvfInfo->ucEndDR,
1345 pXsvfInfo->lRunTestTime, pXsvfInfo->ucMaxRepeat );
1348 /* Calculate number of data mask bits */
1350 for ( i = 0; i < pXsvfInfo->lvDataMask.len; ++i )
1352 ucDataMask = pXsvfInfo->lvDataMask.val[ i ];
1353 while ( ucDataMask )
1355 iDataMaskLen += ( ucDataMask & 1 );
1360 /* Get the number of data pieces, i.e. number of times to shift */
1361 readByte( &ucNumTimes );
1363 /* For numTimes, get data, fix TDI, and shift */
1364 for ( i = 0; !iErrorCode && ( i < ucNumTimes ); ++i )
1366 readVal( &(pXsvfInfo->lvNextData),
1367 xsvfGetAsNumBytes( iDataMaskLen ) );
1368 xsvfDoSDRMasking( &(pXsvfInfo->lvTdi),
1369 &(pXsvfInfo->lvNextData),
1370 &(pXsvfInfo->lvAddressMask),
1371 &(pXsvfInfo->lvDataMask) );
1372 iErrorCode = xsvfShift( &(pXsvfInfo->ucTapState),
1374 pXsvfInfo->lShiftLengthBits,
1375 &(pXsvfInfo->lvTdi),
1376 &(pXsvfInfo->lvTdoCaptured),
1377 &(pXsvfInfo->lvTdoExpected),
1378 &(pXsvfInfo->lvTdoMask),
1380 pXsvfInfo->lRunTestTime,
1381 pXsvfInfo->ucMaxRepeat );
1384 if ( iErrorCode != XSVF_ERROR_NONE )
1386 pXsvfInfo->iErrorCode = iErrorCode;
1388 return( iErrorCode );
1390 #endif /* XSVF_SUPPORT_COMPRESSION */
1392 /*****************************************************************************
1393 * Function: xsvfDoXSDRBCE
1394 * Description: XSDRB/XSDRC/XSDRE <lenVal.TDI[XSDRSIZE]>
1395 * If not already in SHIFTDR, goto SHIFTDR.
1396 * Shift the given TDI data into the JTAG scan chain.
1398 * If cmd==XSDRE, then goto ENDDR. Otherwise, stay in ShiftDR.
1399 * XSDRB, XSDRC, and XSDRE are the same implementation.
1400 * Parameters: pXsvfInfo - XSVF information pointer.
1401 * Returns: int - 0 = success; non-zero = error.
1402 *****************************************************************************/
1403 int xsvfDoXSDRBCE( SXsvfInfo* pXsvfInfo )
1405 unsigned char ucEndDR;
1407 ucEndDR = (unsigned char)(( pXsvfInfo->ucCommand == XSDRE ) ?
1408 pXsvfInfo->ucEndDR : XTAPSTATE_SHIFTDR);
1409 iErrorCode = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1410 pXsvfInfo->lShiftLengthBits,
1411 pXsvfInfo->sShiftLengthBytes,
1412 &(pXsvfInfo->lvTdi),
1413 /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1414 /*plvTdoMask*/0, ucEndDR,
1415 /*lRunTestTime*/0, /*ucMaxRepeat*/0 );
1416 if ( iErrorCode != XSVF_ERROR_NONE )
1418 pXsvfInfo->iErrorCode = iErrorCode;
1420 return( iErrorCode );
1423 /*****************************************************************************
1424 * Function: xsvfDoXSDRTDOBCE
1425 * Description: XSDRB/XSDRC/XSDRE <lenVal.TDI[XSDRSIZE]> <lenVal.TDO[XSDRSIZE]>
1426 * If not already in SHIFTDR, goto SHIFTDR.
1427 * Shift the given TDI data into the JTAG scan chain.
1428 * Compare TDO, but do NOT use XTDOMASK.
1429 * If cmd==XSDRTDOE, then goto ENDDR. Otherwise, stay in ShiftDR.
1430 * XSDRTDOB, XSDRTDOC, and XSDRTDOE are the same implementation.
1431 * Parameters: pXsvfInfo - XSVF information pointer.
1432 * Returns: int - 0 = success; non-zero = error.
1433 *****************************************************************************/
1434 int xsvfDoXSDRTDOBCE( SXsvfInfo* pXsvfInfo )
1436 unsigned char ucEndDR;
1438 ucEndDR = (unsigned char)(( pXsvfInfo->ucCommand == XSDRTDOE ) ?
1439 pXsvfInfo->ucEndDR : XTAPSTATE_SHIFTDR);
1440 iErrorCode = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1441 pXsvfInfo->lShiftLengthBits,
1442 pXsvfInfo->sShiftLengthBytes,
1443 &(pXsvfInfo->lvTdi),
1444 &(pXsvfInfo->lvTdoCaptured),
1445 &(pXsvfInfo->lvTdoExpected),
1446 /*plvTdoMask*/0, ucEndDR,
1447 /*lRunTestTime*/0, /*ucMaxRepeat*/0 );
1448 if ( iErrorCode != XSVF_ERROR_NONE )
1450 pXsvfInfo->iErrorCode = iErrorCode;
1452 return( iErrorCode );
1455 /*****************************************************************************
1456 * Function: xsvfDoXSTATE
1457 * Description: XSTATE <byte>
1458 * <byte> == XTAPSTATE;
1459 * Get the state parameter and transition the TAP to that state.
1460 * Parameters: pXsvfInfo - XSVF information pointer.
1461 * Returns: int - 0 = success; non-zero = error.
1462 *****************************************************************************/
1463 int xsvfDoXSTATE( SXsvfInfo* pXsvfInfo )
1465 unsigned char ucNextState;
1467 readByte( &ucNextState );
1468 iErrorCode = xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucNextState );
1469 if ( iErrorCode != XSVF_ERROR_NONE )
1471 pXsvfInfo->iErrorCode = iErrorCode;
1473 return( iErrorCode );
1476 /*****************************************************************************
1477 * Function: xsvfDoXENDXR
1478 * Description: XENDIR/XENDDR <byte>
1479 * <byte>: 0 = RUNTEST; 1 = PAUSE.
1480 * Get the prespecified XENDIR or XENDDR.
1481 * Both XENDIR and XENDDR use the same implementation.
1482 * Parameters: pXsvfInfo - XSVF information pointer.
1483 * Returns: int - 0 = success; non-zero = error.
1484 *****************************************************************************/
1485 int xsvfDoXENDXR( SXsvfInfo* pXsvfInfo )
1488 unsigned char ucEndState;
1490 iErrorCode = XSVF_ERROR_NONE;
1491 readByte( &ucEndState );
1492 if ( ( ucEndState != XENDXR_RUNTEST ) && ( ucEndState != XENDXR_PAUSE ) )
1494 iErrorCode = XSVF_ERROR_ILLEGALSTATE;
1499 if ( pXsvfInfo->ucCommand == XENDIR )
1501 if ( ucEndState == XENDXR_RUNTEST )
1503 pXsvfInfo->ucEndIR = XTAPSTATE_RUNTEST;
1507 pXsvfInfo->ucEndIR = XTAPSTATE_PAUSEIR;
1509 XSVFDBG_PRINTF1( 3, " ENDIR State = %s\n",
1510 xsvf_pzTapState[ pXsvfInfo->ucEndIR ] );
1514 if ( ucEndState == XENDXR_RUNTEST )
1516 pXsvfInfo->ucEndDR = XTAPSTATE_RUNTEST;
1520 pXsvfInfo->ucEndDR = XTAPSTATE_PAUSEDR;
1522 XSVFDBG_PRINTF1( 3, " ENDDR State = %s\n",
1523 xsvf_pzTapState[ pXsvfInfo->ucEndDR ] );
1527 if ( iErrorCode != XSVF_ERROR_NONE )
1529 pXsvfInfo->iErrorCode = iErrorCode;
1531 return( iErrorCode );
1534 /*****************************************************************************
1535 * Function: xsvfDoXCOMMENT
1536 * Description: XCOMMENT <text string ending in \0>
1537 * <text string ending in \0> == text comment;
1538 * Arbitrary comment embedded in the XSVF.
1539 * Parameters: pXsvfInfo - XSVF information pointer.
1540 * Returns: int - 0 = success; non-zero = error.
1541 *****************************************************************************/
1542 int xsvfDoXCOMMENT( SXsvfInfo* pXsvfInfo )
1544 /* Use the comment for debugging */
1545 /* Otherwise, read through the comment to the end '\0' and ignore */
1546 unsigned char ucText;
1548 if ( xsvf_iDebugLevel > 0 )
1555 readByte( &ucText );
1556 if ( xsvf_iDebugLevel > 0 )
1558 putc( ucText ? ucText : '\n' );
1562 pXsvfInfo->iErrorCode = XSVF_ERROR_NONE;
1564 return( pXsvfInfo->iErrorCode );
1567 /*****************************************************************************
1568 * Function: xsvfDoXWAIT
1569 * Description: XWAIT <wait_state> <end_state> <wait_time>
1570 * If not already in <wait_state>, then go to <wait_state>.
1571 * Wait in <wait_state> for <wait_time> microseconds.
1572 * Finally, if not already in <end_state>, then goto <end_state>.
1573 * Parameters: pXsvfInfo - XSVF information pointer.
1574 * Returns: int - 0 = success; non-zero = error.
1575 *****************************************************************************/
1576 int xsvfDoXWAIT( SXsvfInfo* pXsvfInfo )
1578 unsigned char ucWaitState;
1579 unsigned char ucEndState;
1582 /* Get Parameters */
1584 readVal( &(pXsvfInfo->lvTdi), 1 );
1585 ucWaitState = pXsvfInfo->lvTdi.val[0];
1588 readVal( &(pXsvfInfo->lvTdi), 1 );
1589 ucEndState = pXsvfInfo->lvTdi.val[0];
1592 readVal( &(pXsvfInfo->lvTdi), 4 );
1593 lWaitTime = value( &(pXsvfInfo->lvTdi) );
1594 XSVFDBG_PRINTF2( 3, " XWAIT: state = %s; time = %ld\n",
1595 xsvf_pzTapState[ ucWaitState ], lWaitTime );
1597 /* If not already in <wait_state>, go to <wait_state> */
1598 if ( pXsvfInfo->ucTapState != ucWaitState )
1600 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucWaitState );
1603 /* Wait for <wait_time> microseconds */
1604 waitTime( lWaitTime );
1606 /* If not already in <end_state>, go to <end_state> */
1607 if ( pXsvfInfo->ucTapState != ucEndState )
1609 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucEndState );
1612 return( XSVF_ERROR_NONE );
1616 /*============================================================================
1617 * Execution Control Functions
1618 ============================================================================*/
1620 /*****************************************************************************
1621 * Function: xsvfInitialize
1622 * Description: Initialize the xsvf player.
1623 * Call this before running the player to initialize the data
1624 * in the SXsvfInfo struct.
1625 * xsvfCleanup is called to clean up the data in SXsvfInfo
1626 * after the XSVF is played.
1627 * Parameters: pXsvfInfo - ptr to the XSVF information.
1628 * Returns: int - 0 = success; otherwise error.
1629 *****************************************************************************/
1630 int xsvfInitialize( SXsvfInfo* pXsvfInfo )
1632 /* Initialize values */
1633 pXsvfInfo->iErrorCode = xsvfInfoInit( pXsvfInfo );
1635 if ( !pXsvfInfo->iErrorCode )
1637 /* Initialize the TAPs */
1638 pXsvfInfo->iErrorCode = xsvfGotoTapState( &(pXsvfInfo->ucTapState),
1642 return( pXsvfInfo->iErrorCode );
1645 /*****************************************************************************
1647 * Description: Run the xsvf player for a single command and return.
1648 * First, call xsvfInitialize.
1649 * Then, repeatedly call this function until an error is detected
1650 * or until the pXsvfInfo->ucComplete variable is non-zero.
1651 * Finally, call xsvfCleanup to cleanup any remnants.
1652 * Parameters: pXsvfInfo - ptr to the XSVF information.
1653 * Returns: int - 0 = success; otherwise error.
1654 *****************************************************************************/
1655 int xsvfRun( SXsvfInfo* pXsvfInfo )
1657 /* Process the XSVF commands */
1658 if ( (!pXsvfInfo->iErrorCode) && (!pXsvfInfo->ucComplete) )
1660 /* read 1 byte for the instruction */
1661 readByte( &(pXsvfInfo->ucCommand) );
1662 ++(pXsvfInfo->lCommandCount);
1664 if ( pXsvfInfo->ucCommand < XLASTCMD )
1666 /* Execute the command. Func sets error code. */
1667 XSVFDBG_PRINTF1( 2, " %s\n",
1668 xsvf_pzCommandName[pXsvfInfo->ucCommand] );
1669 /* If your compiler cannot take this form,
1670 then convert to a switch statement */
1671 #if 0 /* test-only */
1672 xsvf_pfDoCmd[ pXsvfInfo->ucCommand ]( pXsvfInfo );
1674 switch (pXsvfInfo->ucCommand) {
1676 xsvfDoXCOMPLETE(pXsvfInfo); /* 0 */
1679 xsvfDoXTDOMASK(pXsvfInfo); /* 1 */
1682 xsvfDoXSIR(pXsvfInfo); /* 2 */
1685 xsvfDoXSDR(pXsvfInfo); /* 3 */
1688 xsvfDoXRUNTEST(pXsvfInfo); /* 4 */
1691 xsvfDoIllegalCmd(pXsvfInfo); /* 5 */
1694 xsvfDoIllegalCmd(pXsvfInfo); /* 6 */
1697 xsvfDoXREPEAT(pXsvfInfo); /* 7 */
1700 xsvfDoXSDRSIZE(pXsvfInfo); /* 8 */
1703 xsvfDoXSDRTDO(pXsvfInfo); /* 9 */
1705 #ifdef XSVF_SUPPORT_COMPRESSION
1707 xsvfDoXSETSDRMASKS(pXsvfInfo); /* 10 */
1710 xsvfDoXSDRINC(pXsvfInfo); /* 11 */
1714 xsvfDoIllegalCmd(pXsvfInfo); /* 10 */
1717 xsvfDoIllegalCmd(pXsvfInfo); /* 11 */
1719 #endif /* XSVF_SUPPORT_COMPRESSION */
1721 xsvfDoXSDRBCE(pXsvfInfo); /* 12 */
1724 xsvfDoXSDRBCE(pXsvfInfo); /* 13 */
1727 xsvfDoXSDRBCE(pXsvfInfo); /* 14 */
1730 xsvfDoXSDRTDOBCE(pXsvfInfo); /* 15 */
1733 xsvfDoXSDRTDOBCE(pXsvfInfo); /* 16 */
1736 xsvfDoXSDRTDOBCE(pXsvfInfo); /* 17 */
1739 xsvfDoXSTATE(pXsvfInfo); /* 18 */
1742 xsvfDoXENDXR(pXsvfInfo); /* 19 */
1745 xsvfDoXENDXR(pXsvfInfo); /* 20 */
1748 xsvfDoXSIR2(pXsvfInfo); /* 21 */
1751 xsvfDoXCOMMENT(pXsvfInfo); /* 22 */
1754 xsvfDoXWAIT(pXsvfInfo); /* 23 */
1761 /* Illegal command value. Func sets error code. */
1762 xsvfDoIllegalCmd( pXsvfInfo );
1766 return( pXsvfInfo->iErrorCode );
1769 /*****************************************************************************
1770 * Function: xsvfCleanup
1771 * Description: cleanup remnants of the xsvf player.
1772 * Parameters: pXsvfInfo - ptr to the XSVF information.
1774 *****************************************************************************/
1775 void xsvfCleanup( SXsvfInfo* pXsvfInfo )
1777 xsvfInfoCleanup( pXsvfInfo );
1781 /*============================================================================
1782 * xsvfExecute() - The primary entry point to the XSVF player
1783 ============================================================================*/
1785 /*****************************************************************************
1786 * Function: xsvfExecute
1787 * Description: Process, interpret, and apply the XSVF commands.
1788 * See port.c:readByte for source of XSVF data.
1790 * Returns: int - Legacy result values: 1 == success; 0 == failed.
1791 *****************************************************************************/
1792 int xsvfExecute(void)
1796 xsvfInitialize( &xsvfInfo );
1798 while ( !xsvfInfo.iErrorCode && (!xsvfInfo.ucComplete) )
1800 xsvfRun( &xsvfInfo );
1803 if ( xsvfInfo.iErrorCode )
1805 XSVFDBG_PRINTF1( 0, "%s\n", xsvf_pzErrorName[
1806 ( xsvfInfo.iErrorCode < XSVF_ERROR_LAST )
1807 ? xsvfInfo.iErrorCode : XSVF_ERROR_UNKNOWN ] );
1808 XSVFDBG_PRINTF2( 0, "ERROR at or near XSVF command #%ld. See line #%ld in the XSVF ASCII file.\n",
1809 xsvfInfo.lCommandCount, xsvfInfo.lCommandCount );
1813 XSVFDBG_PRINTF( 0, "SUCCESS - Completed XSVF execution.\n" );
1816 xsvfCleanup( &xsvfInfo );
1818 return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) );
1822 /*****************************************************************************
1824 * Description: main function.
1825 * Specified here for creating stand-alone debug executable.
1826 * Embedded users should call xsvfExecute() directly.
1827 * Parameters: iArgc - number of command-line arguments.
1828 * ppzArgv - array of ptrs to strings (command-line arguments).
1829 * Returns: int - Legacy return value: 1 = success; 0 = error.
1830 *****************************************************************************/
1831 int do_cpld(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1834 char* pzXsvfFileName;
1835 unsigned long duration;
1836 unsigned long long startClock, endClock;
1839 xsvfdata = (unsigned char *)simple_strtoul(argv[1], NULL, 16);
1841 #ifdef CONFIG_SYS_XSVF_DEFAULT_ADDR
1842 xsvfdata = (unsigned char *)CONFIG_SYS_XSVF_DEFAULT_ADDR;
1844 printf("Usage:\ncpld %s\n", cmdtp->help);
1849 iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE );
1851 xsvf_iDebugLevel = 0;
1853 printf("XSVF Player v%s, Xilinx, Inc.\n", XSVF_VERSION);
1854 printf("Reading XSVF data @ %p\n", xsvfdata);
1856 /* Initialize the I/O. SetPort initializes I/O on first call */
1859 /* Execute the XSVF in the file */
1860 startClock = get_ticks();
1861 iErrorCode = xsvfExecute();
1862 endClock = get_ticks();
1863 duration = (unsigned long)(endClock - startClock);
1864 printf("\nExecution Time = %d seconds\n", (int)(duration/get_tbclk()));
1866 return( iErrorCode );
1869 cpld, 2, 1, do_cpld,
1870 "program onboard CPLD",