1c746f382c724042b9a1448d3223b94ef1cee67e
[fw/openocd] / src / svf / svf.c
1 /*
2  * Copyright (C) 2009 by Simon Qian
3  * SimonQian@SimonQian.com
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20
21 /* The specification for SVF is available here:
22  * http://www.asset-intertech.com/support/svf.pdf
23  * Below, this document is refered to as the "SVF spec".
24  *
25  * The specification for XSVF is available here:
26  * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
27  * Below, this document is refered to as the "XSVF spec".
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <jtag/jtag.h>
35 #include "svf.h"
36 #include <helper/time_support.h>
37
38
39 // SVF command
40 typedef enum
41 {
42         ENDDR,
43         ENDIR,
44         FREQUENCY,
45         HDR,
46         HIR,
47         PIO,
48         PIOMAP,
49         RUNTEST,
50         SDR,
51         SIR,
52         STATE,
53         TDR,
54         TIR,
55         TRST,
56 }svf_command_t;
57
58 static const char *svf_command_name[14] =
59 {
60         "ENDDR",
61         "ENDIR",
62         "FREQUENCY",
63         "HDR",
64         "HIR",
65         "PIO",
66         "PIOMAP",
67         "RUNTEST",
68         "SDR",
69         "SIR",
70         "STATE",
71         "TDR",
72         "TIR",
73         "TRST"
74 };
75
76 typedef enum
77 {
78         TRST_ON,
79         TRST_OFF,
80         TRST_Z,
81         TRST_ABSENT
82 }trst_mode_t;
83
84 static const char *svf_trst_mode_name[4] =
85 {
86         "ON",
87         "OFF",
88         "Z",
89         "ABSENT"
90 };
91
92 struct svf_statemove
93 {
94         tap_state_t from;
95         tap_state_t to;
96         uint32_t num_of_moves;
97         tap_state_t paths[8];
98 };
99
100 /*
101  * These paths are from the SVF specification for the STATE command, to be
102  * used when the STATE command only includes the final state.  The first
103  * element of the path is the "from" (current) state, and the last one is
104  * the "to" (target) state.
105  *
106  * All specified paths are the shortest ones in the JTAG spec, and are thus
107  * not (!!) exact matches for the paths used elsewhere in OpenOCD.  Note
108  * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
109  * which has specific effects on the various registers; they are not NOPs.
110  *
111  * Paths to RESET are disabled here.  As elsewhere in OpenOCD, and in XSVF
112  * and many SVF implementations, we don't want to risk missing that state.
113  * To get to RESET, always we ignore the current state.
114  */
115 static const struct svf_statemove svf_statemoves[] =
116 {
117         // from                 to                              num_of_moves,   paths[8]
118 //      {TAP_RESET,             TAP_RESET,              1,                              {TAP_RESET}},
119         {TAP_RESET,             TAP_IDLE,               2,                              {TAP_RESET, TAP_IDLE}},
120         {TAP_RESET,             TAP_DRPAUSE,    6,                              {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
121         {TAP_RESET,             TAP_IRPAUSE,    7,                              {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
122
123 //      {TAP_IDLE,              TAP_RESET,              4,                              {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
124         {TAP_IDLE,              TAP_IDLE,               1,                              {TAP_IDLE}},
125         {TAP_IDLE,              TAP_DRPAUSE,    5,                              {TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
126         {TAP_IDLE,              TAP_IRPAUSE,    6,                              {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
127
128 //      {TAP_DRPAUSE,   TAP_RESET,              6,                              {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
129         {TAP_DRPAUSE,   TAP_IDLE,               4,                              {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE}},
130         {TAP_DRPAUSE,   TAP_DRPAUSE,    7,                              {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
131         {TAP_DRPAUSE,   TAP_IRPAUSE,    8,                              {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
132
133 //      {TAP_IRPAUSE,   TAP_RESET,              6,                              {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
134         {TAP_IRPAUSE,   TAP_IDLE,               4,                              {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_IDLE}},
135         {TAP_IRPAUSE,   TAP_DRPAUSE,    7,                              {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
136         {TAP_IRPAUSE,   TAP_IRPAUSE,    8,                              {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}}
137 };
138
139
140 #define XXR_TDI                                         (1 << 0)
141 #define XXR_TDO                                         (1 << 1)
142 #define XXR_MASK                                        (1 << 2)
143 #define XXR_SMASK                                       (1 << 3)
144 struct svf_xxr_para
145 {
146         int len;
147         int data_mask;
148         uint8_t *tdi;
149         uint8_t *tdo;
150         uint8_t *mask;
151         uint8_t *smask;
152 };
153
154 struct svf_para
155 {
156         float frequency;
157         tap_state_t ir_end_state;
158         tap_state_t dr_end_state;
159         tap_state_t runtest_run_state;
160         tap_state_t runtest_end_state;
161         trst_mode_t trst_mode;
162
163         struct svf_xxr_para hir_para;
164         struct svf_xxr_para hdr_para;
165         struct svf_xxr_para tir_para;
166         struct svf_xxr_para tdr_para;
167         struct svf_xxr_para sir_para;
168         struct svf_xxr_para sdr_para;
169 };
170
171 static struct svf_para svf_para;
172 static const struct svf_para svf_para_init =
173 {
174 //      frequency,      ir_end_state,   dr_end_state,   runtest_run_state,      runtest_end_state,      trst_mode
175         0,                      TAP_IDLE,               TAP_IDLE,               TAP_IDLE,                       TAP_IDLE,                       TRST_Z,
176 //      hir_para
177 //      {len,   data_mask,      tdi,    tdo,    mask,   smask},
178         {0,             0,                      NULL,   NULL,   NULL,   NULL},
179 //      hdr_para
180 //      {len,   data_mask,      tdi,    tdo,    mask,   smask},
181         {0,             0,                      NULL,   NULL,   NULL,   NULL},
182 //      tir_para
183 //      {len,   data_mask,      tdi,    tdo,    mask,   smask},
184         {0,             0,                      NULL,   NULL,   NULL,   NULL},
185 //      tdr_para
186 //      {len,   data_mask,      tdi,    tdo,    mask,   smask},
187         {0,             0,                      NULL,   NULL,   NULL,   NULL},
188 //      sir_para
189 //      {len,   data_mask,      tdi,    tdo,    mask,   smask},
190         {0,             0,                      NULL,   NULL,   NULL,   NULL},
191 //      sdr_para
192 //      {len,   data_mask,      tdi,    tdo,    mask,   smask},
193         {0,             0,                      NULL,   NULL,   NULL,   NULL},
194 };
195
196 struct svf_check_tdo_para
197 {
198         int line_num;           // used to record line number of the check operation
199                                                 // so more information could be printed
200         int enabled;            // check is enabled or not
201         int buffer_offset;      // buffer_offset to buffers
202         int bit_len;            // bit length to check
203 };
204
205 #define SVF_CHECK_TDO_PARA_SIZE 1024
206 static struct svf_check_tdo_para *svf_check_tdo_para = NULL;
207 static int svf_check_tdo_para_index = 0;
208
209 static int svf_read_command_from_file(int fd);
210 static int svf_check_tdo(void);
211 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
212 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
213
214 static int svf_fd = 0;
215 static char *svf_command_buffer = NULL;
216 static int svf_command_buffer_size = 0;
217 static int svf_line_number = 1;
218
219 static struct jtag_tap *tap = NULL;
220
221 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT   (4 * 1024)
222 static uint8_t *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
223 static int svf_buffer_index = 0, svf_buffer_size = 0;
224 static int svf_quiet = 0;
225
226
227 static void svf_free_xxd_para(struct svf_xxr_para *para)
228 {
229         if (NULL != para)
230         {
231                 if (para->tdi != NULL)
232                 {
233                         free(para->tdi);
234                         para->tdi = NULL;
235                 }
236                 if (para->tdo != NULL)
237                 {
238                         free(para->tdo);
239                         para->tdo = NULL;
240                 }
241                 if (para->mask != NULL)
242                 {
243                         free(para->mask);
244                         para->mask = NULL;
245                 }
246                 if (para->smask != NULL)
247                 {
248                         free(para->smask);
249                         para->smask = NULL;
250                 }
251         }
252 }
253
254 static unsigned svf_get_mask_u32(int bitlen)
255 {
256         uint32_t bitmask;
257
258         if (bitlen < 0)
259         {
260                 bitmask = 0;
261         }
262         else if (bitlen >= 32)
263         {
264                 bitmask = 0xFFFFFFFF;
265         }
266         else
267         {
268                 bitmask = (1 << bitlen) - 1;
269         }
270
271         return bitmask;
272 }
273
274 int svf_add_statemove(tap_state_t state_to)
275 {
276         tap_state_t state_from = cmd_queue_cur_state;
277         uint8_t index;
278
279         /* when resetting, be paranoid and ignore current state */
280         if (state_to == TAP_RESET) {
281                 jtag_add_tlr();
282                 return ERROR_OK;
283         }
284
285         for (index = 0; index < ARRAY_SIZE(svf_statemoves); index++)
286         {
287                 if ((svf_statemoves[index].from == state_from)
288                         && (svf_statemoves[index].to == state_to))
289                 {
290                         /* recorded path includes current state ... avoid extra TCKs! */
291                         if (svf_statemoves[index].num_of_moves > 1)
292                                 jtag_add_pathmove(svf_statemoves[index].num_of_moves - 1,
293                                                 svf_statemoves[index].paths + 1);
294                         else
295                                 jtag_add_pathmove(svf_statemoves[index].num_of_moves,
296                                                 svf_statemoves[index].paths);
297                         return ERROR_OK;
298                 }
299         }
300         LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
301         return ERROR_FAIL;
302 }
303
304 COMMAND_HANDLER(handle_svf_command)
305 {
306 #define SVF_NUM_OF_OPTIONS                      1
307         int command_num = 0;
308         int ret = ERROR_OK;
309         long long time_ago;
310
311         if ((CMD_ARGC < 1) || (CMD_ARGC > (1 + SVF_NUM_OF_OPTIONS)))
312         {
313                 command_print(CMD_CTX, "usage: svf <file> [quiet]");
314                 return ERROR_FAIL;
315         }
316
317         // parse variant
318         svf_quiet = 0;
319         for (unsigned i = 1; i < CMD_ARGC; i++)
320         {
321                 if (!strcmp(CMD_ARGV[i], "quiet"))
322                 {
323                         svf_quiet = 1;
324                 }
325                 else
326                 {
327                         LOG_ERROR("unknown variant for svf: %s", CMD_ARGV[i]);
328
329                         // no need to free anything now
330                         return ERROR_FAIL;
331                 }
332         }
333
334         if ((svf_fd = open(CMD_ARGV[0], O_RDONLY)) < 0)
335         {
336                 command_print(CMD_CTX, "file \"%s\" not found", CMD_ARGV[0]);
337
338                 // no need to free anything now
339                 return ERROR_FAIL;
340         }
341
342         LOG_USER("svf processing file: \"%s\"", CMD_ARGV[0]);
343
344         // get time
345         time_ago = timeval_ms();
346
347         // init
348         svf_line_number = 1;
349         svf_command_buffer_size = 0;
350
351         svf_check_tdo_para_index = 0;
352         svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
353         if (NULL == svf_check_tdo_para)
354         {
355                 LOG_ERROR("not enough memory");
356                 ret = ERROR_FAIL;
357                 goto free_all;
358         }
359
360         svf_buffer_index = 0;
361         // double the buffer size
362         // in case current command cannot be commited, and next command is a bit scan command
363         // here is 32K bits for this big scan command, it should be enough
364         // buffer will be reallocated if buffer size is not enough
365         svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
366         if (NULL == svf_tdi_buffer)
367         {
368                 LOG_ERROR("not enough memory");
369                 ret = ERROR_FAIL;
370                 goto free_all;
371         }
372         svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
373         if (NULL == svf_tdo_buffer)
374         {
375                 LOG_ERROR("not enough memory");
376                 ret = ERROR_FAIL;
377                 goto free_all;
378         }
379         svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
380         if (NULL == svf_mask_buffer)
381         {
382                 LOG_ERROR("not enough memory");
383                 ret = ERROR_FAIL;
384                 goto free_all;
385         }
386         svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
387
388         memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
389
390         // TAP_RESET
391         jtag_add_tlr();
392
393         while (ERROR_OK == svf_read_command_from_file(svf_fd))
394         {
395                 if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer))
396                 {
397                         LOG_ERROR("fail to run command at line %d", svf_line_number);
398                         ret = ERROR_FAIL;
399                         break;
400                 }
401                 command_num++;
402         }
403         if (ERROR_OK != jtag_execute_queue())
404         {
405                 ret = ERROR_FAIL;
406         }
407         else if (ERROR_OK != svf_check_tdo())
408         {
409                 ret = ERROR_FAIL;
410         }
411
412         // print time
413         command_print(CMD_CTX, "%lld ms used", timeval_ms() - time_ago);
414
415 free_all:
416
417         close(svf_fd);
418         svf_fd = 0;
419
420         // free buffers
421         if (svf_command_buffer)
422         {
423                 free(svf_command_buffer);
424                 svf_command_buffer = NULL;
425                 svf_command_buffer_size = 0;
426         }
427         if (svf_check_tdo_para)
428         {
429                 free(svf_check_tdo_para);
430                 svf_check_tdo_para = NULL;
431                 svf_check_tdo_para_index = 0;
432         }
433         if (svf_tdi_buffer)
434         {
435                 free(svf_tdi_buffer);
436                 svf_tdi_buffer = NULL;
437         }
438         if (svf_tdo_buffer)
439         {
440                 free(svf_tdo_buffer);
441                 svf_tdo_buffer = NULL;
442         }
443         if (svf_mask_buffer)
444         {
445                 free(svf_mask_buffer);
446                 svf_mask_buffer = NULL;
447         }
448         svf_buffer_index = 0;
449         svf_buffer_size = 0;
450
451         svf_free_xxd_para(&svf_para.hdr_para);
452         svf_free_xxd_para(&svf_para.hir_para);
453         svf_free_xxd_para(&svf_para.tdr_para);
454         svf_free_xxd_para(&svf_para.tir_para);
455         svf_free_xxd_para(&svf_para.sdr_para);
456         svf_free_xxd_para(&svf_para.sir_para);
457
458         if (ERROR_OK == ret)
459         {
460                 command_print(CMD_CTX, "svf file programmed successfully for %d commands", command_num);
461         }
462         else
463         {
464                 command_print(CMD_CTX, "svf file programmed failed");
465         }
466
467         return ret;
468 }
469
470 #define SVFP_CMD_INC_CNT                        1024
471 static int svf_read_command_from_file(int fd)
472 {
473         char ch, *tmp_buffer = NULL;
474         int cmd_pos = 0, cmd_ok = 0, slash = 0, comment = 0;
475
476         while (!cmd_ok && (read(fd, &ch, 1) > 0))
477         {
478                 switch (ch)
479                 {
480                 case '!':
481                         slash = 0;
482                         comment = 1;
483                         break;
484                 case '/':
485                         if (++slash == 2)
486                         {
487                                 comment = 1;
488                         }
489                         break;
490                 case ';':
491                         slash = 0;
492                         if (!comment)
493                         {
494                                 cmd_ok = 1;
495                         }
496                         break;
497                 case '\n':
498                         svf_line_number++;
499                 case '\r':
500                         slash = 0;
501                         comment = 0;
502                         break;
503                 default:
504                         if (!comment)
505                         {
506                                 if (cmd_pos >= svf_command_buffer_size - 1)
507                                 {
508                                         tmp_buffer = (char*)malloc(svf_command_buffer_size + SVFP_CMD_INC_CNT);         // 1 more byte for '\0'
509                                         if (NULL == tmp_buffer)
510                                         {
511                                                 LOG_ERROR("not enough memory");
512                                                 return ERROR_FAIL;
513                                         }
514                                         if (svf_command_buffer_size > 0)
515                                         {
516                                                 memcpy(tmp_buffer, svf_command_buffer, svf_command_buffer_size);
517                                         }
518                                         if (svf_command_buffer != NULL)
519                                         {
520                                                 free(svf_command_buffer);
521                                         }
522                                         svf_command_buffer = tmp_buffer;
523                                         svf_command_buffer_size += SVFP_CMD_INC_CNT;
524                                         tmp_buffer = NULL;
525                                 }
526                                 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
527                         }
528                         break;
529                 }
530         }
531
532         if (cmd_ok)
533         {
534                 svf_command_buffer[cmd_pos] = '\0';
535                 return ERROR_OK;
536         }
537         else
538         {
539                 return ERROR_FAIL;
540         }
541 }
542
543 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
544 {
545         int pos = 0, num = 0, space_found = 1;
546
547         while (pos < len)
548         {
549                 switch (str[pos])
550                 {
551                 case '\n':
552                 case '\r':
553                 case '!':
554                 case '/':
555                         LOG_ERROR("fail to parse svf command");
556                         return ERROR_FAIL;
557                         break;
558                 case ' ':
559                         space_found = 1;
560                         str[pos] = '\0';
561                         break;
562                 default:
563                         if (space_found)
564                         {
565                                 argus[num++] = &str[pos];
566                                 space_found = 0;
567                         }
568                         break;
569                 }
570                 pos++;
571         }
572
573         *num_of_argu = num;
574
575         return ERROR_OK;
576 }
577
578 bool svf_tap_state_is_stable(tap_state_t state)
579 {
580         return (TAP_RESET == state) || (TAP_IDLE == state)
581                         || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
582 }
583
584 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
585 {
586         int i;
587
588         for (i = 0; i < num_of_element; i++)
589         {
590                 if (!strcmp(str, strs[i]))
591                 {
592                         return i;
593                 }
594         }
595         return 0xFF;
596 }
597
598 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
599 {
600         int new_byte_len = (new_bit_len + 7) >> 3;
601
602         if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
603         {
604                 if (*arr != NULL)
605                 {
606                         free(*arr);
607                         *arr = NULL;
608                 }
609                 *arr = (uint8_t*)malloc(new_byte_len);
610                 if (NULL == *arr)
611                 {
612                         LOG_ERROR("not enough memory");
613                         return ERROR_FAIL;
614                 }
615                 memset(*arr, 0, new_byte_len);
616         }
617         return ERROR_OK;
618 }
619
620 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
621 {
622         int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
623         uint8_t ch = 0;
624
625         if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
626         {
627                 LOG_ERROR("fail to adjust length of array");
628                 return ERROR_FAIL;
629         }
630
631         for (i = 0; i < str_hbyte_len; i++)
632         {
633                 ch = 0;
634                 while (str_len > 0)
635                 {
636                         ch = str[--str_len];
637
638                         if (!isblank(ch))
639                         {
640                                 if ((ch >= '0') && (ch <= '9'))
641                                 {
642                                         ch = ch - '0';
643                                         break;
644                                 }
645                                 else if ((ch >= 'A') && (ch <= 'F'))
646                                 {
647                                         ch = ch - 'A' + 10;
648                                         break;
649                                 }
650                                 else
651                                 {
652                                         LOG_ERROR("invalid hex string");
653                                         return ERROR_FAIL;
654                                 }
655                         }
656
657                         ch = 0;
658                 }
659
660                 // write bin
661                 if (i % 2)
662                 {
663                         // MSB
664                         (*bin)[i / 2] |= ch << 4;
665                 }
666                 else
667                 {
668                         // LSB
669                         (*bin)[i / 2] = 0;
670                         (*bin)[i / 2] |= ch;
671                 }
672         }
673
674         // consume optional leading '0' characters
675         while (str_len > 0 && str[str_len - 1] == '0')
676                 str_len--;
677
678         // check valid
679         if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0)
680         {
681                 LOG_ERROR("value execeeds length");
682                 return ERROR_FAIL;
683         }
684
685         return ERROR_OK;
686 }
687
688 static int svf_check_tdo(void)
689 {
690         int i, len, index;
691
692         for (i = 0; i < svf_check_tdo_para_index; i++)
693         {
694                 index = svf_check_tdo_para[i].buffer_offset;
695                 len = svf_check_tdo_para[i].bit_len;
696                 if ((svf_check_tdo_para[i].enabled)
697                         && buf_cmp_mask(&svf_tdi_buffer[index], &svf_tdo_buffer[index], &svf_mask_buffer[index], len))
698                 {
699                         unsigned bitmask;
700                         unsigned received, expected, tapmask;
701                         bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
702
703                         memcpy(&received, svf_tdi_buffer + index, sizeof(unsigned));
704                         memcpy(&expected, svf_tdo_buffer + index, sizeof(unsigned));
705                         memcpy(&tapmask, svf_mask_buffer + index, sizeof(unsigned));
706                         LOG_ERROR("tdo check error at line %d",
707                                           svf_check_tdo_para[i].line_num);
708                         LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
709                                           received & bitmask,
710                                           expected & bitmask,
711                                           tapmask & bitmask);
712                         return ERROR_FAIL;
713                 }
714         }
715         svf_check_tdo_para_index = 0;
716
717         return ERROR_OK;
718 }
719
720 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
721 {
722         if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
723         {
724                 LOG_ERROR("toooooo many operation undone");
725                 return ERROR_FAIL;
726         }
727
728         svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
729         svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
730         svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
731         svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
732         svf_check_tdo_para_index++;
733
734         return ERROR_OK;
735 }
736
737 static int svf_execute_tap(void)
738 {
739         if (ERROR_OK != jtag_execute_queue())
740         {
741                 return ERROR_FAIL;
742         }
743         else if (ERROR_OK != svf_check_tdo())
744         {
745                 return ERROR_FAIL;
746         }
747
748         svf_buffer_index = 0;
749
750         return ERROR_OK;
751 }
752
753 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
754 {
755         char *argus[256], command;
756         int num_of_argu = 0, i;
757
758         // tmp variable
759         int i_tmp;
760
761         // for RUNTEST
762         int run_count;
763         float min_time, max_time;
764         // for XXR
765         struct svf_xxr_para *xxr_para_tmp;
766         uint8_t **pbuffer_tmp;
767         struct scan_field field;
768         // for STATE
769         tap_state_t *path = NULL, state;
770
771         if (!svf_quiet)
772         {
773                 LOG_USER("%s", svf_command_buffer);
774         }
775
776         if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
777         {
778                 return ERROR_FAIL;
779         }
780
781         /* NOTE: we're a bit loose here, because we ignore case in
782          * TAP state names (instead of insisting on uppercase).
783          */
784
785         command = svf_find_string_in_array(argus[0],
786                         (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
787         switch (command)
788         {
789         case ENDDR:
790         case ENDIR:
791                 if (num_of_argu != 2)
792                 {
793                         LOG_ERROR("invalid parameter of %s", argus[0]);
794                         return ERROR_FAIL;
795                 }
796
797                 i_tmp = tap_state_by_name(argus[1]);
798
799                 if (svf_tap_state_is_stable(i_tmp))
800                 {
801                         if (command == ENDIR)
802                         {
803                                 svf_para.ir_end_state = i_tmp;
804                                 LOG_DEBUG("\tIR end_state = %s",
805                                                 tap_state_name(i_tmp));
806                         }
807                         else
808                         {
809                                 svf_para.dr_end_state = i_tmp;
810                                 LOG_DEBUG("\tDR end_state = %s",
811                                                 tap_state_name(i_tmp));
812                         }
813                 }
814                 else
815                 {
816                         LOG_ERROR("%s: %s is not a stable state",
817                                         argus[0], argus[1]);
818                         return ERROR_FAIL;
819                 }
820                 break;
821         case FREQUENCY:
822                 if ((num_of_argu != 1) && (num_of_argu != 3))
823                 {
824                         LOG_ERROR("invalid parameter of %s", argus[0]);
825                         return ERROR_FAIL;
826                 }
827                 if (1 == num_of_argu)
828                 {
829                         // TODO: set jtag speed to full speed
830                         svf_para.frequency = 0;
831                 }
832                 else
833                 {
834                         if (strcmp(argus[2], "HZ"))
835                         {
836                                 LOG_ERROR("HZ not found in FREQUENCY command");
837                                 return ERROR_FAIL;
838                         }
839                         if (ERROR_OK != svf_execute_tap())
840                         {
841                                 return ERROR_FAIL;
842                         }
843                         svf_para.frequency = atof(argus[1]);
844                         // TODO: set jtag speed to
845                         if (svf_para.frequency > 0)
846                         {
847                                 command_run_linef(cmd_ctx, "jtag_khz %d", (int)svf_para.frequency / 1000);
848                                 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
849                         }
850                 }
851                 break;
852         case HDR:
853                 xxr_para_tmp = &svf_para.hdr_para;
854                 goto XXR_common;
855         case HIR:
856                 xxr_para_tmp = &svf_para.hir_para;
857                 goto XXR_common;
858         case TDR:
859                 xxr_para_tmp = &svf_para.tdr_para;
860                 goto XXR_common;
861         case TIR:
862                 xxr_para_tmp = &svf_para.tir_para;
863                 goto XXR_common;
864         case SDR:
865                 xxr_para_tmp = &svf_para.sdr_para;
866                 goto XXR_common;
867         case SIR:
868                 xxr_para_tmp = &svf_para.sir_para;
869                 goto XXR_common;
870                 XXR_common:
871                 // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
872                 if ((num_of_argu > 10) || (num_of_argu % 2))
873                 {
874                         LOG_ERROR("invalid parameter of %s", argus[0]);
875                         return ERROR_FAIL;
876                 }
877                 i_tmp = xxr_para_tmp->len;
878                 xxr_para_tmp->len = atoi(argus[1]);
879                 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
880                 xxr_para_tmp->data_mask = 0;
881                 for (i = 2; i < num_of_argu; i += 2)
882                 {
883                         if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
884                         {
885                                 LOG_ERROR("data section error");
886                                 return ERROR_FAIL;
887                         }
888                         argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
889                         // TDI, TDO, MASK, SMASK
890                         if (!strcmp(argus[i], "TDI"))
891                         {
892                                 // TDI
893                                 pbuffer_tmp = &xxr_para_tmp->tdi;
894                                 xxr_para_tmp->data_mask |= XXR_TDI;
895                         }
896                         else if (!strcmp(argus[i], "TDO"))
897                         {
898                                 // TDO
899                                 pbuffer_tmp = &xxr_para_tmp->tdo;
900                                 xxr_para_tmp->data_mask |= XXR_TDO;
901                         }
902                         else if (!strcmp(argus[i], "MASK"))
903                         {
904                                 // MASK
905                                 pbuffer_tmp = &xxr_para_tmp->mask;
906                                 xxr_para_tmp->data_mask |= XXR_MASK;
907                         }
908                         else if (!strcmp(argus[i], "SMASK"))
909                         {
910                                 // SMASK
911                                 pbuffer_tmp = &xxr_para_tmp->smask;
912                                 xxr_para_tmp->data_mask |= XXR_SMASK;
913                         }
914                         else
915                         {
916                                 LOG_ERROR("unknow parameter: %s", argus[i]);
917                                 return ERROR_FAIL;
918                         }
919                         if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
920                         {
921                                 LOG_ERROR("fail to parse hex value");
922                                 return ERROR_FAIL;
923                         }
924                         LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
925                 }
926                 // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
927                 // the mask pattern used is all cares
928                 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
929                 {
930                         // MASK not defined and length changed
931                         if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
932                         {
933                                 LOG_ERROR("fail to adjust length of array");
934                                 return ERROR_FAIL;
935                         }
936                         buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
937                 }
938                 // If TDO is absent, no comparison is needed, set the mask to 0
939                 if (!(xxr_para_tmp->data_mask & XXR_TDO))
940                 {
941                         if (NULL == xxr_para_tmp->tdo)
942                         {
943                                 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
944                                 {
945                                         LOG_ERROR("fail to adjust length of array");
946                                         return ERROR_FAIL;
947                                 }
948                         }
949                         if (NULL == xxr_para_tmp->mask)
950                         {
951                                 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
952                                 {
953                                         LOG_ERROR("fail to adjust length of array");
954                                         return ERROR_FAIL;
955                                 }
956                         }
957                         memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
958                 }
959                 // do scan if necessary
960                 if (SDR == command)
961                 {
962                         // check buffer size first, reallocate if necessary
963                         i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
964                         if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
965                         {
966 #if 1
967                                 // simply print error message
968                                 LOG_ERROR("buffer is not enough, report to author");
969                                 return ERROR_FAIL;
970 #else
971                                 uint8_t *buffer_tmp;
972
973                                 // reallocate buffer
974                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
975                                 if (NULL == buffer_tmp)
976                                 {
977                                         LOG_ERROR("not enough memory");
978                                         return ERROR_FAIL;
979                                 }
980                                 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
981                                 // svf_tdi_buffer isn't NULL here
982                                 free(svf_tdi_buffer);
983                                 svf_tdi_buffer = buffer_tmp;
984
985                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
986                                 if (NULL == buffer_tmp)
987                                 {
988                                         LOG_ERROR("not enough memory");
989                                         return ERROR_FAIL;
990                                 }
991                                 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
992                                 // svf_tdo_buffer isn't NULL here
993                                 free(svf_tdo_buffer);
994                                 svf_tdo_buffer = buffer_tmp;
995
996                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
997                                 if (NULL == buffer_tmp)
998                                 {
999                                         LOG_ERROR("not enough memory");
1000                                         return ERROR_FAIL;
1001                                 }
1002                                 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1003                                 // svf_mask_buffer isn't NULL here
1004                                 free(svf_mask_buffer);
1005                                 svf_mask_buffer = buffer_tmp;
1006
1007                                 buffer_tmp = NULL;
1008                                 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1009 #endif
1010                         }
1011
1012                         // assemble dr data
1013                         i = 0;
1014                         buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1015                         i += svf_para.hdr_para.len;
1016                         buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1017                         i += svf_para.sdr_para.len;
1018                         buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1019                         i += svf_para.tdr_para.len;
1020
1021                         // add check data
1022                         if (svf_para.sdr_para.data_mask & XXR_TDO)
1023                         {
1024                                 // assemble dr mask data
1025                                 i = 0;
1026                                 buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1027                                 i += svf_para.hdr_para.len;
1028                                 buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1029                                 i += svf_para.sdr_para.len;
1030                                 buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1031                                 i += svf_para.tdr_para.len;
1032                                 // assemble dr check data
1033                                 i = 0;
1034                                 buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1035                                 i += svf_para.hdr_para.len;
1036                                 buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1037                                 i += svf_para.sdr_para.len;
1038                                 buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1039                                 i += svf_para.tdr_para.len;
1040
1041                                 svf_add_check_para(1, svf_buffer_index, i);
1042                         }
1043                         else
1044                         {
1045                                 svf_add_check_para(0, svf_buffer_index, i);
1046                         }
1047                         field.tap = tap;
1048                         field.num_bits = i;
1049                         field.out_value = &svf_tdi_buffer[svf_buffer_index];
1050                         field.in_value = &svf_tdi_buffer[svf_buffer_index];
1051                         /* NOTE:  doesn't use SVF-specified state paths */
1052                         jtag_add_plain_dr_scan(1, &field, svf_para.dr_end_state);
1053
1054                         svf_buffer_index += (i + 7) >> 3;
1055                 }
1056                 else if (SIR == command)
1057                 {
1058                         // check buffer size first, reallocate if necessary
1059                         i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
1060                         if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1061                         {
1062 #if 1
1063                                 // simply print error message
1064                                 LOG_ERROR("buffer is not enough, report to author");
1065                                 return ERROR_FAIL;
1066 #else
1067                                 uint8_t *buffer_tmp;
1068
1069                                 // reallocate buffer
1070                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1071                                 if (NULL == buffer_tmp)
1072                                 {
1073                                         LOG_ERROR("not enough memory");
1074                                         return ERROR_FAIL;
1075                                 }
1076                                 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1077                                 // svf_tdi_buffer isn't NULL here
1078                                 free(svf_tdi_buffer);
1079                                 svf_tdi_buffer = buffer_tmp;
1080
1081                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1082                                 if (NULL == buffer_tmp)
1083                                 {
1084                                         LOG_ERROR("not enough memory");
1085                                         return ERROR_FAIL;
1086                                 }
1087                                 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1088                                 // svf_tdo_buffer isn't NULL here
1089                                 free(svf_tdo_buffer);
1090                                 svf_tdo_buffer = buffer_tmp;
1091
1092                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1093                                 if (NULL == buffer_tmp)
1094                                 {
1095                                         LOG_ERROR("not enough memory");
1096                                         return ERROR_FAIL;
1097                                 }
1098                                 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1099                                 // svf_mask_buffer isn't NULL here
1100                                 free(svf_mask_buffer);
1101                                 svf_mask_buffer = buffer_tmp;
1102
1103                                 buffer_tmp = NULL;
1104                                 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1105 #endif
1106                         }
1107
1108                         // assemble ir data
1109                         i = 0;
1110                         buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1111                         i += svf_para.hir_para.len;
1112                         buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1113                         i += svf_para.sir_para.len;
1114                         buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1115                         i += svf_para.tir_para.len;
1116
1117                         // add check data
1118                         if (svf_para.sir_para.data_mask & XXR_TDO)
1119                         {
1120                                 // assemble dr mask data
1121                                 i = 0;
1122                                 buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1123                                 i += svf_para.hir_para.len;
1124                                 buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1125                                 i += svf_para.sir_para.len;
1126                                 buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1127                                 i += svf_para.tir_para.len;
1128                                 // assemble dr check data
1129                                 i = 0;
1130                                 buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1131                                 i += svf_para.hir_para.len;
1132                                 buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1133                                 i += svf_para.sir_para.len;
1134                                 buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1135                                 i += svf_para.tir_para.len;
1136
1137                                 svf_add_check_para(1, svf_buffer_index, i);
1138                         }
1139                         else
1140                         {
1141                                 svf_add_check_para(0, svf_buffer_index, i);
1142                         }
1143                         field.tap = tap;
1144                         field.num_bits = i;
1145                         field.out_value = &svf_tdi_buffer[svf_buffer_index];
1146                         field.in_value = &svf_tdi_buffer[svf_buffer_index];
1147                         /* NOTE:  doesn't use SVF-specified state paths */
1148                         jtag_add_plain_ir_scan(1, &field, svf_para.ir_end_state);
1149
1150                         svf_buffer_index += (i + 7) >> 3;
1151                 }
1152                 break;
1153         case PIO:
1154         case PIOMAP:
1155                 LOG_ERROR("PIO and PIOMAP are not supported");
1156                 return ERROR_FAIL;
1157                 break;
1158         case RUNTEST:
1159                 // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
1160                 // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
1161                 if ((num_of_argu < 3) && (num_of_argu > 11))
1162                 {
1163                         LOG_ERROR("invalid parameter of %s", argus[0]);
1164                         return ERROR_FAIL;
1165                 }
1166                 // init
1167                 run_count = 0;
1168                 min_time = 0;
1169                 max_time = 0;
1170                 i = 1;
1171
1172                 // run_state
1173                 i_tmp = tap_state_by_name(argus[i]);
1174                 if (i_tmp != TAP_INVALID)
1175                 {
1176                         if (svf_tap_state_is_stable(i_tmp))
1177                         {
1178                                 svf_para.runtest_run_state = i_tmp;
1179
1180                                 /* When a run_state is specified, the new
1181                                  * run_state becomes the default end_state.
1182                                  */
1183                                 svf_para.runtest_end_state = i_tmp;
1184                                 LOG_DEBUG("\trun_state = %s",
1185                                                 tap_state_name(i_tmp));
1186                                 i++;
1187                         }
1188                         else
1189                         {
1190                                 LOG_ERROR("%s: %s is not a stable state",
1191                                         argus[0], tap_state_name(i_tmp));
1192                                 return ERROR_FAIL;
1193                         }
1194                 }
1195
1196                 // run_count run_clk
1197                 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
1198                 {
1199                         if (!strcmp(argus[i + 1], "TCK"))
1200                         {
1201                                 // clock source is TCK
1202                                 run_count = atoi(argus[i]);
1203                                 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1204                         }
1205                         else
1206                         {
1207                                 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1208                                 return ERROR_FAIL;
1209                         }
1210                         i += 2;
1211                 }
1212                 // min_time SEC
1213                 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
1214                 {
1215                         min_time = atof(argus[i]);
1216                         LOG_DEBUG("\tmin_time = %fs", min_time);
1217                         i += 2;
1218                 }
1219                 // MAXIMUM max_time SEC
1220                 if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
1221                 {
1222                         max_time = atof(argus[i + 1]);
1223                         LOG_DEBUG("\tmax_time = %fs", max_time);
1224                         i += 3;
1225                 }
1226                 // ENDSTATE end_state
1227                 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
1228                 {
1229                         i_tmp = tap_state_by_name(argus[i + 1]);
1230
1231                         if (svf_tap_state_is_stable(i_tmp))
1232                         {
1233                                 svf_para.runtest_end_state = i_tmp;
1234                                 LOG_DEBUG("\tend_state = %s",
1235                                         tap_state_name(i_tmp));
1236                         }
1237                         else
1238                         {
1239                                 LOG_ERROR("%s: %s is not a stable state",
1240                                         argus[0], tap_state_name(i_tmp));
1241                                 return ERROR_FAIL;
1242                         }
1243                         i += 2;
1244                 }
1245                 // calculate run_count
1246                 if ((0 == run_count) && (min_time > 0))
1247                 {
1248                         run_count = min_time * svf_para.frequency;
1249                 }
1250                 // all parameter should be parsed
1251                 if (i == num_of_argu)
1252                 {
1253                         if (run_count > 0)
1254                         {
1255                                 // run_state and end_state is checked to be stable state
1256                                 // TODO: do runtest
1257 #if 1
1258                                 /* FIXME handle statemove failures */
1259                                 int retval;
1260
1261                                 // enter into run_state if necessary
1262                                 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1263                                 {
1264                                         retval = svf_add_statemove(svf_para.runtest_run_state);
1265                                 }
1266
1267                                 // call jtag_add_clocks
1268                                 jtag_add_clocks(run_count);
1269
1270                                 // move to end_state if necessary
1271                                 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1272                                 {
1273                                         retval = svf_add_statemove(svf_para.runtest_end_state);
1274                                 }
1275 #else
1276                                 if (svf_para.runtest_run_state != TAP_IDLE)
1277                                 {
1278                                         LOG_ERROR("cannot runtest in %s state",
1279                                                 tap_state_name(svf_para.runtest_run_state));
1280                                         return ERROR_FAIL;
1281                                 }
1282
1283                                 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1284 #endif
1285                         }
1286                 }
1287                 else
1288                 {
1289                         LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
1290                         return ERROR_FAIL;
1291                 }
1292                 break;
1293         case STATE:
1294                 // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
1295                 if (num_of_argu < 2)
1296                 {
1297                         LOG_ERROR("invalid parameter of %s", argus[0]);
1298                         return ERROR_FAIL;
1299                 }
1300                 if (num_of_argu > 2)
1301                 {
1302                         // STATE pathstate1 ... stable_state
1303                         path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1304                         if (NULL == path)
1305                         {
1306                                 LOG_ERROR("not enough memory");
1307                                 return ERROR_FAIL;
1308                         }
1309                         num_of_argu--;          // num of path
1310                         i_tmp = 1;              /* path is from parameter 1 */
1311                         for (i = 0; i < num_of_argu; i++, i_tmp++)
1312                         {
1313                                 path[i] = tap_state_by_name(argus[i_tmp]);
1314                                 if (path[i] == TAP_INVALID)
1315                                 {
1316                                         LOG_ERROR("%s: %s is not a valid state",
1317                                                 argus[0], argus[i_tmp]);
1318                                         free(path);
1319                                         return ERROR_FAIL;
1320                                 }
1321                                 /* OpenOCD refuses paths containing TAP_RESET */
1322                                 if (TAP_RESET == path[i])
1323                                 {
1324                                         /* FIXME last state MUST be stable! */
1325                                         if (i > 0)
1326                                         {
1327                                                 jtag_add_pathmove(i, path);
1328                                         }
1329                                         jtag_add_tlr();
1330                                         num_of_argu -= i + 1;
1331                                         i = -1;
1332                                 }
1333                         }
1334                         if (num_of_argu > 0)
1335                         {
1336                                 // execute last path if necessary
1337                                 if (svf_tap_state_is_stable(path[num_of_argu - 1]))
1338                                 {
1339                                         // last state MUST be stable state
1340                                         jtag_add_pathmove(num_of_argu, path);
1341                                         LOG_DEBUG("\tmove to %s by path_move",
1342                                                 tap_state_name(path[num_of_argu - 1]));
1343                                 }
1344                                 else
1345                                 {
1346                                         LOG_ERROR("%s: %s is not a stable state",
1347                                                 argus[0],
1348                                                 tap_state_name(path[num_of_argu - 1]));
1349                                         free(path);
1350                                         return ERROR_FAIL;
1351                                 }
1352                         }
1353
1354                         free(path);
1355                         path = NULL;
1356                 }
1357                 else
1358                 {
1359                         // STATE stable_state
1360                         state = tap_state_by_name(argus[1]);
1361                         if (svf_tap_state_is_stable(state))
1362                         {
1363                                 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1364                                                 tap_state_name(state));
1365                                 /* FIXME handle statemove failures */
1366                                 svf_add_statemove(state);
1367                         }
1368                         else
1369                         {
1370                                 LOG_ERROR("%s: %s is not a stable state",
1371                                         argus[0], tap_state_name(state));
1372                                 return ERROR_FAIL;
1373                         }
1374                 }
1375                 break;
1376         case TRST:
1377                 // TRST trst_mode
1378                 if (num_of_argu != 2)
1379                 {
1380                         LOG_ERROR("invalid parameter of %s", argus[0]);
1381                         return ERROR_FAIL;
1382                 }
1383                 if (svf_para.trst_mode != TRST_ABSENT)
1384                 {
1385                         if (ERROR_OK != svf_execute_tap())
1386                         {
1387                                 return ERROR_FAIL;
1388                         }
1389                         i_tmp = svf_find_string_in_array(argus[1],
1390                                         (char **)svf_trst_mode_name,
1391                                         ARRAY_SIZE(svf_trst_mode_name));
1392                         switch (i_tmp)
1393                         {
1394                         case TRST_ON:
1395                                 jtag_add_reset(1, 0);
1396                                 break;
1397                         case TRST_Z:
1398                         case TRST_OFF:
1399                                 jtag_add_reset(0, 0);
1400                                 break;
1401                         case TRST_ABSENT:
1402                                 break;
1403                         default:
1404                                 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1405                                 return ERROR_FAIL;
1406                         }
1407                         svf_para.trst_mode = i_tmp;
1408                         LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1409                 }
1410                 else
1411                 {
1412                         LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1413                         return ERROR_FAIL;
1414                 }
1415                 break;
1416         default:
1417                 LOG_ERROR("invalid svf command: %s", argus[0]);
1418                 return ERROR_FAIL;
1419                 break;
1420         }
1421
1422         if (debug_level >= LOG_LVL_DEBUG)
1423         {
1424                 // for convenient debugging, execute tap if possible
1425                 if ((svf_buffer_index > 0) && \
1426                         (((command != STATE) && (command != RUNTEST)) || \
1427                         ((command == STATE) && (num_of_argu == 2))))
1428                 {
1429                         if (ERROR_OK != svf_execute_tap())
1430                         {
1431                                 return ERROR_FAIL;
1432                         }
1433
1434                         // output debug info
1435                         if ((SIR == command) || (SDR == command))
1436                         {
1437                                 int read_value;
1438                                 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1439                                 // in debug mode, data is from index 0
1440                                 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1441                                 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1442                         }
1443                 }
1444         }
1445         else
1446         {
1447                 // for fast executing, execute tap if necessary
1448                 // half of the buffer is for the next command
1449                 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1450                         (((command != STATE) && (command != RUNTEST)) || \
1451                         ((command == STATE) && (num_of_argu == 2))))
1452                 {
1453                         return svf_execute_tap();
1454                 }
1455         }
1456
1457         return ERROR_OK;
1458 }
1459
1460 static const struct command_registration svf_command_handlers[] = {
1461         {
1462                 .name = "svf",
1463                 .handler = &handle_svf_command,
1464                 .mode = COMMAND_EXEC,
1465                 .help = "Runs a SVF file.",
1466                 .usage = "<file>",
1467         },
1468         COMMAND_REGISTRATION_DONE
1469 };
1470
1471 int svf_register_commands(struct command_context *cmd_ctx)
1472 {
1473         return register_commands(cmd_ctx, NULL, svf_command_handlers);
1474 }