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