]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/esd/common/xilinx_jtag/micro.c
* Patches by Xianghua Xiao, 15 Oct 2003:
[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
70 extern const unsigned char fpgadata[];
71 extern int filesize;
72
73
74 /*============================================================================
75  * XSVF #define
76  ============================================================================*/
77
78 #define XSVF_VERSION    "5.00"
79
80 /*****************************************************************************
81  * Define:       XSVF_SUPPORT_COMPRESSION
82  * Description:  Define this to support the XC9500/XL XSVF data compression
83  *               scheme.
84  *               Code size can be reduced by NOT supporting this feature.
85  *               However, you must use the -nc (no compress) option when
86  *               translating SVF to XSVF using the SVF2XSVF translator.
87  *               Corresponding, uncompressed XSVF may be larger.
88  *****************************************************************************/
89 #ifndef XSVF_SUPPORT_COMPRESSION
90 #define XSVF_SUPPORT_COMPRESSION    1
91 #endif
92
93 /*****************************************************************************
94  * Define:       XSVF_SUPPORT_ERRORCODES
95  * Description:  Define this to support the new XSVF error codes.
96  *               (The original XSVF player just returned 1 for success and
97  *               0 for an unspecified failure.)
98  *****************************************************************************/
99 #ifndef XSVF_SUPPORT_ERRORCODES
100 #define XSVF_SUPPORT_ERRORCODES     1
101 #endif
102
103 #ifdef  XSVF_SUPPORT_ERRORCODES
104 #define XSVF_ERRORCODE(errorCode)   errorCode
105 #else   /* Use legacy error code */
106 #define XSVF_ERRORCODE(errorCode)   ((errorCode==XSVF_ERROR_NONE)?1:0)
107 #endif  /* XSVF_SUPPORT_ERRORCODES */
108
109
110 /*============================================================================
111  * DEBUG_MODE #define
112  ============================================================================*/
113 #define DEBUG_MODE
114
115 #ifdef  DEBUG_MODE
116 #define XSVFDBG_PRINTF(iDebugLevel,pzFormat) \
117                 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
118                     printf( pzFormat ); }
119 #define XSVFDBG_PRINTF1(iDebugLevel,pzFormat,arg1) \
120                 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
121                     printf( pzFormat, arg1 ); }
122 #define XSVFDBG_PRINTF2(iDebugLevel,pzFormat,arg1,arg2) \
123                 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
124                     printf( pzFormat, arg1, arg2 ); }
125 #define XSVFDBG_PRINTF3(iDebugLevel,pzFormat,arg1,arg2,arg3) \
126                 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
127                     printf( pzFormat, arg1, arg2, arg3 ); }
128 #define XSVFDBG_PRINTLENVAL(iDebugLevel,plenVal) \
129                 { if ( xsvf_iDebugLevel >= iDebugLevel ) \
130                     xsvfPrintLenVal(plenVal); }
131 #else   /* !DEBUG_MODE */
132 #define XSVFDBG_PRINTF(iDebugLevel,pzFormat)
133 #define XSVFDBG_PRINTF1(iDebugLevel,pzFormat,arg1)
134 #define XSVFDBG_PRINTF2(iDebugLevel,pzFormat,arg1,arg2)
135 #define XSVFDBG_PRINTF3(iDebugLevel,pzFormat,arg1,arg2,arg3)
136 #define XSVFDBG_PRINTLENVAL(iDebugLevel,plenVal)
137 #endif  /* DEBUG_MODE */
138
139
140 /*============================================================================
141  * XSVF Type Declarations
142  ============================================================================*/
143
144 /*****************************************************************************
145  * Struct:       SXsvfInfo
146  * Description:  This structure contains all of the data used during the
147  *               execution of the XSVF.  Some data is persistent, predefined
148  *               information (e.g. lRunTestTime).  The bulk of this struct's
149  *               size is due to the lenVal structs (defined in lenval.h)
150  *               which contain buffers for the active shift data.  The MAX_LEN
151  *               #define in lenval.h defines the size of these buffers.
152  *               These buffers must be large enough to store the longest
153  *               shift data in your XSVF file.  For example:
154  *                   MAX_LEN >= ( longest_shift_data_in_bits / 8 )
155  *               Because the lenVal struct dominates the space usage of this
156  *               struct, the rough size of this struct is:
157  *                   sizeof( SXsvfInfo ) ~= MAX_LEN * 7 (number of lenVals)
158  *               xsvfInitialize() contains initialization code for the data
159  *               in this struct.
160  *               xsvfCleanup() contains cleanup code for the data in this
161  *               struct.
162  *****************************************************************************/
163 typedef struct tagSXsvfInfo
164 {
165         /* XSVF status information */
166         unsigned char   ucComplete;         /* 0 = running; 1 = complete */
167         unsigned char   ucCommand;          /* Current XSVF command byte */
168         long            lCommandCount;      /* Number of commands processed */
169         int             iErrorCode;         /* An error code. 0 = no error. */
170
171         /* TAP state/sequencing information */
172         unsigned char   ucTapState;         /* Current TAP state */
173         unsigned char   ucEndIR;            /* ENDIR TAP state (See SVF) */
174         unsigned char   ucEndDR;            /* ENDDR TAP state (See SVF) */
175
176         /* RUNTEST information */
177         unsigned char   ucMaxRepeat;        /* Max repeat loops (for xc9500/xl) */
178         long            lRunTestTime;       /* Pre-specified RUNTEST time (usec) */
179
180         /* Shift Data Info and Buffers */
181         long            lShiftLengthBits;   /* Len. current shift data in bits */
182         short           sShiftLengthBytes;  /* Len. current shift data in bytes */
183
184         lenVal          lvTdi;              /* Current TDI shift data */
185         lenVal          lvTdoExpected;      /* Expected TDO shift data */
186         lenVal          lvTdoCaptured;      /* Captured TDO shift data */
187         lenVal          lvTdoMask;          /* TDO mask: 0=dontcare; 1=compare */
188
189 #ifdef  XSVF_SUPPORT_COMPRESSION
190         /* XSDRINC Data Buffers */
191         lenVal          lvAddressMask;      /* Address mask for XSDRINC */
192         lenVal          lvDataMask;         /* Data mask for XSDRINC */
193         lenVal          lvNextData;         /* Next data for XSDRINC */
194 #endif  /* XSVF_SUPPORT_COMPRESSION */
195 } SXsvfInfo;
196
197 /* Declare pointer to functions that perform XSVF commands */
198 typedef int (*TXsvfDoCmdFuncPtr)( SXsvfInfo* );
199
200 /*============================================================================
201  * XSVF Command Bytes
202  ============================================================================*/
203
204 /* encodings of xsvf instructions */
205 #define XCOMPLETE        0
206 #define XTDOMASK         1
207 #define XSIR             2
208 #define XSDR             3
209 #define XRUNTEST         4
210 /* Reserved              5 */
211 /* Reserved              6 */
212 #define XREPEAT          7
213 #define XSDRSIZE         8
214 #define XSDRTDO          9
215 #define XSETSDRMASKS     10
216 #define XSDRINC          11
217 #define XSDRB            12
218 #define XSDRC            13
219 #define XSDRE            14
220 #define XSDRTDOB         15
221 #define XSDRTDOC         16
222 #define XSDRTDOE         17
223 #define XSTATE           18         /* 4.00 */
224 #define XENDIR           19         /* 4.04 */
225 #define XENDDR           20         /* 4.04 */
226 #define XSIR2            21         /* 4.10 */
227 #define XCOMMENT         22         /* 4.14 */
228 #define XWAIT            23         /* 5.00 */
229 /* Insert new commands here */
230 /* and add corresponding xsvfDoCmd function to xsvf_pfDoCmd below. */
231 #define XLASTCMD         24         /* Last command marker */
232
233
234 /*============================================================================
235  * XSVF Command Parameter Values
236  ============================================================================*/
237
238 #define XSTATE_RESET     0          /* 4.00 parameter for XSTATE */
239 #define XSTATE_RUNTEST   1          /* 4.00 parameter for XSTATE */
240
241 #define XENDXR_RUNTEST   0          /* 4.04 parameter for XENDIR/DR */
242 #define XENDXR_PAUSE     1          /* 4.04 parameter for XENDIR/DR */
243
244 /* TAP states */
245 #define XTAPSTATE_RESET     0x00
246 #define XTAPSTATE_RUNTEST   0x01    /* a.k.a. IDLE */
247 #define XTAPSTATE_SELECTDR  0x02
248 #define XTAPSTATE_CAPTUREDR 0x03
249 #define XTAPSTATE_SHIFTDR   0x04
250 #define XTAPSTATE_EXIT1DR   0x05
251 #define XTAPSTATE_PAUSEDR   0x06
252 #define XTAPSTATE_EXIT2DR   0x07
253 #define XTAPSTATE_UPDATEDR  0x08
254 #define XTAPSTATE_IRSTATES  0x09    /* All IR states begin here */
255 #define XTAPSTATE_SELECTIR  0x09
256 #define XTAPSTATE_CAPTUREIR 0x0A
257 #define XTAPSTATE_SHIFTIR   0x0B
258 #define XTAPSTATE_EXIT1IR   0x0C
259 #define XTAPSTATE_PAUSEIR   0x0D
260 #define XTAPSTATE_EXIT2IR   0x0E
261 #define XTAPSTATE_UPDATEIR  0x0F
262
263 /*============================================================================
264  * XSVF Function Prototypes
265  ============================================================================*/
266
267 int xsvfDoIllegalCmd( SXsvfInfo* pXsvfInfo );   /* Illegal command function */
268 int xsvfDoXCOMPLETE( SXsvfInfo* pXsvfInfo );
269 int xsvfDoXTDOMASK( SXsvfInfo* pXsvfInfo );
270 int xsvfDoXSIR( SXsvfInfo* pXsvfInfo );
271 int xsvfDoXSIR2( SXsvfInfo* pXsvfInfo );
272 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo );
273 int xsvfDoXRUNTEST( SXsvfInfo* pXsvfInfo );
274 int xsvfDoXREPEAT( SXsvfInfo* pXsvfInfo );
275 int xsvfDoXSDRSIZE( SXsvfInfo* pXsvfInfo );
276 int xsvfDoXSDRTDO( SXsvfInfo* pXsvfInfo );
277 int xsvfDoXSETSDRMASKS( SXsvfInfo* pXsvfInfo );
278 int xsvfDoXSDRINC( SXsvfInfo* pXsvfInfo );
279 int xsvfDoXSDRBCE( SXsvfInfo* pXsvfInfo );
280 int xsvfDoXSDRTDOBCE( SXsvfInfo* pXsvfInfo );
281 int xsvfDoXSTATE( SXsvfInfo* pXsvfInfo );
282 int xsvfDoXENDXR( SXsvfInfo* pXsvfInfo );
283 int xsvfDoXCOMMENT( SXsvfInfo* pXsvfInfo );
284 int xsvfDoXWAIT( SXsvfInfo* pXsvfInfo );
285 /* Insert new command functions here */
286
287 /*============================================================================
288  * XSVF Global Variables
289  ============================================================================*/
290
291 /* Array of XSVF command functions.  Must follow command byte value order! */
292 /* If your compiler cannot take this form, then convert to a switch statement*/
293 TXsvfDoCmdFuncPtr   xsvf_pfDoCmd[]  =
294 {
295         xsvfDoXCOMPLETE,        /*  0 */
296         xsvfDoXTDOMASK,         /*  1 */
297         xsvfDoXSIR,             /*  2 */
298         xsvfDoXSDR,             /*  3 */
299         xsvfDoXRUNTEST,         /*  4 */
300         xsvfDoIllegalCmd,       /*  5 */
301         xsvfDoIllegalCmd,       /*  6 */
302         xsvfDoXREPEAT,          /*  7 */
303         xsvfDoXSDRSIZE,         /*  8 */
304         xsvfDoXSDRTDO,          /*  9 */
305 #ifdef  XSVF_SUPPORT_COMPRESSION
306         xsvfDoXSETSDRMASKS,     /* 10 */
307         xsvfDoXSDRINC,          /* 11 */
308 #else
309         xsvfDoIllegalCmd,       /* 10 */
310         xsvfDoIllegalCmd,       /* 11 */
311 #endif  /* XSVF_SUPPORT_COMPRESSION */
312         xsvfDoXSDRBCE,          /* 12 */
313         xsvfDoXSDRBCE,          /* 13 */
314         xsvfDoXSDRBCE,          /* 14 */
315         xsvfDoXSDRTDOBCE,       /* 15 */
316         xsvfDoXSDRTDOBCE,       /* 16 */
317         xsvfDoXSDRTDOBCE,       /* 17 */
318         xsvfDoXSTATE,           /* 18 */
319         xsvfDoXENDXR,           /* 19 */
320         xsvfDoXENDXR,           /* 20 */
321         xsvfDoXSIR2,            /* 21 */
322         xsvfDoXCOMMENT,         /* 22 */
323         xsvfDoXWAIT             /* 23 */
324 /* Insert new command functions here */
325 };
326
327 #ifdef  DEBUG_MODE
328 char* xsvf_pzCommandName[]  =
329 {
330         "XCOMPLETE",
331         "XTDOMASK",
332         "XSIR",
333         "XSDR",
334         "XRUNTEST",
335         "Reserved5",
336         "Reserved6",
337         "XREPEAT",
338         "XSDRSIZE",
339         "XSDRTDO",
340         "XSETSDRMASKS",
341         "XSDRINC",
342         "XSDRB",
343         "XSDRC",
344         "XSDRE",
345         "XSDRTDOB",
346         "XSDRTDOC",
347         "XSDRTDOE",
348         "XSTATE",
349         "XENDIR",
350         "XENDDR",
351         "XSIR2",
352         "XCOMMENT",
353         "XWAIT"
354 };
355
356 char*   xsvf_pzErrorName[]  =
357 {
358         "No error",
359         "ERROR:  Unknown",
360         "ERROR:  TDO mismatch",
361         "ERROR:  TDO mismatch and exceeded max retries",
362         "ERROR:  Unsupported XSVF command",
363         "ERROR:  Illegal state specification",
364         "ERROR:  Data overflows allocated MAX_LEN buffer size"
365 };
366
367 char*   xsvf_pzTapState[] =
368 {
369         "RESET",        /* 0x00 */
370         "RUNTEST/IDLE", /* 0x01 */
371         "DRSELECT",     /* 0x02 */
372         "DRCAPTURE",    /* 0x03 */
373         "DRSHIFT",      /* 0x04 */
374         "DREXIT1",      /* 0x05 */
375         "DRPAUSE",      /* 0x06 */
376         "DREXIT2",      /* 0x07 */
377         "DRUPDATE",     /* 0x08 */
378         "IRSELECT",     /* 0x09 */
379         "IRCAPTURE",    /* 0x0A */
380         "IRSHIFT",      /* 0x0B */
381         "IREXIT1",      /* 0x0C */
382         "IRPAUSE",      /* 0x0D */
383         "IREXIT2",      /* 0x0E */
384         "IRUPDATE"      /* 0x0F */
385 };
386 #endif  /* DEBUG_MODE */
387
388 /*#ifdef DEBUG_MODE     */
389 /*    FILE* in;   /XXX* Legacy DEBUG_MODE file pointer */
390 int xsvf_iDebugLevel;
391 /*#endif /XXX* DEBUG_MODE */
392
393 /*============================================================================
394  * Utility Functions
395  ============================================================================*/
396
397 /*****************************************************************************
398  * Function:     xsvfPrintLenVal
399  * Description:  Print the lenval value in hex.
400  * Parameters:   plv     - ptr to lenval.
401  * Returns:      void.
402  *****************************************************************************/
403 #ifdef  DEBUG_MODE
404 void xsvfPrintLenVal( lenVal *plv )
405 {
406         int i;
407
408         if ( plv )
409         {
410                 printf( "0x" );
411                 for ( i = 0; i < plv->len; ++i )
412                 {
413                         printf( "%02x", ((unsigned int)(plv->val[ i ])) );
414                 }
415         }
416 }
417 #endif  /* DEBUG_MODE */
418
419
420 /*****************************************************************************
421  * Function:     xsvfInfoInit
422  * Description:  Initialize the xsvfInfo data.
423  * Parameters:   pXsvfInfo   - ptr to the XSVF info structure.
424  * Returns:      int         - 0 = success; otherwise error.
425  *****************************************************************************/
426 int xsvfInfoInit( SXsvfInfo* pXsvfInfo )
427 {
428         XSVFDBG_PRINTF1( 4, "    sizeof( SXsvfInfo ) = %d bytes\n",
429                          sizeof( SXsvfInfo ) );
430
431         pXsvfInfo->ucComplete       = 0;
432         pXsvfInfo->ucCommand        = XCOMPLETE;
433         pXsvfInfo->lCommandCount    = 0;
434         pXsvfInfo->iErrorCode       = XSVF_ERROR_NONE;
435         pXsvfInfo->ucMaxRepeat      = 0;
436         pXsvfInfo->ucTapState       = XTAPSTATE_RESET;
437         pXsvfInfo->ucEndIR          = XTAPSTATE_RUNTEST;
438         pXsvfInfo->ucEndDR          = XTAPSTATE_RUNTEST;
439         pXsvfInfo->lShiftLengthBits = 0L;
440         pXsvfInfo->sShiftLengthBytes= 0;
441         pXsvfInfo->lRunTestTime     = 0L;
442
443         return( 0 );
444 }
445
446 /*****************************************************************************
447  * Function:     xsvfInfoCleanup
448  * Description:  Cleanup the xsvfInfo data.
449  * Parameters:   pXsvfInfo   - ptr to the XSVF info structure.
450  * Returns:      void.
451  *****************************************************************************/
452 void xsvfInfoCleanup( SXsvfInfo* pXsvfInfo )
453 {
454 }
455
456 /*****************************************************************************
457  * Function:     xsvfGetAsNumBytes
458  * Description:  Calculate the number of bytes the given number of bits
459  *               consumes.
460  * Parameters:   lNumBits    - the number of bits.
461  * Returns:      short       - the number of bytes to store the number of bits.
462  *****************************************************************************/
463 short xsvfGetAsNumBytes( long lNumBits )
464 {
465         return( (short)( ( lNumBits + 7L ) / 8L ) );
466 }
467
468 /*****************************************************************************
469  * Function:     xsvfTmsTransition
470  * Description:  Apply TMS and transition TAP controller by applying one TCK
471  *               cycle.
472  * Parameters:   sTms    - new TMS value.
473  * Returns:      void.
474  *****************************************************************************/
475 void xsvfTmsTransition( short sTms )
476 {
477         setPort( TMS, sTms );
478         setPort( TCK, 0 );
479         setPort( TCK, 1 );
480 }
481
482 /*****************************************************************************
483  * Function:     xsvfGotoTapState
484  * Description:  From the current TAP state, go to the named TAP state.
485  *               A target state of RESET ALWAYS causes TMS reset sequence.
486  *               All SVF standard stable state paths are supported.
487  *               All state transitions are supported except for the following
488  *               which cause an XSVF_ERROR_ILLEGALSTATE:
489  *                   - Target==DREXIT2;  Start!=DRPAUSE
490  *                   - Target==IREXIT2;  Start!=IRPAUSE
491  * Parameters:   pucTapState     - Current TAP state; returns final TAP state.
492  *               ucTargetState   - New target TAP state.
493  * Returns:      int             - 0 = success; otherwise error.
494  *****************************************************************************/
495 int xsvfGotoTapState( unsigned char*   pucTapState,
496                       unsigned char    ucTargetState )
497 {
498         int i;
499         int iErrorCode;
500
501         iErrorCode  = XSVF_ERROR_NONE;
502         if ( ucTargetState == XTAPSTATE_RESET )
503         {
504                 /* If RESET, always perform TMS reset sequence to reset/sync TAPs */
505                 xsvfTmsTransition( 1 );
506                 for ( i = 0; i < 5; ++i )
507                 {
508                         setPort( TCK, 0 );
509                         setPort( TCK, 1 );
510                 }
511                 *pucTapState    = XTAPSTATE_RESET;
512                 XSVFDBG_PRINTF( 3, "   TMS Reset Sequence -> Test-Logic-Reset\n" );
513                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
514                                  xsvf_pzTapState[ *pucTapState ] );
515         } else if ( ( ucTargetState != *pucTapState ) &&
516                   ( ( ( ucTargetState == XTAPSTATE_EXIT2DR ) && ( *pucTapState != XTAPSTATE_PAUSEDR ) ) ||
517                     ( ( ucTargetState == XTAPSTATE_EXIT2IR ) && ( *pucTapState != XTAPSTATE_PAUSEIR ) ) ) )
518         {
519                 /* Trap illegal TAP state path specification */
520                 iErrorCode      = XSVF_ERROR_ILLEGALSTATE;
521         } else {
522                 if ( ucTargetState == *pucTapState )
523                 {
524                         /* Already in target state.  Do nothing except when in DRPAUSE
525                            or in IRPAUSE to comply with SVF standard */
526                         if ( ucTargetState == XTAPSTATE_PAUSEDR )
527                         {
528                                 xsvfTmsTransition( 1 );
529                                 *pucTapState    = XTAPSTATE_EXIT2DR;
530                                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
531                                                  xsvf_pzTapState[ *pucTapState ] );
532                         }
533                         else if ( ucTargetState == XTAPSTATE_PAUSEIR )
534                         {
535                                 xsvfTmsTransition( 1 );
536                                 *pucTapState    = XTAPSTATE_EXIT2IR;
537                                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
538                                                  xsvf_pzTapState[ *pucTapState ] );
539                         }
540                 }
541
542                 /* Perform TAP state transitions to get to the target state */
543                 while ( ucTargetState != *pucTapState )
544                 {
545                         switch ( *pucTapState )
546                         {
547                         case XTAPSTATE_RESET:
548                                 xsvfTmsTransition( 0 );
549                                 *pucTapState    = XTAPSTATE_RUNTEST;
550                                 break;
551                         case XTAPSTATE_RUNTEST:
552                                 xsvfTmsTransition( 1 );
553                                 *pucTapState    = XTAPSTATE_SELECTDR;
554                                 break;
555                         case XTAPSTATE_SELECTDR:
556                                 if ( ucTargetState >= XTAPSTATE_IRSTATES )
557                                 {
558                                         xsvfTmsTransition( 1 );
559                                         *pucTapState    = XTAPSTATE_SELECTIR;
560                                 }
561                                 else
562                                 {
563                                         xsvfTmsTransition( 0 );
564                                         *pucTapState    = XTAPSTATE_CAPTUREDR;
565                                 }
566                                 break;
567                         case XTAPSTATE_CAPTUREDR:
568                                 if ( ucTargetState == XTAPSTATE_SHIFTDR )
569                                 {
570                                         xsvfTmsTransition( 0 );
571                                         *pucTapState    = XTAPSTATE_SHIFTDR;
572                                 }
573                                 else
574                                 {
575                                         xsvfTmsTransition( 1 );
576                                         *pucTapState    = XTAPSTATE_EXIT1DR;
577                                 }
578                                 break;
579                         case XTAPSTATE_SHIFTDR:
580                                 xsvfTmsTransition( 1 );
581                                 *pucTapState    = XTAPSTATE_EXIT1DR;
582                                 break;
583                         case XTAPSTATE_EXIT1DR:
584                                 if ( ucTargetState == XTAPSTATE_PAUSEDR )
585                                 {
586                                         xsvfTmsTransition( 0 );
587                                         *pucTapState    = XTAPSTATE_PAUSEDR;
588                                 }
589                                 else
590                                 {
591                                         xsvfTmsTransition( 1 );
592                                         *pucTapState    = XTAPSTATE_UPDATEDR;
593                                 }
594                                 break;
595                         case XTAPSTATE_PAUSEDR:
596                                 xsvfTmsTransition( 1 );
597                                 *pucTapState    = XTAPSTATE_EXIT2DR;
598                                 break;
599                         case XTAPSTATE_EXIT2DR:
600                                 if ( ucTargetState == XTAPSTATE_SHIFTDR )
601                                 {
602                                         xsvfTmsTransition( 0 );
603                                         *pucTapState    = XTAPSTATE_SHIFTDR;
604                                 }
605                                 else
606                                 {
607                                         xsvfTmsTransition( 1 );
608                                         *pucTapState    = XTAPSTATE_UPDATEDR;
609                                 }
610                                 break;
611                         case XTAPSTATE_UPDATEDR:
612                                 if ( ucTargetState == XTAPSTATE_RUNTEST )
613                                 {
614                                         xsvfTmsTransition( 0 );
615                                         *pucTapState    = XTAPSTATE_RUNTEST;
616                                 }
617                                 else
618                                 {
619                                         xsvfTmsTransition( 1 );
620                                         *pucTapState    = XTAPSTATE_SELECTDR;
621                                 }
622                                 break;
623                         case XTAPSTATE_SELECTIR:
624                                 xsvfTmsTransition( 0 );
625                                 *pucTapState    = XTAPSTATE_CAPTUREIR;
626                                 break;
627                         case XTAPSTATE_CAPTUREIR:
628                                 if ( ucTargetState == XTAPSTATE_SHIFTIR )
629                                 {
630                                         xsvfTmsTransition( 0 );
631                                         *pucTapState    = XTAPSTATE_SHIFTIR;
632                                 }
633                                 else
634                                 {
635                                         xsvfTmsTransition( 1 );
636                                         *pucTapState    = XTAPSTATE_EXIT1IR;
637                                 }
638                                 break;
639                         case XTAPSTATE_SHIFTIR:
640                                 xsvfTmsTransition( 1 );
641                                 *pucTapState    = XTAPSTATE_EXIT1IR;
642                                 break;
643                         case XTAPSTATE_EXIT1IR:
644                                 if ( ucTargetState == XTAPSTATE_PAUSEIR )
645                                 {
646                                         xsvfTmsTransition( 0 );
647                                         *pucTapState    = XTAPSTATE_PAUSEIR;
648                                 }
649                                 else
650                                 {
651                                         xsvfTmsTransition( 1 );
652                                         *pucTapState    = XTAPSTATE_UPDATEIR;
653                                 }
654                                 break;
655                         case XTAPSTATE_PAUSEIR:
656                                 xsvfTmsTransition( 1 );
657                                 *pucTapState    = XTAPSTATE_EXIT2IR;
658                                 break;
659                         case XTAPSTATE_EXIT2IR:
660                                 if ( ucTargetState == XTAPSTATE_SHIFTIR )
661                                 {
662                                         xsvfTmsTransition( 0 );
663                                         *pucTapState    = XTAPSTATE_SHIFTIR;
664                                 }
665                                 else
666                                 {
667                                         xsvfTmsTransition( 1 );
668                                         *pucTapState    = XTAPSTATE_UPDATEIR;
669                                 }
670                                 break;
671                         case XTAPSTATE_UPDATEIR:
672                                 if ( ucTargetState == XTAPSTATE_RUNTEST )
673                                 {
674                                         xsvfTmsTransition( 0 );
675                                         *pucTapState    = XTAPSTATE_RUNTEST;
676                                 }
677                                 else
678                                 {
679                                         xsvfTmsTransition( 1 );
680                                         *pucTapState    = XTAPSTATE_SELECTDR;
681                                 }
682                                 break;
683                         default:
684                                 iErrorCode      = XSVF_ERROR_ILLEGALSTATE;
685                                 *pucTapState    = ucTargetState;    /* Exit while loop */
686                                 break;
687                         }
688                         XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
689                                          xsvf_pzTapState[ *pucTapState ] );
690                 }
691         }
692
693         return( iErrorCode );
694 }
695
696 /*****************************************************************************
697  * Function:     xsvfShiftOnly
698  * Description:  Assumes that starting TAP state is SHIFT-DR or SHIFT-IR.
699  *               Shift the given TDI data into the JTAG scan chain.
700  *               Optionally, save the TDO data shifted out of the scan chain.
701  *               Last shift cycle is special:  capture last TDO, set last TDI,
702  *               but does not pulse TCK.  Caller must pulse TCK and optionally
703  *               set TMS=1 to exit shift state.
704  * Parameters:   lNumBits        - number of bits to shift.
705  *               plvTdi          - ptr to lenval for TDI data.
706  *               plvTdoCaptured  - ptr to lenval for storing captured TDO data.
707  *               iExitShift      - 1=exit at end of shift; 0=stay in Shift-DR.
708  * Returns:      void.
709  *****************************************************************************/
710 void xsvfShiftOnly( long    lNumBits,
711                     lenVal* plvTdi,
712                     lenVal* plvTdoCaptured,
713                     int     iExitShift )
714 {
715         unsigned char*  pucTdi;
716         unsigned char*  pucTdo;
717         unsigned char   ucTdiByte;
718         unsigned char   ucTdoByte;
719         unsigned char   ucTdoBit;
720         int             i;
721
722         /* assert( ( ( lNumBits + 7 ) / 8 ) == plvTdi->len ); */
723
724         /* Initialize TDO storage len == TDI len */
725         pucTdo  = 0;
726         if ( plvTdoCaptured )
727         {
728                 plvTdoCaptured->len = plvTdi->len;
729                 pucTdo              = plvTdoCaptured->val + plvTdi->len;
730         }
731
732         /* Shift LSB first.  val[N-1] == LSB.  val[0] == MSB. */
733         pucTdi  = plvTdi->val + plvTdi->len;
734         while ( lNumBits )
735         {
736                 /* Process on a byte-basis */
737                 ucTdiByte   = (*(--pucTdi));
738                 ucTdoByte   = 0;
739                 for ( i = 0; ( lNumBits && ( i < 8 ) ); ++i )
740                 {
741                         --lNumBits;
742                         if ( iExitShift && !lNumBits )
743                         {
744                                 /* Exit Shift-DR state */
745                                 setPort( TMS, 1 );
746                         }
747
748                         /* Set the new TDI value */
749                         setPort( TDI, (short)(ucTdiByte & 1) );
750                         ucTdiByte   >>= 1;
751
752                         /* Set TCK low */
753                         setPort( TCK, 0 );
754
755                         if ( pucTdo )
756                         {
757                                 /* Save the TDO value */
758                                 ucTdoBit    = readTDOBit();
759                                 ucTdoByte   |= ( ucTdoBit << i );
760                         }
761
762                         /* Set TCK high */
763                         setPort( TCK, 1 );
764                 }
765
766                 /* Save the TDO byte value */
767                 if ( pucTdo )
768                 {
769                         (*(--pucTdo))   = ucTdoByte;
770                 }
771         }
772 }
773
774 /*****************************************************************************
775  * Function:     xsvfShift
776  * Description:  Goes to the given starting TAP state.
777  *               Calls xsvfShiftOnly to shift in the given TDI data and
778  *               optionally capture the TDO data.
779  *               Compares the TDO captured data against the TDO expected
780  *               data.
781  *               If a data mismatch occurs, then executes the exception
782  *               handling loop upto ucMaxRepeat times.
783  * Parameters:   pucTapState     - Ptr to current TAP state.
784  *               ucStartState    - Starting shift state: Shift-DR or Shift-IR.
785  *               lNumBits        - number of bits to shift.
786  *               plvTdi          - ptr to lenval for TDI data.
787  *               plvTdoCaptured  - ptr to lenval for storing TDO data.
788  *               plvTdoExpected  - ptr to expected TDO data.
789  *               plvTdoMask      - ptr to TDO mask.
790  *               ucEndState      - state in which to end the shift.
791  *               lRunTestTime    - amount of time to wait after the shift.
792  *               ucMaxRepeat     - Maximum number of retries on TDO mismatch.
793  * Returns:      int             - 0 = success; otherwise TDO mismatch.
794  * Notes:        XC9500XL-only Optimization:
795  *               Skip the waitTime() if plvTdoMask->val[0:plvTdoMask->len-1]
796  *               is NOT all zeros and sMatch==1.
797  *****************************************************************************/
798 int xsvfShift( unsigned char*   pucTapState,
799                unsigned char    ucStartState,
800                long             lNumBits,
801                lenVal*          plvTdi,
802                lenVal*          plvTdoCaptured,
803                lenVal*          plvTdoExpected,
804                lenVal*          plvTdoMask,
805                unsigned char    ucEndState,
806                long             lRunTestTime,
807                unsigned char    ucMaxRepeat )
808 {
809         int             iErrorCode;
810         int             iMismatch;
811         unsigned char   ucRepeat;
812         int             iExitShift;
813
814         iErrorCode  = XSVF_ERROR_NONE;
815         iMismatch   = 0;
816         ucRepeat    = 0;
817         iExitShift  = ( ucStartState != ucEndState );
818
819         XSVFDBG_PRINTF1( 3, "   Shift Length = %ld\n", lNumBits );
820         XSVFDBG_PRINTF( 4, "    TDI          = ");
821         XSVFDBG_PRINTLENVAL( 4, plvTdi );
822         XSVFDBG_PRINTF( 4, "\n");
823         XSVFDBG_PRINTF( 4, "    TDO Expected = ");
824         XSVFDBG_PRINTLENVAL( 4, plvTdoExpected );
825         XSVFDBG_PRINTF( 4, "\n");
826
827         if ( !lNumBits )
828         {
829                 /* Compatibility with XSVF2.00:  XSDR 0 = no shift, but wait in RTI */
830                 if ( lRunTestTime )
831                 {
832                         /* Wait for prespecified XRUNTEST time */
833                         xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
834                         XSVFDBG_PRINTF1( 3, "   Wait = %ld usec\n", lRunTestTime );
835                         waitTime( lRunTestTime );
836                 }
837         }
838         else
839         {
840                 do
841                 {
842                         /* Goto Shift-DR or Shift-IR */
843                         xsvfGotoTapState( pucTapState, ucStartState );
844
845                         /* Shift TDI and capture TDO */
846                         xsvfShiftOnly( lNumBits, plvTdi, plvTdoCaptured, iExitShift );
847
848                         if ( plvTdoExpected )
849                         {
850                                 /* Compare TDO data to expected TDO data */
851                                 iMismatch   = !EqualLenVal( plvTdoExpected,
852                                                             plvTdoCaptured,
853                                                             plvTdoMask );
854                         }
855
856                         if ( iExitShift )
857                         {
858                                 /* Update TAP state:  Shift->Exit */
859                                 ++(*pucTapState);
860                                 XSVFDBG_PRINTF1( 3, "   TAP State = %s\n",
861                                                  xsvf_pzTapState[ *pucTapState ] );
862
863                                 if ( iMismatch && lRunTestTime && ( ucRepeat < ucMaxRepeat ) )
864                                 {
865                                         XSVFDBG_PRINTF( 4, "    TDO Expected = ");
866                                         XSVFDBG_PRINTLENVAL( 4, plvTdoExpected );
867                                         XSVFDBG_PRINTF( 4, "\n");
868                                         XSVFDBG_PRINTF( 4, "    TDO Captured = ");
869                                         XSVFDBG_PRINTLENVAL( 4, plvTdoCaptured );
870                                         XSVFDBG_PRINTF( 4, "\n");
871                                         XSVFDBG_PRINTF( 4, "    TDO Mask     = ");
872                                         XSVFDBG_PRINTLENVAL( 4, plvTdoMask );
873                                         XSVFDBG_PRINTF( 4, "\n");
874                                         XSVFDBG_PRINTF1( 3, "   Retry #%d\n", ( ucRepeat + 1 ) );
875                                         /* Do exception handling retry - ShiftDR only */
876                                         xsvfGotoTapState( pucTapState, XTAPSTATE_PAUSEDR );
877                                         /* Shift 1 extra bit */
878                                         xsvfGotoTapState( pucTapState, XTAPSTATE_SHIFTDR );
879                                         /* Increment RUNTEST time by an additional 25% */
880                                         lRunTestTime    += ( lRunTestTime >> 2 );
881                                 }
882                                 else
883                                 {
884                                         /* Do normal exit from Shift-XR */
885                                         xsvfGotoTapState( pucTapState, ucEndState );
886                                 }
887
888                                 if ( lRunTestTime )
889                                 {
890                                         /* Wait for prespecified XRUNTEST time */
891                                         xsvfGotoTapState( pucTapState, XTAPSTATE_RUNTEST );
892                                         XSVFDBG_PRINTF1( 3, "   Wait = %ld usec\n", lRunTestTime );
893                                         waitTime( lRunTestTime );
894                                 }
895                         }
896                 } while ( iMismatch && ( ucRepeat++ < ucMaxRepeat ) );
897         }
898
899         if ( iMismatch )
900         {
901                 XSVFDBG_PRINTF( 1, " TDO Expected = ");
902                 XSVFDBG_PRINTLENVAL( 1, plvTdoExpected );
903                 XSVFDBG_PRINTF( 1, "\n");
904                 XSVFDBG_PRINTF( 1, " TDO Captured = ");
905                 XSVFDBG_PRINTLENVAL( 1, plvTdoCaptured );
906                 XSVFDBG_PRINTF( 1, "\n");
907                 XSVFDBG_PRINTF( 1, " TDO Mask     = ");
908                 XSVFDBG_PRINTLENVAL( 1, plvTdoMask );
909                 XSVFDBG_PRINTF( 1, "\n");
910                 if ( ucMaxRepeat && ( ucRepeat > ucMaxRepeat ) )
911                 {
912                         iErrorCode  = XSVF_ERROR_MAXRETRIES;
913                 }
914                 else
915                 {
916                         iErrorCode  = XSVF_ERROR_TDOMISMATCH;
917                 }
918         }
919
920         return( iErrorCode );
921 }
922
923 /*****************************************************************************
924  * Function:     xsvfBasicXSDRTDO
925  * Description:  Get the XSDRTDO parameters and execute the XSDRTDO command.
926  *               This is the common function for all XSDRTDO commands.
927  * Parameters:   pucTapState         - Current TAP state.
928  *               lShiftLengthBits    - number of bits to shift.
929  *               sShiftLengthBytes   - number of bytes to read.
930  *               plvTdi              - ptr to lenval for TDI data.
931  *               lvTdoCaptured       - ptr to lenval for storing TDO data.
932  *               iEndState           - state in which to end the shift.
933  *               lRunTestTime        - amount of time to wait after the shift.
934  *               ucMaxRepeat         - maximum xc9500/xl retries.
935  * Returns:      int                 - 0 = success; otherwise TDO mismatch.
936  *****************************************************************************/
937 int xsvfBasicXSDRTDO( unsigned char*    pucTapState,
938                       long              lShiftLengthBits,
939                       short             sShiftLengthBytes,
940                       lenVal*           plvTdi,
941                       lenVal*           plvTdoCaptured,
942                       lenVal*           plvTdoExpected,
943                       lenVal*           plvTdoMask,
944                       unsigned char     ucEndState,
945                       long              lRunTestTime,
946                       unsigned char     ucMaxRepeat )
947 {
948         readVal( plvTdi, sShiftLengthBytes );
949         if ( plvTdoExpected )
950         {
951                 readVal( plvTdoExpected, sShiftLengthBytes );
952         }
953         return( xsvfShift( pucTapState, XTAPSTATE_SHIFTDR, lShiftLengthBits,
954                            plvTdi, plvTdoCaptured, plvTdoExpected, plvTdoMask,
955                            ucEndState, lRunTestTime, ucMaxRepeat ) );
956 }
957
958 /*****************************************************************************
959  * Function:     xsvfDoSDRMasking
960  * Description:  Update the data value with the next XSDRINC data and address.
961  * Example:      dataVal=0x01ff, nextData=0xab, addressMask=0x0100,
962  *               dataMask=0x00ff, should set dataVal to 0x02ab
963  * Parameters:   plvTdi          - The current TDI value.
964  *               plvNextData     - the next data value.
965  *               plvAddressMask  - the address mask.
966  *               plvDataMask     - the data mask.
967  * Returns:      void.
968  *****************************************************************************/
969 #ifdef  XSVF_SUPPORT_COMPRESSION
970 void xsvfDoSDRMasking( lenVal*  plvTdi,
971                        lenVal*  plvNextData,
972                        lenVal*  plvAddressMask,
973                        lenVal*  plvDataMask )
974 {
975         int             i;
976         unsigned char   ucTdi;
977         unsigned char   ucTdiMask;
978         unsigned char   ucDataMask;
979         unsigned char   ucNextData;
980         unsigned char   ucNextMask;
981         short           sNextData;
982
983         /* add the address Mask to dataVal and return as a new dataVal */
984         addVal( plvTdi, plvTdi, plvAddressMask );
985
986         ucNextData  = 0;
987         ucNextMask  = 0;
988         sNextData   = plvNextData->len;
989         for ( i = plvDataMask->len - 1; i >= 0; --i )
990         {
991                 /* Go through data mask in reverse order looking for mask (1) bits */
992                 ucDataMask  = plvDataMask->val[ i ];
993                 if ( ucDataMask )
994                 {
995                         /* Retrieve the corresponding TDI byte value */
996                         ucTdi       = plvTdi->val[ i ];
997
998                         /* For each bit in the data mask byte, look for 1's */
999                         ucTdiMask   = 1;
1000                         while ( ucDataMask )
1001                         {
1002                                 if ( ucDataMask & 1 )
1003                                 {
1004                                         if ( !ucNextMask )
1005                                         {
1006                                                 /* Get the next data byte */
1007                                                 ucNextData  = plvNextData->val[ --sNextData ];
1008                                                 ucNextMask  = 1;
1009                                         }
1010
1011                                         /* Set or clear the data bit according to the next data */
1012                                         if ( ucNextData & ucNextMask )
1013                                         {
1014                                                 ucTdi   |= ucTdiMask;       /* Set bit */
1015                                         }
1016                                         else
1017                                         {
1018                                                 ucTdi   &= ( ~ucTdiMask );  /* Clear bit */
1019                                         }
1020
1021                                         /* Update the next data */
1022                                         ucNextMask  <<= 1;
1023                                 }
1024                                 ucTdiMask   <<= 1;
1025                                 ucDataMask  >>= 1;
1026                         }
1027
1028                         /* Update the TDI value */
1029                         plvTdi->val[ i ]    = ucTdi;
1030                 }
1031         }
1032 }
1033 #endif  /* XSVF_SUPPORT_COMPRESSION */
1034
1035 /*============================================================================
1036  * XSVF Command Functions (type = TXsvfDoCmdFuncPtr)
1037  * These functions update pXsvfInfo->iErrorCode only on an error.
1038  * Otherwise, the error code is left alone.
1039  * The function returns the error code from the function.
1040  ============================================================================*/
1041
1042 /*****************************************************************************
1043  * Function:     xsvfDoIllegalCmd
1044  * Description:  Function place holder for illegal/unsupported commands.
1045  * Parameters:   pXsvfInfo   - XSVF information pointer.
1046  * Returns:      int         - 0 = success;  non-zero = error.
1047  *****************************************************************************/
1048 int xsvfDoIllegalCmd( SXsvfInfo* pXsvfInfo )
1049 {
1050         XSVFDBG_PRINTF2( 0, "ERROR:  Encountered unsupported command #%d (%s)\n",
1051                          ((unsigned int)(pXsvfInfo->ucCommand)),
1052                          ((pXsvfInfo->ucCommand < XLASTCMD)
1053                           ? (xsvf_pzCommandName[pXsvfInfo->ucCommand])
1054                           : "Unknown") );
1055         pXsvfInfo->iErrorCode   = XSVF_ERROR_ILLEGALCMD;
1056         return( pXsvfInfo->iErrorCode );
1057 }
1058
1059 /*****************************************************************************
1060  * Function:     xsvfDoXCOMPLETE
1061  * Description:  XCOMPLETE (no parameters)
1062  *               Update complete status for XSVF player.
1063  * Parameters:   pXsvfInfo   - XSVF information pointer.
1064  * Returns:      int         - 0 = success;  non-zero = error.
1065  *****************************************************************************/
1066 int xsvfDoXCOMPLETE( SXsvfInfo* pXsvfInfo )
1067 {
1068         pXsvfInfo->ucComplete   = 1;
1069         return( XSVF_ERROR_NONE );
1070 }
1071
1072 /*****************************************************************************
1073  * Function:     xsvfDoXTDOMASK
1074  * Description:  XTDOMASK <lenVal.TdoMask[XSDRSIZE]>
1075  *               Prespecify the TDO compare mask.
1076  * Parameters:   pXsvfInfo   - XSVF information pointer.
1077  * Returns:      int         - 0 = success;  non-zero = error.
1078  *****************************************************************************/
1079 int xsvfDoXTDOMASK( SXsvfInfo* pXsvfInfo )
1080 {
1081         readVal( &(pXsvfInfo->lvTdoMask), pXsvfInfo->sShiftLengthBytes );
1082         XSVFDBG_PRINTF( 4, "    TDO Mask     = ");
1083         XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvTdoMask) );
1084         XSVFDBG_PRINTF( 4, "\n");
1085         return( XSVF_ERROR_NONE );
1086 }
1087
1088 /*****************************************************************************
1089  * Function:     xsvfDoXSIR
1090  * Description:  XSIR <(byte)shiftlen> <lenVal.TDI[shiftlen]>
1091  *               Get the instruction and shift the instruction into the TAP.
1092  *               If prespecified XRUNTEST!=0, goto RUNTEST and wait after
1093  *               the shift for XRUNTEST usec.
1094  * Parameters:   pXsvfInfo   - XSVF information pointer.
1095  * Returns:      int         - 0 = success;  non-zero = error.
1096  *****************************************************************************/
1097 int xsvfDoXSIR( SXsvfInfo* pXsvfInfo )
1098 {
1099         unsigned char   ucShiftIrBits;
1100         short           sShiftIrBytes;
1101         int             iErrorCode;
1102
1103         /* Get the shift length and store */
1104         readByte( &ucShiftIrBits );
1105         sShiftIrBytes   = xsvfGetAsNumBytes( ucShiftIrBits );
1106         XSVFDBG_PRINTF1( 3, "   XSIR length = %d\n",
1107                          ((unsigned int)ucShiftIrBits) );
1108
1109         if ( sShiftIrBytes > MAX_LEN )
1110         {
1111                 iErrorCode  = XSVF_ERROR_DATAOVERFLOW;
1112         }
1113         else
1114         {
1115                 /* Get and store instruction to shift in */
1116                 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( ucShiftIrBits ) );
1117
1118                 /* Shift the data */
1119                 iErrorCode  = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTIR,
1120                                          ucShiftIrBits, &(pXsvfInfo->lvTdi),
1121                                          /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1122                                          /*plvTdoMask*/0, pXsvfInfo->ucEndIR,
1123                                          pXsvfInfo->lRunTestTime, /*ucMaxRepeat*/0 );
1124         }
1125
1126         if ( iErrorCode != XSVF_ERROR_NONE )
1127         {
1128                 pXsvfInfo->iErrorCode   = iErrorCode;
1129         }
1130         return( iErrorCode );
1131 }
1132
1133 /*****************************************************************************
1134  * Function:     xsvfDoXSIR2
1135  * Description:  XSIR <(2-byte)shiftlen> <lenVal.TDI[shiftlen]>
1136  *               Get the instruction and shift the instruction into the TAP.
1137  *               If prespecified XRUNTEST!=0, goto RUNTEST and wait after
1138  *               the shift for XRUNTEST usec.
1139  * Parameters:   pXsvfInfo   - XSVF information pointer.
1140  * Returns:      int         - 0 = success;  non-zero = error.
1141  *****************************************************************************/
1142 int xsvfDoXSIR2( SXsvfInfo* pXsvfInfo )
1143 {
1144         long            lShiftIrBits;
1145         short           sShiftIrBytes;
1146         int             iErrorCode;
1147
1148         /* Get the shift length and store */
1149         readVal( &(pXsvfInfo->lvTdi), 2 );
1150         lShiftIrBits    = value( &(pXsvfInfo->lvTdi) );
1151         sShiftIrBytes   = xsvfGetAsNumBytes( lShiftIrBits );
1152         XSVFDBG_PRINTF1( 3, "   XSIR2 length = %d\n", (int)lShiftIrBits);
1153
1154         if ( sShiftIrBytes > MAX_LEN )
1155         {
1156                 iErrorCode  = XSVF_ERROR_DATAOVERFLOW;
1157         }
1158         else
1159         {
1160                 /* Get and store instruction to shift in */
1161                 readVal( &(pXsvfInfo->lvTdi), xsvfGetAsNumBytes( lShiftIrBits ) );
1162
1163                 /* Shift the data */
1164                 iErrorCode  = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTIR,
1165                                          lShiftIrBits, &(pXsvfInfo->lvTdi),
1166                                          /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1167                                          /*plvTdoMask*/0, pXsvfInfo->ucEndIR,
1168                                          pXsvfInfo->lRunTestTime, /*ucMaxRepeat*/0 );
1169         }
1170
1171         if ( iErrorCode != XSVF_ERROR_NONE )
1172         {
1173                 pXsvfInfo->iErrorCode   = iErrorCode;
1174         }
1175         return( iErrorCode );
1176 }
1177
1178 /*****************************************************************************
1179  * Function:     xsvfDoXSDR
1180  * Description:  XSDR <lenVal.TDI[XSDRSIZE]>
1181  *               Shift the given TDI data into the JTAG scan chain.
1182  *               Compare the captured TDO with the expected TDO from the
1183  *               previous XSDRTDO command using the previously specified
1184  *               XTDOMASK.
1185  * Parameters:   pXsvfInfo   - XSVF information pointer.
1186  * Returns:      int         - 0 = success;  non-zero = error.
1187  *****************************************************************************/
1188 int xsvfDoXSDR( SXsvfInfo* pXsvfInfo )
1189 {
1190         int iErrorCode;
1191         readVal( &(pXsvfInfo->lvTdi), pXsvfInfo->sShiftLengthBytes );
1192         /* use TDOExpected from last XSDRTDO instruction */
1193         iErrorCode  = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTDR,
1194                                  pXsvfInfo->lShiftLengthBits, &(pXsvfInfo->lvTdi),
1195                                  &(pXsvfInfo->lvTdoCaptured),
1196                                  &(pXsvfInfo->lvTdoExpected),
1197                                  &(pXsvfInfo->lvTdoMask), pXsvfInfo->ucEndDR,
1198                                  pXsvfInfo->lRunTestTime, pXsvfInfo->ucMaxRepeat );
1199         if ( iErrorCode != XSVF_ERROR_NONE )
1200         {
1201                 pXsvfInfo->iErrorCode   = iErrorCode;
1202         }
1203         return( iErrorCode );
1204 }
1205
1206 /*****************************************************************************
1207  * Function:     xsvfDoXRUNTEST
1208  * Description:  XRUNTEST <uint32>
1209  *               Prespecify the XRUNTEST wait time for shift operations.
1210  * Parameters:   pXsvfInfo   - XSVF information pointer.
1211  * Returns:      int         - 0 = success;  non-zero = error.
1212  *****************************************************************************/
1213 int xsvfDoXRUNTEST( SXsvfInfo* pXsvfInfo )
1214 {
1215         readVal( &(pXsvfInfo->lvTdi), 4 );
1216         pXsvfInfo->lRunTestTime = value( &(pXsvfInfo->lvTdi) );
1217         XSVFDBG_PRINTF1( 3, "   XRUNTEST = %ld\n", pXsvfInfo->lRunTestTime );
1218         return( XSVF_ERROR_NONE );
1219 }
1220
1221 /*****************************************************************************
1222  * Function:     xsvfDoXREPEAT
1223  * Description:  XREPEAT <byte>
1224  *               Prespecify the maximum number of XC9500/XL retries.
1225  * Parameters:   pXsvfInfo   - XSVF information pointer.
1226  * Returns:      int         - 0 = success;  non-zero = error.
1227  *****************************************************************************/
1228 int xsvfDoXREPEAT( SXsvfInfo* pXsvfInfo )
1229 {
1230         readByte( &(pXsvfInfo->ucMaxRepeat) );
1231         XSVFDBG_PRINTF1( 3, "   XREPEAT = %d\n",
1232                          ((unsigned int)(pXsvfInfo->ucMaxRepeat)) );
1233         return( XSVF_ERROR_NONE );
1234 }
1235
1236 /*****************************************************************************
1237  * Function:     xsvfDoXSDRSIZE
1238  * Description:  XSDRSIZE <uint32>
1239  *               Prespecify the XRUNTEST wait time for shift operations.
1240  * Parameters:   pXsvfInfo   - XSVF information pointer.
1241  * Returns:      int         - 0 = success;  non-zero = error.
1242  *****************************************************************************/
1243 int xsvfDoXSDRSIZE( SXsvfInfo* pXsvfInfo )
1244 {
1245         int iErrorCode;
1246         iErrorCode  = XSVF_ERROR_NONE;
1247         readVal( &(pXsvfInfo->lvTdi), 4 );
1248         pXsvfInfo->lShiftLengthBits = value( &(pXsvfInfo->lvTdi) );
1249         pXsvfInfo->sShiftLengthBytes= xsvfGetAsNumBytes( pXsvfInfo->lShiftLengthBits );
1250         XSVFDBG_PRINTF1( 3, "   XSDRSIZE = %ld\n", pXsvfInfo->lShiftLengthBits );
1251         if ( pXsvfInfo->sShiftLengthBytes > MAX_LEN )
1252         {
1253                 iErrorCode  = XSVF_ERROR_DATAOVERFLOW;
1254                 pXsvfInfo->iErrorCode   = iErrorCode;
1255         }
1256         return( iErrorCode );
1257 }
1258
1259 /*****************************************************************************
1260  * Function:     xsvfDoXSDRTDO
1261  * Description:  XSDRTDO <lenVal.TDI[XSDRSIZE]> <lenVal.TDO[XSDRSIZE]>
1262  *               Get the TDI and expected TDO values.  Then, shift.
1263  *               Compare the expected TDO with the captured TDO using the
1264  *               prespecified XTDOMASK.
1265  * Parameters:   pXsvfInfo   - XSVF information pointer.
1266  * Returns:      int         - 0 = success;  non-zero = error.
1267  *****************************************************************************/
1268 int xsvfDoXSDRTDO( SXsvfInfo* pXsvfInfo )
1269 {
1270         int iErrorCode;
1271         iErrorCode  = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1272                                         pXsvfInfo->lShiftLengthBits,
1273                                         pXsvfInfo->sShiftLengthBytes,
1274                                         &(pXsvfInfo->lvTdi),
1275                                         &(pXsvfInfo->lvTdoCaptured),
1276                                         &(pXsvfInfo->lvTdoExpected),
1277                                         &(pXsvfInfo->lvTdoMask),
1278                                         pXsvfInfo->ucEndDR,
1279                                         pXsvfInfo->lRunTestTime,
1280                                         pXsvfInfo->ucMaxRepeat );
1281         if ( iErrorCode != XSVF_ERROR_NONE )
1282         {
1283                 pXsvfInfo->iErrorCode   = iErrorCode;
1284         }
1285         return( iErrorCode );
1286 }
1287
1288 /*****************************************************************************
1289  * Function:     xsvfDoXSETSDRMASKS
1290  * Description:  XSETSDRMASKS <lenVal.AddressMask[XSDRSIZE]>
1291  *                            <lenVal.DataMask[XSDRSIZE]>
1292  *               Get the prespecified address and data mask for the XSDRINC
1293  *               command.
1294  *               Used for xc9500/xl compressed XSVF data.
1295  * Parameters:   pXsvfInfo   - XSVF information pointer.
1296  * Returns:      int         - 0 = success;  non-zero = error.
1297  *****************************************************************************/
1298 #ifdef  XSVF_SUPPORT_COMPRESSION
1299 int xsvfDoXSETSDRMASKS( SXsvfInfo* pXsvfInfo )
1300 {
1301         /* read the addressMask */
1302         readVal( &(pXsvfInfo->lvAddressMask), pXsvfInfo->sShiftLengthBytes );
1303         /* read the dataMask    */
1304         readVal( &(pXsvfInfo->lvDataMask), pXsvfInfo->sShiftLengthBytes );
1305
1306         XSVFDBG_PRINTF( 4, "    Address Mask = " );
1307         XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvAddressMask) );
1308         XSVFDBG_PRINTF( 4, "\n" );
1309         XSVFDBG_PRINTF( 4, "    Data Mask    = " );
1310         XSVFDBG_PRINTLENVAL( 4, &(pXsvfInfo->lvDataMask) );
1311         XSVFDBG_PRINTF( 4, "\n" );
1312
1313         return( XSVF_ERROR_NONE );
1314 }
1315 #endif  /* XSVF_SUPPORT_COMPRESSION */
1316
1317 /*****************************************************************************
1318  * Function:     xsvfDoXSDRINC
1319  * Description:  XSDRINC <lenVal.firstTDI[XSDRSIZE]> <byte(numTimes)>
1320  *                       <lenVal.data[XSETSDRMASKS.dataMask.len]> ...
1321  *               Get the XSDRINC parameters and execute the XSDRINC command.
1322  *               XSDRINC starts by loading the first TDI shift value.
1323  *               Then, for numTimes, XSDRINC gets the next piece of data,
1324  *               replaces the bits from the starting TDI as defined by the
1325  *               XSETSDRMASKS.dataMask, adds the address mask from
1326  *               XSETSDRMASKS.addressMask, shifts the new TDI value,
1327  *               and compares the TDO to the expected TDO from the previous
1328  *               XSDRTDO command using the XTDOMASK.
1329  *               Used for xc9500/xl compressed XSVF data.
1330  * Parameters:   pXsvfInfo   - XSVF information pointer.
1331  * Returns:      int         - 0 = success;  non-zero = error.
1332  *****************************************************************************/
1333 #ifdef  XSVF_SUPPORT_COMPRESSION
1334 int xsvfDoXSDRINC( SXsvfInfo* pXsvfInfo )
1335 {
1336         int             iErrorCode;
1337         int             iDataMaskLen;
1338         unsigned char   ucDataMask;
1339         unsigned char   ucNumTimes;
1340         unsigned char   i;
1341
1342         readVal( &(pXsvfInfo->lvTdi), pXsvfInfo->sShiftLengthBytes );
1343         iErrorCode  = xsvfShift( &(pXsvfInfo->ucTapState), XTAPSTATE_SHIFTDR,
1344                                  pXsvfInfo->lShiftLengthBits,
1345                                  &(pXsvfInfo->lvTdi), &(pXsvfInfo->lvTdoCaptured),
1346                                  &(pXsvfInfo->lvTdoExpected),
1347                                  &(pXsvfInfo->lvTdoMask), pXsvfInfo->ucEndDR,
1348                                  pXsvfInfo->lRunTestTime, pXsvfInfo->ucMaxRepeat );
1349         if ( !iErrorCode )
1350         {
1351                 /* Calculate number of data mask bits */
1352                 iDataMaskLen    = 0;
1353                 for ( i = 0; i < pXsvfInfo->lvDataMask.len; ++i )
1354                 {
1355                         ucDataMask  = pXsvfInfo->lvDataMask.val[ i ];
1356                         while ( ucDataMask )
1357                         {
1358                                 iDataMaskLen    += ( ucDataMask & 1 );
1359                                 ucDataMask      >>= 1;
1360                         }
1361                 }
1362
1363                 /* Get the number of data pieces, i.e. number of times to shift */
1364                 readByte( &ucNumTimes );
1365
1366                 /* For numTimes, get data, fix TDI, and shift */
1367                 for ( i = 0; !iErrorCode && ( i < ucNumTimes ); ++i )
1368                 {
1369                         readVal( &(pXsvfInfo->lvNextData),
1370                                  xsvfGetAsNumBytes( iDataMaskLen ) );
1371                         xsvfDoSDRMasking( &(pXsvfInfo->lvTdi),
1372                                           &(pXsvfInfo->lvNextData),
1373                                           &(pXsvfInfo->lvAddressMask),
1374                                           &(pXsvfInfo->lvDataMask) );
1375                         iErrorCode  = xsvfShift( &(pXsvfInfo->ucTapState),
1376                                                  XTAPSTATE_SHIFTDR,
1377                                                  pXsvfInfo->lShiftLengthBits,
1378                                                  &(pXsvfInfo->lvTdi),
1379                                                  &(pXsvfInfo->lvTdoCaptured),
1380                                                  &(pXsvfInfo->lvTdoExpected),
1381                                                  &(pXsvfInfo->lvTdoMask),
1382                                                  pXsvfInfo->ucEndDR,
1383                                                  pXsvfInfo->lRunTestTime,
1384                                                  pXsvfInfo->ucMaxRepeat );
1385                 }
1386         }
1387         if ( iErrorCode != XSVF_ERROR_NONE )
1388         {
1389                 pXsvfInfo->iErrorCode   = iErrorCode;
1390         }
1391         return( iErrorCode );
1392 }
1393 #endif  /* XSVF_SUPPORT_COMPRESSION */
1394
1395 /*****************************************************************************
1396  * Function:     xsvfDoXSDRBCE
1397  * Description:  XSDRB/XSDRC/XSDRE <lenVal.TDI[XSDRSIZE]>
1398  *               If not already in SHIFTDR, goto SHIFTDR.
1399  *               Shift the given TDI data into the JTAG scan chain.
1400  *               Ignore TDO.
1401  *               If cmd==XSDRE, then goto ENDDR.  Otherwise, stay in ShiftDR.
1402  *               XSDRB, XSDRC, and XSDRE are the same implementation.
1403  * Parameters:   pXsvfInfo   - XSVF information pointer.
1404  * Returns:      int         - 0 = success;  non-zero = error.
1405  *****************************************************************************/
1406 int xsvfDoXSDRBCE( SXsvfInfo* pXsvfInfo )
1407 {
1408         unsigned char   ucEndDR;
1409         int             iErrorCode;
1410         ucEndDR = (unsigned char)(( pXsvfInfo->ucCommand == XSDRE ) ?
1411                                   pXsvfInfo->ucEndDR : XTAPSTATE_SHIFTDR);
1412         iErrorCode  = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1413                                         pXsvfInfo->lShiftLengthBits,
1414                                         pXsvfInfo->sShiftLengthBytes,
1415                                         &(pXsvfInfo->lvTdi),
1416                                         /*plvTdoCaptured*/0, /*plvTdoExpected*/0,
1417                                         /*plvTdoMask*/0, ucEndDR,
1418                                         /*lRunTestTime*/0, /*ucMaxRepeat*/0 );
1419         if ( iErrorCode != XSVF_ERROR_NONE )
1420         {
1421                 pXsvfInfo->iErrorCode   = iErrorCode;
1422         }
1423         return( iErrorCode );
1424 }
1425
1426 /*****************************************************************************
1427  * Function:     xsvfDoXSDRTDOBCE
1428  * Description:  XSDRB/XSDRC/XSDRE <lenVal.TDI[XSDRSIZE]> <lenVal.TDO[XSDRSIZE]>
1429  *               If not already in SHIFTDR, goto SHIFTDR.
1430  *               Shift the given TDI data into the JTAG scan chain.
1431  *               Compare TDO, but do NOT use XTDOMASK.
1432  *               If cmd==XSDRTDOE, then goto ENDDR.  Otherwise, stay in ShiftDR.
1433  *               XSDRTDOB, XSDRTDOC, and XSDRTDOE are the same implementation.
1434  * Parameters:   pXsvfInfo   - XSVF information pointer.
1435  * Returns:      int         - 0 = success;  non-zero = error.
1436  *****************************************************************************/
1437 int xsvfDoXSDRTDOBCE( SXsvfInfo* pXsvfInfo )
1438 {
1439         unsigned char   ucEndDR;
1440         int             iErrorCode;
1441         ucEndDR = (unsigned char)(( pXsvfInfo->ucCommand == XSDRTDOE ) ?
1442                                   pXsvfInfo->ucEndDR : XTAPSTATE_SHIFTDR);
1443         iErrorCode  = xsvfBasicXSDRTDO( &(pXsvfInfo->ucTapState),
1444                                         pXsvfInfo->lShiftLengthBits,
1445                                         pXsvfInfo->sShiftLengthBytes,
1446                                         &(pXsvfInfo->lvTdi),
1447                                         &(pXsvfInfo->lvTdoCaptured),
1448                                         &(pXsvfInfo->lvTdoExpected),
1449                                         /*plvTdoMask*/0, ucEndDR,
1450                                         /*lRunTestTime*/0, /*ucMaxRepeat*/0 );
1451         if ( iErrorCode != XSVF_ERROR_NONE )
1452         {
1453                 pXsvfInfo->iErrorCode   = iErrorCode;
1454         }
1455         return( iErrorCode );
1456 }
1457
1458 /*****************************************************************************
1459  * Function:     xsvfDoXSTATE
1460  * Description:  XSTATE <byte>
1461  *               <byte> == XTAPSTATE;
1462  *               Get the state parameter and transition the TAP to that state.
1463  * Parameters:   pXsvfInfo   - XSVF information pointer.
1464  * Returns:      int         - 0 = success;  non-zero = error.
1465  *****************************************************************************/
1466 int xsvfDoXSTATE( SXsvfInfo* pXsvfInfo )
1467 {
1468         unsigned char   ucNextState;
1469         int             iErrorCode;
1470         readByte( &ucNextState );
1471         iErrorCode  = xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucNextState );
1472         if ( iErrorCode != XSVF_ERROR_NONE )
1473         {
1474                 pXsvfInfo->iErrorCode   = iErrorCode;
1475         }
1476         return( iErrorCode );
1477 }
1478
1479 /*****************************************************************************
1480  * Function:     xsvfDoXENDXR
1481  * Description:  XENDIR/XENDDR <byte>
1482  *               <byte>:  0 = RUNTEST;  1 = PAUSE.
1483  *               Get the prespecified XENDIR or XENDDR.
1484  *               Both XENDIR and XENDDR use the same implementation.
1485  * Parameters:   pXsvfInfo   - XSVF information pointer.
1486  * Returns:      int         - 0 = success;  non-zero = error.
1487  *****************************************************************************/
1488 int xsvfDoXENDXR( SXsvfInfo* pXsvfInfo )
1489 {
1490         int             iErrorCode;
1491         unsigned char   ucEndState;
1492
1493         iErrorCode  = XSVF_ERROR_NONE;
1494         readByte( &ucEndState );
1495         if ( ( ucEndState != XENDXR_RUNTEST ) && ( ucEndState != XENDXR_PAUSE ) )
1496         {
1497                 iErrorCode  = XSVF_ERROR_ILLEGALSTATE;
1498         }
1499         else
1500         {
1501
1502                 if ( pXsvfInfo->ucCommand == XENDIR )
1503                 {
1504                         if ( ucEndState == XENDXR_RUNTEST )
1505                         {
1506                                 pXsvfInfo->ucEndIR  = XTAPSTATE_RUNTEST;
1507                         }
1508                         else
1509                         {
1510                                 pXsvfInfo->ucEndIR  = XTAPSTATE_PAUSEIR;
1511                         }
1512                         XSVFDBG_PRINTF1( 3, "   ENDIR State = %s\n",
1513                                          xsvf_pzTapState[ pXsvfInfo->ucEndIR ] );
1514                 }
1515                 else    /* XENDDR */
1516                 {
1517                         if ( ucEndState == XENDXR_RUNTEST )
1518                         {
1519                                 pXsvfInfo->ucEndDR  = XTAPSTATE_RUNTEST;
1520                         }
1521                         else
1522                         {
1523                                 pXsvfInfo->ucEndDR  = XTAPSTATE_PAUSEDR;
1524                         }
1525                         XSVFDBG_PRINTF1( 3, "   ENDDR State = %s\n",
1526                                          xsvf_pzTapState[ pXsvfInfo->ucEndDR ] );
1527                 }
1528         }
1529
1530         if ( iErrorCode != XSVF_ERROR_NONE )
1531         {
1532                 pXsvfInfo->iErrorCode   = iErrorCode;
1533         }
1534         return( iErrorCode );
1535 }
1536
1537 /*****************************************************************************
1538  * Function:     xsvfDoXCOMMENT
1539  * Description:  XCOMMENT <text string ending in \0>
1540  *               <text string ending in \0> == text comment;
1541  *               Arbitrary comment embedded in the XSVF.
1542  * Parameters:   pXsvfInfo   - XSVF information pointer.
1543  * Returns:      int         - 0 = success;  non-zero = error.
1544  *****************************************************************************/
1545 int xsvfDoXCOMMENT( SXsvfInfo* pXsvfInfo )
1546 {
1547         /* Use the comment for debugging */
1548         /* Otherwise, read through the comment to the end '\0' and ignore */
1549         unsigned char   ucText;
1550
1551         if ( xsvf_iDebugLevel > 0 )
1552         {
1553                 putc( ' ' );
1554         }
1555
1556         do
1557         {
1558                 readByte( &ucText );
1559                 if ( xsvf_iDebugLevel > 0 )
1560                 {
1561                         putc( ucText ? ucText : '\n' );
1562                 }
1563         } while ( ucText );
1564
1565         pXsvfInfo->iErrorCode   = XSVF_ERROR_NONE;
1566
1567         return( pXsvfInfo->iErrorCode );
1568 }
1569
1570 /*****************************************************************************
1571  * Function:     xsvfDoXWAIT
1572  * Description:  XWAIT <wait_state> <end_state> <wait_time>
1573  *               If not already in <wait_state>, then go to <wait_state>.
1574  *               Wait in <wait_state> for <wait_time> microseconds.
1575  *               Finally, if not already in <end_state>, then goto <end_state>.
1576  * Parameters:   pXsvfInfo   - XSVF information pointer.
1577  * Returns:      int         - 0 = success;  non-zero = error.
1578  *****************************************************************************/
1579 int xsvfDoXWAIT( SXsvfInfo* pXsvfInfo )
1580 {
1581         unsigned char   ucWaitState;
1582         unsigned char   ucEndState;
1583         long            lWaitTime;
1584
1585         /* Get Parameters */
1586         /* <wait_state> */
1587         readVal( &(pXsvfInfo->lvTdi), 1 );
1588         ucWaitState = pXsvfInfo->lvTdi.val[0];
1589
1590         /* <end_state> */
1591         readVal( &(pXsvfInfo->lvTdi), 1 );
1592         ucEndState = pXsvfInfo->lvTdi.val[0];
1593
1594         /* <wait_time> */
1595         readVal( &(pXsvfInfo->lvTdi), 4 );
1596         lWaitTime = value( &(pXsvfInfo->lvTdi) );
1597         XSVFDBG_PRINTF2( 3, "   XWAIT:  state = %s; time = %ld\n",
1598                          xsvf_pzTapState[ ucWaitState ], lWaitTime );
1599
1600         /* If not already in <wait_state>, go to <wait_state> */
1601         if ( pXsvfInfo->ucTapState != ucWaitState )
1602         {
1603                 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucWaitState );
1604         }
1605
1606         /* Wait for <wait_time> microseconds */
1607         waitTime( lWaitTime );
1608
1609         /* If not already in <end_state>, go to <end_state> */
1610         if ( pXsvfInfo->ucTapState != ucEndState )
1611         {
1612                 xsvfGotoTapState( &(pXsvfInfo->ucTapState), ucEndState );
1613         }
1614
1615         return( XSVF_ERROR_NONE );
1616 }
1617
1618
1619 /*============================================================================
1620  * Execution Control Functions
1621  ============================================================================*/
1622
1623 /*****************************************************************************
1624  * Function:     xsvfInitialize
1625  * Description:  Initialize the xsvf player.
1626  *               Call this before running the player to initialize the data
1627  *               in the SXsvfInfo struct.
1628  *               xsvfCleanup is called to clean up the data in SXsvfInfo
1629  *               after the XSVF is played.
1630  * Parameters:   pXsvfInfo   - ptr to the XSVF information.
1631  * Returns:      int - 0 = success; otherwise error.
1632  *****************************************************************************/
1633 int xsvfInitialize( SXsvfInfo* pXsvfInfo )
1634 {
1635         /* Initialize values */
1636         pXsvfInfo->iErrorCode   = xsvfInfoInit( pXsvfInfo );
1637
1638         if ( !pXsvfInfo->iErrorCode )
1639         {
1640                 /* Initialize the TAPs */
1641                 pXsvfInfo->iErrorCode   = xsvfGotoTapState( &(pXsvfInfo->ucTapState),
1642                                                             XTAPSTATE_RESET );
1643         }
1644
1645         return( pXsvfInfo->iErrorCode );
1646 }
1647
1648 /*****************************************************************************
1649  * Function:     xsvfRun
1650  * Description:  Run the xsvf player for a single command and return.
1651  *               First, call xsvfInitialize.
1652  *               Then, repeatedly call this function until an error is detected
1653  *               or until the pXsvfInfo->ucComplete variable is non-zero.
1654  *               Finally, call xsvfCleanup to cleanup any remnants.
1655  * Parameters:   pXsvfInfo   - ptr to the XSVF information.
1656  * Returns:      int         - 0 = success; otherwise error.
1657  *****************************************************************************/
1658 int xsvfRun( SXsvfInfo* pXsvfInfo )
1659 {
1660         /* Process the XSVF commands */
1661         if ( (!pXsvfInfo->iErrorCode) && (!pXsvfInfo->ucComplete) )
1662         {
1663                 /* read 1 byte for the instruction */
1664                 readByte( &(pXsvfInfo->ucCommand) );
1665                 ++(pXsvfInfo->lCommandCount);
1666
1667                 if ( pXsvfInfo->ucCommand < XLASTCMD )
1668                 {
1669                         /* Execute the command.  Func sets error code. */
1670                         XSVFDBG_PRINTF1( 2, "  %s\n",
1671                                          xsvf_pzCommandName[pXsvfInfo->ucCommand] );
1672                         /* If your compiler cannot take this form,
1673                            then convert to a switch statement */
1674 #if 0 /* test-only */
1675                         xsvf_pfDoCmd[ pXsvfInfo->ucCommand ]( pXsvfInfo );
1676 #else
1677                         switch (pXsvfInfo->ucCommand) {
1678                         case 0:
1679                                 xsvfDoXCOMPLETE(pXsvfInfo);        /*  0 */
1680                                 break;
1681                         case 1:
1682                                 xsvfDoXTDOMASK(pXsvfInfo);         /*  1 */
1683                                 break;
1684                         case 2:
1685                                 xsvfDoXSIR(pXsvfInfo);             /*  2 */
1686                                 break;
1687                         case 3:
1688                                 xsvfDoXSDR(pXsvfInfo);             /*  3 */
1689                                 break;
1690                         case 4:
1691                                 xsvfDoXRUNTEST(pXsvfInfo);         /*  4 */
1692                                 break;
1693                         case 5:
1694                                 xsvfDoIllegalCmd(pXsvfInfo);       /*  5 */
1695                                 break;
1696                         case 6:
1697                                 xsvfDoIllegalCmd(pXsvfInfo);       /*  6 */
1698                                 break;
1699                         case 7:
1700                                 xsvfDoXREPEAT(pXsvfInfo);          /*  7 */
1701                                 break;
1702                         case 8:
1703                                 xsvfDoXSDRSIZE(pXsvfInfo);         /*  8 */
1704                                 break;
1705                         case 9:
1706                                 xsvfDoXSDRTDO(pXsvfInfo);          /*  9 */
1707                                 break;
1708 #ifdef  XSVF_SUPPORT_COMPRESSION
1709                         case 10:
1710                                 xsvfDoXSETSDRMASKS(pXsvfInfo);     /* 10 */
1711                                 break;
1712                         case 11:
1713                                 xsvfDoXSDRINC(pXsvfInfo);          /* 11 */
1714                                 break;
1715 #else
1716                         case 10:
1717                                 xsvfDoIllegalCmd(pXsvfInfo);       /* 10 */
1718                                 break;
1719                         case 11:
1720                                 xsvfDoIllegalCmd(pXsvfInfo);       /* 11 */
1721                                 break;
1722 #endif  /* XSVF_SUPPORT_COMPRESSION */
1723                         case 12:
1724                                 xsvfDoXSDRBCE(pXsvfInfo);          /* 12 */
1725                                 break;
1726                         case 13:
1727                                 xsvfDoXSDRBCE(pXsvfInfo);          /* 13 */
1728                                 break;
1729                         case 14:
1730                                 xsvfDoXSDRBCE(pXsvfInfo);          /* 14 */
1731                                 break;
1732                         case 15:
1733                                 xsvfDoXSDRTDOBCE(pXsvfInfo);       /* 15 */
1734                                 break;
1735                         case 16:
1736                                 xsvfDoXSDRTDOBCE(pXsvfInfo);       /* 16 */
1737                                 break;
1738                         case 17:
1739                                 xsvfDoXSDRTDOBCE(pXsvfInfo);       /* 17 */
1740                                 break;
1741                         case 18:
1742                                 xsvfDoXSTATE(pXsvfInfo);           /* 18 */
1743                                 break;
1744                         case 19:
1745                                 xsvfDoXENDXR(pXsvfInfo);           /* 19 */
1746                                 break;
1747                         case 20:
1748                                 xsvfDoXENDXR(pXsvfInfo);           /* 20 */
1749                                 break;
1750                         case 21:
1751                                 xsvfDoXSIR2(pXsvfInfo);            /* 21 */
1752                                 break;
1753                         case 22:
1754                                 xsvfDoXCOMMENT(pXsvfInfo);         /* 22 */
1755                                 break;
1756                         case 23:
1757                                 xsvfDoXWAIT(pXsvfInfo);             /* 23 */
1758                                 break;
1759                         }
1760 #endif
1761                 }
1762                 else
1763                 {
1764                         /* Illegal command value.  Func sets error code. */
1765                         xsvfDoIllegalCmd( pXsvfInfo );
1766                 }
1767         }
1768
1769         return( pXsvfInfo->iErrorCode );
1770 }
1771
1772 /*****************************************************************************
1773  * Function:     xsvfCleanup
1774  * Description:  cleanup remnants of the xsvf player.
1775  * Parameters:   pXsvfInfo   - ptr to the XSVF information.
1776  * Returns:      void.
1777  *****************************************************************************/
1778 void xsvfCleanup( SXsvfInfo* pXsvfInfo )
1779 {
1780         xsvfInfoCleanup( pXsvfInfo );
1781 }
1782
1783
1784 /*============================================================================
1785  * xsvfExecute() - The primary entry point to the XSVF player
1786  ============================================================================*/
1787
1788 /*****************************************************************************
1789  * Function:     xsvfExecute
1790  * Description:  Process, interpret, and apply the XSVF commands.
1791  *               See port.c:readByte for source of XSVF data.
1792  * Parameters:   none.
1793  * Returns:      int - Legacy result values:  1 == success;  0 == failed.
1794  *****************************************************************************/
1795 int xsvfExecute(void)
1796 {
1797         SXsvfInfo   xsvfInfo;
1798
1799         xsvfInitialize( &xsvfInfo );
1800
1801         while ( !xsvfInfo.iErrorCode && (!xsvfInfo.ucComplete) )
1802         {
1803                 xsvfRun( &xsvfInfo );
1804         }
1805
1806         if ( xsvfInfo.iErrorCode )
1807         {
1808                 XSVFDBG_PRINTF1( 0, "%s\n", xsvf_pzErrorName[
1809                                          ( xsvfInfo.iErrorCode < XSVF_ERROR_LAST )
1810                                          ? xsvfInfo.iErrorCode : XSVF_ERROR_UNKNOWN ] );
1811                 XSVFDBG_PRINTF2( 0, "ERROR at or near XSVF command #%ld.  See line #%ld in the XSVF ASCII file.\n",
1812                                  xsvfInfo.lCommandCount, xsvfInfo.lCommandCount );
1813         }
1814         else
1815         {
1816                 XSVFDBG_PRINTF( 0, "SUCCESS - Completed XSVF execution.\n" );
1817         }
1818
1819         xsvfCleanup( &xsvfInfo );
1820
1821         return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) );
1822 }
1823
1824
1825 /*****************************************************************************
1826  * Function:     do_cpld
1827  * Description:  main function.
1828  *               Specified here for creating stand-alone debug executable.
1829  *               Embedded users should call xsvfExecute() directly.
1830  * Parameters:   iArgc    - number of command-line arguments.
1831  *               ppzArgv  - array of ptrs to strings (command-line arguments).
1832  * Returns:      int      - Legacy return value:  1 = success; 0 = error.
1833  *****************************************************************************/
1834 int do_cpld(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1835 {
1836         int     iErrorCode;
1837         char*   pzXsvfFileName;
1838         unsigned long duration;
1839         unsigned long long startClock, endClock;
1840
1841         iErrorCode          = XSVF_ERRORCODE( XSVF_ERROR_NONE );
1842         pzXsvfFileName      = 0;
1843         xsvf_iDebugLevel    = 0;
1844
1845         printf("XSVF Player v%s, Xilinx, Inc.\n", XSVF_VERSION);
1846         printf("XSVF Filesize = %d bytes\n", filesize);
1847
1848         /* Initialize the I/O.  SetPort initializes I/O on first call */
1849         setPort( TMS, 1 );
1850
1851         /* Execute the XSVF in the file */
1852         startClock  = get_ticks();
1853         iErrorCode  = xsvfExecute();
1854         endClock    = get_ticks();
1855         duration    = (unsigned long)(endClock - startClock);
1856         printf("\nExecution Time = %d seconds\n", (int)(duration/get_tbclk()));
1857
1858         return( iErrorCode );
1859 }
1860 U_BOOT_CMD(
1861         cpld,   1,      1,      do_cpld,
1862         "cpld    - Program onboard CPLD\n",
1863         NULL
1864         );