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