You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1397 lines
34 KiB

  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 *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. /* The specification for SVF is available here:
  21. * http://www.asset-intertech.com/support/svf.pdf
  22. * Below, this document is refered to as the "SVF spec".
  23. *
  24. * The specification for XSVF is available here:
  25. * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
  26. * Below, this document is refered to as the "XSVF spec".
  27. */
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "svf.h"
  32. #include "jtag.h"
  33. #include "time_support.h"
  34. // SVF command
  35. typedef enum
  36. {
  37. ENDDR,
  38. ENDIR,
  39. FREQUENCY,
  40. HDR,
  41. HIR,
  42. PIO,
  43. PIOMAP,
  44. RUNTEST,
  45. SDR,
  46. SIR,
  47. STATE,
  48. TDR,
  49. TIR,
  50. TRST,
  51. }svf_command_t;
  52. const char *svf_command_name[14] =
  53. {
  54. "ENDDR",
  55. "ENDIR",
  56. "FREQUENCY",
  57. "HDR",
  58. "HIR",
  59. "PIO",
  60. "PIOMAP",
  61. "RUNTEST",
  62. "SDR",
  63. "SIR",
  64. "STATE",
  65. "TDR",
  66. "TIR",
  67. "TRST"
  68. };
  69. typedef enum
  70. {
  71. TRST_ON,
  72. TRST_OFF,
  73. TRST_Z,
  74. TRST_ABSENT
  75. }trst_mode_t;
  76. const char *svf_trst_mode_name[4] =
  77. {
  78. "ON",
  79. "OFF",
  80. "Z",
  81. "ABSENT"
  82. };
  83. char *svf_tap_state_name[TAP_NUM_STATES];
  84. #define XXR_TDI (1 << 0)
  85. #define XXR_TDO (1 << 1)
  86. #define XXR_MASK (1 << 2)
  87. #define XXR_SMASK (1 << 3)
  88. typedef struct
  89. {
  90. int len;
  91. int data_mask;
  92. u8 *tdi;
  93. u8 *tdo;
  94. u8 *mask;
  95. u8 *smask;
  96. }svf_xxr_para_t;
  97. typedef struct
  98. {
  99. float frequency;
  100. tap_state_t ir_end_state;
  101. tap_state_t dr_end_state;
  102. tap_state_t runtest_run_state;
  103. tap_state_t runtest_end_state;
  104. trst_mode_t trst_mode;
  105. svf_xxr_para_t hir_para;
  106. svf_xxr_para_t hdr_para;
  107. svf_xxr_para_t tir_para;
  108. svf_xxr_para_t tdr_para;
  109. svf_xxr_para_t sir_para;
  110. svf_xxr_para_t sdr_para;
  111. }svf_para_t;
  112. svf_para_t svf_para;
  113. const svf_para_t svf_para_init =
  114. {
  115. // frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode
  116. 0, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TRST_Z,
  117. // hir_para
  118. // {len, data_mask, tdi, tdo, mask, smask},
  119. {0, 0, NULL, NULL, NULL, NULL},
  120. // hdr_para
  121. // {len, data_mask, tdi, tdo, mask, smask},
  122. {0, 0, NULL, NULL, NULL, NULL},
  123. // tir_para
  124. // {len, data_mask, tdi, tdo, mask, smask},
  125. {0, 0, NULL, NULL, NULL, NULL},
  126. // tdr_para
  127. // {len, data_mask, tdi, tdo, mask, smask},
  128. {0, 0, NULL, NULL, NULL, NULL},
  129. // sir_para
  130. // {len, data_mask, tdi, tdo, mask, smask},
  131. {0, 0, NULL, NULL, NULL, NULL},
  132. // sdr_para
  133. // {len, data_mask, tdi, tdo, mask, smask},
  134. {0, 0, NULL, NULL, NULL, NULL},
  135. };
  136. typedef struct
  137. {
  138. int line_num; // used to record line number of the check operation
  139. // so more information could be printed
  140. int enabled; // check is enabled or not
  141. int buffer_offset; // buffer_offset to buffers
  142. int bit_len; // bit length to check
  143. }svf_check_tdo_para_t;
  144. #define SVF_CHECK_TDO_PARA_SIZE 1024
  145. static svf_check_tdo_para_t *svf_check_tdo_para = NULL;
  146. static int svf_check_tdo_para_index = 0;
  147. #define dimof(a) (sizeof(a) / sizeof((a)[0]))
  148. static int svf_read_command_from_file(int fd);
  149. static int svf_check_tdo(void);
  150. static int svf_add_check_para(u8 enabled, int buffer_offset, int bit_len);
  151. static int svf_run_command(struct command_context_s *cmd_ctx, char *cmd_str);
  152. static int handle_svf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  153. static int svf_fd = 0;
  154. static char *svf_command_buffer = NULL;
  155. static int svf_command_buffer_size = 0;
  156. static int svf_line_number = 1;
  157. static jtag_tap_t *tap = NULL;
  158. static tap_state_t last_state = TAP_RESET;
  159. #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (4 * 1024)
  160. static u8 *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
  161. static int svf_buffer_index = 0, svf_buffer_size = 0;
  162. static int svf_quiet = 0;
  163. int svf_register_commands(struct command_context_s *cmd_ctx)
  164. {
  165. register_command(cmd_ctx, NULL, "svf", handle_svf_command,
  166. COMMAND_EXEC, "run svf <file>");
  167. return ERROR_OK;
  168. }
  169. void svf_free_xxd_para(svf_xxr_para_t *para)
  170. {
  171. if (NULL != para)
  172. {
  173. if (para->tdi != NULL)
  174. {
  175. free(para->tdi);
  176. para->tdi = NULL;
  177. }
  178. if (para->tdo != NULL)
  179. {
  180. free(para->tdo);
  181. para->tdo = NULL;
  182. }
  183. if (para->mask != NULL)
  184. {
  185. free(para->mask);
  186. para->mask = NULL;
  187. }
  188. if (para->smask != NULL)
  189. {
  190. free(para->smask);
  191. para->smask = NULL;
  192. }
  193. }
  194. }
  195. static int handle_svf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  196. {
  197. #define SVF_NUM_OF_OPTIONS 1
  198. int command_num = 0, i;
  199. int ret = ERROR_OK;
  200. long long time_ago;
  201. if ((argc < 1) || (argc > (1 + SVF_NUM_OF_OPTIONS)))
  202. {
  203. command_print(cmd_ctx, "usage: svf <file> [quiet]");
  204. return ERROR_FAIL;
  205. }
  206. // parse variant
  207. svf_quiet = 0;
  208. for (i = 1; i < argc; i++)
  209. {
  210. if (!strcmp(args[i], "quiet"))
  211. {
  212. svf_quiet = 1;
  213. }
  214. else
  215. {
  216. LOG_ERROR("unknown variant for svf: %s", args[i]);
  217. // no need to free anything now
  218. return ERROR_FAIL;
  219. }
  220. }
  221. if ((svf_fd = open(args[0], O_RDONLY)) < 0)
  222. {
  223. command_print(cmd_ctx, "file \"%s\" not found", args[0]);
  224. // no need to free anything now
  225. return ERROR_FAIL;
  226. }
  227. LOG_USER("svf processing file: \"%s\"", args[0]);
  228. // get time
  229. time_ago = timeval_ms();
  230. // init
  231. svf_line_number = 1;
  232. svf_command_buffer_size = 0;
  233. svf_check_tdo_para_index = 0;
  234. svf_check_tdo_para = malloc(sizeof(svf_check_tdo_para_t) * SVF_CHECK_TDO_PARA_SIZE);
  235. if (NULL == svf_check_tdo_para)
  236. {
  237. LOG_ERROR("not enough memory");
  238. ret = ERROR_FAIL;
  239. goto free_all;
  240. }
  241. svf_buffer_index = 0;
  242. // double the buffer size
  243. // in case current command cannot be commited, and next command is a bit scan command
  244. // here is 32K bits for this big scan command, it should be enough
  245. // buffer will be reallocated if buffer size is not enough
  246. svf_tdi_buffer = (u8 *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
  247. if (NULL == svf_tdi_buffer)
  248. {
  249. LOG_ERROR("not enough memory");
  250. ret = ERROR_FAIL;
  251. goto free_all;
  252. }
  253. svf_tdo_buffer = (u8 *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
  254. if (NULL == svf_tdo_buffer)
  255. {
  256. LOG_ERROR("not enough memory");
  257. ret = ERROR_FAIL;
  258. goto free_all;
  259. }
  260. svf_mask_buffer = (u8 *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
  261. if (NULL == svf_mask_buffer)
  262. {
  263. LOG_ERROR("not enough memory");
  264. ret = ERROR_FAIL;
  265. goto free_all;
  266. }
  267. svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
  268. memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
  269. for (i = 0; i < (int)dimof(svf_tap_state_name); i++)
  270. {
  271. svf_tap_state_name[i] = (char *)tap_state_name(i);
  272. }
  273. // TAP_RESET
  274. jtag_add_tlr();
  275. while ( ERROR_OK == svf_read_command_from_file(svf_fd) )
  276. {
  277. if (ERROR_OK != svf_run_command(cmd_ctx, svf_command_buffer))
  278. {
  279. LOG_ERROR("fail to run command at line %d", svf_line_number);
  280. ret = ERROR_FAIL;
  281. break;
  282. }
  283. command_num++;
  284. }
  285. if (ERROR_OK != jtag_execute_queue())
  286. {
  287. ret = ERROR_FAIL;
  288. }
  289. else if (ERROR_OK != svf_check_tdo())
  290. {
  291. ret = ERROR_FAIL;
  292. }
  293. // print time
  294. command_print(cmd_ctx, "%lld ms used", timeval_ms() - time_ago);
  295. free_all:
  296. close(svf_fd);
  297. svf_fd = 0;
  298. // free buffers
  299. if (svf_command_buffer)
  300. {
  301. free(svf_command_buffer);
  302. svf_command_buffer = NULL;
  303. svf_command_buffer_size = 0;
  304. }
  305. if (svf_check_tdo_para)
  306. {
  307. free(svf_check_tdo_para);
  308. svf_check_tdo_para = NULL;
  309. svf_check_tdo_para_index = 0;
  310. }
  311. if (svf_tdi_buffer)
  312. {
  313. free(svf_tdi_buffer);
  314. svf_tdi_buffer = NULL;
  315. }
  316. if (svf_tdo_buffer)
  317. {
  318. free(svf_tdo_buffer);
  319. svf_tdo_buffer = NULL;
  320. }
  321. if (svf_mask_buffer)
  322. {
  323. free(svf_mask_buffer);
  324. svf_mask_buffer = NULL;
  325. }
  326. svf_buffer_index = 0;
  327. svf_buffer_size = 0;
  328. svf_free_xxd_para(&svf_para.hdr_para);
  329. svf_free_xxd_para(&svf_para.hir_para);
  330. svf_free_xxd_para(&svf_para.tdr_para);
  331. svf_free_xxd_para(&svf_para.tir_para);
  332. svf_free_xxd_para(&svf_para.sdr_para);
  333. svf_free_xxd_para(&svf_para.sir_para);
  334. if (ERROR_OK == ret)
  335. {
  336. command_print(cmd_ctx, "svf file programmed successfully for %d commands", command_num);
  337. }
  338. else
  339. {
  340. command_print(cmd_ctx, "svf file programmed failed");
  341. }
  342. return ret;
  343. }
  344. #define SVFP_CMD_INC_CNT 1024
  345. static int svf_read_command_from_file(int fd)
  346. {
  347. char ch, *tmp_buffer = NULL;
  348. int cmd_pos = 0, cmd_ok = 0, slash = 0, comment = 0;
  349. while (!cmd_ok && (read(fd, &ch, 1) > 0) )
  350. {
  351. switch(ch)
  352. {
  353. case '!':
  354. slash = 0;
  355. comment = 1;
  356. break;
  357. case '/':
  358. if (++slash == 2)
  359. {
  360. comment = 1;
  361. }
  362. break;
  363. case ';':
  364. slash = 0;
  365. if (!comment)
  366. {
  367. cmd_ok = 1;
  368. }
  369. break;
  370. case '\n':
  371. svf_line_number++;
  372. case '\r':
  373. slash = 0;
  374. comment = 0;
  375. break;
  376. default:
  377. if (!comment)
  378. {
  379. if (cmd_pos >= svf_command_buffer_size - 1)
  380. {
  381. tmp_buffer = (char*)malloc(svf_command_buffer_size + SVFP_CMD_INC_CNT); // 1 more byte for '\0'
  382. if (NULL == tmp_buffer)
  383. {
  384. LOG_ERROR("not enough memory");
  385. return ERROR_FAIL;
  386. }
  387. if (svf_command_buffer_size > 0)
  388. {
  389. memcpy(tmp_buffer, svf_command_buffer, svf_command_buffer_size);
  390. }
  391. if (svf_command_buffer != NULL)
  392. {
  393. free(svf_command_buffer);
  394. }
  395. svf_command_buffer = tmp_buffer;
  396. svf_command_buffer_size += SVFP_CMD_INC_CNT;
  397. tmp_buffer = NULL;
  398. }
  399. svf_command_buffer[cmd_pos++] = (char)toupper(ch);
  400. }
  401. break;
  402. }
  403. }
  404. if (cmd_ok)
  405. {
  406. svf_command_buffer[cmd_pos] = '\0';
  407. return ERROR_OK;
  408. }
  409. else
  410. {
  411. return ERROR_FAIL;
  412. }
  413. }
  414. static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
  415. {
  416. int pos = 0, num = 0, space_found = 1;
  417. while (pos < len)
  418. {
  419. switch(str[pos])
  420. {
  421. case '\n':
  422. case '\r':
  423. case '!':
  424. case '/':
  425. LOG_ERROR("fail to parse svf command");
  426. return ERROR_FAIL;
  427. break;
  428. case ' ':
  429. space_found = 1;
  430. str[pos] = '\0';
  431. break;
  432. default:
  433. if (space_found)
  434. {
  435. argus[num++] = &str[pos];
  436. space_found = 0;
  437. }
  438. break;
  439. }
  440. pos++;
  441. }
  442. *num_of_argu = num;
  443. return ERROR_OK;
  444. }
  445. static int svf_tap_state_is_stable(tap_state_t state)
  446. {
  447. return ((TAP_RESET == state) || (TAP_IDLE == state) || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state));
  448. }
  449. static int svf_tap_state_is_valid(tap_state_t state)
  450. {
  451. return state >= 0 && state < TAP_NUM_STATES;
  452. }
  453. static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
  454. {
  455. int i;
  456. for (i = 0; i < num_of_element; i++)
  457. {
  458. if (!strcmp(str, strs[i]))
  459. {
  460. return i;
  461. }
  462. }
  463. return 0xFF;
  464. }
  465. static int svf_adjust_array_length(u8 **arr, int orig_bit_len, int new_bit_len)
  466. {
  467. int new_byte_len = (new_bit_len + 7) >> 3;
  468. if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
  469. {
  470. if (*arr != NULL)
  471. {
  472. free(*arr);
  473. *arr = NULL;
  474. }
  475. *arr = (u8*)malloc(new_byte_len);
  476. if (NULL == *arr)
  477. {
  478. LOG_ERROR("not enough memory");
  479. return ERROR_FAIL;
  480. }
  481. memset(*arr, 0, new_byte_len);
  482. }
  483. return ERROR_OK;
  484. }
  485. static int svf_copy_hexstring_to_binary(char *str, u8 **bin, int orig_bit_len, int bit_len)
  486. {
  487. int i, str_len = strlen(str), str_byte_len = (bit_len + 3) >> 2, loop_cnt;
  488. u8 ch, need_write = 1;
  489. if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
  490. {
  491. LOG_ERROR("fail to adjust length of array");
  492. return ERROR_FAIL;
  493. }
  494. if (str_byte_len > str_len)
  495. {
  496. loop_cnt = str_byte_len;
  497. }
  498. else
  499. {
  500. loop_cnt = str_len;
  501. }
  502. for (i = 0; i < loop_cnt; i++)
  503. {
  504. if (i < str_len)
  505. {
  506. ch = str[str_len - i - 1];
  507. if ((ch >= '0') && (ch <= '9'))
  508. {
  509. ch = ch - '0';
  510. }
  511. else if ((ch >= 'A') && (ch <= 'F'))
  512. {
  513. ch = ch - 'A' + 10;
  514. }
  515. else
  516. {
  517. LOG_ERROR("invalid hex string");
  518. return ERROR_FAIL;
  519. }
  520. }
  521. else
  522. {
  523. ch = 0;
  524. }
  525. // check valid
  526. if (i >= str_byte_len)
  527. {
  528. // all data written, other data should be all '0's and needn't to be written
  529. need_write = 0;
  530. if (ch != 0)
  531. {
  532. LOG_ERROR("value execede length");
  533. return ERROR_FAIL;
  534. }
  535. }
  536. else if (i == (str_byte_len - 1))
  537. {
  538. // last data byte, written if valid
  539. if ((ch & ~((1 << (bit_len - 4 * i)) - 1)) != 0)
  540. {
  541. LOG_ERROR("value execede length");
  542. return ERROR_FAIL;
  543. }
  544. }
  545. if (need_write)
  546. {
  547. // write bin
  548. if (i % 2)
  549. {
  550. // MSB
  551. (*bin)[i / 2] |= ch << 4;
  552. }
  553. else
  554. {
  555. // LSB
  556. (*bin)[i / 2] = 0;
  557. (*bin)[i / 2] |= ch;
  558. }
  559. }
  560. }
  561. return ERROR_OK;
  562. }
  563. static int svf_check_tdo(void)
  564. {
  565. int i, len, index;
  566. for (i = 0; i < svf_check_tdo_para_index; i++)
  567. {
  568. index = svf_check_tdo_para[i].buffer_offset;
  569. len = svf_check_tdo_para[i].bit_len;
  570. if ((svf_check_tdo_para[i].enabled)
  571. && buf_cmp_mask(&svf_tdi_buffer[index], &svf_tdo_buffer[index], &svf_mask_buffer[index], len))
  572. {
  573. unsigned bitmask;
  574. unsigned received, expected, tapmask;
  575. if (svf_check_tdo_para[i].bit_len >= 32)
  576. {
  577. bitmask = 0xFFFFFFFF;
  578. }
  579. else
  580. {
  581. bitmask = (1 << svf_check_tdo_para[i].bit_len) - 1;
  582. }
  583. memcpy(&received, svf_tdi_buffer + index, sizeof(unsigned));
  584. memcpy(&expected, svf_tdo_buffer + index, sizeof(unsigned));
  585. memcpy(&tapmask, svf_mask_buffer + index, sizeof(unsigned));
  586. LOG_ERROR("tdo check error at line %d",
  587. svf_check_tdo_para[i].line_num);
  588. LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
  589. received & bitmask,
  590. expected & bitmask,
  591. tapmask & bitmask);
  592. return ERROR_FAIL;
  593. }
  594. }
  595. svf_check_tdo_para_index = 0;
  596. return ERROR_OK;
  597. }
  598. static int svf_add_check_para(u8 enabled, int buffer_offset, int bit_len)
  599. {
  600. if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
  601. {
  602. LOG_ERROR("toooooo many operation undone");
  603. return ERROR_FAIL;
  604. }
  605. svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
  606. svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
  607. svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
  608. svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
  609. svf_check_tdo_para_index++;
  610. return ERROR_OK;
  611. }
  612. static int svf_execute_tap(void)
  613. {
  614. if (ERROR_OK != jtag_execute_queue())
  615. {
  616. return ERROR_FAIL;
  617. }
  618. else if (ERROR_OK != svf_check_tdo())
  619. {
  620. return ERROR_FAIL;
  621. }
  622. svf_buffer_index = 0;
  623. return ERROR_OK;
  624. }
  625. // not good to use this
  626. extern void* cmd_queue_alloc(size_t size);
  627. extern void jtag_queue_command(jtag_command_t * cmd);
  628. static int svf_run_command(struct command_context_s *cmd_ctx, char *cmd_str)
  629. {
  630. char *argus[256], command;
  631. int num_of_argu = 0, i;
  632. // tmp variable
  633. int i_tmp;
  634. // for RUNTEST
  635. int run_count;
  636. float min_time, max_time;
  637. // for XXR
  638. svf_xxr_para_t *xxr_para_tmp;
  639. u8 **pbuffer_tmp;
  640. scan_field_t field;
  641. // for STATE
  642. tap_state_t *path = NULL, state;
  643. if (!svf_quiet)
  644. {
  645. LOG_USER("%s", svf_command_buffer);
  646. }
  647. if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
  648. {
  649. return ERROR_FAIL;
  650. }
  651. command = svf_find_string_in_array(argus[0], (char **)svf_command_name, dimof(svf_command_name));
  652. switch(command)
  653. {
  654. case ENDDR:
  655. case ENDIR:
  656. if (num_of_argu != 2)
  657. {
  658. LOG_ERROR("invalid parameter of %s", argus[0]);
  659. return ERROR_FAIL;
  660. }
  661. i_tmp = svf_find_string_in_array(argus[1], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
  662. if (svf_tap_state_is_stable(i_tmp))
  663. {
  664. if (command == ENDIR)
  665. {
  666. svf_para.ir_end_state = i_tmp;
  667. LOG_DEBUG("\tir_end_state = %s", svf_tap_state_name[svf_para.ir_end_state]);
  668. }
  669. else
  670. {
  671. svf_para.dr_end_state = i_tmp;
  672. LOG_DEBUG("\tdr_end_state = %s", svf_tap_state_name[svf_para.dr_end_state]);
  673. }
  674. }
  675. else
  676. {
  677. LOG_ERROR("%s is not valid state", argus[1]);
  678. return ERROR_FAIL;
  679. }
  680. break;
  681. case FREQUENCY:
  682. if ((num_of_argu != 1) && (num_of_argu != 3))
  683. {
  684. LOG_ERROR("invalid parameter of %s", argus[0]);
  685. return ERROR_FAIL;
  686. }
  687. if (1 == num_of_argu)
  688. {
  689. // TODO: set jtag speed to full speed
  690. svf_para.frequency = 0;
  691. }
  692. else
  693. {
  694. if (strcmp(argus[2], "HZ"))
  695. {
  696. LOG_ERROR("HZ not found in FREQUENCY command");
  697. return ERROR_FAIL;
  698. }
  699. if (ERROR_OK != svf_execute_tap())
  700. {
  701. return ERROR_FAIL;
  702. }
  703. svf_para.frequency = atof(argus[1]);
  704. // TODO: set jtag speed to
  705. if (svf_para.frequency > 0)
  706. {
  707. command_run_linef(cmd_ctx, "jtag_khz %d", (int)svf_para.frequency / 1000);
  708. LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
  709. }
  710. }
  711. break;
  712. case HDR:
  713. xxr_para_tmp = &svf_para.hdr_para;
  714. goto XXR_common;
  715. case HIR:
  716. xxr_para_tmp = &svf_para.hir_para;
  717. goto XXR_common;
  718. case TDR:
  719. xxr_para_tmp = &svf_para.tdr_para;
  720. goto XXR_common;
  721. case TIR:
  722. xxr_para_tmp = &svf_para.tir_para;
  723. goto XXR_common;
  724. case SDR:
  725. xxr_para_tmp = &svf_para.sdr_para;
  726. goto XXR_common;
  727. case SIR:
  728. xxr_para_tmp = &svf_para.sir_para;
  729. goto XXR_common;
  730. XXR_common:
  731. // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
  732. if ((num_of_argu > 10) || (num_of_argu % 2))
  733. {
  734. LOG_ERROR("invalid parameter of %s", argus[0]);
  735. return ERROR_FAIL;
  736. }
  737. i_tmp = xxr_para_tmp->len;
  738. xxr_para_tmp->len = atoi(argus[1]);
  739. LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
  740. xxr_para_tmp->data_mask = 0;
  741. for (i = 2; i < num_of_argu; i += 2)
  742. {
  743. if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
  744. {
  745. LOG_ERROR("data section error");
  746. return ERROR_FAIL;
  747. }
  748. argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
  749. // TDI, TDO, MASK, SMASK
  750. if (!strcmp(argus[i], "TDI"))
  751. {
  752. // TDI
  753. pbuffer_tmp = &xxr_para_tmp->tdi;
  754. xxr_para_tmp->data_mask |= XXR_TDI;
  755. }
  756. else if (!strcmp(argus[i], "TDO"))
  757. {
  758. // TDO
  759. pbuffer_tmp = &xxr_para_tmp->tdo;
  760. xxr_para_tmp->data_mask |= XXR_TDO;
  761. }
  762. else if (!strcmp(argus[i], "MASK"))
  763. {
  764. // MASK
  765. pbuffer_tmp = &xxr_para_tmp->mask;
  766. xxr_para_tmp->data_mask |= XXR_MASK;
  767. }
  768. else if (!strcmp(argus[i], "SMASK"))
  769. {
  770. // SMASK
  771. pbuffer_tmp = &xxr_para_tmp->smask;
  772. xxr_para_tmp->data_mask |= XXR_SMASK;
  773. }
  774. else
  775. {
  776. LOG_ERROR("unknow parameter: %s", argus[i]);
  777. return ERROR_FAIL;
  778. }
  779. if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
  780. {
  781. LOG_ERROR("fail to parse hex value");
  782. return ERROR_FAIL;
  783. }
  784. LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & ((1 << (xxr_para_tmp->len)) - 1));
  785. }
  786. // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
  787. // the mask pattern used is all cares
  788. if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
  789. {
  790. // MASK not defined and length changed
  791. if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
  792. {
  793. LOG_ERROR("fail to adjust length of array");
  794. return ERROR_FAIL;
  795. }
  796. buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
  797. }
  798. // do scan if necessary
  799. if (SDR == command)
  800. {
  801. // check buffer size first, reallocate if necessary
  802. i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
  803. if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
  804. {
  805. #if 1
  806. // simply print error message
  807. LOG_ERROR("buffer is not enough, report to author");
  808. return ERROR_FAIL;
  809. #else
  810. u8 *buffer_tmp;
  811. // reallocate buffer
  812. buffer_tmp = (u8 *)malloc(svf_buffer_index + ((i + 7) >> 3));
  813. if (NULL == buffer_tmp)
  814. {
  815. LOG_ERROR("not enough memory");
  816. return ERROR_FAIL;
  817. }
  818. memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
  819. // svf_tdi_buffer isn't NULL here
  820. free(svf_tdi_buffer);
  821. svf_tdi_buffer = buffer_tmp;
  822. buffer_tmp = (u8 *)malloc(svf_buffer_index + ((i + 7) >> 3));
  823. if (NULL == buffer_tmp)
  824. {
  825. LOG_ERROR("not enough memory");
  826. return ERROR_FAIL;
  827. }
  828. memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
  829. // svf_tdo_buffer isn't NULL here
  830. free(svf_tdo_buffer);
  831. svf_tdo_buffer = buffer_tmp;
  832. buffer_tmp = (u8 *)malloc(svf_buffer_index + ((i + 7) >> 3));
  833. if (NULL == buffer_tmp)
  834. {
  835. LOG_ERROR("not enough memory");
  836. return ERROR_FAIL;
  837. }
  838. memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
  839. // svf_mask_buffer isn't NULL here
  840. free(svf_mask_buffer);
  841. svf_mask_buffer = buffer_tmp;
  842. buffer_tmp = NULL;
  843. svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
  844. #endif
  845. }
  846. // assemble dr data
  847. i = 0;
  848. buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
  849. i += svf_para.hdr_para.len;
  850. buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
  851. i += svf_para.sdr_para.len;
  852. buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
  853. i += svf_para.tdr_para.len;
  854. // add check data
  855. if (svf_para.sdr_para.data_mask & XXR_TDO)
  856. {
  857. // assemble dr mask data
  858. i = 0;
  859. buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
  860. i += svf_para.hdr_para.len;
  861. buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
  862. i += svf_para.sdr_para.len;
  863. buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
  864. i += svf_para.tdr_para.len;
  865. // assemble dr check data
  866. i = 0;
  867. buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
  868. i += svf_para.hdr_para.len;
  869. buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
  870. i += svf_para.sdr_para.len;
  871. buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
  872. i += svf_para.tdr_para.len;
  873. svf_add_check_para(1, svf_buffer_index, i);
  874. }
  875. else
  876. {
  877. svf_add_check_para(0, svf_buffer_index, i);
  878. }
  879. field.tap = tap;
  880. field.num_bits = i;
  881. field.out_value = &svf_tdi_buffer[svf_buffer_index];
  882. field.in_value = &svf_tdi_buffer[svf_buffer_index];
  883. jtag_add_plain_dr_scan(1, &field, svf_para.dr_end_state);
  884. svf_buffer_index += (i + 7) >> 3;
  885. last_state = svf_para.dr_end_state;
  886. }
  887. else if (SIR == command)
  888. {
  889. // check buffer size first, reallocate if necessary
  890. i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
  891. if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
  892. {
  893. #if 1
  894. // simply print error message
  895. LOG_ERROR("buffer is not enough, report to author");
  896. return ERROR_FAIL;
  897. #else
  898. u8 *buffer_tmp;
  899. // reallocate buffer
  900. buffer_tmp = (u8 *)malloc(svf_buffer_index + ((i + 7) >> 3));
  901. if (NULL == buffer_tmp)
  902. {
  903. LOG_ERROR("not enough memory");
  904. return ERROR_FAIL;
  905. }
  906. memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
  907. // svf_tdi_buffer isn't NULL here
  908. free(svf_tdi_buffer);
  909. svf_tdi_buffer = buffer_tmp;
  910. buffer_tmp = (u8 *)malloc(svf_buffer_index + ((i + 7) >> 3));
  911. if (NULL == buffer_tmp)
  912. {
  913. LOG_ERROR("not enough memory");
  914. return ERROR_FAIL;
  915. }
  916. memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
  917. // svf_tdo_buffer isn't NULL here
  918. free(svf_tdo_buffer);
  919. svf_tdo_buffer = buffer_tmp;
  920. buffer_tmp = (u8 *)malloc(svf_buffer_index + ((i + 7) >> 3));
  921. if (NULL == buffer_tmp)
  922. {
  923. LOG_ERROR("not enough memory");
  924. return ERROR_FAIL;
  925. }
  926. memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
  927. // svf_mask_buffer isn't NULL here
  928. free(svf_mask_buffer);
  929. svf_mask_buffer = buffer_tmp;
  930. buffer_tmp = NULL;
  931. svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
  932. #endif
  933. }
  934. // assemble ir data
  935. i = 0;
  936. buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
  937. i += svf_para.hir_para.len;
  938. buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
  939. i += svf_para.sir_para.len;
  940. buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
  941. i += svf_para.tir_para.len;
  942. // add check data
  943. if (svf_para.sir_para.data_mask & XXR_TDO)
  944. {
  945. // assemble dr mask data
  946. i = 0;
  947. buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
  948. i += svf_para.hir_para.len;
  949. buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
  950. i += svf_para.sir_para.len;
  951. buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
  952. i += svf_para.tir_para.len;
  953. // assemble dr check data
  954. i = 0;
  955. buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
  956. i += svf_para.hir_para.len;
  957. buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
  958. i += svf_para.sir_para.len;
  959. buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
  960. i += svf_para.tir_para.len;
  961. svf_add_check_para(1, svf_buffer_index, i);
  962. }
  963. else
  964. {
  965. svf_add_check_para(0, svf_buffer_index, i);
  966. }
  967. field.tap = tap;
  968. field.num_bits = i;
  969. field.out_value = &svf_tdi_buffer[svf_buffer_index];
  970. field.in_value = &svf_tdi_buffer[svf_buffer_index];
  971. jtag_add_plain_ir_scan(1, &field, svf_para.ir_end_state);
  972. svf_buffer_index += (i + 7) >> 3;
  973. last_state = svf_para.ir_end_state;
  974. }
  975. break;
  976. case PIO:
  977. case PIOMAP:
  978. LOG_ERROR("PIO and PIOMAP are not supported");
  979. return ERROR_FAIL;
  980. break;
  981. case RUNTEST:
  982. // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
  983. // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
  984. if ((num_of_argu < 3) && (num_of_argu > 11))
  985. {
  986. LOG_ERROR("invalid parameter of %s", argus[0]);
  987. return ERROR_FAIL;
  988. }
  989. // init
  990. run_count = 0;
  991. min_time = 0;
  992. max_time = 0;
  993. i = 1;
  994. // run_state
  995. i_tmp = svf_find_string_in_array(argus[i], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
  996. if (svf_tap_state_is_valid(i_tmp))
  997. {
  998. if (svf_tap_state_is_stable(i_tmp))
  999. {
  1000. svf_para.runtest_run_state = i_tmp;
  1001. // When a run_state is specified, the new run_state becomes the default end_state
  1002. svf_para.runtest_end_state = i_tmp;
  1003. LOG_DEBUG("\trun_state = %s", svf_tap_state_name[svf_para.runtest_run_state]);
  1004. i++;
  1005. }
  1006. else
  1007. {
  1008. LOG_ERROR("%s is not valid state", svf_tap_state_name[i_tmp]);
  1009. return ERROR_FAIL;
  1010. }
  1011. }
  1012. // run_count run_clk
  1013. if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
  1014. {
  1015. if (!strcmp(argus[i + 1], "TCK"))
  1016. {
  1017. // clock source is TCK
  1018. run_count = atoi(argus[i]);
  1019. LOG_DEBUG("\trun_count@TCK = %d", run_count);
  1020. }
  1021. else
  1022. {
  1023. LOG_ERROR("%s not supported for clock", argus[i + 1]);
  1024. return ERROR_FAIL;
  1025. }
  1026. i += 2;
  1027. }
  1028. // min_time SEC
  1029. if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
  1030. {
  1031. min_time = atof(argus[i]);
  1032. LOG_DEBUG("\tmin_time = %fs", min_time);
  1033. i += 2;
  1034. }
  1035. // MAXIMUM max_time SEC
  1036. if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
  1037. {
  1038. max_time = atof(argus[i + 1]);
  1039. LOG_DEBUG("\tmax_time = %fs", max_time);
  1040. i += 3;
  1041. }
  1042. // ENDSTATE end_state
  1043. if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
  1044. {
  1045. i_tmp = svf_find_string_in_array(argus[i + 1], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
  1046. if (svf_tap_state_is_stable(i_tmp))
  1047. {
  1048. svf_para.runtest_end_state = i_tmp;
  1049. LOG_DEBUG("\tend_state = %s", svf_tap_state_name[svf_para.runtest_end_state]);
  1050. }
  1051. else
  1052. {
  1053. LOG_ERROR("%s is not valid state", svf_tap_state_name[i_tmp]);
  1054. return ERROR_FAIL;
  1055. }
  1056. i += 2;
  1057. }
  1058. // calculate run_count
  1059. if ((0 == run_count) && (min_time > 0))
  1060. {
  1061. run_count = min_time * svf_para.frequency;
  1062. }
  1063. // all parameter should be parsed
  1064. if (i == num_of_argu)
  1065. {
  1066. if (run_count > 0)
  1067. {
  1068. // run_state and end_state is checked to be stable state
  1069. // TODO: do runtest
  1070. #if 1
  1071. // enter into run_state if necessary
  1072. if (last_state != svf_para.runtest_run_state)
  1073. {
  1074. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  1075. jtag_queue_command(cmd);
  1076. cmd->type = JTAG_STATEMOVE;
  1077. cmd->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  1078. cmd->cmd.statemove->end_state = svf_para.runtest_run_state;
  1079. cmd_queue_end_state = cmd_queue_cur_state = cmd->cmd.statemove->end_state;
  1080. }
  1081. // call jtag_add_clocks
  1082. jtag_add_clocks(run_count);
  1083. if (svf_para.runtest_end_state != svf_para.runtest_run_state)
  1084. {
  1085. // move to end_state
  1086. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  1087. jtag_queue_command(cmd);
  1088. cmd->type = JTAG_STATEMOVE;
  1089. cmd->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  1090. cmd->cmd.statemove->end_state = svf_para.runtest_end_state;
  1091. cmd_queue_end_state = cmd_queue_cur_state = cmd->cmd.statemove->end_state;
  1092. }
  1093. last_state = svf_para.runtest_end_state;
  1094. #else
  1095. if (svf_para.runtest_run_state != TAP_IDLE)
  1096. {
  1097. // RUNTEST can only executed in TAP_IDLE
  1098. LOG_ERROR("cannot runtest in %s state", svf_tap_state_name[svf_para.runtest_run_state]);
  1099. return ERROR_FAIL;
  1100. }
  1101. jtag_add_runtest(run_count, svf_para.runtest_end_state);
  1102. #endif
  1103. }
  1104. }
  1105. else
  1106. {
  1107. LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
  1108. return ERROR_FAIL;
  1109. }
  1110. break;
  1111. case STATE:
  1112. // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
  1113. if (num_of_argu < 2)
  1114. {
  1115. LOG_ERROR("invalid parameter of %s", argus[0]);
  1116. return ERROR_FAIL;
  1117. }
  1118. if (num_of_argu > 2)
  1119. {
  1120. // STATE pathstate1 ... stable_state
  1121. path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
  1122. if (NULL == path)
  1123. {
  1124. LOG_ERROR("not enough memory");
  1125. return ERROR_FAIL;
  1126. }
  1127. num_of_argu--; // num of path
  1128. i_tmp = 1; // path is from patameter 1
  1129. for (i = 0; i < num_of_argu; i++)
  1130. {
  1131. path[i] = svf_find_string_in_array(argus[i_tmp++], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
  1132. if (!svf_tap_state_is_valid(path[i]))
  1133. {
  1134. LOG_ERROR("%s is not valid state", svf_tap_state_name[path[i]]);
  1135. return ERROR_FAIL;
  1136. }
  1137. if (TAP_RESET == path[i])
  1138. {
  1139. if (i > 0)
  1140. {
  1141. jtag_add_pathmove(i, path);
  1142. }
  1143. jtag_add_tlr();
  1144. num_of_argu -= i + 1;
  1145. i = -1;
  1146. }
  1147. }
  1148. if (num_of_argu > 0)
  1149. {
  1150. // execute last path if necessary
  1151. if (svf_tap_state_is_stable(path[num_of_argu - 1]))
  1152. {
  1153. // last state MUST be stable state
  1154. // TODO: call path_move
  1155. jtag_add_pathmove(num_of_argu, path);
  1156. last_state = path[num_of_argu - 1];
  1157. LOG_DEBUG("\tmove to %s by path_move", svf_tap_state_name[path[num_of_argu - 1]]);
  1158. }
  1159. else
  1160. {
  1161. LOG_ERROR("%s is not valid state", svf_tap_state_name[path[num_of_argu - 1]]);
  1162. return ERROR_FAIL;
  1163. }
  1164. }
  1165. // no need to keep this memory, in jtag_add_pathmove, path will be duplicated
  1166. if (NULL != path)
  1167. {
  1168. free(path);
  1169. path = NULL;
  1170. }
  1171. }
  1172. else
  1173. {
  1174. // STATE stable_state
  1175. state = svf_find_string_in_array(argus[1], (char **)svf_tap_state_name, dimof(svf_tap_state_name));
  1176. if (svf_tap_state_is_stable(state))
  1177. {
  1178. // TODO: move to state
  1179. jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
  1180. jtag_queue_command(cmd);
  1181. cmd->type = JTAG_STATEMOVE;
  1182. cmd->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
  1183. cmd->cmd.statemove->end_state = state;
  1184. cmd_queue_end_state = cmd_queue_cur_state = cmd->cmd.statemove->end_state;
  1185. last_state = state;
  1186. LOG_DEBUG("\tmove to %s by state_move", svf_tap_state_name[state]);
  1187. }
  1188. else
  1189. {
  1190. LOG_ERROR("%s is not valid state", svf_tap_state_name[state]);
  1191. return ERROR_FAIL;
  1192. }
  1193. }
  1194. break;
  1195. case TRST:
  1196. // TRST trst_mode
  1197. if (num_of_argu != 2)
  1198. {
  1199. LOG_ERROR("invalid parameter of %s", argus[0]);
  1200. return ERROR_FAIL;
  1201. }
  1202. if (svf_para.trst_mode != TRST_ABSENT)
  1203. {
  1204. if (ERROR_OK != svf_execute_tap())
  1205. {
  1206. return ERROR_FAIL;
  1207. }
  1208. i_tmp = svf_find_string_in_array(argus[1], (char **)svf_trst_mode_name, dimof(svf_trst_mode_name));
  1209. switch (i_tmp)
  1210. {
  1211. case TRST_ON:
  1212. last_state = TAP_RESET;
  1213. jtag_add_reset(1, 0);
  1214. break;
  1215. case TRST_Z:
  1216. case TRST_OFF:
  1217. jtag_add_reset(0, 0);
  1218. break;
  1219. case TRST_ABSENT:
  1220. break;
  1221. default:
  1222. LOG_ERROR("unknown TRST mode: %s", argus[1]);
  1223. return ERROR_FAIL;
  1224. }
  1225. svf_para.trst_mode = i_tmp;
  1226. LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
  1227. }
  1228. else
  1229. {
  1230. LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
  1231. return ERROR_FAIL;
  1232. }
  1233. break;
  1234. default:
  1235. LOG_ERROR("invalid svf command: %s", argus[0]);
  1236. return ERROR_FAIL;
  1237. break;
  1238. }
  1239. if (debug_level >= LOG_LVL_DEBUG)
  1240. {
  1241. // for convenient debugging, execute tap if possible
  1242. if ((svf_buffer_index > 0) && \
  1243. (((command != STATE) && (command != RUNTEST)) || \
  1244. ((command == STATE) && (num_of_argu == 2))))
  1245. {
  1246. if (ERROR_OK != svf_execute_tap())
  1247. {
  1248. return ERROR_FAIL;
  1249. }
  1250. // output debug info
  1251. if ((SIR == command) || (SDR == command))
  1252. {
  1253. int read_value;
  1254. memcpy(&read_value, svf_tdi_buffer, sizeof(int));
  1255. // in debug mode, data is from index 0
  1256. int read_mask = (1 << (svf_check_tdo_para[0].bit_len)) - 1;
  1257. LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
  1258. }
  1259. }
  1260. }
  1261. else
  1262. {
  1263. // for fast executing, execute tap if necessary
  1264. // half of the buffer is for the next command
  1265. if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
  1266. (((command != STATE) && (command != RUNTEST)) || \
  1267. ((command == STATE) && (num_of_argu == 2))))
  1268. {
  1269. return svf_execute_tap();
  1270. }
  1271. }
  1272. return ERROR_OK;
  1273. }