]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/esd/common/xilinx_jtag/micro.c
imported Freescale specific U-Boot additions for i.MX28,... release L2.6.31_10.08.01
[karo-tx-uboot.git] / board / esd / common / xilinx_jtag / micro.c
1 /*
2  * (C) Copyright 2003
3  * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 /*****************************************************************************
25  * file:         micro.c
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.
58  *                         Added XWAIT.
59  *****************************************************************************/
60
61 #include <common.h>
62 #include <command.h>
63 #include <asm/processor.h>
64
65 #include "micro.h"
66 #include "lenval.h"
67 #include "ports.h"
68
69 const unsigned char *xsvfdata;
70
71 /*============================================================================
72  * XSVF #define
73  ============================================================================*/
74
75 #define XSVF_VERSION    "5.00"
76
77 /*****************************************************************************
78  * Define:       XSVF_SUPPORT_COMPRESSION
79  * Description:  Define this to support the XC9500/XL XSVF data compression
80  *               scheme.
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
88 #endif
89
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
98 #endif
99
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 */
105
106
107 /*============================================================================
108  * DEBUG_MODE #define
109  ============================================================================*/
110 #define DEBUG_MODE
111
112 #ifdef  DEBUG_MODE
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 */
135
136
137 /*============================================================================
138  * XSVF Type Declarations
139  ============================================================================*/
140
141 /*****************************************************************************
142  * Struct:       SXsvfInfo
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
156  *               in this struct.
157  *               xsvfCleanup() contains cleanup code for the data in this
158  *               struct.
159  *****************************************************************************/
160 typedef struct tagSXsvfInfo
161 {
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. */
167
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) */
172
173         /* RUNTEST information */
174         unsigned char   ucMaxRepeat;        /* Max repeat loops (for xc9500/xl) */
175         long            lRunTestTime;       /* Pre-specified RUNTEST time (usec) */
176
177         /* Shift Data Info and Buffers */
178         long            lShiftLengthBits;   /* Len. current shift data in bits */
179         short           sShiftLengthBytes;  /* Len. current shift data in bytes */
180
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 */
185
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 */
192 } SXsvfInfo;
193
194 /* Declare pointer to functions that perform XSVF commands */
195 typedef int (*TXsvfDoCmdFuncPtr)( SXsvfInfo* );
196
197 /*============================================================================
198  * XSVF Command Bytes
199  ============================================================================*/
200
201 /* encodings of xsvf instructions */
202 #define XCOMPLETE        0
203 #define XTDOMASK         1
204 #define XSIR             2
205 #define XSDR             3
206 #define XRUNTEST         4
207 /* Reserved              5 */
208 /* Reserved              6 */
209 #define XREPEAT          7
210 #define XSDRSIZE         8
211 #define XSDRTDO          9
212 #define XSETSDRMASKS     10
213 #define XSDRINC          11
214 #define XSDRB            12
215 #define XSDRC            13
216 #define XSDRE            14
217 #define XSDRTDOB         15
218 #define XSDRTDOC         16
219 #define XSDRTDOE         17
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 */
229
230
231 /*============================================================================
232  * XSVF Command Parameter Values
233  ============================================================================*/
234
235 #define XSTATE_RESET     0          /* 4.00 parameter for XSTATE */
236 #define XSTATE_RUNTEST   1          /* 4.00 parameter for XSTATE */
237
238 #define XENDXR_RUNTEST   0          /* 4.04 parameter for XENDIR/DR */
239 #define XENDXR_PAUSE     1          /* 4.04 parameter for XENDIR/DR */
240
241 /* TAP states */
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
259
260 /*============================================================================
261  * XSVF Function Prototypes
262  ============================================================================*/
263
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 */
283
284 /*============================================================================
285  * XSVF Global Variables
286  ============================================================================*/
287
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[]  =
291 {
292         xsvfDoXCOMPLETE,        /*  0 */
293         xsvfDoXTDOMASK,         /*  1 */
294         xsvfDoXSIR,             /*  2 */
295         xsvfDoXSDR,             /*  3 */
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 */
305 #else
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 */
320         xsvfDoXWAIT             /* 23 */
321 /* Insert new command functions here */
322 };
323
324 #ifdef  DEBUG_MODE
325 char* xsvf_pzCommandName[]  =
326 {
327         "XCOMPLETE",
328         "XTDOMASK",
329         "XSIR",
330         "XSDR",
331         "XRUNTEST",
332         "Reserved5",
333         "Reserved6",
334         "XREPEAT",
335         "XSDRSIZE",
336         "XSDRTDO",
337         "XSETSDRMASKS",
338         "XSDRINC",
339         "XSDRB",
340         "XSDRC",
341         "XSDRE",
342         "XSDRTDOB",
343         "XSDRTDOC",
344         "XSDRTDOE",
345         "XSTATE",
346         "XENDIR",
347         "XENDDR",
348         "XSIR2",
349         "XCOMMENT",
350         "XWAIT"
351 };
352
353 char*   xsvf_pzErrorName[]  =
354 {
355         "No error",
356         "ERROR:  Unknown",
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"
362 };
363
364 char*   xsvf_pzTapState[] =
365 {
366         "RESET",        /* 0x00 */
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 */
382 };
383 #endif  /* DEBUG_MODE */
384
385 /*#ifdef DEBUG_MODE     */
386 /*    FILE* in;   /XXX* Legacy DEBUG_MODE file pointer */
387 int xsvf_iDebugLevel;
388 /*#endif /XXX* DEBUG_MODE */
389
390 /*============================================================================
391  * Utility Functions
392  ============================================================================*/
393
394 /*****************************************************************************
395  * Function:     xsvfPrintLenVal
396  * Description:  Print the lenval value in hex.
397  * Parameters:   plv     - ptr to lenval.
398  * Returns:      void.
399  *****************************************************************************/
400 #ifdef  DEBUG_MODE
401 void xsvfPrintLenVal( lenVal *plv )
402 {
403         int i;
404
405         if ( plv )
406         {
407                 printf( "0x" );
408                 for ( i = 0; i < plv->len; ++i )
409                 {
410                         printf( "%02x", ((unsigned int)(plv->val[ i ])) );
411                 }
412         }
413 }
414 #endif  /* DEBUG_MODE */
415
416
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 )
424 {
425         XSVFDBG_PRINTF1( 4, "    sizeof( SXsvfInfo ) = %d bytes\n",
426                          sizeof( SXsvfInfo ) );
427
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;
439
440         return( 0 );
441 }
442
443 /*****************************************************************************
444  * Function:     xsvfInfoCleanup
445  * Description:  Cleanup the xsvfInfo data.
446  * Parameters:   pXsvfInfo   - ptr to the XSVF info structure.
447  * Returns:      void.
448  *****************************************************************************/
449 void xsvfInfoCleanup( SXsvfInfo* pXsvfInfo )
450 {
451 }
452
453 /*****************************************************************************
454  * Function:     xsvfGetAsNumBytes
455  * Description:  Calculate the number of bytes the given number of bits
456  *               consumes.
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 )
461 {
462         return( (short)( ( lNumBits + 7L ) / 8L ) );
463 }
464
465 /*****************************************************************************
466  * Function:     xsvfTmsTransition
467  * Description:  Apply TMS and transition TAP controller by applying one TCK
468  *               cycle.
469  * Parameters:   sTms    - new TMS value.
470  * Returns:      void.
471  *****************************************************************************/
472 void xsvfTmsTransition( short sTms )
473 {
474         setPort( TMS, sTms );
475         setPort( TCK, 0 );
476         setPort( TCK, 1 );
477 }
478
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 )
494 {
495         int i;
496         int iErrorCode;
497
498         iErrorCode  = XSVF_ERROR_NONE;
499         if ( ucTargetState == XTAPSTATE_RESET )
500         {
501                 /* If RESET, always perform TMS reset sequence to reset/sync TAPs */
502                 xsvfTmsTransition( 1 );
503                 for ( i = 0; i < 5; ++i )
504                 {
505                         setPort( TCK, 0 );
506                         setPort( TCK, 1 );
507                 }
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 ) ) ) )
515         {
516                 /* Trap illegal TAP state path specification */
517                 iErrorCode      = XSVF_ERROR_ILLEGALSTATE;
518         } else {
519                 if ( ucTargetState == *pucTapState )
520                 {
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 )
524                         {
525                                 xsvfTmsTransition( 1 );
526                                 *pucTapState    = XTAPSTATE_EXIT2DR;
527                                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
528                                                  xsvf_pzTapState[ *pucTapState ] );
529                         }
530                         else if ( ucTargetState == XTAPSTATE_PAUSEIR )
531                         {
532                                 xsvfTmsTransition( 1 );
533                                 *pucTapState    = XTAPSTATE_EXIT2IR;
534                                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
535                                                  xsvf_pzTapState[ *pucTapState ] );
536                         }
537                 }
538
539                 /* Perform TAP state transitions to get to the target state */
540                 while ( ucTargetState != *pucTapState )
541                 {
542                         switch ( *pucTapState )
543                         {
544                         case XTAPSTATE_RESET:
545                                 xsvfTmsTransition( 0 );
546                                 *pucTapState    = XTAPSTATE_RUNTEST;
547                                 break;
548                         case XTAPSTATE_RUNTEST:
549                                 xsvfTmsTransition( 1 );
550                                 *pucTapState    = XTAPSTATE_SELECTDR;
551                                 break;
552                         case XTAPSTATE_SELECTDR:
553                                 if ( ucTargetState >= XTAPSTATE_IRSTATES )
554                                 {
555                                         xsvfTmsTransition( 1 );
556                                         *pucTapState    = XTAPSTATE_SELECTIR;
557                                 }
558                                 else
559                                 {
560                                         xsvfTmsTransition( 0 );
561                                         *pucTapState    = XTAPSTATE_CAPTUREDR;
562                                 }
563                                 break;
564                         case XTAPSTATE_CAPTUREDR:
565                                 if ( ucTargetState == XTAPSTATE_SHIFTDR )
566                                 {
567                                         xsvfTmsTransition( 0 );
568                                         *pucTapState    = XTAPSTATE_SHIFTDR;
569                                 }
570                                 else
571                                 {
572                                         xsvfTmsTransition( 1 );
573                                         *pucTapState    = XTAPSTATE_EXIT1DR;
574                                 }
575                                 break;
576                         case XTAPSTATE_SHIFTDR:
577                                 xsvfTmsTransition( 1 );
578                                 *pucTapState    = XTAPSTATE_EXIT1DR;
579                                 break;
580                         case XTAPSTATE_EXIT1DR:
581                                 if ( ucTargetState == XTAPSTATE_PAUSEDR )
582                                 {
583                                         xsvfTmsTransition( 0 );
584                                         *pucTapState    = XTAPSTATE_PAUSEDR;
585                                 }
586                                 else
587                                 {
588                                         xsvfTmsTransition( 1 );
589                                         *pucTapState    = XTAPSTATE_UPDATEDR;
590                                 }
591                                 break;
592                         case XTAPSTATE_PAUSEDR:
593                                 xsvfTmsTransition( 1 );
594                                 *pucTapState    = XTAPSTATE_EXIT2DR;
595                                 break;
596                         case XTAPSTATE_EXIT2DR:
597                                 if ( ucTargetState == XTAPSTATE_SHIFTDR )
598                                 {
599                                         xsvfTmsTransition( 0 );
600                                         *pucTapState    = XTAPSTATE_SHIFTDR;
601                                 }
602                                 else
603                                 {
604                                         xsvfTmsTransition( 1 );
605                                         *pucTapState    = XTAPSTATE_UPDATEDR;
606                                 }
607                                 break;
608                         case XTAPSTATE_UPDATEDR:
609                                 if ( ucTargetState == XTAPSTATE_RUNTEST )
610                                 {
611                                         xsvfTmsTransition( 0 );
612                                         *pucTapState    = XTAPSTATE_RUNTEST;
613                                 }
614                                 else
615                                 {
616                                         xsvfTmsTransition( 1 );
617                                         *pucTapState    = XTAPSTATE_SELECTDR;
618                                 }
619                                 break;
620                         case XTAPSTATE_SELECTIR:
621                                 xsvfTmsTransition( 0 );
622                                 *pucTapState    = XTAPSTATE_CAPTUREIR;
623                                 break;
624                         case XTAPSTATE_CAPTUREIR:
625                                 if ( ucTargetState == XTAPSTATE_SHIFTIR )
626                                 {
627                                         xsvfTmsTransition( 0 );
628                                         *pucTapState    = XTAPSTATE_SHIFTIR;
629                                 }
630                                 else
631                                 {
632                                         xsvfTmsTransition( 1 );
633                                         *pucTapState    = XTAPSTATE_EXIT1IR;
634                                 }
635                                 break;
636                         case XTAPSTATE_SHIFTIR:
637                                 xsvfTmsTransition( 1 );
638                                 *pucTapState    = XTAPSTATE_EXIT1IR;
639                                 break;
640                         case XTAPSTATE_EXIT1IR:
641                                 if ( ucTargetState == XTAPSTATE_PAUSEIR )
642                                 {
643                                         xsvfTmsTransition( 0 );
644                                         *pucTapState    = XTAPSTATE_PAUSEIR;
645                                 }
646                                 else
647                                 {
648                                         xsvfTmsTransition( 1 );
649                                         *pucTapState    = XTAPSTATE_UPDATEIR;
650                                 }
651                                 break;
652                         case XTAPSTATE_PAUSEIR:
653                                 xsvfTmsTransition( 1 );
654                                 *pucTapState    = XTAPSTATE_EXIT2IR;
655                                 break;
656                         case XTAPSTATE_EXIT2IR:
657                                 if ( ucTargetState == XTAPSTATE_SHIFTIR )
658                                 {
659                                         xsvfTmsTransition( 0 );
660                                         *pucTapState    = XTAPSTATE_SHIFTIR;
661                                 }
662                                 else
663                                 {
664                                         xsvfTmsTransition( 1 );
665                                         *pucTapState    = XTAPSTATE_UPDATEIR;
666                                 }
667                                 break;
668                         case XTAPSTATE_UPDATEIR:
669                                 if ( ucTargetState == XTAPSTATE_RUNTEST )
670                                 {
671                                         xsvfTmsTransition( 0 );
672                                         *pucTapState    = XTAPSTATE_RUNTEST;
673                                 }
674                                 else
675                                 {
676                                         xsvfTmsTransition( 1 );
677                                         *pucTapState    = XTAPSTATE_SELECTDR;
678                                 }
679                                 break;
680                         default:
681                                 iErrorCode      = XSVF_ERROR_ILLEGALSTATE;
682                                 *pucTapState    = ucTargetState;    /* Exit while loop */
683                                 break;
684                         }
685                         XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
686                                          xsvf_pzTapState[ *pucTapState ] );
687                 }
688         }
689
690         return( iErrorCode );
691 }
692
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.
705  * Returns:      void.
706  *****************************************************************************/
707 void xsvfShiftOnly( long    lNumBits,
708                     lenVal* plvTdi,
709                     lenVal* plvTdoCaptured,
710                     int     iExitShift )
711 {
712         unsigned char*  pucTdi;
713         unsigned char*  pucTdo;
714         unsigned char   ucTdiByte;
715         unsigned char   ucTdoByte;
716         unsigned char   ucTdoBit;
717         int             i;
718
719         /* assert( ( ( lNumBits + 7 ) / 8 ) == plvTdi->len ); */
720
721         /* Initialize TDO storage len == TDI len */
722         pucTdo  = 0;
723         if ( plvTdoCaptured )
724         {
725                 plvTdoCaptured->len = plvTdi->len;
726                 pucTdo              = plvTdoCaptured->val + plvTdi->len;
727         }
728
729         /* Shift LSB first.  val[N-1] == LSB.  val[0] == MSB. */
730         pucTdi  = plvTdi->val + plvTdi->len;
731         while ( lNumBits )
732         {
733                 /* Process on a byte-basis */
734                 ucTdiByte   = (*(--pucTdi));
735                 ucTdoByte   = 0;
736                 for ( i = 0; ( lNumBits && ( i < 8 ) ); ++i )
737                 {
738                         --lNumBits;
739                         if ( iExitShift && !lNumBits )
740                         {
741                                 /* Exit Shift-DR state */
742                                 setPort( TMS, 1 );
743                         }
744
745                         /* Set the new TDI value */
746                         setPort( TDI, (short)(ucTdiByte & 1) );
747                         ucTdiByte   >>= 1;
748
749                         /* Set TCK low */
750                         setPort( TCK, 0 );
751
752                         if ( pucTdo )
753                         {
754                                 /* Save the TDO value */
755                                 ucTdoBit    = readTDOBit();
756                                 ucTdoByte   |= ( ucTdoBit << i );
757                         }
758
759                         /* Set TCK high */
760                         setPort( TCK, 1 );
761                 }
762
763                 /* Save the TDO byte value */
764                 if ( pucTdo )
765                 {
766                         (*(--pucTdo))   = ucTdoByte;
767                 }
768         }
769 }
770
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
777  *               data.
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,
797                long             lNumBits,
798                lenVal*          plvTdi,
799                lenVal*          plvTdoCaptured,
800                lenVal*          plvTdoExpected,
801                lenVal*          plvTdoMask,
802                unsigned char    ucEndState,
803                long             lRunTestTime,
804                unsigned char    ucMaxRepeat )
805 {
806         int             iErrorCode;
807         int             iMismatch;
808         unsigned char   ucRepeat;
809         int             iExitShift;
810
811         iErrorCode  = XSVF_ERROR_NONE;
812         iMismatch   = 0;
813         ucRepeat    = 0;
814         iExitShift  = ( ucStartState != ucEndState );
815
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");
823
824         if ( !lNumBits )
825         {
826                 /* Compatibility with XSVF2.00:  XSDR 0 = no shift, but wait in RTI */
827                 if ( lRunTestTime )
828                 {
829                         /* Wait for prespecified XRUNTEST time */
830                         xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
831                         XSVFDBG_PRINTF1( 3, "   Wait = %ld usec\n", lRunTestTime );
832                         waitTime( lRunTestTime );
833                 }
834         }
835         else
836         {
837                 do
838                 {
839                         /* Goto Shift-DR or Shift-IR */
840                         xsvfGotoTapState( pucTapState, ucStartState );
841
842                         /* Shift TDI and capture TDO */
843                         xsvfShiftOnly( lNumBits, plvTdi, plvTdoCaptured, iExitShift );
844
845                         if ( plvTdoExpected )
846                         {
847                                 /* Compare TDO data to expected TDO data */
848                                 iMismatch   = !EqualLenVal( plvTdoExpected,
849                                                             plvTdoCaptured,
850                                                             plvTdoMask );
851                         }
852
853                         if ( iExitShift )
854                         {
855                                 /* Update TAP state:  Shift->Exit */
856                                 ++(*pucTapState);
857                                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
858                                                  xsvf_pzTapState[ *pucTapState ] );
859
860                                 if ( iMismatch && lRunTestTime && ( ucRepeat < ucMaxRepeat ) )
861                                 {
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 );
878                                 }
879                                 else
880                                 {
881                                         /* Do normal exit from Shift-XR */
882                                         xsvfGotoTapState( pucTapState, ucEndState );
883                                 }
884
885                                 if ( lRunTestTime )
886                                 {
887                                         /* Wait for prespecified XRUNTEST time */
888                                         xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
889                                         XSVFDBG_PRINTF1( 3, "   Wait = %ld usec\n", lRunTestTime );
890                                         waitTime( lRunTestTime );
891                                 }
892                         }
893                 } while ( iMismatch && ( ucRepeat++ < ucMaxRepeat ) );
894         }
895
896         if ( iMismatch )
897         {
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 ) )
908                 {
909                         iErrorCode  = XSVF_ERROR_MAXRETRIES;
910                 }
911                 else
912                 {
913                         iErrorCode  = XSVF_ERROR_TDOMISMATCH;
914                 }
915         }
916
917         return( iErrorCode );
918 }
919
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,
937                       lenVal*           plvTdi,
938                       lenVal*           plvTdoCaptured,
939                       lenVal*           plvTdoExpected,
940                       lenVal*           plvTdoMask,
941                       unsigned char     ucEndState,
942                       long              lRunTestTime,
943                       unsigned char     ucMaxRepeat )
944 {
945         readVal( plvTdi, sShiftLengthBytes );
946         if ( plvTdoExpected )
947         {
948                 readVal( plvTdoExpected, sShiftLengthBytes );
949         }
950         return( xsvfShift( pucTapState, XTAPSTATE_SHIFTDR, lShiftLengthBits,
951                            plvTdi, plvTdoCaptured, plvTdoExpected, plvTdoMask,
952                            ucEndState, lRunTestTime, ucMaxRepeat ) );
953 }
954
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.
964  * Returns:      void.
965  *****************************************************************************/
966 #ifdef  XSVF_SUPPORT_COMPRESSION
967 void xsvfDoSDRMasking( lenVal*  plvTdi,
968                        lenVal*  plvNextData,
969                        lenVal*  plvAddressMask,
970                        lenVal*  plvDataMask )
971 {
972         int             i;
973         unsigned char   ucTdi;
974         unsigned char   ucTdiMask;
975         unsigned char   ucDataMask;
976         unsigned char   ucNextData;
977         unsigned char   ucNextMask;
978         short           sNextData;
979
980         /* add the address Mask to dataVal and return as a new dataVal */
981         addVal( plvTdi, plvTdi, plvAddressMask );
982
983         ucNextData  = 0;
984         ucNextMask  = 0;
985         sNextData   = plvNextData->len;
986         for ( i = plvDataMask->len - 1; i >= 0; --i )
987         {
988                 /* Go through data mask in reverse order looking for mask (1) bits */
989                 ucDataMask  = plvDataMask->val[ i ];
990                 if ( ucDataMask )
991                 {
992                         /* Retrieve the corresponding TDI byte value */
993                         ucTdi       = plvTdi->val[ i ];
994
995                         /* For each bit in the data mask byte, look for 1's */
996                         ucTdiMask   = 1;
997                         while ( ucDataMask )
998                         {
999                                 if ( ucDataMask & 1 )
1000                                 {
1001                                         if ( !ucNextMask )
1002                                         {
1003                                                 /* Get the next data byte */
1004                                                 ucNextData  = plvNextData->val[ --sNextData ];
1005                                                 ucNextMask  = 1;
1006                                         }
1007
1008                                         /* Set or clear the data bit according to the next data */
1009                                         if ( ucNextData & ucNextMask )
1010                                         {
1011                                                 ucTdi   |= ucTdiMask;       /* Set bit */
1012                                         }
1013                                         else
1014                                         {
1015                                                 ucTdi   &= ( ~ucTdiMask );  /* Clear bit */
1016                                         }
1017
1018                                         /* Update the next data */
1019                                         ucNextMask  <<= 1;
1020                                 }
1021                                 ucTdiMask   <<= 1;
1022                                 ucDataMask  >>= 1;
1023                         }
1024
1025                         /* Update the TDI value */
1026                         plvTdi->val[ i ]    = ucTdi;
1027                 }
1028         }
1029 }
1030 #endif  /* XSVF_SUPPORT_COMPRESSION */
1031
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  ============================================================================*/
1038
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 )
1046 {
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])
1051                           : "Unknown") );
1052         pXsvfInfo->iErrorCode   = XSVF_ERROR_ILLEGALCMD;
1053         return( pXsvfInfo->iErrorCode );
1054 }
1055
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 )
1064 {
1065         pXsvfInfo->ucComplete   = 1;
1066         return( XSVF_ERROR_NONE );
1067 }
1068
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 )
1077 {
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 );
1083 }
1084
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 )
1095 {
1096         unsigned char   ucShiftIrBits;
1097         short           sShiftIrBytes;
1098         int             iErrorCode;
1099
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) );
1105
1106         if ( sShiftIrBytes > MAX_LEN )
1107         {
1108                 iErrorCode  = XSVF_ERROR_DATAOVERFLOW;
1109         }
1110         else
1111         {
1112                 /* Get and store instruction to shift in */
1113                 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( ucShiftIrBits ) );
1114
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 );
1121         }
1122
1123         if ( iErrorCode != XSVF_ERROR_NONE )
1124         {
1125                 pXsvfInfo->iErrorCode   = iErrorCode;
1126         }
1127         return( iErrorCode );
1128 }
1129
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 )
1140 {
1141         long            lShiftIrBits;
1142         short           sShiftIrBytes;
1143         int             iErrorCode;
1144
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);
1150
1151         if ( sShiftIrBytes > MAX_LEN )
1152         {
1153                 iErrorCode  = XSVF_ERROR_DATAOVERFLOW;
1154         }
1155         else
1156         {
1157                 /* Get and store instruction to shift in */
1158                 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( lShiftIrBits ) );
1159
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 );
1166         }
1167
1168         if ( iErrorCode != XSVF_ERROR_NONE )
1169         {
1170                 pXsvfInfo->iErrorCode   = iErrorCode;
1171         }
1172         return( iErrorCode );
1173 }
1174
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
1181  *               XTDOMASK.
1182  * Parameters:   pXsvfInfo   - XSVF information pointer.
1183  * Returns:      int         - 0 = success;  non-zero = error.
1184  *****************************************************************************/
1185 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo )
1186 {
1187         int iErrorCode;
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 )
1197         {
1198                 pXsvfInfo->iErrorCode   = iErrorCode;
1199         }
1200         return( iErrorCode );
1201 }
1202
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 )
1211 {
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 );
1216 }
1217
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 )
1226 {
1227         readByte( &(pXsvfInfo->ucMaxRepeat) );
1228         XSVFDBG_PRINTF1( 3, "   XREPEAT = %d\n",
1229                          ((unsigned int)(pXsvfInfo->ucMaxRepeat)) );
1230         return( XSVF_ERROR_NONE );
1231 }
1232
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 )
1241 {
1242         int iErrorCode;
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 )
1249         {
1250                 iErrorCode  = XSVF_ERROR_DATAOVERFLOW;
1251                 pXsvfInfo->iErrorCode   = iErrorCode;
1252         }
1253         return( iErrorCode );
1254 }
1255
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 )
1266 {
1267         int iErrorCode;
1268         iErrorCode  = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1269                                         pXsvfInfo->lShiftLengthBits,
1270                                         pXsvfInfo->sShiftLengthBytes,
1271                                         &(pXsvfInfo->lvTdi),
1272                                         &(pXsvfInfo->lvTdoCaptured),
1273                                         &(pXsvfInfo->lvTdoExpected),
1274                                         &(pXsvfInfo->lvTdoMask),
1275                                         pXsvfInfo->ucEndDR,
1276                                         pXsvfInfo->lRunTestTime,
1277                                         pXsvfInfo->ucMaxRepeat );
1278         if ( iErrorCode != XSVF_ERROR_NONE )
1279         {
1280                 pXsvfInfo->iErrorCode   = iErrorCode;
1281         }
1282         return( iErrorCode );
1283 }
1284
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
1290  *               command.
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 )
1297 {
1298         /* read the addressMask */
1299         readVal( &(pXsvfInfo->lvAddressMask), pXsvfInfo->sShiftLengthBytes );
1300         /* read the dataMask    */
1301         readVal( &(pXsvfInfo->lvDataMask), pXsvfInfo->sShiftLengthBytes );
1302
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" );
1309
1310         return( XSVF_ERROR_NONE );
1311 }
1312 #endif  /* XSVF_SUPPORT_COMPRESSION */
1313
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 )
1332 {
1333         int             iErrorCode;
1334         int             iDataMaskLen;
1335         unsigned char   ucDataMask;
1336         unsigned char   ucNumTimes;
1337         unsigned char   i;
1338
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 );
1346         if ( !iErrorCode )
1347         {
1348                 /* Calculate number of data mask bits */
1349                 iDataMaskLen    = 0;
1350                 for ( i = 0; i < pXsvfInfo->lvDataMask.len; ++i )
1351                 {
1352                         ucDataMask  = pXsvfInfo->lvDataMask.val[ i ];
1353                         while ( ucDataMask )
1354                         {
1355                                 iDataMaskLen    += ( ucDataMask & 1 );
1356                                 ucDataMask      >>= 1;
1357                         }
1358                 }
1359
1360                 /* Get the number of data pieces, i.e. number of times to shift */
1361                 readByte( &ucNumTimes );
1362
1363                 /* For numTimes, get data, fix TDI, and shift */
1364                 for ( i = 0; !iErrorCode && ( i < ucNumTimes ); ++i )
1365                 {
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),
1373                                                  XTAPSTATE_SHIFTDR,
1374                                                  pXsvfInfo->lShiftLengthBits,
1375                                                  &(pXsvfInfo->lvTdi),
1376                                                  &(pXsvfInfo->lvTdoCaptured),
1377                                                  &(pXsvfInfo->lvTdoExpected),
1378                                                  &(pXsvfInfo->lvTdoMask),
1379                                                  pXsvfInfo->ucEndDR,
1380                                                  pXsvfInfo->lRunTestTime,
1381                                                  pXsvfInfo->ucMaxRepeat );
1382                 }
1383         }
1384         if ( iErrorCode != XSVF_ERROR_NONE )
1385         {
1386                 pXsvfInfo->iErrorCode   = iErrorCode;
1387         }
1388         return( iErrorCode );
1389 }
1390 #endif  /* XSVF_SUPPORT_COMPRESSION */
1391
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.
1397  *               Ignore TDO.
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 )
1404 {
1405         unsigned char   ucEndDR;
1406         int             iErrorCode;
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 )
1417         {
1418                 pXsvfInfo->iErrorCode   = iErrorCode;
1419         }
1420         return( iErrorCode );
1421 }
1422
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 )
1435 {
1436         unsigned char   ucEndDR;
1437         int             iErrorCode;
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 )
1449         {
1450                 pXsvfInfo->iErrorCode   = iErrorCode;
1451         }
1452         return( iErrorCode );
1453 }
1454
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 )
1464 {
1465         unsigned char   ucNextState;
1466         int             iErrorCode;
1467         readByte( &ucNextState );
1468         iErrorCode  = xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucNextState );
1469         if ( iErrorCode != XSVF_ERROR_NONE )
1470         {
1471                 pXsvfInfo->iErrorCode   = iErrorCode;
1472         }
1473         return( iErrorCode );
1474 }
1475
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 )
1486 {
1487         int             iErrorCode;
1488         unsigned char   ucEndState;
1489
1490         iErrorCode  = XSVF_ERROR_NONE;
1491         readByte( &ucEndState );
1492         if ( ( ucEndState != XENDXR_RUNTEST ) && ( ucEndState != XENDXR_PAUSE ) )
1493         {
1494                 iErrorCode  = XSVF_ERROR_ILLEGALSTATE;
1495         }
1496         else
1497         {
1498
1499                 if ( pXsvfInfo->ucCommand == XENDIR )
1500                 {
1501                         if ( ucEndState == XENDXR_RUNTEST )
1502                         {
1503                                 pXsvfInfo->ucEndIR  = XTAPSTATE_RUNTEST;
1504                         }
1505                         else
1506                         {
1507                                 pXsvfInfo->ucEndIR  = XTAPSTATE_PAUSEIR;
1508                         }
1509                         XSVFDBG_PRINTF1( 3, "   ENDIR State = %s\n",
1510                                          xsvf_pzTapState[ pXsvfInfo->ucEndIR ] );
1511                 }
1512                 else    /* XENDDR */
1513                 {
1514                         if ( ucEndState == XENDXR_RUNTEST )
1515                         {
1516                                 pXsvfInfo->ucEndDR  = XTAPSTATE_RUNTEST;
1517                         }
1518                         else
1519                         {
1520                                 pXsvfInfo->ucEndDR  = XTAPSTATE_PAUSEDR;
1521                         }
1522                         XSVFDBG_PRINTF1( 3, "   ENDDR State = %s\n",
1523                                          xsvf_pzTapState[ pXsvfInfo->ucEndDR ] );
1524                 }
1525         }
1526
1527         if ( iErrorCode != XSVF_ERROR_NONE )
1528         {
1529                 pXsvfInfo->iErrorCode   = iErrorCode;
1530         }
1531         return( iErrorCode );
1532 }
1533
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 )
1543 {
1544         /* Use the comment for debugging */
1545         /* Otherwise, read through the comment to the end '\0' and ignore */
1546         unsigned char   ucText;
1547
1548         if ( xsvf_iDebugLevel > 0 )
1549         {
1550                 putc( ' ' );
1551         }
1552
1553         do
1554         {
1555                 readByte( &ucText );
1556                 if ( xsvf_iDebugLevel > 0 )
1557                 {
1558                         putc( ucText ? ucText : '\n' );
1559                 }
1560         } while ( ucText );
1561
1562         pXsvfInfo->iErrorCode   = XSVF_ERROR_NONE;
1563
1564         return( pXsvfInfo->iErrorCode );
1565 }
1566
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 )
1577 {
1578         unsigned char   ucWaitState;
1579         unsigned char   ucEndState;
1580         long            lWaitTime;
1581
1582         /* Get Parameters */
1583         /* <wait_state> */
1584         readVal( &(pXsvfInfo->lvTdi), 1 );
1585         ucWaitState = pXsvfInfo->lvTdi.val[0];
1586
1587         /* <end_state> */
1588         readVal( &(pXsvfInfo->lvTdi), 1 );
1589         ucEndState = pXsvfInfo->lvTdi.val[0];
1590
1591         /* <wait_time> */
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 );
1596
1597         /* If not already in <wait_state>, go to <wait_state> */
1598         if ( pXsvfInfo->ucTapState != ucWaitState )
1599         {
1600                 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucWaitState );
1601         }
1602
1603         /* Wait for <wait_time> microseconds */
1604         waitTime( lWaitTime );
1605
1606         /* If not already in <end_state>, go to <end_state> */
1607         if ( pXsvfInfo->ucTapState != ucEndState )
1608         {
1609                 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucEndState );
1610         }
1611
1612         return( XSVF_ERROR_NONE );
1613 }
1614
1615
1616 /*============================================================================
1617  * Execution Control Functions
1618  ============================================================================*/
1619
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 )
1631 {
1632         /* Initialize values */
1633         pXsvfInfo->iErrorCode   = xsvfInfoInit( pXsvfInfo );
1634
1635         if ( !pXsvfInfo->iErrorCode )
1636         {
1637                 /* Initialize the TAPs */
1638                 pXsvfInfo->iErrorCode   = xsvfGotoTapState( &(pXsvfInfo->ucTapState),
1639                                                             XTAPSTATE_RESET );
1640         }
1641
1642         return( pXsvfInfo->iErrorCode );
1643 }
1644
1645 /*****************************************************************************
1646  * Function:     xsvfRun
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 )
1656 {
1657         /* Process the XSVF commands */
1658         if ( (!pXsvfInfo->iErrorCode) && (!pXsvfInfo->ucComplete) )
1659         {
1660                 /* read 1 byte for the instruction */
1661                 readByte( &(pXsvfInfo->ucCommand) );
1662                 ++(pXsvfInfo->lCommandCount);
1663
1664                 if ( pXsvfInfo->ucCommand < XLASTCMD )
1665                 {
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 );
1673 #else
1674                         switch (pXsvfInfo->ucCommand) {
1675                         case 0:
1676                                 xsvfDoXCOMPLETE(pXsvfInfo);        /*  0 */
1677                                 break;
1678                         case 1:
1679                                 xsvfDoXTDOMASK(pXsvfInfo);         /*  1 */
1680                                 break;
1681                         case 2:
1682                                 xsvfDoXSIR(pXsvfInfo);             /*  2 */
1683                                 break;
1684                         case 3:
1685                                 xsvfDoXSDR(pXsvfInfo);             /*  3 */
1686                                 break;
1687                         case 4:
1688                                 xsvfDoXRUNTEST(pXsvfInfo);         /*  4 */
1689                                 break;
1690                         case 5:
1691                                 xsvfDoIllegalCmd(pXsvfInfo);       /*  5 */
1692                                 break;
1693                         case 6:
1694                                 xsvfDoIllegalCmd(pXsvfInfo);       /*  6 */
1695                                 break;
1696                         case 7:
1697                                 xsvfDoXREPEAT(pXsvfInfo);          /*  7 */
1698                                 break;
1699                         case 8:
1700                                 xsvfDoXSDRSIZE(pXsvfInfo);         /*  8 */
1701                                 break;
1702                         case 9:
1703                                 xsvfDoXSDRTDO(pXsvfInfo);          /*  9 */
1704                                 break;
1705 #ifdef  XSVF_SUPPORT_COMPRESSION
1706                         case 10:
1707                                 xsvfDoXSETSDRMASKS(pXsvfInfo);     /* 10 */
1708                                 break;
1709                         case 11:
1710                                 xsvfDoXSDRINC(pXsvfInfo);          /* 11 */
1711                                 break;
1712 #else
1713                         case 10:
1714                                 xsvfDoIllegalCmd(pXsvfInfo);       /* 10 */
1715                                 break;
1716                         case 11:
1717                                 xsvfDoIllegalCmd(pXsvfInfo);       /* 11 */
1718                                 break;
1719 #endif  /* XSVF_SUPPORT_COMPRESSION */
1720                         case 12:
1721                                 xsvfDoXSDRBCE(pXsvfInfo);          /* 12 */
1722                                 break;
1723                         case 13:
1724                                 xsvfDoXSDRBCE(pXsvfInfo);          /* 13 */
1725                                 break;
1726                         case 14:
1727                                 xsvfDoXSDRBCE(pXsvfInfo);          /* 14 */
1728                                 break;
1729                         case 15:
1730                                 xsvfDoXSDRTDOBCE(pXsvfInfo);       /* 15 */
1731                                 break;
1732                         case 16:
1733                                 xsvfDoXSDRTDOBCE(pXsvfInfo);       /* 16 */
1734                                 break;
1735                         case 17:
1736                                 xsvfDoXSDRTDOBCE(pXsvfInfo);       /* 17 */
1737                                 break;
1738                         case 18:
1739                                 xsvfDoXSTATE(pXsvfInfo);           /* 18 */
1740                                 break;
1741                         case 19:
1742                                 xsvfDoXENDXR(pXsvfInfo);           /* 19 */
1743                                 break;
1744                         case 20:
1745                                 xsvfDoXENDXR(pXsvfInfo);           /* 20 */
1746                                 break;
1747                         case 21:
1748                                 xsvfDoXSIR2(pXsvfInfo);            /* 21 */
1749                                 break;
1750                         case 22:
1751                                 xsvfDoXCOMMENT(pXsvfInfo);         /* 22 */
1752                                 break;
1753                         case 23:
1754                                 xsvfDoXWAIT(pXsvfInfo);             /* 23 */
1755                                 break;
1756                         }
1757 #endif
1758                 }
1759                 else
1760                 {
1761                         /* Illegal command value.  Func sets error code. */
1762                         xsvfDoIllegalCmd( pXsvfInfo );
1763                 }
1764         }
1765
1766         return( pXsvfInfo->iErrorCode );
1767 }
1768
1769 /*****************************************************************************
1770  * Function:     xsvfCleanup
1771  * Description:  cleanup remnants of the xsvf player.
1772  * Parameters:   pXsvfInfo   - ptr to the XSVF information.
1773  * Returns:      void.
1774  *****************************************************************************/
1775 void xsvfCleanup( SXsvfInfo* pXsvfInfo )
1776 {
1777         xsvfInfoCleanup( pXsvfInfo );
1778 }
1779
1780
1781 /*============================================================================
1782  * xsvfExecute() - The primary entry point to the XSVF player
1783  ============================================================================*/
1784
1785 /*****************************************************************************
1786  * Function:     xsvfExecute
1787  * Description:  Process, interpret, and apply the XSVF commands.
1788  *               See port.c:readByte for source of XSVF data.
1789  * Parameters:   none.
1790  * Returns:      int - Legacy result values:  1 == success;  0 == failed.
1791  *****************************************************************************/
1792 int xsvfExecute(void)
1793 {
1794         SXsvfInfo   xsvfInfo;
1795
1796         xsvfInitialize( &xsvfInfo );
1797
1798         while ( !xsvfInfo.iErrorCode && (!xsvfInfo.ucComplete) )
1799         {
1800                 xsvfRun( &xsvfInfo );
1801         }
1802
1803         if ( xsvfInfo.iErrorCode )
1804         {
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 );
1810         }
1811         else
1812         {
1813                 XSVFDBG_PRINTF( 0, "SUCCESS - Completed XSVF execution.\n" );
1814         }
1815
1816         xsvfCleanup( &xsvfInfo );
1817
1818         return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) );
1819 }
1820
1821
1822 /*****************************************************************************
1823  * Function:     do_cpld
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[])
1832 {
1833         int     iErrorCode;
1834         char*   pzXsvfFileName;
1835         unsigned long duration;
1836         unsigned long long startClock, endClock;
1837
1838         if (argc == 2)
1839                 xsvfdata = (unsigned char *)simple_strtoul(argv[1], NULL, 16);
1840         else {
1841 #ifdef CONFIG_SYS_XSVF_DEFAULT_ADDR
1842                 xsvfdata = (unsigned char *)CONFIG_SYS_XSVF_DEFAULT_ADDR;
1843 #else
1844                 printf("Usage:\ncpld %s\n", cmdtp->help);
1845                 return -1;
1846 #endif
1847         }
1848
1849         iErrorCode          = XSVF_ERRORCODE( XSVF_ERROR_NONE );
1850         pzXsvfFileName      = 0;
1851         xsvf_iDebugLevel    = 0;
1852
1853         printf("XSVF Player v%s, Xilinx, Inc.\n", XSVF_VERSION);
1854         printf("Reading XSVF data @ %p\n", xsvfdata);
1855
1856         /* Initialize the I/O.  SetPort initializes I/O on first call */
1857         setPort( TMS, 1 );
1858
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()));
1865
1866         return( iErrorCode );
1867 }
1868 U_BOOT_CMD(
1869         cpld,   2,      1,      do_cpld,
1870         "program onboard CPLD",
1871         "<xsvf-addr>"
1872 );