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