XSVF: use svf_add_statemove()
[openocd/ztw.git] / src / svf / svf.c
blobdfbbde4bec03fa648518395dd801db43ba21b241
1 /*
2 * Copyright (C) 2009 by Simon Qian
3 * SimonQian@SimonQian.com
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.
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.
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
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".
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".
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include "jtag.h"
35 #include "svf.h"
36 #include "time_support.h"
39 // SVF command
40 typedef enum
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;
58 const char *svf_command_name[14] =
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"
76 typedef enum
78 TRST_ON,
79 TRST_OFF,
80 TRST_Z,
81 TRST_ABSENT
82 }trst_mode_t;
84 const char *svf_trst_mode_name[4] =
86 "ON",
87 "OFF",
88 "Z",
89 "ABSENT"
92 typedef struct
94 tap_state_t from;
95 tap_state_t to;
96 uint32_t num_of_moves;
97 tap_state_t paths[8];
98 }svf_statemove_t;
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.
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.
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.
115 static const svf_statemove_t svf_statemoves[] =
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}},
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}},
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}},
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}}
139 char *svf_tap_state_name[TAP_NUM_STATES];
141 #define XXR_TDI (1 << 0)
142 #define XXR_TDO (1 << 1)
143 #define XXR_MASK (1 << 2)
144 #define XXR_SMASK (1 << 3)
145 typedef struct
147 int len;
148 int data_mask;
149 uint8_t *tdi;
150 uint8_t *tdo;
151 uint8_t *mask;
152 uint8_t *smask;
153 }svf_xxr_para_t;
155 typedef struct
157 float frequency;
158 tap_state_t ir_end_state;
159 tap_state_t dr_end_state;
160 tap_state_t runtest_run_state;
161 tap_state_t runtest_end_state;
162 trst_mode_t trst_mode;
164 svf_xxr_para_t hir_para;
165 svf_xxr_para_t hdr_para;
166 svf_xxr_para_t tir_para;
167 svf_xxr_para_t tdr_para;
168 svf_xxr_para_t sir_para;
169 svf_xxr_para_t sdr_para;
170 }svf_para_t;
172 svf_para_t svf_para;
173 const svf_para_t svf_para_init =
175 // frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode
176 0, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TRST_Z,
177 // hir_para
178 // {len, data_mask, tdi, tdo, mask, smask},
179 {0, 0, NULL, NULL, NULL, NULL},
180 // hdr_para
181 // {len, data_mask, tdi, tdo, mask, smask},
182 {0, 0, NULL, NULL, NULL, NULL},
183 // tir_para
184 // {len, data_mask, tdi, tdo, mask, smask},
185 {0, 0, NULL, NULL, NULL, NULL},
186 // tdr_para
187 // {len, data_mask, tdi, tdo, mask, smask},
188 {0, 0, NULL, NULL, NULL, NULL},
189 // sir_para
190 // {len, data_mask, tdi, tdo, mask, smask},
191 {0, 0, NULL, NULL, NULL, NULL},
192 // sdr_para
193 // {len, data_mask, tdi, tdo, mask, smask},
194 {0, 0, NULL, NULL, NULL, NULL},
197 typedef struct
199 int line_num; // used to record line number of the check operation
200 // so more information could be printed
201 int enabled; // check is enabled or not
202 int buffer_offset; // buffer_offset to buffers
203 int bit_len; // bit length to check
204 }svf_check_tdo_para_t;
206 #define SVF_CHECK_TDO_PARA_SIZE 1024
207 static svf_check_tdo_para_t *svf_check_tdo_para = NULL;
208 static int svf_check_tdo_para_index = 0;
210 #define dimof(a) (sizeof(a) / sizeof((a)[0]))
212 static int svf_read_command_from_file(int fd);
213 static int svf_check_tdo(void);
214 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
215 static int svf_run_command(struct command_context_s *cmd_ctx, char *cmd_str);
216 static int handle_svf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
218 static int svf_fd = 0;
219 static char *svf_command_buffer = NULL;
220 static int svf_command_buffer_size = 0;
221 static int svf_line_number = 1;
223 static jtag_tap_t *tap = NULL;
225 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (4 * 1024)
226 static uint8_t *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
227 static int svf_buffer_index = 0, svf_buffer_size = 0;
228 static int svf_quiet = 0;
231 int svf_register_commands(struct command_context_s *cmd_ctx)
233 register_command(cmd_ctx, NULL, "svf", handle_svf_command,
234 COMMAND_EXEC, "run svf <file>");
236 return ERROR_OK;
239 void svf_free_xxd_para(svf_xxr_para_t *para)
241 if (NULL != para)
243 if (para->tdi != NULL)
245 free(para->tdi);
246 para->tdi = NULL;
248 if (para->tdo != NULL)
250 free(para->tdo);
251 para->tdo = NULL;
253 if (para->mask != NULL)
255 free(para->mask);
256 para->mask = NULL;
258 if (para->smask != NULL)
260 free(para->smask);
261 para->smask = NULL;
266 unsigned svf_get_mask_u32(int bitlen)
268 uint32_t bitmask;
270 if (bitlen < 0)
272 bitmask = 0;
274 else if (bitlen >= 32)
276 bitmask = 0xFFFFFFFF;
278 else
280 bitmask = (1 << bitlen) - 1;
283 return bitmask;
286 static const char* tap_state_svf_name(tap_state_t state)
288 const char* ret;
290 switch (state)
292 case TAP_RESET: ret = "RESET"; break;
293 case TAP_IDLE: ret = "IDLE"; break;
294 case TAP_DRSELECT: ret = "DRSELECT"; break;
295 case TAP_DRCAPTURE: ret = "DRCAPTURE"; break;
296 case TAP_DRSHIFT: ret = "DRSHIFT"; break;
297 case TAP_DREXIT1: ret = "DREXIT1"; break;
298 case TAP_DRPAUSE: ret = "DRPAUSE"; break;
299 case TAP_DREXIT2: ret = "DREXIT2"; break;
300 case TAP_DRUPDATE: ret = "DRUPDATE"; break;
301 case TAP_IRSELECT: ret = "IRSELECT"; break;
302 case TAP_IRCAPTURE: ret = "IRCAPTURE"; break;
303 case TAP_IRSHIFT: ret = "IRSHIFT"; break;
304 case TAP_IREXIT1: ret = "IREXIT1"; break;
305 case TAP_IRPAUSE: ret = "IRPAUSE"; break;
306 case TAP_IREXIT2: ret = "IREXIT2"; break;
307 case TAP_IRUPDATE: ret = "IRUPDATE"; break;
308 default: ret = "???"; break;
311 return ret;
314 int svf_add_statemove(tap_state_t state_to)
316 tap_state_t state_from = cmd_queue_cur_state;
317 uint8_t index;
319 /* when resetting, be paranoid and ignore current state */
320 if (state_to == TAP_RESET) {
321 jtag_add_tlr();
322 return ERROR_OK;
325 for (index = 0; index < dimof(svf_statemoves); index++)
327 if ((svf_statemoves[index].from == state_from)
328 && (svf_statemoves[index].to == state_to))
330 /* recorded path includes current state ... avoid extra TCKs! */
331 if (svf_statemoves[index].num_of_moves > 1)
332 jtag_add_pathmove(svf_statemoves[index].num_of_moves - 1,
333 svf_statemoves[index].paths + 1);
334 else
335 jtag_add_pathmove(svf_statemoves[index].num_of_moves,
336 svf_statemoves[index].paths);
337 return ERROR_OK;
340 LOG_ERROR("SVF: can not move to %s", tap_state_svf_name(state_to));
341 return ERROR_FAIL;
344 static int handle_svf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
346 #define SVF_NUM_OF_OPTIONS 1
347 int command_num = 0, i;
348 int ret = ERROR_OK;
349 long long time_ago;
351 if ((argc < 1) || (argc > (1 + SVF_NUM_OF_OPTIONS)))
353 command_print(cmd_ctx, "usage: svf <file> [quiet]");
354 return ERROR_FAIL;
357 // parse variant
358 svf_quiet = 0;
359 for (i = 1; i < argc; i++)
361 if (!strcmp(args[i], "quiet"))
363 svf_quiet = 1;
365 else
367 LOG_ERROR("unknown variant for svf: %s", args[i]);
369 // no need to free anything now
370 return ERROR_FAIL;
374 if ((svf_fd = open(args[0], O_RDONLY)) < 0)
376 command_print(cmd_ctx, "file \"%s\" not found", args[0]);
378 // no need to free anything now
379 return ERROR_FAIL;
382 LOG_USER("svf processing file: \"%s\"", args[0]);
384 // get time
385 time_ago = timeval_ms();
387 // init
388 svf_line_number = 1;
389 svf_command_buffer_size = 0;
391 svf_check_tdo_para_index = 0;
392 svf_check_tdo_para = malloc(sizeof(svf_check_tdo_para_t) * SVF_CHECK_TDO_PARA_SIZE);
393 if (NULL == svf_check_tdo_para)
395 LOG_ERROR("not enough memory");
396 ret = ERROR_FAIL;
397 goto free_all;
400 svf_buffer_index = 0;
401 // double the buffer size
402 // in case current command cannot be commited, and next command is a bit scan command
403 // here is 32K bits for this big scan command, it should be enough
404 // buffer will be reallocated if buffer size is not enough
405 svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
406 if (NULL == svf_tdi_buffer)
408 LOG_ERROR("not enough memory");
409 ret = ERROR_FAIL;
410 goto free_all;
412 svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
413 if (NULL == svf_tdo_buffer)
415 LOG_ERROR("not enough memory");
416 ret = ERROR_FAIL;
417 goto free_all;
419 svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
420 if (NULL == svf_mask_buffer)
422 LOG_ERROR("not enough memory");
423 ret = ERROR_FAIL;
424 goto free_all;
426 svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
428 memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
429 for (i = 0; i < (int)dimof(svf_tap_state_name); i++)
431 svf_tap_state_name[i] = (char *)tap_state_svf_name(i);
434 // TAP_RESET
435 jtag_add_tlr();
437 while (ERROR_OK == svf_read_command_from_file(svf_fd))
439 if (ERROR_OK != svf_run_command(cmd_ctx, svf_command_buffer))
441 LOG_ERROR("fail to run command at line %d", svf_line_number);
442 ret = ERROR_FAIL;
443 break;
445 command_num++;
447 if (ERROR_OK != jtag_execute_queue())
449 ret = ERROR_FAIL;
451 else if (ERROR_OK != svf_check_tdo())
453 ret = ERROR_FAIL;
456 // print time
457 command_print(cmd_ctx, "%lld ms used", timeval_ms() - time_ago);
459 free_all:
461 close(svf_fd);
462 svf_fd = 0;
464 // free buffers
465 if (svf_command_buffer)
467 free(svf_command_buffer);
468 svf_command_buffer = NULL;
469 svf_command_buffer_size = 0;
471 if (svf_check_tdo_para)
473 free(svf_check_tdo_para);
474 svf_check_tdo_para = NULL;
475 svf_check_tdo_para_index = 0;
477 if (svf_tdi_buffer)
479 free(svf_tdi_buffer);
480 svf_tdi_buffer = NULL;
482 if (svf_tdo_buffer)
484 free(svf_tdo_buffer);
485 svf_tdo_buffer = NULL;
487 if (svf_mask_buffer)
489 free(svf_mask_buffer);
490 svf_mask_buffer = NULL;
492 svf_buffer_index = 0;
493 svf_buffer_size = 0;
495 svf_free_xxd_para(&svf_para.hdr_para);
496 svf_free_xxd_para(&svf_para.hir_para);
497 svf_free_xxd_para(&svf_para.tdr_para);
498 svf_free_xxd_para(&svf_para.tir_para);
499 svf_free_xxd_para(&svf_para.sdr_para);
500 svf_free_xxd_para(&svf_para.sir_para);
502 if (ERROR_OK == ret)
504 command_print(cmd_ctx, "svf file programmed successfully for %d commands", command_num);
506 else
508 command_print(cmd_ctx, "svf file programmed failed");
511 return ret;
514 #define SVFP_CMD_INC_CNT 1024
515 static int svf_read_command_from_file(int fd)
517 char ch, *tmp_buffer = NULL;
518 int cmd_pos = 0, cmd_ok = 0, slash = 0, comment = 0;
520 while (!cmd_ok && (read(fd, &ch, 1) > 0))
522 switch (ch)
524 case '!':
525 slash = 0;
526 comment = 1;
527 break;
528 case '/':
529 if (++slash == 2)
531 comment = 1;
533 break;
534 case ';':
535 slash = 0;
536 if (!comment)
538 cmd_ok = 1;
540 break;
541 case '\n':
542 svf_line_number++;
543 case '\r':
544 slash = 0;
545 comment = 0;
546 break;
547 default:
548 if (!comment)
550 if (cmd_pos >= svf_command_buffer_size - 1)
552 tmp_buffer = (char*)malloc(svf_command_buffer_size + SVFP_CMD_INC_CNT); // 1 more byte for '\0'
553 if (NULL == tmp_buffer)
555 LOG_ERROR("not enough memory");
556 return ERROR_FAIL;
558 if (svf_command_buffer_size > 0)
560 memcpy(tmp_buffer, svf_command_buffer, svf_command_buffer_size);
562 if (svf_command_buffer != NULL)
564 free(svf_command_buffer);
566 svf_command_buffer = tmp_buffer;
567 svf_command_buffer_size += SVFP_CMD_INC_CNT;
568 tmp_buffer = NULL;
570 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
572 break;
576 if (cmd_ok)
578 svf_command_buffer[cmd_pos] = '\0';
579 return ERROR_OK;
581 else
583 return ERROR_FAIL;
587 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
589 int pos = 0, num = 0, space_found = 1;
591 while (pos < len)
593 switch (str[pos])
595 case '\n':
596 case '\r':
597 case '!':
598 case '/':
599 LOG_ERROR("fail to parse svf command");
600 return ERROR_FAIL;
601 break;
602 case ' ':
603 space_found = 1;
604 str[pos] = '\0';
605 break;
606 default:
607 if (space_found)
609 argus[num++] = &str[pos];
610 space_found = 0;
612 break;
614 pos++;
617 *num_of_argu = num;
619 return ERROR_OK;
622 bool svf_tap_state_is_stable(tap_state_t state)
624 return (TAP_RESET == state) || (TAP_IDLE == state)
625 || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
628 static int svf_tap_state_is_valid(tap_state_t state)
630 return state >= 0 && state < TAP_NUM_STATES;
633 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
635 int i;
637 for (i = 0; i < num_of_element; i++)
639 if (!strcmp(str, strs[i]))
641 return i;
644 return 0xFF;
647 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
649 int new_byte_len = (new_bit_len + 7) >> 3;
651 if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
653 if (*arr != NULL)
655 free(*arr);
656 *arr = NULL;
658 *arr = (uint8_t*)malloc(new_byte_len);
659 if (NULL == *arr)
661 LOG_ERROR("not enough memory");
662 return ERROR_FAIL;
664 memset(*arr, 0, new_byte_len);
666 return ERROR_OK;
669 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
671 int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
672 uint8_t ch = 0;
674 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
676 LOG_ERROR("fail to adjust length of array");
677 return ERROR_FAIL;
680 for (i = 0; i < str_hbyte_len; i++)
682 ch = 0;
683 while (str_len > 0)
685 ch = str[--str_len];
687 if (!isblank(ch))
689 if ((ch >= '0') && (ch <= '9'))
691 ch = ch - '0';
692 break;
694 else if ((ch >= 'A') && (ch <= 'F'))
696 ch = ch - 'A' + 10;
697 break;
699 else
701 LOG_ERROR("invalid hex string");
702 return ERROR_FAIL;
706 ch = 0;
709 // write bin
710 if (i % 2)
712 // MSB
713 (*bin)[i / 2] |= ch << 4;
715 else
717 // LSB
718 (*bin)[i / 2] = 0;
719 (*bin)[i / 2] |= ch;
723 // check valid
724 if (str_len > 0 || (ch & ~((1 << (4 - (bit_len % 4))) - 1)) != 0)
726 LOG_ERROR("value execede length");
727 return ERROR_FAIL;
730 return ERROR_OK;
733 static int svf_check_tdo(void)
735 int i, len, index;
737 for (i = 0; i < svf_check_tdo_para_index; i++)
739 index = svf_check_tdo_para[i].buffer_offset;
740 len = svf_check_tdo_para[i].bit_len;
741 if ((svf_check_tdo_para[i].enabled)
742 && buf_cmp_mask(&svf_tdi_buffer[index], &svf_tdo_buffer[index], &svf_mask_buffer[index], len))
744 unsigned bitmask;
745 unsigned received, expected, tapmask;
746 bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
748 memcpy(&received, svf_tdi_buffer + index, sizeof(unsigned));
749 memcpy(&expected, svf_tdo_buffer + index, sizeof(unsigned));
750 memcpy(&tapmask, svf_mask_buffer + index, sizeof(unsigned));
751 LOG_ERROR("tdo check error at line %d",
752 svf_check_tdo_para[i].line_num);
753 LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
754 received & bitmask,
755 expected & bitmask,
756 tapmask & bitmask);
757 return ERROR_FAIL;
760 svf_check_tdo_para_index = 0;
762 return ERROR_OK;
765 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
767 if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
769 LOG_ERROR("toooooo many operation undone");
770 return ERROR_FAIL;
773 svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
774 svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
775 svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
776 svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
777 svf_check_tdo_para_index++;
779 return ERROR_OK;
782 static int svf_execute_tap(void)
784 if (ERROR_OK != jtag_execute_queue())
786 return ERROR_FAIL;
788 else if (ERROR_OK != svf_check_tdo())
790 return ERROR_FAIL;
793 svf_buffer_index = 0;
795 return ERROR_OK;
798 static int svf_run_command(struct command_context_s *cmd_ctx, char *cmd_str)
800 char *argus[256], command;
801 int num_of_argu = 0, i;
803 // tmp variable
804 int i_tmp;
806 // for RUNTEST
807 int run_count;
808 float min_time, max_time;
809 // for XXR
810 svf_xxr_para_t *xxr_para_tmp;
811 uint8_t **pbuffer_tmp;
812 scan_field_t field;
813 // for STATE
814 tap_state_t *path = NULL, state;
816 if (!svf_quiet)
818 LOG_USER("%s", svf_command_buffer);
821 if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
823 return ERROR_FAIL;
826 command = svf_find_string_in_array(argus[0], (char **)svf_command_name, dimof(svf_command_name));
827 switch (command)
829 case ENDDR:
830 case ENDIR:
831 if (num_of_argu != 2)
833 LOG_ERROR("invalid parameter of %s", argus[0]);
834 return ERROR_FAIL;
836 i_tmp = svf_find_string_in_array(argus[1], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
837 if (svf_tap_state_is_stable(i_tmp))
839 if (command == ENDIR)
841 svf_para.ir_end_state = i_tmp;
842 LOG_DEBUG("\tir_end_state = %s", svf_tap_state_name[svf_para.ir_end_state]);
844 else
846 svf_para.dr_end_state = i_tmp;
847 LOG_DEBUG("\tdr_end_state = %s", svf_tap_state_name[svf_para.dr_end_state]);
850 else
852 LOG_ERROR("%s is not valid state", argus[1]);
853 return ERROR_FAIL;
855 break;
856 case FREQUENCY:
857 if ((num_of_argu != 1) && (num_of_argu != 3))
859 LOG_ERROR("invalid parameter of %s", argus[0]);
860 return ERROR_FAIL;
862 if (1 == num_of_argu)
864 // TODO: set jtag speed to full speed
865 svf_para.frequency = 0;
867 else
869 if (strcmp(argus[2], "HZ"))
871 LOG_ERROR("HZ not found in FREQUENCY command");
872 return ERROR_FAIL;
874 if (ERROR_OK != svf_execute_tap())
876 return ERROR_FAIL;
878 svf_para.frequency = atof(argus[1]);
879 // TODO: set jtag speed to
880 if (svf_para.frequency > 0)
882 command_run_linef(cmd_ctx, "jtag_khz %d", (int)svf_para.frequency / 1000);
883 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
886 break;
887 case HDR:
888 xxr_para_tmp = &svf_para.hdr_para;
889 goto XXR_common;
890 case HIR:
891 xxr_para_tmp = &svf_para.hir_para;
892 goto XXR_common;
893 case TDR:
894 xxr_para_tmp = &svf_para.tdr_para;
895 goto XXR_common;
896 case TIR:
897 xxr_para_tmp = &svf_para.tir_para;
898 goto XXR_common;
899 case SDR:
900 xxr_para_tmp = &svf_para.sdr_para;
901 goto XXR_common;
902 case SIR:
903 xxr_para_tmp = &svf_para.sir_para;
904 goto XXR_common;
905 XXR_common:
906 // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
907 if ((num_of_argu > 10) || (num_of_argu % 2))
909 LOG_ERROR("invalid parameter of %s", argus[0]);
910 return ERROR_FAIL;
912 i_tmp = xxr_para_tmp->len;
913 xxr_para_tmp->len = atoi(argus[1]);
914 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
915 xxr_para_tmp->data_mask = 0;
916 for (i = 2; i < num_of_argu; i += 2)
918 if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
920 LOG_ERROR("data section error");
921 return ERROR_FAIL;
923 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
924 // TDI, TDO, MASK, SMASK
925 if (!strcmp(argus[i], "TDI"))
927 // TDI
928 pbuffer_tmp = &xxr_para_tmp->tdi;
929 xxr_para_tmp->data_mask |= XXR_TDI;
931 else if (!strcmp(argus[i], "TDO"))
933 // TDO
934 pbuffer_tmp = &xxr_para_tmp->tdo;
935 xxr_para_tmp->data_mask |= XXR_TDO;
937 else if (!strcmp(argus[i], "MASK"))
939 // MASK
940 pbuffer_tmp = &xxr_para_tmp->mask;
941 xxr_para_tmp->data_mask |= XXR_MASK;
943 else if (!strcmp(argus[i], "SMASK"))
945 // SMASK
946 pbuffer_tmp = &xxr_para_tmp->smask;
947 xxr_para_tmp->data_mask |= XXR_SMASK;
949 else
951 LOG_ERROR("unknow parameter: %s", argus[i]);
952 return ERROR_FAIL;
954 if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
956 LOG_ERROR("fail to parse hex value");
957 return ERROR_FAIL;
959 LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
961 // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
962 // the mask pattern used is all cares
963 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
965 // MASK not defined and length changed
966 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
968 LOG_ERROR("fail to adjust length of array");
969 return ERROR_FAIL;
971 buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
973 // If TDO is absent, no comparison is needed, set the mask to 0
974 if (!(xxr_para_tmp->data_mask & XXR_TDO))
976 if (NULL == xxr_para_tmp->tdo)
978 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
980 LOG_ERROR("fail to adjust length of array");
981 return ERROR_FAIL;
984 if (NULL == xxr_para_tmp->mask)
986 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
988 LOG_ERROR("fail to adjust length of array");
989 return ERROR_FAIL;
992 memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
994 // do scan if necessary
995 if (SDR == command)
997 // check buffer size first, reallocate if necessary
998 i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
999 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1001 #if 1
1002 // simply print error message
1003 LOG_ERROR("buffer is not enough, report to author");
1004 return ERROR_FAIL;
1005 #else
1006 uint8_t *buffer_tmp;
1008 // reallocate buffer
1009 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1010 if (NULL == buffer_tmp)
1012 LOG_ERROR("not enough memory");
1013 return ERROR_FAIL;
1015 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1016 // svf_tdi_buffer isn't NULL here
1017 free(svf_tdi_buffer);
1018 svf_tdi_buffer = buffer_tmp;
1020 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1021 if (NULL == buffer_tmp)
1023 LOG_ERROR("not enough memory");
1024 return ERROR_FAIL;
1026 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1027 // svf_tdo_buffer isn't NULL here
1028 free(svf_tdo_buffer);
1029 svf_tdo_buffer = buffer_tmp;
1031 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1032 if (NULL == buffer_tmp)
1034 LOG_ERROR("not enough memory");
1035 return ERROR_FAIL;
1037 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1038 // svf_mask_buffer isn't NULL here
1039 free(svf_mask_buffer);
1040 svf_mask_buffer = buffer_tmp;
1042 buffer_tmp = NULL;
1043 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1044 #endif
1047 // assemble dr data
1048 i = 0;
1049 buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1050 i += svf_para.hdr_para.len;
1051 buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1052 i += svf_para.sdr_para.len;
1053 buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1054 i += svf_para.tdr_para.len;
1056 // add check data
1057 if (svf_para.sdr_para.data_mask & XXR_TDO)
1059 // assemble dr mask data
1060 i = 0;
1061 buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1062 i += svf_para.hdr_para.len;
1063 buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1064 i += svf_para.sdr_para.len;
1065 buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1066 i += svf_para.tdr_para.len;
1067 // assemble dr check data
1068 i = 0;
1069 buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1070 i += svf_para.hdr_para.len;
1071 buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1072 i += svf_para.sdr_para.len;
1073 buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1074 i += svf_para.tdr_para.len;
1076 svf_add_check_para(1, svf_buffer_index, i);
1078 else
1080 svf_add_check_para(0, svf_buffer_index, i);
1082 field.tap = tap;
1083 field.num_bits = i;
1084 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1085 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1086 /* NOTE: doesn't use SVF-specified state paths */
1087 jtag_add_plain_dr_scan(1, &field, svf_para.dr_end_state);
1089 svf_buffer_index += (i + 7) >> 3;
1091 else if (SIR == command)
1093 // check buffer size first, reallocate if necessary
1094 i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
1095 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1097 #if 1
1098 // simply print error message
1099 LOG_ERROR("buffer is not enough, report to author");
1100 return ERROR_FAIL;
1101 #else
1102 uint8_t *buffer_tmp;
1104 // reallocate buffer
1105 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1106 if (NULL == buffer_tmp)
1108 LOG_ERROR("not enough memory");
1109 return ERROR_FAIL;
1111 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1112 // svf_tdi_buffer isn't NULL here
1113 free(svf_tdi_buffer);
1114 svf_tdi_buffer = buffer_tmp;
1116 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1117 if (NULL == buffer_tmp)
1119 LOG_ERROR("not enough memory");
1120 return ERROR_FAIL;
1122 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1123 // svf_tdo_buffer isn't NULL here
1124 free(svf_tdo_buffer);
1125 svf_tdo_buffer = buffer_tmp;
1127 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1128 if (NULL == buffer_tmp)
1130 LOG_ERROR("not enough memory");
1131 return ERROR_FAIL;
1133 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1134 // svf_mask_buffer isn't NULL here
1135 free(svf_mask_buffer);
1136 svf_mask_buffer = buffer_tmp;
1138 buffer_tmp = NULL;
1139 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1140 #endif
1143 // assemble ir data
1144 i = 0;
1145 buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1146 i += svf_para.hir_para.len;
1147 buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1148 i += svf_para.sir_para.len;
1149 buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1150 i += svf_para.tir_para.len;
1152 // add check data
1153 if (svf_para.sir_para.data_mask & XXR_TDO)
1155 // assemble dr mask data
1156 i = 0;
1157 buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1158 i += svf_para.hir_para.len;
1159 buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1160 i += svf_para.sir_para.len;
1161 buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1162 i += svf_para.tir_para.len;
1163 // assemble dr check data
1164 i = 0;
1165 buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1166 i += svf_para.hir_para.len;
1167 buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1168 i += svf_para.sir_para.len;
1169 buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1170 i += svf_para.tir_para.len;
1172 svf_add_check_para(1, svf_buffer_index, i);
1174 else
1176 svf_add_check_para(0, svf_buffer_index, i);
1178 field.tap = tap;
1179 field.num_bits = i;
1180 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1181 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1182 /* NOTE: doesn't use SVF-specified state paths */
1183 jtag_add_plain_ir_scan(1, &field, svf_para.ir_end_state);
1185 svf_buffer_index += (i + 7) >> 3;
1187 break;
1188 case PIO:
1189 case PIOMAP:
1190 LOG_ERROR("PIO and PIOMAP are not supported");
1191 return ERROR_FAIL;
1192 break;
1193 case RUNTEST:
1194 // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
1195 // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
1196 if ((num_of_argu < 3) && (num_of_argu > 11))
1198 LOG_ERROR("invalid parameter of %s", argus[0]);
1199 return ERROR_FAIL;
1201 // init
1202 run_count = 0;
1203 min_time = 0;
1204 max_time = 0;
1205 i = 1;
1206 // run_state
1207 i_tmp = svf_find_string_in_array(argus[i], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
1208 if (svf_tap_state_is_valid(i_tmp))
1210 if (svf_tap_state_is_stable(i_tmp))
1212 svf_para.runtest_run_state = i_tmp;
1214 // When a run_state is specified, the new run_state becomes the default end_state
1215 svf_para.runtest_end_state = i_tmp;
1216 LOG_DEBUG("\trun_state = %s", svf_tap_state_name[svf_para.runtest_run_state]);
1217 i++;
1219 else
1221 LOG_ERROR("%s is not valid state", svf_tap_state_name[i_tmp]);
1222 return ERROR_FAIL;
1225 // run_count run_clk
1226 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
1228 if (!strcmp(argus[i + 1], "TCK"))
1230 // clock source is TCK
1231 run_count = atoi(argus[i]);
1232 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1234 else
1236 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1237 return ERROR_FAIL;
1239 i += 2;
1241 // min_time SEC
1242 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
1244 min_time = atof(argus[i]);
1245 LOG_DEBUG("\tmin_time = %fs", min_time);
1246 i += 2;
1248 // MAXIMUM max_time SEC
1249 if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
1251 max_time = atof(argus[i + 1]);
1252 LOG_DEBUG("\tmax_time = %fs", max_time);
1253 i += 3;
1255 // ENDSTATE end_state
1256 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
1258 i_tmp = svf_find_string_in_array(argus[i + 1], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
1259 if (svf_tap_state_is_stable(i_tmp))
1261 svf_para.runtest_end_state = i_tmp;
1262 LOG_DEBUG("\tend_state = %s", svf_tap_state_name[svf_para.runtest_end_state]);
1264 else
1266 LOG_ERROR("%s is not valid state", svf_tap_state_name[i_tmp]);
1267 return ERROR_FAIL;
1269 i += 2;
1271 // calculate run_count
1272 if ((0 == run_count) && (min_time > 0))
1274 run_count = min_time * svf_para.frequency;
1276 // all parameter should be parsed
1277 if (i == num_of_argu)
1279 if (run_count > 0)
1281 // run_state and end_state is checked to be stable state
1282 // TODO: do runtest
1283 #if 1
1284 /* FIXME handle statemove failures */
1285 int retval;
1287 // enter into run_state if necessary
1288 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1290 retval = svf_add_statemove(svf_para.runtest_run_state);
1293 // call jtag_add_clocks
1294 jtag_add_clocks(run_count);
1296 // move to end_state if necessary
1297 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1299 retval = svf_add_statemove(svf_para.runtest_end_state);
1301 #else
1302 if (svf_para.runtest_run_state != TAP_IDLE)
1304 // RUNTEST can only executed in TAP_IDLE
1305 LOG_ERROR("cannot runtest in %s state", svf_tap_state_name[svf_para.runtest_run_state]);
1306 return ERROR_FAIL;
1309 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1310 #endif
1313 else
1315 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
1316 return ERROR_FAIL;
1318 break;
1319 case STATE:
1320 // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
1321 if (num_of_argu < 2)
1323 LOG_ERROR("invalid parameter of %s", argus[0]);
1324 return ERROR_FAIL;
1326 if (num_of_argu > 2)
1328 // STATE pathstate1 ... stable_state
1329 path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1330 if (NULL == path)
1332 LOG_ERROR("not enough memory");
1333 return ERROR_FAIL;
1335 num_of_argu--; // num of path
1336 i_tmp = 1; // path is from patameter 1
1337 for (i = 0; i < num_of_argu; i++)
1339 path[i] = svf_find_string_in_array(argus[i_tmp++], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
1340 if (!svf_tap_state_is_valid(path[i]))
1342 LOG_ERROR("%s is not valid state", svf_tap_state_name[path[i]]);
1343 free(path);
1344 return ERROR_FAIL;
1346 /* OpenOCD refuses paths containing TAP_RESET */
1347 if (TAP_RESET == path[i])
1349 /* FIXME last state MUST be stable! */
1350 if (i > 0)
1352 jtag_add_pathmove(i, path);
1354 jtag_add_tlr();
1355 num_of_argu -= i + 1;
1356 i = -1;
1359 if (num_of_argu > 0)
1361 // execute last path if necessary
1362 if (svf_tap_state_is_stable(path[num_of_argu - 1]))
1364 // last state MUST be stable state
1365 // TODO: call path_move
1366 jtag_add_pathmove(num_of_argu, path);
1367 LOG_DEBUG("\tmove to %s by path_move", svf_tap_state_name[path[num_of_argu - 1]]);
1369 else
1371 LOG_ERROR("%s is not valid state", svf_tap_state_name[path[num_of_argu - 1]]);
1372 free(path);
1373 return ERROR_FAIL;
1376 // no need to keep this memory, in jtag_add_pathmove, path will be duplicated
1377 if (NULL != path)
1379 free(path);
1380 path = NULL;
1383 else
1385 // STATE stable_state
1386 state = svf_find_string_in_array(argus[1], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
1387 if (svf_tap_state_is_stable(state))
1389 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1390 svf_tap_state_name[state]);
1391 /* FIXME handle statemove failures */
1392 svf_add_statemove(state);
1394 else
1396 LOG_ERROR("%s is not valid state", svf_tap_state_name[state]);
1397 return ERROR_FAIL;
1400 break;
1401 case TRST:
1402 // TRST trst_mode
1403 if (num_of_argu != 2)
1405 LOG_ERROR("invalid parameter of %s", argus[0]);
1406 return ERROR_FAIL;
1408 if (svf_para.trst_mode != TRST_ABSENT)
1410 if (ERROR_OK != svf_execute_tap())
1412 return ERROR_FAIL;
1414 i_tmp = svf_find_string_in_array(argus[1], (char **)svf_trst_mode_name, dimof(svf_trst_mode_name));
1415 switch (i_tmp)
1417 case TRST_ON:
1418 jtag_add_reset(1, 0);
1419 break;
1420 case TRST_Z:
1421 case TRST_OFF:
1422 jtag_add_reset(0, 0);
1423 break;
1424 case TRST_ABSENT:
1425 break;
1426 default:
1427 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1428 return ERROR_FAIL;
1430 svf_para.trst_mode = i_tmp;
1431 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1433 else
1435 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1436 return ERROR_FAIL;
1438 break;
1439 default:
1440 LOG_ERROR("invalid svf command: %s", argus[0]);
1441 return ERROR_FAIL;
1442 break;
1445 if (debug_level >= LOG_LVL_DEBUG)
1447 // for convenient debugging, execute tap if possible
1448 if ((svf_buffer_index > 0) && \
1449 (((command != STATE) && (command != RUNTEST)) || \
1450 ((command == STATE) && (num_of_argu == 2))))
1452 if (ERROR_OK != svf_execute_tap())
1454 return ERROR_FAIL;
1457 // output debug info
1458 if ((SIR == command) || (SDR == command))
1460 int read_value;
1461 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1462 // in debug mode, data is from index 0
1463 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1464 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1468 else
1470 // for fast executing, execute tap if necessary
1471 // half of the buffer is for the next command
1472 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1473 (((command != STATE) && (command != RUNTEST)) || \
1474 ((command == STATE) && (num_of_argu == 2))))
1476 return svf_execute_tap();
1480 return ERROR_OK;