cygwin 1.7 build 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 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                         break;
504                 default:
505                         if (!comment)
506                         {
507                                 if (cmd_pos >= svf_command_buffer_size - 1)
508                                 {
509                                         tmp_buffer = (char*)malloc(svf_command_buffer_size + SVFP_CMD_INC_CNT);         // 1 more byte for '\0'
510                                         if (NULL == tmp_buffer)
511                                         {
512                                                 LOG_ERROR("not enough memory");
513                                                 return ERROR_FAIL;
514                                         }
515                                         if (svf_command_buffer_size > 0)
516                                         {
517                                                 memcpy(tmp_buffer, svf_command_buffer, svf_command_buffer_size);
518                                         }
519                                         if (svf_command_buffer != NULL)
520                                         {
521                                                 free(svf_command_buffer);
522                                         }
523                                         svf_command_buffer = tmp_buffer;
524                                         svf_command_buffer_size += SVFP_CMD_INC_CNT;
525                                         tmp_buffer = NULL;
526                                 }
527                                 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
528                         }
529                         break;
530                 }
531         }
532
533         if (cmd_ok)
534         {
535                 svf_command_buffer[cmd_pos] = '\0';
536                 return ERROR_OK;
537         }
538         else
539         {
540                 return ERROR_FAIL;
541         }
542 }
543
544 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
545 {
546         int pos = 0, num = 0, space_found = 1;
547
548         while (pos < len)
549         {
550                 switch (str[pos])
551                 {
552                 case '\n':
553                 case '\r':
554                 case '!':
555                 case '/':
556                         LOG_ERROR("fail to parse svf command");
557                         return ERROR_FAIL;
558                         break;
559                 case ' ':
560                         space_found = 1;
561                         str[pos] = '\0';
562                         break;
563                 default:
564                         if (space_found)
565                         {
566                                 argus[num++] = &str[pos];
567                                 space_found = 0;
568                         }
569                         break;
570                 }
571                 pos++;
572         }
573
574         *num_of_argu = num;
575
576         return ERROR_OK;
577 }
578
579 bool svf_tap_state_is_stable(tap_state_t state)
580 {
581         return (TAP_RESET == state) || (TAP_IDLE == state)
582                         || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
583 }
584
585 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
586 {
587         int i;
588
589         for (i = 0; i < num_of_element; i++)
590         {
591                 if (!strcmp(str, strs[i]))
592                 {
593                         return i;
594                 }
595         }
596         return 0xFF;
597 }
598
599 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
600 {
601         int new_byte_len = (new_bit_len + 7) >> 3;
602
603         if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
604         {
605                 if (*arr != NULL)
606                 {
607                         free(*arr);
608                         *arr = NULL;
609                 }
610                 *arr = (uint8_t*)malloc(new_byte_len);
611                 if (NULL == *arr)
612                 {
613                         LOG_ERROR("not enough memory");
614                         return ERROR_FAIL;
615                 }
616                 memset(*arr, 0, new_byte_len);
617         }
618         return ERROR_OK;
619 }
620
621 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
622 {
623         int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
624         uint8_t ch = 0;
625
626         if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
627         {
628                 LOG_ERROR("fail to adjust length of array");
629                 return ERROR_FAIL;
630         }
631
632         for (i = 0; i < str_hbyte_len; i++)
633         {
634                 ch = 0;
635                 while (str_len > 0)
636                 {
637                         ch = str[--str_len];
638
639                         if (!isblank(ch))
640                         {
641                                 if ((ch >= '0') && (ch <= '9'))
642                                 {
643                                         ch = ch - '0';
644                                         break;
645                                 }
646                                 else if ((ch >= 'A') && (ch <= 'F'))
647                                 {
648                                         ch = ch - 'A' + 10;
649                                         break;
650                                 }
651                                 else
652                                 {
653                                         LOG_ERROR("invalid hex string");
654                                         return ERROR_FAIL;
655                                 }
656                         }
657
658                         ch = 0;
659                 }
660
661                 // write bin
662                 if (i % 2)
663                 {
664                         // MSB
665                         (*bin)[i / 2] |= ch << 4;
666                 }
667                 else
668                 {
669                         // LSB
670                         (*bin)[i / 2] = 0;
671                         (*bin)[i / 2] |= ch;
672                 }
673         }
674
675         // consume optional leading '0' characters
676         while (str_len > 0 && str[str_len - 1] == '0')
677                 str_len--;
678
679         // check valid
680         if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0)
681         {
682                 LOG_ERROR("value execeeds length");
683                 return ERROR_FAIL;
684         }
685
686         return ERROR_OK;
687 }
688
689 static int svf_check_tdo(void)
690 {
691         int i, len, index;
692
693         for (i = 0; i < svf_check_tdo_para_index; i++)
694         {
695                 index = svf_check_tdo_para[i].buffer_offset;
696                 len = svf_check_tdo_para[i].bit_len;
697                 if ((svf_check_tdo_para[i].enabled)
698                         && buf_cmp_mask(&svf_tdi_buffer[index], &svf_tdo_buffer[index], &svf_mask_buffer[index], len))
699                 {
700                         unsigned bitmask;
701                         unsigned received, expected, tapmask;
702                         bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
703
704                         memcpy(&received, svf_tdi_buffer + index, sizeof(unsigned));
705                         memcpy(&expected, svf_tdo_buffer + index, sizeof(unsigned));
706                         memcpy(&tapmask, svf_mask_buffer + index, sizeof(unsigned));
707                         LOG_ERROR("tdo check error at line %d",
708                                           svf_check_tdo_para[i].line_num);
709                         LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
710                                           received & bitmask,
711                                           expected & bitmask,
712                                           tapmask & bitmask);
713                         return ERROR_FAIL;
714                 }
715         }
716         svf_check_tdo_para_index = 0;
717
718         return ERROR_OK;
719 }
720
721 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
722 {
723         if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
724         {
725                 LOG_ERROR("toooooo many operation undone");
726                 return ERROR_FAIL;
727         }
728
729         svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
730         svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
731         svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
732         svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
733         svf_check_tdo_para_index++;
734
735         return ERROR_OK;
736 }
737
738 static int svf_execute_tap(void)
739 {
740         if (ERROR_OK != jtag_execute_queue())
741         {
742                 return ERROR_FAIL;
743         }
744         else if (ERROR_OK != svf_check_tdo())
745         {
746                 return ERROR_FAIL;
747         }
748
749         svf_buffer_index = 0;
750
751         return ERROR_OK;
752 }
753
754 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
755 {
756         char *argus[256], command;
757         int num_of_argu = 0, i;
758
759         // tmp variable
760         int i_tmp;
761
762         // for RUNTEST
763         int run_count;
764         float min_time, max_time;
765         // for XXR
766         struct svf_xxr_para *xxr_para_tmp;
767         uint8_t **pbuffer_tmp;
768         struct scan_field field;
769         // for STATE
770         tap_state_t *path = NULL, state;
771
772         if (!svf_quiet)
773         {
774                 LOG_USER("%s", svf_command_buffer);
775         }
776
777         if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
778         {
779                 return ERROR_FAIL;
780         }
781
782         /* NOTE: we're a bit loose here, because we ignore case in
783          * TAP state names (instead of insisting on uppercase).
784          */
785
786         command = svf_find_string_in_array(argus[0],
787                         (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
788         switch (command)
789         {
790         case ENDDR:
791         case ENDIR:
792                 if (num_of_argu != 2)
793                 {
794                         LOG_ERROR("invalid parameter of %s", argus[0]);
795                         return ERROR_FAIL;
796                 }
797
798                 i_tmp = tap_state_by_name(argus[1]);
799
800                 if (svf_tap_state_is_stable(i_tmp))
801                 {
802                         if (command == ENDIR)
803                         {
804                                 svf_para.ir_end_state = i_tmp;
805                                 LOG_DEBUG("\tIR end_state = %s",
806                                                 tap_state_name(i_tmp));
807                         }
808                         else
809                         {
810                                 svf_para.dr_end_state = i_tmp;
811                                 LOG_DEBUG("\tDR end_state = %s",
812                                                 tap_state_name(i_tmp));
813                         }
814                 }
815                 else
816                 {
817                         LOG_ERROR("%s: %s is not a stable state",
818                                         argus[0], argus[1]);
819                         return ERROR_FAIL;
820                 }
821                 break;
822         case FREQUENCY:
823                 if ((num_of_argu != 1) && (num_of_argu != 3))
824                 {
825                         LOG_ERROR("invalid parameter of %s", argus[0]);
826                         return ERROR_FAIL;
827                 }
828                 if (1 == num_of_argu)
829                 {
830                         // TODO: set jtag speed to full speed
831                         svf_para.frequency = 0;
832                 }
833                 else
834                 {
835                         if (strcmp(argus[2], "HZ"))
836                         {
837                                 LOG_ERROR("HZ not found in FREQUENCY command");
838                                 return ERROR_FAIL;
839                         }
840                         if (ERROR_OK != svf_execute_tap())
841                         {
842                                 return ERROR_FAIL;
843                         }
844                         svf_para.frequency = atof(argus[1]);
845                         // TODO: set jtag speed to
846                         if (svf_para.frequency > 0)
847                         {
848                                 command_run_linef(cmd_ctx, "jtag_khz %d", (int)svf_para.frequency / 1000);
849                                 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
850                         }
851                 }
852                 break;
853         case HDR:
854                 xxr_para_tmp = &svf_para.hdr_para;
855                 goto XXR_common;
856         case HIR:
857                 xxr_para_tmp = &svf_para.hir_para;
858                 goto XXR_common;
859         case TDR:
860                 xxr_para_tmp = &svf_para.tdr_para;
861                 goto XXR_common;
862         case TIR:
863                 xxr_para_tmp = &svf_para.tir_para;
864                 goto XXR_common;
865         case SDR:
866                 xxr_para_tmp = &svf_para.sdr_para;
867                 goto XXR_common;
868         case SIR:
869                 xxr_para_tmp = &svf_para.sir_para;
870                 goto XXR_common;
871                 XXR_common:
872                 // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
873                 if ((num_of_argu > 10) || (num_of_argu % 2))
874                 {
875                         LOG_ERROR("invalid parameter of %s", argus[0]);
876                         return ERROR_FAIL;
877                 }
878                 i_tmp = xxr_para_tmp->len;
879                 xxr_para_tmp->len = atoi(argus[1]);
880                 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
881                 xxr_para_tmp->data_mask = 0;
882                 for (i = 2; i < num_of_argu; i += 2)
883                 {
884                         if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
885                         {
886                                 LOG_ERROR("data section error");
887                                 return ERROR_FAIL;
888                         }
889                         argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
890                         // TDI, TDO, MASK, SMASK
891                         if (!strcmp(argus[i], "TDI"))
892                         {
893                                 // TDI
894                                 pbuffer_tmp = &xxr_para_tmp->tdi;
895                                 xxr_para_tmp->data_mask |= XXR_TDI;
896                         }
897                         else if (!strcmp(argus[i], "TDO"))
898                         {
899                                 // TDO
900                                 pbuffer_tmp = &xxr_para_tmp->tdo;
901                                 xxr_para_tmp->data_mask |= XXR_TDO;
902                         }
903                         else if (!strcmp(argus[i], "MASK"))
904                         {
905                                 // MASK
906                                 pbuffer_tmp = &xxr_para_tmp->mask;
907                                 xxr_para_tmp->data_mask |= XXR_MASK;
908                         }
909                         else if (!strcmp(argus[i], "SMASK"))
910                         {
911                                 // SMASK
912                                 pbuffer_tmp = &xxr_para_tmp->smask;
913                                 xxr_para_tmp->data_mask |= XXR_SMASK;
914                         }
915                         else
916                         {
917                                 LOG_ERROR("unknow parameter: %s", argus[i]);
918                                 return ERROR_FAIL;
919                         }
920                         if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
921                         {
922                                 LOG_ERROR("fail to parse hex value");
923                                 return ERROR_FAIL;
924                         }
925                         LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
926                 }
927                 // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
928                 // the mask pattern used is all cares
929                 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
930                 {
931                         // MASK not defined and length changed
932                         if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
933                         {
934                                 LOG_ERROR("fail to adjust length of array");
935                                 return ERROR_FAIL;
936                         }
937                         buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
938                 }
939                 // If TDO is absent, no comparison is needed, set the mask to 0
940                 if (!(xxr_para_tmp->data_mask & XXR_TDO))
941                 {
942                         if (NULL == xxr_para_tmp->tdo)
943                         {
944                                 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
945                                 {
946                                         LOG_ERROR("fail to adjust length of array");
947                                         return ERROR_FAIL;
948                                 }
949                         }
950                         if (NULL == xxr_para_tmp->mask)
951                         {
952                                 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
953                                 {
954                                         LOG_ERROR("fail to adjust length of array");
955                                         return ERROR_FAIL;
956                                 }
957                         }
958                         memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
959                 }
960                 // do scan if necessary
961                 if (SDR == command)
962                 {
963                         // check buffer size first, reallocate if necessary
964                         i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
965                         if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
966                         {
967 #if 1
968                                 // simply print error message
969                                 LOG_ERROR("buffer is not enough, report to author");
970                                 return ERROR_FAIL;
971 #else
972                                 uint8_t *buffer_tmp;
973
974                                 // reallocate buffer
975                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
976                                 if (NULL == buffer_tmp)
977                                 {
978                                         LOG_ERROR("not enough memory");
979                                         return ERROR_FAIL;
980                                 }
981                                 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
982                                 // svf_tdi_buffer isn't NULL here
983                                 free(svf_tdi_buffer);
984                                 svf_tdi_buffer = buffer_tmp;
985
986                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
987                                 if (NULL == buffer_tmp)
988                                 {
989                                         LOG_ERROR("not enough memory");
990                                         return ERROR_FAIL;
991                                 }
992                                 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
993                                 // svf_tdo_buffer isn't NULL here
994                                 free(svf_tdo_buffer);
995                                 svf_tdo_buffer = buffer_tmp;
996
997                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
998                                 if (NULL == buffer_tmp)
999                                 {
1000                                         LOG_ERROR("not enough memory");
1001                                         return ERROR_FAIL;
1002                                 }
1003                                 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1004                                 // svf_mask_buffer isn't NULL here
1005                                 free(svf_mask_buffer);
1006                                 svf_mask_buffer = buffer_tmp;
1007
1008                                 buffer_tmp = NULL;
1009                                 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1010 #endif
1011                         }
1012
1013                         // assemble dr data
1014                         i = 0;
1015                         buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1016                         i += svf_para.hdr_para.len;
1017                         buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1018                         i += svf_para.sdr_para.len;
1019                         buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1020                         i += svf_para.tdr_para.len;
1021
1022                         // add check data
1023                         if (svf_para.sdr_para.data_mask & XXR_TDO)
1024                         {
1025                                 // assemble dr mask data
1026                                 i = 0;
1027                                 buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1028                                 i += svf_para.hdr_para.len;
1029                                 buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1030                                 i += svf_para.sdr_para.len;
1031                                 buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1032                                 i += svf_para.tdr_para.len;
1033                                 // assemble dr check data
1034                                 i = 0;
1035                                 buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1036                                 i += svf_para.hdr_para.len;
1037                                 buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1038                                 i += svf_para.sdr_para.len;
1039                                 buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1040                                 i += svf_para.tdr_para.len;
1041
1042                                 svf_add_check_para(1, svf_buffer_index, i);
1043                         }
1044                         else
1045                         {
1046                                 svf_add_check_para(0, svf_buffer_index, i);
1047                         }
1048                         field.tap = tap;
1049                         field.num_bits = i;
1050                         field.out_value = &svf_tdi_buffer[svf_buffer_index];
1051                         field.in_value = &svf_tdi_buffer[svf_buffer_index];
1052                         /* NOTE:  doesn't use SVF-specified state paths */
1053                         jtag_add_plain_dr_scan(1, &field, svf_para.dr_end_state);
1054
1055                         svf_buffer_index += (i + 7) >> 3;
1056                 }
1057                 else if (SIR == command)
1058                 {
1059                         // check buffer size first, reallocate if necessary
1060                         i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
1061                         if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1062                         {
1063 #if 1
1064                                 // simply print error message
1065                                 LOG_ERROR("buffer is not enough, report to author");
1066                                 return ERROR_FAIL;
1067 #else
1068                                 uint8_t *buffer_tmp;
1069
1070                                 // reallocate buffer
1071                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1072                                 if (NULL == buffer_tmp)
1073                                 {
1074                                         LOG_ERROR("not enough memory");
1075                                         return ERROR_FAIL;
1076                                 }
1077                                 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1078                                 // svf_tdi_buffer isn't NULL here
1079                                 free(svf_tdi_buffer);
1080                                 svf_tdi_buffer = buffer_tmp;
1081
1082                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1083                                 if (NULL == buffer_tmp)
1084                                 {
1085                                         LOG_ERROR("not enough memory");
1086                                         return ERROR_FAIL;
1087                                 }
1088                                 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1089                                 // svf_tdo_buffer isn't NULL here
1090                                 free(svf_tdo_buffer);
1091                                 svf_tdo_buffer = buffer_tmp;
1092
1093                                 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1094                                 if (NULL == buffer_tmp)
1095                                 {
1096                                         LOG_ERROR("not enough memory");
1097                                         return ERROR_FAIL;
1098                                 }
1099                                 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1100                                 // svf_mask_buffer isn't NULL here
1101                                 free(svf_mask_buffer);
1102                                 svf_mask_buffer = buffer_tmp;
1103
1104                                 buffer_tmp = NULL;
1105                                 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1106 #endif
1107                         }
1108
1109                         // assemble ir data
1110                         i = 0;
1111                         buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1112                         i += svf_para.hir_para.len;
1113                         buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1114                         i += svf_para.sir_para.len;
1115                         buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1116                         i += svf_para.tir_para.len;
1117
1118                         // add check data
1119                         if (svf_para.sir_para.data_mask & XXR_TDO)
1120                         {
1121                                 // assemble dr mask data
1122                                 i = 0;
1123                                 buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1124                                 i += svf_para.hir_para.len;
1125                                 buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1126                                 i += svf_para.sir_para.len;
1127                                 buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1128                                 i += svf_para.tir_para.len;
1129                                 // assemble dr check data
1130                                 i = 0;
1131                                 buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1132                                 i += svf_para.hir_para.len;
1133                                 buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1134                                 i += svf_para.sir_para.len;
1135                                 buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1136                                 i += svf_para.tir_para.len;
1137
1138                                 svf_add_check_para(1, svf_buffer_index, i);
1139                         }
1140                         else
1141                         {
1142                                 svf_add_check_para(0, svf_buffer_index, i);
1143                         }
1144                         field.tap = tap;
1145                         field.num_bits = i;
1146                         field.out_value = &svf_tdi_buffer[svf_buffer_index];
1147                         field.in_value = &svf_tdi_buffer[svf_buffer_index];
1148                         /* NOTE:  doesn't use SVF-specified state paths */
1149                         jtag_add_plain_ir_scan(1, &field, svf_para.ir_end_state);
1150
1151                         svf_buffer_index += (i + 7) >> 3;
1152                 }
1153                 break;
1154         case PIO:
1155         case PIOMAP:
1156                 LOG_ERROR("PIO and PIOMAP are not supported");
1157                 return ERROR_FAIL;
1158                 break;
1159         case RUNTEST:
1160                 // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
1161                 // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
1162                 if ((num_of_argu < 3) && (num_of_argu > 11))
1163                 {
1164                         LOG_ERROR("invalid parameter of %s", argus[0]);
1165                         return ERROR_FAIL;
1166                 }
1167                 // init
1168                 run_count = 0;
1169                 min_time = 0;
1170                 max_time = 0;
1171                 i = 1;
1172
1173                 // run_state
1174                 i_tmp = tap_state_by_name(argus[i]);
1175                 if (i_tmp != TAP_INVALID)
1176                 {
1177                         if (svf_tap_state_is_stable(i_tmp))
1178                         {
1179                                 svf_para.runtest_run_state = i_tmp;
1180
1181                                 /* When a run_state is specified, the new
1182                                  * run_state becomes the default end_state.
1183                                  */
1184                                 svf_para.runtest_end_state = i_tmp;
1185                                 LOG_DEBUG("\trun_state = %s",
1186                                                 tap_state_name(i_tmp));
1187                                 i++;
1188                         }
1189                         else
1190                         {
1191                                 LOG_ERROR("%s: %s is not a stable state",
1192                                         argus[0], tap_state_name(i_tmp));
1193                                 return ERROR_FAIL;
1194                         }
1195                 }
1196
1197                 // run_count run_clk
1198                 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
1199                 {
1200                         if (!strcmp(argus[i + 1], "TCK"))
1201                         {
1202                                 // clock source is TCK
1203                                 run_count = atoi(argus[i]);
1204                                 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1205                         }
1206                         else
1207                         {
1208                                 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1209                                 return ERROR_FAIL;
1210                         }
1211                         i += 2;
1212                 }
1213                 // min_time SEC
1214                 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
1215                 {
1216                         min_time = atof(argus[i]);
1217                         LOG_DEBUG("\tmin_time = %fs", min_time);
1218                         i += 2;
1219                 }
1220                 // MAXIMUM max_time SEC
1221                 if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
1222                 {
1223                         max_time = atof(argus[i + 1]);
1224                         LOG_DEBUG("\tmax_time = %fs", max_time);
1225                         i += 3;
1226                 }
1227                 // ENDSTATE end_state
1228                 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
1229                 {
1230                         i_tmp = tap_state_by_name(argus[i + 1]);
1231
1232                         if (svf_tap_state_is_stable(i_tmp))
1233                         {
1234                                 svf_para.runtest_end_state = i_tmp;
1235                                 LOG_DEBUG("\tend_state = %s",
1236                                         tap_state_name(i_tmp));
1237                         }
1238                         else
1239                         {
1240                                 LOG_ERROR("%s: %s is not a stable state",
1241                                         argus[0], tap_state_name(i_tmp));
1242                                 return ERROR_FAIL;
1243                         }
1244                         i += 2;
1245                 }
1246                 // calculate run_count
1247                 if ((0 == run_count) && (min_time > 0))
1248                 {
1249                         run_count = min_time * svf_para.frequency;
1250                 }
1251                 // all parameter should be parsed
1252                 if (i == num_of_argu)
1253                 {
1254                         if (run_count > 0)
1255                         {
1256                                 // run_state and end_state is checked to be stable state
1257                                 // TODO: do runtest
1258 #if 1
1259                                 /* FIXME handle statemove failures */
1260                                 int retval;
1261
1262                                 // enter into run_state if necessary
1263                                 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1264                                 {
1265                                         retval = svf_add_statemove(svf_para.runtest_run_state);
1266                                 }
1267
1268                                 // call jtag_add_clocks
1269                                 jtag_add_clocks(run_count);
1270
1271                                 // move to end_state if necessary
1272                                 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1273                                 {
1274                                         retval = svf_add_statemove(svf_para.runtest_end_state);
1275                                 }
1276 #else
1277                                 if (svf_para.runtest_run_state != TAP_IDLE)
1278                                 {
1279                                         LOG_ERROR("cannot runtest in %s state",
1280                                                 tap_state_name(svf_para.runtest_run_state));
1281                                         return ERROR_FAIL;
1282                                 }
1283
1284                                 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1285 #endif
1286                         }
1287                 }
1288                 else
1289                 {
1290                         LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
1291                         return ERROR_FAIL;
1292                 }
1293                 break;
1294         case STATE:
1295                 // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
1296                 if (num_of_argu < 2)
1297                 {
1298                         LOG_ERROR("invalid parameter of %s", argus[0]);
1299                         return ERROR_FAIL;
1300                 }
1301                 if (num_of_argu > 2)
1302                 {
1303                         // STATE pathstate1 ... stable_state
1304                         path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1305                         if (NULL == path)
1306                         {
1307                                 LOG_ERROR("not enough memory");
1308                                 return ERROR_FAIL;
1309                         }
1310                         num_of_argu--;          // num of path
1311                         i_tmp = 1;              /* path is from parameter 1 */
1312                         for (i = 0; i < num_of_argu; i++, i_tmp++)
1313                         {
1314                                 path[i] = tap_state_by_name(argus[i_tmp]);
1315                                 if (path[i] == TAP_INVALID)
1316                                 {
1317                                         LOG_ERROR("%s: %s is not a valid state",
1318                                                 argus[0], argus[i_tmp]);
1319                                         free(path);
1320                                         return ERROR_FAIL;
1321                                 }
1322                                 /* OpenOCD refuses paths containing TAP_RESET */
1323                                 if (TAP_RESET == path[i])
1324                                 {
1325                                         /* FIXME last state MUST be stable! */
1326                                         if (i > 0)
1327                                         {
1328                                                 jtag_add_pathmove(i, path);
1329                                         }
1330                                         jtag_add_tlr();
1331                                         num_of_argu -= i + 1;
1332                                         i = -1;
1333                                 }
1334                         }
1335                         if (num_of_argu > 0)
1336                         {
1337                                 // execute last path if necessary
1338                                 if (svf_tap_state_is_stable(path[num_of_argu - 1]))
1339                                 {
1340                                         // last state MUST be stable state
1341                                         jtag_add_pathmove(num_of_argu, path);
1342                                         LOG_DEBUG("\tmove to %s by path_move",
1343                                                 tap_state_name(path[num_of_argu - 1]));
1344                                 }
1345                                 else
1346                                 {
1347                                         LOG_ERROR("%s: %s is not a stable state",
1348                                                 argus[0],
1349                                                 tap_state_name(path[num_of_argu - 1]));
1350                                         free(path);
1351                                         return ERROR_FAIL;
1352                                 }
1353                         }
1354
1355                         free(path);
1356                         path = NULL;
1357                 }
1358                 else
1359                 {
1360                         // STATE stable_state
1361                         state = tap_state_by_name(argus[1]);
1362                         if (svf_tap_state_is_stable(state))
1363                         {
1364                                 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1365                                                 tap_state_name(state));
1366                                 /* FIXME handle statemove failures */
1367                                 svf_add_statemove(state);
1368                         }
1369                         else
1370                         {
1371                                 LOG_ERROR("%s: %s is not a stable state",
1372                                         argus[0], tap_state_name(state));
1373                                 return ERROR_FAIL;
1374                         }
1375                 }
1376                 break;
1377         case TRST:
1378                 // TRST trst_mode
1379                 if (num_of_argu != 2)
1380                 {
1381                         LOG_ERROR("invalid parameter of %s", argus[0]);
1382                         return ERROR_FAIL;
1383                 }
1384                 if (svf_para.trst_mode != TRST_ABSENT)
1385                 {
1386                         if (ERROR_OK != svf_execute_tap())
1387                         {
1388                                 return ERROR_FAIL;
1389                         }
1390                         i_tmp = svf_find_string_in_array(argus[1],
1391                                         (char **)svf_trst_mode_name,
1392                                         ARRAY_SIZE(svf_trst_mode_name));
1393                         switch (i_tmp)
1394                         {
1395                         case TRST_ON:
1396                                 jtag_add_reset(1, 0);
1397                                 break;
1398                         case TRST_Z:
1399                         case TRST_OFF:
1400                                 jtag_add_reset(0, 0);
1401                                 break;
1402                         case TRST_ABSENT:
1403                                 break;
1404                         default:
1405                                 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1406                                 return ERROR_FAIL;
1407                         }
1408                         svf_para.trst_mode = i_tmp;
1409                         LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1410                 }
1411                 else
1412                 {
1413                         LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1414                         return ERROR_FAIL;
1415                 }
1416                 break;
1417         default:
1418                 LOG_ERROR("invalid svf command: %s", argus[0]);
1419                 return ERROR_FAIL;
1420                 break;
1421         }
1422
1423         if (debug_level >= LOG_LVL_DEBUG)
1424         {
1425                 // for convenient debugging, execute tap if possible
1426                 if ((svf_buffer_index > 0) && \
1427                         (((command != STATE) && (command != RUNTEST)) || \
1428                         ((command == STATE) && (num_of_argu == 2))))
1429                 {
1430                         if (ERROR_OK != svf_execute_tap())
1431                         {
1432                                 return ERROR_FAIL;
1433                         }
1434
1435                         // output debug info
1436                         if ((SIR == command) || (SDR == command))
1437                         {
1438                                 int read_value;
1439                                 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1440                                 // in debug mode, data is from index 0
1441                                 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1442                                 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1443                         }
1444                 }
1445         }
1446         else
1447         {
1448                 // for fast executing, execute tap if necessary
1449                 // half of the buffer is for the next command
1450                 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1451                         (((command != STATE) && (command != RUNTEST)) || \
1452                         ((command == STATE) && (num_of_argu == 2))))
1453                 {
1454                         return svf_execute_tap();
1455                 }
1456         }
1457
1458         return ERROR_OK;
1459 }
1460
1461 static const struct command_registration svf_command_handlers[] = {
1462         {
1463                 .name = "svf",
1464                 .handler = &handle_svf_command,
1465                 .mode = COMMAND_EXEC,
1466                 .help = "Runs a SVF file.",
1467                 .usage = "<file>",
1468         },
1469         COMMAND_REGISTRATION_DONE
1470 };
1471
1472 int svf_register_commands(struct command_context *cmd_ctx)
1473 {
1474         return register_commands(cmd_ctx, NULL, svf_command_handlers);
1475 }