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