]> git.gag.com Git - fw/openocd/blob - src/xsvf/xsvf.c
Encapsulate JTAG Cable API and interface structure, plan for new header file.
[fw/openocd] / src / xsvf / xsvf.c
1 /***************************************************************************
2  *       Copyright (C) 2005 by Dominic Rath                                                                        *
3  *       Dominic.Rath@gmx.de                                                                                                       *
4  *                                                                                                                                                 *
5  *       Copyright (C) 2007,2008 Ã˜yvind Harboe                                                             *
6  *       oyvind.harboe@zylin.com                                                                                                   *
7  *                                                                                                                                                 *
8  *       Copyright (C) 2008 Peter Hettkamp                                                                         *
9  *       peter.hettkamp@htp-tel.de                                                                                         *
10  *                                                                                                                                                 *
11  *       Copyright (C) 2009 SoftPLC Corporation. http://softplc.com             *
12  *       Dick Hollenbeck <dick@softplc.com>                                                                        *
13  *                                                                          *
14  *       This program is free software; you can redistribute it and/or modify   *
15  *       it under the terms of the GNU General Public License as published by   *
16  *       the Free Software Foundation; either version 2 of the License, or         *
17  *       (at your option) any later version.                                                                       *
18  *                                                                                                                                                 *
19  *       This program is distributed in the hope that it will be useful,                   *
20  *       but WITHOUT ANY WARRANTY; without even the implied warranty of            *
21  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
22  *       GNU General Public License for more details.                                              *
23  *                                                                                                                                                 *
24  *       You should have received a copy of the GNU General Public License         *
25  *       along with this program; if not, write to the                                             *
26  *       Free Software Foundation, Inc.,                                                                                   *
27  *       59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                         *
28  ***************************************************************************/
29
30
31 /* The specification for SVF is available here:
32  * http://www.asset-intertech.com/support/svf.pdf
33  * Below, this document is refered to as the "SVF spec".
34  *
35  * The specification for XSVF is available here:
36  * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
37  * Below, this document is refered to as the "XSVF spec".
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #define INCLUDE_JTAG_INTERFACE_H
45 #include "xsvf.h"
46 #include "jtag.h"
47
48
49 /* XSVF commands, from appendix B of xapp503.pdf  */
50 #define XCOMPLETE               0x00
51 #define XTDOMASK                        0x01
52 #define XSIR                            0x02
53 #define XSDR                            0x03
54 #define XRUNTEST                        0x04
55 #define XREPEAT                 0x07
56 #define XSDRSIZE                        0x08
57 #define XSDRTDO                 0x09
58 #define XSETSDRMASKS            0x0A
59 #define XSDRINC                 0x0B
60 #define XSDRB                   0x0C
61 #define XSDRC                   0x0D
62 #define XSDRE                   0x0E
63 #define XSDRTDOB                        0x0F
64 #define XSDRTDOC                        0x10
65 #define XSDRTDOE                        0x11
66 #define XSTATE                  0x12
67 #define XENDIR                  0x13
68 #define XENDDR                  0x14
69 #define XSIR2                   0x15
70 #define XCOMMENT                        0x16
71 #define XWAIT                   0x17
72
73 /* XWAITSTATE is not in the xilinx XSVF spec, but the svf2xsvf.py translator
74  * generates this.  Arguably it is needed because the XSVF XRUNTEST command
75  * was ill conceived and does not directly flow out of the SVF RUNTEST command.
76  * This XWAITSTATE does map directly from the SVF RUNTEST command.
77  */
78 #define XWAITSTATE              0x18
79
80 /* Lattice has extended the SVF file format, and Dick Hollenbeck's python based
81  * SVF2XSVF converter supports these 3 additional XSVF opcodes, LCOUNT, LDELAY, LSDR.
82  * Here is an example of usage of the 3 lattice opcode extensions:
83
84 ! Set the maximum loop count to 25.
85 LCOUNT  25;
86 ! Step to DRPAUSE give 5 clocks and wait for 1.00e+000 SEC.
87 LDELAY  DRPAUSE 5 TCK   1.00E-003 SEC;
88 ! Test for the completed status. Match means pass.
89 ! Loop back to LDELAY line if not match and loop count less than 25.
90
91 LSDR 1  TDI  (0)
92                 TDO  (1);
93 */
94
95 #define LCOUNT                  0x19
96 #define LDELAY                  0x1A
97 #define LSDR                            0x1B
98 #define XTRST                   0x1C
99
100
101 /* XSVF valid state values for the XSTATE command, from appendix B of xapp503.pdf */
102 #define XSV_RESET               0x00
103 #define XSV_IDLE                        0x01
104 #define XSV_DRSELECT            0x02
105 #define XSV_DRCAPTURE   0x03
106 #define XSV_DRSHIFT             0x04
107 #define XSV_DREXIT1             0x05
108 #define XSV_DRPAUSE             0x06
109 #define XSV_DREXIT2             0x07
110 #define XSV_DRUPDATE            0x08
111 #define XSV_IRSELECT            0x09
112 #define XSV_IRCAPTURE   0x0A
113 #define XSV_IRSHIFT             0x0B
114 #define XSV_IREXIT1             0x0C
115 #define XSV_IRPAUSE             0x0D
116 #define XSV_IREXIT2             0x0E
117 #define XSV_IRUPDATE            0x0F
118
119 /* arguments to XTRST */
120 #define XTRST_ON                        0
121 #define XTRST_OFF               1
122 #define XTRST_Z                 2
123 #define XTRST_ABSENT            3
124
125 #define XSTATE_MAX_PATH 12
126
127 static int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
128
129 static int xsvf_fd = 0;
130
131
132 /* map xsvf tap state to an openocd "tap_state_t" */
133 static tap_state_t xsvf_to_tap( int xsvf_state )
134 {
135         tap_state_t     ret;
136
137         switch( xsvf_state )
138         {
139         case XSV_RESET:                 ret = TAP_RESET;                        break;
140         case XSV_IDLE:                  ret = TAP_IDLE;                 break;
141         case XSV_DRSELECT:              ret = TAP_DRSELECT;             break;
142         case XSV_DRCAPTURE:             ret = TAP_DRCAPTURE;            break;
143         case XSV_DRSHIFT:               ret = TAP_DRSHIFT;              break;
144         case XSV_DREXIT1:               ret = TAP_DREXIT1;              break;
145         case XSV_DRPAUSE:               ret = TAP_DRPAUSE;              break;
146         case XSV_DREXIT2:               ret = TAP_DREXIT2;              break;
147         case XSV_DRUPDATE:              ret = TAP_DRUPDATE;             break;
148         case XSV_IRSELECT:              ret = TAP_IRSELECT;             break;
149         case XSV_IRCAPTURE:             ret = TAP_IRCAPTURE;            break;
150         case XSV_IRSHIFT:               ret = TAP_IRSHIFT;              break;
151         case XSV_IREXIT1:               ret = TAP_IREXIT1;              break;
152         case XSV_IRPAUSE:               ret = TAP_IRPAUSE;              break;
153         case XSV_IREXIT2:               ret = TAP_IREXIT2;              break;
154         case XSV_IRUPDATE:              ret = TAP_IRUPDATE;             break;
155         default:
156                 LOG_ERROR( "UNKNOWN XSVF STATE 0x%02X", xsvf_state );
157                 exit(1);
158         }
159
160         return ret;
161 }
162
163
164
165 int xsvf_register_commands(struct command_context_s *cmd_ctx)
166 {
167         register_command(cmd_ctx, NULL, "xsvf", handle_xsvf_command,
168                 COMMAND_EXEC, "run xsvf <file> [virt2] [quiet]");
169
170         return ERROR_OK;
171 }
172
173 static int xsvf_read_buffer(int num_bits, int fd, u8* buf)
174 {
175         int num_bytes;
176
177         for (num_bytes = (num_bits + 7) / 8; num_bytes > 0; num_bytes--)
178         {
179                 /* reverse the order of bytes as they are read sequentially from file */
180                 if (read(fd, buf + num_bytes - 1, 1) < 0)
181                         return ERROR_XSVF_EOF;
182         }
183
184         return ERROR_OK;
185 }
186
187
188 static int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
189 {
190         u8 *dr_out_buf = NULL;                          /* from host to device (TDI) */
191         u8 *dr_in_buf = NULL;                           /* from device to host (TDO) */
192         u8 *dr_in_mask = NULL;
193
194         int xsdrsize = 0;
195         int xruntest = 0;                                       /* number of TCK cycles OR microseconds */
196         int xrepeat      = 0;                                   /* number of retries */
197
198         tap_state_t     xendir = TAP_IDLE;              /* see page 8 of the SVF spec, initial xendir to be TAP_IDLE */
199         tap_state_t xenddr = TAP_IDLE;
200
201         u8              opcode;
202         u8              uc;
203         long            file_offset = 0;
204
205         int             loop_count = 0;
206         tap_state_t     loop_state = TAP_IDLE;
207         int             loop_clocks = 0;
208         int             loop_usecs = 0;
209
210         int             do_abort = 0;
211         int             unsupported = 0;
212         int             tdo_mismatch = 0;
213         int             result;
214         int             verbose = 1;
215         char*   filename;
216
217         int             runtest_requires_tck = 0;       /* a flag telling whether to clock TCK during waits, or simply sleep, controled by virt2 */
218
219
220         /* use NULL to indicate a "plain" xsvf file which accounts for
221            additional devices in the scan chain, otherwise the device
222            that should be affected
223         */
224         jtag_tap_t *tap = NULL;
225
226         if (argc < 2)
227         {
228                 command_print(cmd_ctx, "usage: xsvf <device#|plain> <file> [<variant>] [quiet]");
229                 return ERROR_FAIL;
230         }
231
232         filename = args[1];             /* we mess with args starting point below, snapshot filename here */
233
234         if (strcmp(args[0], "plain") != 0)
235         {
236                 tap = jtag_TapByString( args[0] );
237                 if (!tap )
238                 {
239                         command_print( cmd_ctx, "Tap: %s unknown", args[0] );
240                         return ERROR_FAIL;
241                 }
242         }
243
244         if ((xsvf_fd = open(filename, O_RDONLY)) < 0)
245         {
246                 command_print(cmd_ctx, "file \"%s\" not found", filename);
247                 return ERROR_FAIL;
248         }
249
250         /* if this argument is present, then interpret xruntest counts as TCK cycles rather than as usecs */
251         if ((argc > 2) && (strcmp(args[2], "virt2") == 0))
252         {
253                 runtest_requires_tck = 1;
254                 --argc;
255                 ++args;
256         }
257
258         if ((argc > 2) && (strcmp(args[2], "quiet") == 0))
259         {
260                 verbose = 0;
261         }
262
263         LOG_USER("xsvf processing file: \"%s\"", filename);
264
265         while( read(xsvf_fd, &opcode, 1) > 0 )
266         {
267                 /* record the position of the just read opcode within the file */
268                 file_offset = lseek(xsvf_fd, 0, SEEK_CUR) - 1;
269
270                 switch (opcode)
271                 {
272                 case XCOMPLETE:
273                         LOG_DEBUG("XCOMPLETE");
274
275                         result = jtag_execute_queue();
276                         if (result != ERROR_OK)
277                         {
278                                 tdo_mismatch = 1;
279                                 break;
280                         }
281                         break;
282
283                 case XTDOMASK:
284                         LOG_DEBUG("XTDOMASK");
285                         if (dr_in_mask && (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_mask) != ERROR_OK))
286                                 do_abort = 1;
287                         break;
288
289                 case XRUNTEST:
290                         {
291                                 u8      xruntest_buf[4];
292
293                                 if (read(xsvf_fd, xruntest_buf, 4) < 0)
294                                 {
295                                         do_abort = 1;
296                                         break;
297                                 }
298
299                                 xruntest = be_to_h_u32(xruntest_buf);
300                                 LOG_DEBUG("XRUNTEST %d 0x%08X", xruntest, xruntest);
301                         }
302                         break;
303
304                 case XREPEAT:
305                         {
306                                 u8 myrepeat;
307
308                                 if (read(xsvf_fd, &myrepeat, 1) < 0)
309                                         do_abort = 1;
310                                 else
311                                 {
312                                         xrepeat = myrepeat;
313                                         LOG_DEBUG("XREPEAT %d", xrepeat );
314                                 }
315                         }
316                         break;
317
318                 case XSDRSIZE:
319                         {
320                                 u8      xsdrsize_buf[4];
321
322                                 if (read(xsvf_fd, xsdrsize_buf, 4) < 0)
323                                 {
324                                         do_abort = 1;
325                                         break;
326                                 }
327
328                                 xsdrsize = be_to_h_u32(xsdrsize_buf);
329                                 LOG_DEBUG("XSDRSIZE %d", xsdrsize);
330
331                                 if( dr_out_buf ) free(dr_out_buf);
332                                 if( dr_in_buf)   free(dr_in_buf);
333                                 if( dr_in_mask)  free(dr_in_mask);
334
335                                 dr_out_buf = malloc((xsdrsize + 7) / 8);
336                                 dr_in_buf = malloc((xsdrsize + 7) / 8);
337                                 dr_in_mask = malloc((xsdrsize + 7) / 8);
338                         }
339                         break;
340
341                 case XSDR:              /* these two are identical except for the dr_in_buf */
342                 case XSDRTDO:
343                         {
344                                 int limit = xrepeat;
345                                 int     matched = 0;
346                                 int attempt;
347
348                                 const char* op_name = (opcode == XSDR ? "XSDR" : "XSDRTDO");
349
350                                 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
351                                 {
352                                         do_abort = 1;
353                                         break;
354                                 }
355
356                                 if (opcode == XSDRTDO)
357                                 {
358                                         if(xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_buf)  != ERROR_OK )
359                                         {
360                                                 do_abort = 1;
361                                                 break;
362                                         }
363                                 }
364
365                                 if (limit < 1)
366                                         limit = 1;
367
368                                 LOG_DEBUG("%s %d", op_name, xsdrsize);
369
370                                 for( attempt=0; attempt<limit;  ++attempt )
371                                 {
372                                         scan_field_t field;
373
374                                         if( attempt>0 )
375                                         {
376                                                 /* perform the XC9500 exception handling sequence shown in xapp067.pdf and
377                                                    illustrated in psuedo code at end of this file.  We start from state
378                                                    DRPAUSE:
379                                                    go to Exit2-DR
380                                                    go to Shift-DR
381                                                    go to Exit1-DR
382                                                    go to Update-DR
383                                                    go to Run-Test/Idle
384
385                                                    This sequence should be harmless for other devices, and it
386                                                    will be skipped entirely if xrepeat is set to zero.
387                                                 */
388
389                                                 static tap_state_t exception_path[] = {
390                                                         TAP_DREXIT2,
391                                                         TAP_DRSHIFT,
392                                                         TAP_DREXIT1,
393                                                         TAP_DRUPDATE,
394                                                         TAP_IDLE,
395                                                 };
396
397                                                 jtag_add_pathmove( DIM(exception_path), exception_path );
398
399                                                 if (verbose)
400                                                         LOG_USER("%s mismatch, xsdrsize=%d retry=%d", op_name, xsdrsize, attempt);
401                                         }
402
403                                         field.tap = tap;
404                                         field.num_bits = xsdrsize;
405                                         field.out_value = dr_out_buf;
406                                         field.in_value = calloc(CEIL(field.num_bits, 8), 1);
407
408                                         if (tap == NULL)
409                                                 jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
410                                         else
411                                                 jtag_add_dr_scan(1, &field, TAP_DRPAUSE);
412
413                                         jtag_check_value_mask(&field, dr_in_buf, dr_in_mask);
414
415                                         free(field.in_value);
416
417
418                                         /* LOG_DEBUG("FLUSHING QUEUE"); */
419                                         result = jtag_execute_queue();
420                                         if (result == ERROR_OK)
421                                         {
422                                                 matched = 1;
423                                                 break;
424                                         }
425                                 }
426
427                                 if (!matched)
428                                 {
429                                         LOG_USER( "%s mismatch", op_name);
430                                         tdo_mismatch = 1;
431                                         break;
432                                 }
433
434                                 /* See page 19 of XSVF spec regarding opcode "XSDR" */
435                                 if (xruntest)
436                                 {
437                                         jtag_add_statemove(TAP_IDLE);
438
439                                         if (runtest_requires_tck)
440                                                 jtag_add_clocks(xruntest);
441                                         else
442                                                 jtag_add_sleep(xruntest);
443                                 }
444                                 else if (xendir != TAP_DRPAUSE) /* we are already in TAP_DRPAUSE */
445                                         jtag_add_statemove(xenddr);
446                         }
447                         break;
448
449                 case XSETSDRMASKS:
450                         LOG_ERROR("unsupported XSETSDRMASKS\n");
451                         unsupported = 1;
452                         break;
453
454                 case XSDRINC:
455                         LOG_ERROR("unsupported XSDRINC\n");
456                         unsupported = 1;
457                         break;
458
459                 case XSDRB:
460                         LOG_ERROR("unsupported XSDRB\n");
461                         unsupported = 1;
462                         break;
463
464                 case XSDRC:
465                         LOG_ERROR("unsupported XSDRC\n");
466                         unsupported = 1;
467                         break;
468
469                 case XSDRE:
470                         LOG_ERROR("unsupported XSDRE\n");
471                         unsupported = 1;
472                         break;
473
474                 case XSDRTDOB:
475                         LOG_ERROR("unsupported XSDRTDOB\n");
476                         unsupported = 1;
477                         break;
478
479                 case XSDRTDOC:
480                         LOG_ERROR("unsupported XSDRTDOC\n");
481                         unsupported = 1;
482                         break;
483
484                 case XSDRTDOE:
485                         LOG_ERROR("unsupported XSDRTDOE\n");
486                         unsupported = 1;
487                         break;
488
489                 case XSTATE:
490                         {
491                                 tap_state_t     mystate;
492                                 u8                      uc;
493
494                                 if (read(xsvf_fd, &uc, 1) < 0)
495                                 {
496                                         do_abort = 1;
497                                         break;
498                                 }
499
500                                 mystate = xsvf_to_tap(uc);
501
502                                 LOG_DEBUG("XSTATE 0x%02X %s", uc, tap_state_name(mystate) );
503
504                                 /*      there is no need for the lookahead code that was here since we
505                                         queue up the jtag commands anyway.  This is a simple way to handle
506                                         the XSTATE.
507                                 */
508
509                                 if( jtag_add_statemove( mystate ) != ERROR_OK )
510                                 {
511                                         /*      For special states known as stable states
512                                                 (Test-Logic-Reset, Run-Test/Idle, Pause-DR, Pause- IR),
513                                                 an XSVF interpreter follows predefined TAP state paths
514                                                 when the starting state is a stable state and when the
515                                                 XSTATE specifies a new stable state (see the STATE
516                                                 command in the [Ref 5] for the TAP state paths between
517                                                 stable states). For non-stable states, XSTATE should
518                                                 specify a state that is only one TAP state transition
519                                                 distance from the current TAP state to avoid undefined
520                                                 TAP state paths. A sequence of multiple XSTATE commands
521                                                 can be issued to transition the TAP through a specific
522                                                 state path.
523                                         */
524
525                                         LOG_ERROR("XSTATE %s is not reachable from current state %s in one clock cycle",
526                                                 tap_state_name(mystate),
527                                                 tap_state_name(cmd_queue_cur_state)
528                                                 );
529                                 }
530                         }
531                         break;
532
533                 case XENDIR:
534
535                         if (read(xsvf_fd, &uc, 1) < 0)
536                         {
537                                 do_abort = 1;
538                                 break;
539                         }
540
541                         /* see page 22 of XSVF spec */
542                         if( uc == 0 )
543                                 xendir = TAP_IDLE;
544                         else if( uc == 1 )
545                                 xendir = TAP_IRPAUSE;
546                         else
547                         {
548                                 LOG_ERROR("illegial XENDIR argument: 0x%02X", uc);
549                                 unsupported = 1;
550                                 break;
551                         }
552
553                         LOG_DEBUG("XENDIR 0x%02X %s", uc, tap_state_name(xendir));
554                         break;
555
556                 case XENDDR:
557
558                         if (read(xsvf_fd, &uc, 1) < 0)
559                         {
560                                 do_abort = 1;
561                                 break;
562                         }
563
564                         /* see page 22 of XSVF spec */
565                         if( uc == 0 )
566                                 xenddr = TAP_IDLE;
567                         else if( uc == 1 )
568                                 xenddr = TAP_DRPAUSE;
569                         else
570                         {
571                                 LOG_ERROR("illegial XENDDR argument: 0x%02X", uc);
572                                 unsupported = 1;
573                                 break;
574                         }
575
576                         LOG_DEBUG("XENDDR %02X %s", uc, tap_state_name(xenddr));
577                         break;
578
579                 case XSIR:
580                 case XSIR2:
581                         {
582                                 u8      short_buf[2];
583                                 u8*     ir_buf;
584                                 int bitcount;
585                                 tap_state_t my_end_state = xruntest ? TAP_IDLE : xendir;
586
587                                 if( opcode == XSIR )
588                                 {
589                                         /* one byte bitcount */
590                                         if (read(xsvf_fd, short_buf, 1) < 0)
591                                         {
592                                                 do_abort = 1;
593                                                 break;
594                                         }
595                                         bitcount = short_buf[0];
596                                         LOG_DEBUG("XSIR %d", bitcount);
597                                 }
598                                 else
599                                 {
600                                         if (read(xsvf_fd, short_buf, 2) < 0)
601                                         {
602                                                 do_abort = 1;
603                                                 break;
604                                         }
605                                         bitcount = be_to_h_u16(short_buf);
606                                         LOG_DEBUG("XSIR2 %d", bitcount);
607                                 }
608
609                                 ir_buf = malloc((bitcount+7) / 8);
610
611                                 if (xsvf_read_buffer(bitcount, xsvf_fd, ir_buf) != ERROR_OK)
612                                         do_abort = 1;
613                                 else
614                                 {
615                                         scan_field_t field;
616
617                                         field.tap = tap;
618                                         field.num_bits = bitcount;
619                                         field.out_value = ir_buf;
620
621                                         field.in_value = NULL;
622
623
624                                         
625
626                                         if (tap == NULL)
627                                                 jtag_add_plain_ir_scan(1, &field, my_end_state);
628                                         else
629                                                 jtag_add_ir_scan(1, &field, my_end_state);
630
631                                         if (xruntest)
632                                         {
633                                                 if (runtest_requires_tck)
634                                                         jtag_add_clocks(xruntest);
635                                                 else
636                                                         jtag_add_sleep(xruntest);
637                                         }
638
639                                         /* Note that an -irmask of non-zero in your config file
640                                          * can cause this to fail.  Setting -irmask to zero cand work
641                                          * around the problem.
642                                          */
643
644                                         /* LOG_DEBUG("FLUSHING QUEUE"); */
645                                         result = jtag_execute_queue();
646                                         if(result != ERROR_OK)
647                                         {
648                                                 tdo_mismatch = 1;
649                                         }
650                                 }
651                                 free(ir_buf);
652                         }
653                         break;
654
655                 case XCOMMENT:
656                         {
657                                 unsigned int ndx = 0;
658                                 char    comment[128];
659
660                                 do
661                                 {
662                                         if (read(xsvf_fd, &uc, 1) < 0)
663                                         {
664                                                 do_abort = 1;
665                                                 break;
666                                         }
667
668                                         if ( ndx < sizeof(comment)-1 )
669                                                 comment[ndx++] = uc;
670
671                                 } while (uc != 0);
672
673                                 comment[sizeof(comment)-1] = 0;         /* regardless, terminate */
674                                 if (verbose)
675                                         LOG_USER("# %s", comment);
676                         }
677                         break;
678
679                 case XWAIT:
680                         {
681                                 /* expected in stream:
682                                    XWAIT <u8 wait_state> <u8 end_state> <u32 usecs>
683                                 */
684
685                                 u8      wait;
686                                 u8      end;
687                                 u8      delay_buf[4];
688
689                                 tap_state_t wait_state;
690                                 tap_state_t end_state;
691                                 int     delay;
692
693                                 if ( read(xsvf_fd, &wait, 1) < 0
694                                   || read(xsvf_fd, &end, 1) < 0
695                                   || read(xsvf_fd, delay_buf, 4) < 0)
696                                 {
697                                         do_abort = 1;
698                                         break;
699                                 }
700
701                                 wait_state = xsvf_to_tap(wait);
702                                 end_state  = xsvf_to_tap(end);
703                                 delay      = be_to_h_u32(delay_buf);
704
705                                 LOG_DEBUG("XWAIT %s %s usecs:%d", tap_state_name(wait_state), tap_state_name(end_state), delay);
706
707                                 if (runtest_requires_tck && wait_state == TAP_IDLE )
708                                 {
709                                         jtag_add_runtest(delay, end_state);
710                                 }
711                                 else
712                                 {
713                                         jtag_add_statemove( wait_state );
714                                         jtag_add_sleep(delay);
715                                         jtag_add_statemove( end_state );
716                                 }
717                         }
718                         break;
719
720                 case XWAITSTATE:
721                         {
722                                 /* expected in stream:
723                                    XWAITSTATE <u8 wait_state> <u8 end_state> <u32 clock_count> <u32 usecs>
724                                 */
725
726                                 u8  clock_buf[4];
727                                 u8      usecs_buf[4];
728                                 u8      wait;
729                                 u8      end;
730                                 tap_state_t wait_state;
731                                 tap_state_t end_state;
732                                 int clock_count;
733                                 int usecs;
734
735                                 if ( read(xsvf_fd, &wait, 1) < 0
736                                  ||  read(xsvf_fd, &end, 1) < 0
737                                  ||  read(xsvf_fd, clock_buf, 4) < 0
738                                  ||  read(xsvf_fd, usecs_buf, 4) < 0 )
739                                 {
740                                         do_abort = 1;
741                                         break;
742                                 }
743
744                                 wait_state = xsvf_to_tap( wait );
745                                 end_state  = xsvf_to_tap( end );
746
747                                 clock_count = be_to_h_u32(clock_buf);
748                                 usecs       = be_to_h_u32(usecs_buf);
749
750                                 LOG_DEBUG("XWAITSTATE %s %s clocks:%i usecs:%i",
751                                         tap_state_name(wait_state),
752                                         tap_state_name(end_state),
753                                         clock_count, usecs);
754
755                                 /* the following states are 'stable', meaning that they have a transition
756                                  * in the state diagram back to themselves.  This is necessary because we will
757                                  * be issuing a number of clocks in this state.  This set of allowed states is also
758                                  * determined by the SVF RUNTEST command's allowed states.
759                                  */
760                                 if (wait_state != TAP_IRPAUSE && wait_state != TAP_DRPAUSE && wait_state != TAP_RESET && wait_state != TAP_IDLE)
761                                 {
762                                         LOG_ERROR("illegal XWAITSTATE wait_state: \"%s\"", tap_state_name( wait_state ));
763                                         unsupported = 1;
764                                 }
765
766                                 jtag_add_statemove( wait_state );
767
768                                 jtag_add_clocks( clock_count );
769
770                                 jtag_add_sleep( usecs );
771
772                                 jtag_add_statemove( end_state );
773                         }
774                         break;
775
776                 case LCOUNT:
777                         {
778                                 /* expected in stream:
779                                    LCOUNT <u32 loop_count>
780                                 */
781                                 u8  count_buf[4];
782
783                                 if ( read(xsvf_fd, count_buf, 4) < 0 )
784                                 {
785                                         do_abort = 1;
786                                         break;
787                                 }
788
789                                 loop_count = be_to_h_u32(count_buf);
790                                 LOG_DEBUG("LCOUNT %d", loop_count);
791                         }
792                         break;
793
794                 case LDELAY:
795                         {
796                                 /* expected in stream:
797                                    LDELAY <u8 wait_state> <u32 clock_count> <u32 usecs_to_sleep>
798                                 */
799                                 u8      state;
800                                 u8  clock_buf[4];
801                                 u8  usecs_buf[4];
802
803                                 if ( read(xsvf_fd, &state, 1) < 0
804                                   || read(xsvf_fd, clock_buf, 4) < 0
805                                   ||     read(xsvf_fd, usecs_buf, 4) < 0 )
806                                 {
807                                         do_abort = 1;
808                                         break;
809                                 }
810
811                                 loop_state  = xsvf_to_tap(state);
812                                 loop_clocks = be_to_h_u32(clock_buf);
813                                 loop_usecs  = be_to_h_u32(usecs_buf);
814
815                                 LOG_DEBUG("LDELAY %s clocks:%d usecs:%d", tap_state_name(loop_state), loop_clocks, loop_usecs);
816                         }
817                         break;
818
819                 /* LSDR is more like XSDRTDO than it is like XSDR.  It uses LDELAY which
820                  * comes with clocks !AND! sleep requirements.
821                  */
822                 case LSDR:
823                         {
824                                 int limit = loop_count;
825                                 int matched = 0;
826                                 int attempt;
827
828                                 LOG_DEBUG("LSDR");
829
830                                 if ( xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK
831                                   || xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_buf) != ERROR_OK )
832                                 {
833                                         do_abort = 1;
834                                         break;
835                                 }
836
837                                 if (limit < 1)
838                                         limit = 1;
839
840                                 for( attempt=0; attempt<limit;  ++attempt )
841                                 {
842                                         scan_field_t field;
843
844                                         jtag_add_statemove( loop_state );
845                                         jtag_add_clocks(loop_clocks);
846                                         jtag_add_sleep(loop_usecs);
847
848                                         field.tap = tap;
849                                         field.num_bits = xsdrsize;
850                                         field.out_value = dr_out_buf;
851                                         field.in_value = calloc(CEIL(field.num_bits, 8), 1);
852
853                                         if (attempt > 0 && verbose)
854                                                 LOG_USER("LSDR retry %d", attempt);
855
856                                         if (tap == NULL)
857                                                 jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
858                                         else
859                                                 jtag_add_dr_scan(1, &field, TAP_DRPAUSE);
860
861                                         jtag_check_value_mask(&field, dr_in_buf, dr_in_mask);
862
863                                         free(field.in_value);
864
865
866                                         /* LOG_DEBUG("FLUSHING QUEUE"); */
867                                         result = jtag_execute_queue();
868                                         if(result == ERROR_OK)
869                                         {
870                                                 matched = 1;
871                                                 break;
872                                         }
873                                 }
874
875                                 if (!matched )
876                                 {
877                                         LOG_USER( "LSDR mismatch" );
878                                         tdo_mismatch = 1;
879                                         break;
880                                 }
881                         }
882                         break;
883
884                 case XTRST:
885                         {
886                                 u8      trst_mode;
887
888                                 if (read(xsvf_fd, &trst_mode, 1) < 0)
889                                 {
890                                         do_abort = 1;
891                                         break;
892                                 }
893
894                                 switch( trst_mode )
895                                 {
896                                 case XTRST_ON:
897                                         jtag_add_reset(1, 0);
898                                         break;
899                                 case XTRST_OFF:
900                                 case XTRST_Z:
901                                         jtag_add_reset(0, 0);
902                                         break;
903                                 case XTRST_ABSENT:
904                                         break;
905                                 default:
906                                         LOG_ERROR( "XTRST mode argument (0x%02X) out of range", trst_mode );
907                                         do_abort = 1;
908                                 }
909                         }
910                         break;
911
912                 default:
913                         LOG_ERROR("unknown xsvf command (0x%02X)\n", uc);
914                         unsupported = 1;
915                 }
916
917                 if (do_abort || unsupported || tdo_mismatch)
918                 {
919                         LOG_DEBUG("xsvf failed, setting taps to reasonable state");
920
921                         /* upon error, return the TAPs to a reasonable state */
922                         jtag_add_statemove( TAP_IDLE );
923                         jtag_execute_queue();
924                         break;
925                 }
926         }
927
928         if (tdo_mismatch)
929         {
930                 command_print(cmd_ctx, "TDO mismatch, somewhere near offset %lu in xsvf file, aborting",
931                                           file_offset );
932
933
934                 return ERROR_FAIL;
935         }
936
937         if (unsupported)
938         {
939                 off_t offset = lseek(xsvf_fd, 0, SEEK_CUR) - 1;
940                 command_print(cmd_ctx,
941                                 "unsupported xsvf command (0x%02X) at offset %jd, aborting",
942                                 uc, (intmax_t)offset);
943                 return ERROR_FAIL;
944         }
945
946         if (do_abort)
947         {
948                 command_print(cmd_ctx, "premature end of xsvf file detected, aborting");
949                 return ERROR_FAIL;
950         }
951
952         if (dr_out_buf)
953                 free(dr_out_buf);
954
955         if (dr_in_buf)
956                 free(dr_in_buf);
957
958         if (dr_in_mask)
959                 free(dr_in_mask);
960
961         close(xsvf_fd);
962
963         command_print(cmd_ctx, "XSVF file programmed successfully");
964
965         return ERROR_OK;
966 }
967
968
969 #if 0   /* this comment style used to try and keep uncrustify from adding * at begin of line */
970
971 PSUEDO-Code from Xilinx Appnote XAPP067.pdf:
972
973 the following pseudo code clarifies the intent of the xrepeat support.  The
974 flow given is for the entire processing of an SVF file, not an XSVF file.
975 No idea if this is just for the XC9500/XL/XV devices or all Xilinx parts.
976
977 "Pseudo-Code Algorithm for SVF-Based ISP"
978
979 1. Go to Test-Logic-Reset state
980 2. Go to Run-Test Idle state
981 3. Read SVF record
982
983 4. if SIR record then
984            go to Shift-IR state
985            Scan in <TDI value>
986
987 5. else if SDR record then
988            set <repeat count> to 0
989            store <TDI value> as <current TDI value>
990            store <TDO value> as <current TDO value>
991 6. go to Shift-DR state
992            scan in <current TDI value>
993            if <current TDO value> is specified then
994                    if <current TDO value> does not equal <actual TDO value> then
995                            if <repeat count> > 32 then
996                                    LOG ERROR
997                                    go to Run-Test Idle state
998                                    go to Step 3
999                            end if
1000                            go to Pause-DR
1001                            go to Exit2-DR
1002                            go to Shift-DR
1003                            go to Exit1-DR
1004                            go to Update-DR
1005                            go to Run-Test/Idle
1006                            increment <repeat count> by 1
1007                            pause <current pause time> microseconds
1008                            go to Step 6)
1009                    end if
1010            else
1011                    go to Run-Test Idle state
1012                    go to Step 3
1013            endif
1014 else if RUNTEST record then
1015    pause tester for <TCK value> microseconds
1016    store <TCK value> as <current pause time>
1017 end if
1018
1019 #endif