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.
 
 
 
 
 
 

1115 lines
26 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 Peter Hettkamp *
  9. * peter.hettkamp@htp-tel.de *
  10. * *
  11. * Copyright (C) 2009 SoftPLC Corporation. http://softplc.com *
  12. * Dick Hollenbeck <dick@softplc.com> *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program; if not, write to the *
  26. * Free Software Foundation, Inc., *
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  28. ***************************************************************************/
  29. /* The specification for SVF is available here:
  30. * http://www.asset-intertech.com/support/svf.pdf
  31. * Below, this document is refered to as the "SVF spec".
  32. *
  33. * The specification for XSVF is available here:
  34. * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
  35. * Below, this document is refered to as the "XSVF spec".
  36. */
  37. #ifdef HAVE_CONFIG_H
  38. #include "config.h"
  39. #endif
  40. #include "xsvf.h"
  41. #include <jtag/jtag.h>
  42. #include <svf/svf.h>
  43. /* XSVF commands, from appendix B of xapp503.pdf */
  44. #define XCOMPLETE 0x00
  45. #define XTDOMASK 0x01
  46. #define XSIR 0x02
  47. #define XSDR 0x03
  48. #define XRUNTEST 0x04
  49. #define XREPEAT 0x07
  50. #define XSDRSIZE 0x08
  51. #define XSDRTDO 0x09
  52. #define XSETSDRMASKS 0x0A
  53. #define XSDRINC 0x0B
  54. #define XSDRB 0x0C
  55. #define XSDRC 0x0D
  56. #define XSDRE 0x0E
  57. #define XSDRTDOB 0x0F
  58. #define XSDRTDOC 0x10
  59. #define XSDRTDOE 0x11
  60. #define XSTATE 0x12
  61. #define XENDIR 0x13
  62. #define XENDDR 0x14
  63. #define XSIR2 0x15
  64. #define XCOMMENT 0x16
  65. #define XWAIT 0x17
  66. /* XWAITSTATE is not in the xilinx XSVF spec, but the svf2xsvf.py translator
  67. * generates this. Arguably it is needed because the XSVF XRUNTEST command
  68. * was ill conceived and does not directly flow out of the SVF RUNTEST command.
  69. * This XWAITSTATE does map directly from the SVF RUNTEST command.
  70. */
  71. #define XWAITSTATE 0x18
  72. /* Lattice has extended the SVF file format, and Dick Hollenbeck's python based
  73. * SVF2XSVF converter supports these 3 additional XSVF opcodes, LCOUNT, LDELAY, LSDR.
  74. * Here is an example of usage of the 3 lattice opcode extensions:
  75. ! Set the maximum loop count to 25.
  76. LCOUNT 25;
  77. ! Step to DRPAUSE give 5 clocks and wait for 1.00e + 000 SEC.
  78. LDELAY DRPAUSE 5 TCK 1.00E-003 SEC;
  79. ! Test for the completed status. Match means pass.
  80. ! Loop back to LDELAY line if not match and loop count less than 25.
  81. LSDR 1 TDI (0)
  82. TDO (1);
  83. */
  84. #define LCOUNT 0x19
  85. #define LDELAY 0x1A
  86. #define LSDR 0x1B
  87. #define XTRST 0x1C
  88. /* XSVF valid state values for the XSTATE command, from appendix B of xapp503.pdf */
  89. #define XSV_RESET 0x00
  90. #define XSV_IDLE 0x01
  91. #define XSV_DRSELECT 0x02
  92. #define XSV_DRCAPTURE 0x03
  93. #define XSV_DRSHIFT 0x04
  94. #define XSV_DREXIT1 0x05
  95. #define XSV_DRPAUSE 0x06
  96. #define XSV_DREXIT2 0x07
  97. #define XSV_DRUPDATE 0x08
  98. #define XSV_IRSELECT 0x09
  99. #define XSV_IRCAPTURE 0x0A
  100. #define XSV_IRSHIFT 0x0B
  101. #define XSV_IREXIT1 0x0C
  102. #define XSV_IRPAUSE 0x0D
  103. #define XSV_IREXIT2 0x0E
  104. #define XSV_IRUPDATE 0x0F
  105. /* arguments to XTRST */
  106. #define XTRST_ON 0
  107. #define XTRST_OFF 1
  108. #define XTRST_Z 2
  109. #define XTRST_ABSENT 3
  110. #define XSTATE_MAX_PATH 12
  111. static int xsvf_fd;
  112. /* map xsvf tap state to an openocd "tap_state_t" */
  113. static tap_state_t xsvf_to_tap(int xsvf_state)
  114. {
  115. tap_state_t ret;
  116. switch (xsvf_state) {
  117. case XSV_RESET:
  118. ret = TAP_RESET;
  119. break;
  120. case XSV_IDLE:
  121. ret = TAP_IDLE;
  122. break;
  123. case XSV_DRSELECT:
  124. ret = TAP_DRSELECT;
  125. break;
  126. case XSV_DRCAPTURE:
  127. ret = TAP_DRCAPTURE;
  128. break;
  129. case XSV_DRSHIFT:
  130. ret = TAP_DRSHIFT;
  131. break;
  132. case XSV_DREXIT1:
  133. ret = TAP_DREXIT1;
  134. break;
  135. case XSV_DRPAUSE:
  136. ret = TAP_DRPAUSE;
  137. break;
  138. case XSV_DREXIT2:
  139. ret = TAP_DREXIT2;
  140. break;
  141. case XSV_DRUPDATE:
  142. ret = TAP_DRUPDATE;
  143. break;
  144. case XSV_IRSELECT:
  145. ret = TAP_IRSELECT;
  146. break;
  147. case XSV_IRCAPTURE:
  148. ret = TAP_IRCAPTURE;
  149. break;
  150. case XSV_IRSHIFT:
  151. ret = TAP_IRSHIFT;
  152. break;
  153. case XSV_IREXIT1:
  154. ret = TAP_IREXIT1;
  155. break;
  156. case XSV_IRPAUSE:
  157. ret = TAP_IRPAUSE;
  158. break;
  159. case XSV_IREXIT2:
  160. ret = TAP_IREXIT2;
  161. break;
  162. case XSV_IRUPDATE:
  163. ret = TAP_IRUPDATE;
  164. break;
  165. default:
  166. LOG_ERROR("UNKNOWN XSVF STATE 0x%02X", xsvf_state);
  167. exit(1);
  168. }
  169. return ret;
  170. }
  171. static int xsvf_read_buffer(int num_bits, int fd, uint8_t *buf)
  172. {
  173. int num_bytes;
  174. for (num_bytes = (num_bits + 7) / 8; num_bytes > 0; num_bytes--) {
  175. /* reverse the order of bytes as they are read sequentially from file */
  176. if (read(fd, buf + num_bytes - 1, 1) < 0)
  177. return ERROR_XSVF_EOF;
  178. }
  179. return ERROR_OK;
  180. }
  181. COMMAND_HANDLER(handle_xsvf_command)
  182. {
  183. uint8_t *dr_out_buf = NULL; /* from host to device (TDI) */
  184. uint8_t *dr_in_buf = NULL; /* from device to host (TDO) */
  185. uint8_t *dr_in_mask = NULL;
  186. int xsdrsize = 0;
  187. int xruntest = 0; /* number of TCK cycles OR *microseconds */
  188. int xrepeat = 0; /* number of retries */
  189. tap_state_t xendir = TAP_IDLE; /* see page 8 of the SVF spec, initial
  190. *xendir to be TAP_IDLE */
  191. tap_state_t xenddr = TAP_IDLE;
  192. uint8_t opcode;
  193. uint8_t uc = 0;
  194. long file_offset = 0;
  195. int loop_count = 0;
  196. tap_state_t loop_state = TAP_IDLE;
  197. int loop_clocks = 0;
  198. int loop_usecs = 0;
  199. int do_abort = 0;
  200. int unsupported = 0;
  201. int tdo_mismatch = 0;
  202. int result;
  203. int verbose = 1;
  204. bool collecting_path = false;
  205. tap_state_t path[XSTATE_MAX_PATH];
  206. unsigned pathlen = 0;
  207. /* a flag telling whether to clock TCK during waits,
  208. * or simply sleep, controled by virt2
  209. */
  210. int runtest_requires_tck = 0;
  211. /* use NULL to indicate a "plain" xsvf file which accounts for
  212. * additional devices in the scan chain, otherwise the device
  213. * that should be affected
  214. */
  215. struct jtag_tap *tap = NULL;
  216. if (CMD_ARGC < 2)
  217. return ERROR_COMMAND_SYNTAX_ERROR;
  218. /* we mess with CMD_ARGV starting point below, snapshot filename here */
  219. const char *filename = CMD_ARGV[1];
  220. if (strcmp(CMD_ARGV[0], "plain") != 0) {
  221. tap = jtag_tap_by_string(CMD_ARGV[0]);
  222. if (!tap) {
  223. command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[0]);
  224. return ERROR_FAIL;
  225. }
  226. }
  227. xsvf_fd = open(filename, O_RDONLY);
  228. if (xsvf_fd < 0) {
  229. command_print(CMD_CTX, "file \"%s\" not found", filename);
  230. return ERROR_FAIL;
  231. }
  232. /* if this argument is present, then interpret xruntest counts as TCK cycles rather than as
  233. *usecs */
  234. if ((CMD_ARGC > 2) && (strcmp(CMD_ARGV[2], "virt2") == 0)) {
  235. runtest_requires_tck = 1;
  236. --CMD_ARGC;
  237. ++CMD_ARGV;
  238. }
  239. if ((CMD_ARGC > 2) && (strcmp(CMD_ARGV[2], "quiet") == 0))
  240. verbose = 0;
  241. LOG_USER("xsvf processing file: \"%s\"", filename);
  242. while (read(xsvf_fd, &opcode, 1) > 0) {
  243. /* record the position of this opcode within the file */
  244. file_offset = lseek(xsvf_fd, 0, SEEK_CUR) - 1;
  245. /* maybe collect another state for a pathmove();
  246. * or terminate a path.
  247. */
  248. if (collecting_path) {
  249. tap_state_t mystate;
  250. switch (opcode) {
  251. case XCOMMENT:
  252. /* ignore/show comments between XSTATE ops */
  253. break;
  254. case XSTATE:
  255. /* try to collect another transition */
  256. if (pathlen == XSTATE_MAX_PATH) {
  257. LOG_ERROR("XSVF: path too long");
  258. do_abort = 1;
  259. break;
  260. }
  261. if (read(xsvf_fd, &uc, 1) < 0) {
  262. do_abort = 1;
  263. break;
  264. }
  265. mystate = xsvf_to_tap(uc);
  266. path[pathlen++] = mystate;
  267. LOG_DEBUG("XSTATE 0x%02X %s", uc,
  268. tap_state_name(mystate));
  269. /* If path is incomplete, collect more */
  270. if (!svf_tap_state_is_stable(mystate))
  271. continue;
  272. /* Else execute the path transitions we've
  273. * collected so far.
  274. *
  275. * NOTE: Punting on the saved path is not
  276. * strictly correct, but we must to do this
  277. * unless jtag_add_pathmove() stops rejecting
  278. * paths containing RESET. This is probably
  279. * harmless, since there aren't many options
  280. * for going from a stable state to reset;
  281. * at the worst, we may issue extra clocks
  282. * once we get to RESET.
  283. */
  284. if (mystate == TAP_RESET) {
  285. LOG_WARNING("XSVF: dodgey RESET");
  286. path[0] = mystate;
  287. }
  288. /* FALL THROUGH */
  289. default:
  290. /* Execute the path we collected
  291. *
  292. * NOTE: OpenOCD requires something that XSVF
  293. * doesn't: the last TAP state in the path
  294. * must be stable. In practice, tools that
  295. * create XSVF seem to follow that rule too.
  296. */
  297. collecting_path = false;
  298. if (path[0] == TAP_RESET)
  299. jtag_add_tlr();
  300. else
  301. jtag_add_pathmove(pathlen, path);
  302. result = jtag_execute_queue();
  303. if (result != ERROR_OK) {
  304. LOG_ERROR("XSVF: pathmove error %d", result);
  305. do_abort = 1;
  306. break;
  307. }
  308. continue;
  309. }
  310. }
  311. switch (opcode) {
  312. case XCOMPLETE:
  313. LOG_DEBUG("XCOMPLETE");
  314. result = jtag_execute_queue();
  315. if (result != ERROR_OK) {
  316. tdo_mismatch = 1;
  317. break;
  318. }
  319. break;
  320. case XTDOMASK:
  321. LOG_DEBUG("XTDOMASK");
  322. if (dr_in_mask &&
  323. (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_mask) != ERROR_OK))
  324. do_abort = 1;
  325. break;
  326. case XRUNTEST:
  327. {
  328. uint8_t xruntest_buf[4];
  329. if (read(xsvf_fd, xruntest_buf, 4) < 0) {
  330. do_abort = 1;
  331. break;
  332. }
  333. xruntest = be_to_h_u32(xruntest_buf);
  334. LOG_DEBUG("XRUNTEST %d 0x%08X", xruntest, xruntest);
  335. }
  336. break;
  337. case XREPEAT:
  338. {
  339. uint8_t myrepeat;
  340. if (read(xsvf_fd, &myrepeat, 1) < 0)
  341. do_abort = 1;
  342. else {
  343. xrepeat = myrepeat;
  344. LOG_DEBUG("XREPEAT %d", xrepeat);
  345. }
  346. }
  347. break;
  348. case XSDRSIZE:
  349. {
  350. uint8_t xsdrsize_buf[4];
  351. if (read(xsvf_fd, xsdrsize_buf, 4) < 0) {
  352. do_abort = 1;
  353. break;
  354. }
  355. xsdrsize = be_to_h_u32(xsdrsize_buf);
  356. LOG_DEBUG("XSDRSIZE %d", xsdrsize);
  357. if (dr_out_buf)
  358. free(dr_out_buf);
  359. if (dr_in_buf)
  360. free(dr_in_buf);
  361. if (dr_in_mask)
  362. free(dr_in_mask);
  363. dr_out_buf = malloc((xsdrsize + 7) / 8);
  364. dr_in_buf = malloc((xsdrsize + 7) / 8);
  365. dr_in_mask = malloc((xsdrsize + 7) / 8);
  366. }
  367. break;
  368. case XSDR: /* these two are identical except for the dr_in_buf */
  369. case XSDRTDO:
  370. {
  371. int limit = xrepeat;
  372. int matched = 0;
  373. int attempt;
  374. const char *op_name = (opcode == XSDR ? "XSDR" : "XSDRTDO");
  375. if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK) {
  376. do_abort = 1;
  377. break;
  378. }
  379. if (opcode == XSDRTDO) {
  380. if (xsvf_read_buffer(xsdrsize, xsvf_fd,
  381. dr_in_buf) != ERROR_OK) {
  382. do_abort = 1;
  383. break;
  384. }
  385. }
  386. if (limit < 1)
  387. limit = 1;
  388. LOG_DEBUG("%s %d", op_name, xsdrsize);
  389. for (attempt = 0; attempt < limit; ++attempt) {
  390. struct scan_field field;
  391. if (attempt > 0) {
  392. /* perform the XC9500 exception handling sequence shown in xapp067.pdf and
  393. * illustrated in psuedo code at end of this file. We start from state
  394. * DRPAUSE:
  395. * go to Exit2-DR
  396. * go to Shift-DR
  397. * go to Exit1-DR
  398. * go to Update-DR
  399. * go to Run-Test/Idle
  400. *
  401. * This sequence should be harmless for other devices, and it
  402. * will be skipped entirely if xrepeat is set to zero.
  403. */
  404. static tap_state_t exception_path[] = {
  405. TAP_DREXIT2,
  406. TAP_DRSHIFT,
  407. TAP_DREXIT1,
  408. TAP_DRUPDATE,
  409. TAP_IDLE,
  410. };
  411. jtag_add_pathmove(ARRAY_SIZE(exception_path), exception_path);
  412. if (verbose)
  413. LOG_USER("%s mismatch, xsdrsize=%d retry=%d",
  414. op_name,
  415. xsdrsize,
  416. attempt);
  417. }
  418. field.num_bits = xsdrsize;
  419. field.out_value = dr_out_buf;
  420. field.in_value = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
  421. if (tap == NULL)
  422. jtag_add_plain_dr_scan(field.num_bits,
  423. field.out_value,
  424. field.in_value,
  425. TAP_DRPAUSE);
  426. else
  427. jtag_add_dr_scan(tap, 1, &field, TAP_DRPAUSE);
  428. jtag_check_value_mask(&field, dr_in_buf, dr_in_mask);
  429. free(field.in_value);
  430. /* LOG_DEBUG("FLUSHING QUEUE"); */
  431. result = jtag_execute_queue();
  432. if (result == ERROR_OK) {
  433. matched = 1;
  434. break;
  435. }
  436. }
  437. if (!matched) {
  438. LOG_USER("%s mismatch", op_name);
  439. tdo_mismatch = 1;
  440. break;
  441. }
  442. /* See page 19 of XSVF spec regarding opcode "XSDR" */
  443. if (xruntest) {
  444. result = svf_add_statemove(TAP_IDLE);
  445. if (result != ERROR_OK)
  446. return result;
  447. if (runtest_requires_tck)
  448. jtag_add_clocks(xruntest);
  449. else
  450. jtag_add_sleep(xruntest);
  451. } else if (xendir != TAP_DRPAUSE) {
  452. /* we are already in TAP_DRPAUSE */
  453. result = svf_add_statemove(xenddr);
  454. if (result != ERROR_OK)
  455. return result;
  456. }
  457. }
  458. break;
  459. case XSETSDRMASKS:
  460. LOG_ERROR("unsupported XSETSDRMASKS");
  461. unsupported = 1;
  462. break;
  463. case XSDRINC:
  464. LOG_ERROR("unsupported XSDRINC");
  465. unsupported = 1;
  466. break;
  467. case XSDRB:
  468. LOG_ERROR("unsupported XSDRB");
  469. unsupported = 1;
  470. break;
  471. case XSDRC:
  472. LOG_ERROR("unsupported XSDRC");
  473. unsupported = 1;
  474. break;
  475. case XSDRE:
  476. LOG_ERROR("unsupported XSDRE");
  477. unsupported = 1;
  478. break;
  479. case XSDRTDOB:
  480. LOG_ERROR("unsupported XSDRTDOB");
  481. unsupported = 1;
  482. break;
  483. case XSDRTDOC:
  484. LOG_ERROR("unsupported XSDRTDOC");
  485. unsupported = 1;
  486. break;
  487. case XSDRTDOE:
  488. LOG_ERROR("unsupported XSDRTDOE");
  489. unsupported = 1;
  490. break;
  491. case XSTATE:
  492. {
  493. tap_state_t mystate;
  494. if (read(xsvf_fd, &uc, 1) < 0) {
  495. do_abort = 1;
  496. break;
  497. }
  498. mystate = xsvf_to_tap(uc);
  499. LOG_DEBUG("XSTATE 0x%02X %s", uc, tap_state_name(mystate));
  500. if (mystate == TAP_INVALID) {
  501. LOG_ERROR("XSVF: bad XSTATE %02x", uc);
  502. do_abort = 1;
  503. break;
  504. }
  505. /* NOTE: the current state is SVF-stable! */
  506. /* no change == NOP */
  507. if (mystate == cmd_queue_cur_state
  508. && mystate != TAP_RESET)
  509. break;
  510. /* Hand off to SVF? */
  511. if (svf_tap_state_is_stable(mystate)) {
  512. result = svf_add_statemove(mystate);
  513. if (result != ERROR_OK)
  514. unsupported = 1;
  515. break;
  516. }
  517. /*
  518. * A sequence of XSTATE transitions, each TAP
  519. * state adjacent to the previous one. Start
  520. * collecting them.
  521. */
  522. collecting_path = true;
  523. pathlen = 1;
  524. path[0] = mystate;
  525. }
  526. break;
  527. case XENDIR:
  528. if (read(xsvf_fd, &uc, 1) < 0) {
  529. do_abort = 1;
  530. break;
  531. }
  532. /* see page 22 of XSVF spec */
  533. if (uc == 0)
  534. xendir = TAP_IDLE;
  535. else if (uc == 1)
  536. xendir = TAP_IRPAUSE;
  537. else {
  538. LOG_ERROR("illegial XENDIR argument: 0x%02X", uc);
  539. unsupported = 1;
  540. break;
  541. }
  542. LOG_DEBUG("XENDIR 0x%02X %s", uc, tap_state_name(xendir));
  543. break;
  544. case XENDDR:
  545. if (read(xsvf_fd, &uc, 1) < 0) {
  546. do_abort = 1;
  547. break;
  548. }
  549. /* see page 22 of XSVF spec */
  550. if (uc == 0)
  551. xenddr = TAP_IDLE;
  552. else if (uc == 1)
  553. xenddr = TAP_DRPAUSE;
  554. else {
  555. LOG_ERROR("illegial XENDDR argument: 0x%02X", uc);
  556. unsupported = 1;
  557. break;
  558. }
  559. LOG_DEBUG("XENDDR %02X %s", uc, tap_state_name(xenddr));
  560. break;
  561. case XSIR:
  562. case XSIR2:
  563. {
  564. uint8_t short_buf[2];
  565. uint8_t *ir_buf;
  566. int bitcount;
  567. tap_state_t my_end_state = xruntest ? TAP_IDLE : xendir;
  568. if (opcode == XSIR) {
  569. /* one byte bitcount */
  570. if (read(xsvf_fd, short_buf, 1) < 0) {
  571. do_abort = 1;
  572. break;
  573. }
  574. bitcount = short_buf[0];
  575. LOG_DEBUG("XSIR %d", bitcount);
  576. } else {
  577. if (read(xsvf_fd, short_buf, 2) < 0) {
  578. do_abort = 1;
  579. break;
  580. }
  581. bitcount = be_to_h_u16(short_buf);
  582. LOG_DEBUG("XSIR2 %d", bitcount);
  583. }
  584. ir_buf = malloc((bitcount + 7) / 8);
  585. if (xsvf_read_buffer(bitcount, xsvf_fd, ir_buf) != ERROR_OK)
  586. do_abort = 1;
  587. else {
  588. struct scan_field field;
  589. field.num_bits = bitcount;
  590. field.out_value = ir_buf;
  591. field.in_value = NULL;
  592. if (tap == NULL)
  593. jtag_add_plain_ir_scan(field.num_bits,
  594. field.out_value, field.in_value, my_end_state);
  595. else
  596. jtag_add_ir_scan(tap, &field, my_end_state);
  597. if (xruntest) {
  598. if (runtest_requires_tck)
  599. jtag_add_clocks(xruntest);
  600. else
  601. jtag_add_sleep(xruntest);
  602. }
  603. /* Note that an -irmask of non-zero in your config file
  604. * can cause this to fail. Setting -irmask to zero cand work
  605. * around the problem.
  606. */
  607. /* LOG_DEBUG("FLUSHING QUEUE"); */
  608. result = jtag_execute_queue();
  609. if (result != ERROR_OK)
  610. tdo_mismatch = 1;
  611. }
  612. free(ir_buf);
  613. }
  614. break;
  615. case XCOMMENT:
  616. {
  617. unsigned int ndx = 0;
  618. char comment[128];
  619. do {
  620. if (read(xsvf_fd, &uc, 1) < 0) {
  621. do_abort = 1;
  622. break;
  623. }
  624. if (ndx < sizeof(comment)-1)
  625. comment[ndx++] = uc;
  626. } while (uc != 0);
  627. comment[sizeof(comment)-1] = 0; /* regardless, terminate */
  628. if (verbose)
  629. LOG_USER("# %s", comment);
  630. }
  631. break;
  632. case XWAIT:
  633. {
  634. /* expected in stream:
  635. XWAIT <uint8_t wait_state> <uint8_t end_state> <uint32_t usecs>
  636. */
  637. uint8_t wait_local;
  638. uint8_t end;
  639. uint8_t delay_buf[4];
  640. tap_state_t wait_state;
  641. tap_state_t end_state;
  642. int delay;
  643. if (read(xsvf_fd, &wait_local, 1) < 0
  644. || read(xsvf_fd, &end, 1) < 0
  645. || read(xsvf_fd, delay_buf, 4) < 0) {
  646. do_abort = 1;
  647. break;
  648. }
  649. wait_state = xsvf_to_tap(wait_local);
  650. end_state = xsvf_to_tap(end);
  651. delay = be_to_h_u32(delay_buf);
  652. LOG_DEBUG("XWAIT %s %s usecs:%d", tap_state_name(
  653. wait_state), tap_state_name(end_state), delay);
  654. if (runtest_requires_tck && wait_state == TAP_IDLE)
  655. jtag_add_runtest(delay, end_state);
  656. else {
  657. /* FIXME handle statemove errors ... */
  658. result = svf_add_statemove(wait_state);
  659. if (result != ERROR_OK)
  660. return result;
  661. jtag_add_sleep(delay);
  662. result = svf_add_statemove(end_state);
  663. if (result != ERROR_OK)
  664. return result;
  665. }
  666. }
  667. break;
  668. case XWAITSTATE:
  669. {
  670. /* expected in stream:
  671. * XWAITSTATE <uint8_t wait_state> <uint8_t end_state> <uint32_t clock_count>
  672. * <uint32_t usecs>
  673. */
  674. uint8_t clock_buf[4];
  675. uint8_t usecs_buf[4];
  676. uint8_t wait_local;
  677. uint8_t end;
  678. tap_state_t wait_state;
  679. tap_state_t end_state;
  680. int clock_count;
  681. int usecs;
  682. if (read(xsvf_fd, &wait_local, 1) < 0
  683. || read(xsvf_fd, &end, 1) < 0
  684. || read(xsvf_fd, clock_buf, 4) < 0
  685. || read(xsvf_fd, usecs_buf, 4) < 0) {
  686. do_abort = 1;
  687. break;
  688. }
  689. wait_state = xsvf_to_tap(wait_local);
  690. end_state = xsvf_to_tap(end);
  691. clock_count = be_to_h_u32(clock_buf);
  692. usecs = be_to_h_u32(usecs_buf);
  693. LOG_DEBUG("XWAITSTATE %s %s clocks:%i usecs:%i",
  694. tap_state_name(wait_state),
  695. tap_state_name(end_state),
  696. clock_count, usecs);
  697. /* the following states are 'stable', meaning that they have a transition
  698. * in the state diagram back to themselves. This is necessary because we will
  699. * be issuing a number of clocks in this state. This set of allowed states is also
  700. * determined by the SVF RUNTEST command's allowed states.
  701. */
  702. if (!svf_tap_state_is_stable(wait_state)) {
  703. LOG_ERROR("illegal XWAITSTATE wait_state: \"%s\"",
  704. tap_state_name(wait_state));
  705. unsupported = 1;
  706. /* REVISIT "break" so we won't run? */
  707. }
  708. /* FIXME handle statemove errors ... */
  709. result = svf_add_statemove(wait_state);
  710. if (result != ERROR_OK)
  711. return result;
  712. jtag_add_clocks(clock_count);
  713. jtag_add_sleep(usecs);
  714. result = svf_add_statemove(end_state);
  715. if (result != ERROR_OK)
  716. return result;
  717. }
  718. break;
  719. case LCOUNT:
  720. {
  721. /* expected in stream:
  722. * LCOUNT <uint32_t loop_count>
  723. */
  724. uint8_t count_buf[4];
  725. if (read(xsvf_fd, count_buf, 4) < 0) {
  726. do_abort = 1;
  727. break;
  728. }
  729. loop_count = be_to_h_u32(count_buf);
  730. LOG_DEBUG("LCOUNT %d", loop_count);
  731. }
  732. break;
  733. case LDELAY:
  734. {
  735. /* expected in stream:
  736. * LDELAY <uint8_t wait_state> <uint32_t clock_count> <uint32_t usecs_to_sleep>
  737. */
  738. uint8_t state;
  739. uint8_t clock_buf[4];
  740. uint8_t usecs_buf[4];
  741. if (read(xsvf_fd, &state, 1) < 0
  742. || read(xsvf_fd, clock_buf, 4) < 0
  743. || read(xsvf_fd, usecs_buf, 4) < 0) {
  744. do_abort = 1;
  745. break;
  746. }
  747. /* NOTE: loop_state must be stable! */
  748. loop_state = xsvf_to_tap(state);
  749. loop_clocks = be_to_h_u32(clock_buf);
  750. loop_usecs = be_to_h_u32(usecs_buf);
  751. LOG_DEBUG("LDELAY %s clocks:%d usecs:%d", tap_state_name(
  752. loop_state), loop_clocks, loop_usecs);
  753. }
  754. break;
  755. /* LSDR is more like XSDRTDO than it is like XSDR. It uses LDELAY which
  756. * comes with clocks !AND! sleep requirements.
  757. */
  758. case LSDR:
  759. {
  760. int limit = loop_count;
  761. int matched = 0;
  762. int attempt;
  763. LOG_DEBUG("LSDR");
  764. if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK
  765. || xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_buf) != ERROR_OK) {
  766. do_abort = 1;
  767. break;
  768. }
  769. if (limit < 1)
  770. limit = 1;
  771. for (attempt = 0; attempt < limit; ++attempt) {
  772. struct scan_field field;
  773. result = svf_add_statemove(loop_state);
  774. if (result != ERROR_OK)
  775. return result;
  776. jtag_add_clocks(loop_clocks);
  777. jtag_add_sleep(loop_usecs);
  778. field.num_bits = xsdrsize;
  779. field.out_value = dr_out_buf;
  780. field.in_value = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
  781. if (attempt > 0 && verbose)
  782. LOG_USER("LSDR retry %d", attempt);
  783. if (tap == NULL)
  784. jtag_add_plain_dr_scan(field.num_bits,
  785. field.out_value,
  786. field.in_value,
  787. TAP_DRPAUSE);
  788. else
  789. jtag_add_dr_scan(tap, 1, &field, TAP_DRPAUSE);
  790. jtag_check_value_mask(&field, dr_in_buf, dr_in_mask);
  791. free(field.in_value);
  792. /* LOG_DEBUG("FLUSHING QUEUE"); */
  793. result = jtag_execute_queue();
  794. if (result == ERROR_OK) {
  795. matched = 1;
  796. break;
  797. }
  798. }
  799. if (!matched) {
  800. LOG_USER("LSDR mismatch");
  801. tdo_mismatch = 1;
  802. break;
  803. }
  804. }
  805. break;
  806. case XTRST:
  807. {
  808. uint8_t trst_mode;
  809. if (read(xsvf_fd, &trst_mode, 1) < 0) {
  810. do_abort = 1;
  811. break;
  812. }
  813. switch (trst_mode) {
  814. case XTRST_ON:
  815. jtag_add_reset(1, 0);
  816. break;
  817. case XTRST_OFF:
  818. case XTRST_Z:
  819. jtag_add_reset(0, 0);
  820. break;
  821. case XTRST_ABSENT:
  822. break;
  823. default:
  824. LOG_ERROR("XTRST mode argument (0x%02X) out of range", trst_mode);
  825. do_abort = 1;
  826. }
  827. }
  828. break;
  829. default:
  830. LOG_ERROR("unknown xsvf command (0x%02X)", uc);
  831. unsupported = 1;
  832. }
  833. if (do_abort || unsupported || tdo_mismatch) {
  834. LOG_DEBUG("xsvf failed, setting taps to reasonable state");
  835. /* upon error, return the TAPs to a reasonable state */
  836. result = svf_add_statemove(TAP_IDLE);
  837. if (result != ERROR_OK)
  838. return result;
  839. result = jtag_execute_queue();
  840. if (result != ERROR_OK)
  841. return result;
  842. break;
  843. }
  844. }
  845. if (tdo_mismatch) {
  846. command_print(CMD_CTX,
  847. "TDO mismatch, somewhere near offset %lu in xsvf file, aborting",
  848. file_offset);
  849. return ERROR_FAIL;
  850. }
  851. if (unsupported) {
  852. off_t offset = lseek(xsvf_fd, 0, SEEK_CUR) - 1;
  853. command_print(CMD_CTX,
  854. "unsupported xsvf command (0x%02X) at offset %jd, aborting",
  855. uc, (intmax_t)offset);
  856. return ERROR_FAIL;
  857. }
  858. if (do_abort) {
  859. command_print(CMD_CTX, "premature end of xsvf file detected, aborting");
  860. return ERROR_FAIL;
  861. }
  862. if (dr_out_buf)
  863. free(dr_out_buf);
  864. if (dr_in_buf)
  865. free(dr_in_buf);
  866. if (dr_in_mask)
  867. free(dr_in_mask);
  868. close(xsvf_fd);
  869. command_print(CMD_CTX, "XSVF file programmed successfully");
  870. return ERROR_OK;
  871. }
  872. static const struct command_registration xsvf_command_handlers[] = {
  873. {
  874. .name = "xsvf",
  875. .handler = handle_xsvf_command,
  876. .mode = COMMAND_EXEC,
  877. .help = "Runs a XSVF file. If 'virt2' is given, xruntest "
  878. "counts are interpreted as TCK cycles rather than "
  879. "as microseconds. Without the 'quiet' option, all "
  880. "comments, retries, and mismatches will be reported.",
  881. .usage = "(tapname|'plain') filename ['virt2'] ['quiet']",
  882. },
  883. COMMAND_REGISTRATION_DONE
  884. };
  885. int xsvf_register_commands(struct command_context *cmd_ctx)
  886. {
  887. return register_commands(cmd_ctx, NULL, xsvf_command_handlers);
  888. }
  889. /*
  890. PSUEDO-Code from Xilinx Appnote XAPP067.pdf :
  891. the following pseudo code clarifies the intent of the xrepeat support.The
  892. flow given is for the entire processing of an SVF file, not an XSVF file.
  893. No idea if this is just for the XC9500/XL/XV devices or all Xilinx parts.
  894. "Pseudo-Code Algorithm for SVF-Based ISP"
  895. 1. Go to Test-Logic-Reset state
  896. 2. Go to Run-Test Idle state
  897. 3. Read SVF record
  898. 4. if SIR record then
  899. go to Shift-IR state
  900. Scan in <TDI value>
  901. 5. else if SDR record then
  902. set <repeat count> to 0
  903. store <TDI value> as <current TDI value>
  904. store <TDO value> as <current TDO value>
  905. 6. go to Shift-DR state
  906. scan in <current TDI value>
  907. if < current TDO value > is specified then
  908. if < current TDO value > does not equal <actual TDO value> then
  909. if < repeat count > > 32 then
  910. LOG ERROR
  911. go to Run-Test Idle state
  912. go to Step 3
  913. end if
  914. go to Pause-DR
  915. go to Exit2-DR
  916. go to Shift-DR
  917. go to Exit1-DR
  918. go to Update-DR
  919. go to Run-Test/Idle
  920. increment <repeat count> by 1
  921. pause <current pause time> microseconds
  922. go to Step 6)
  923. end if
  924. else
  925. go to Run-Test Idle state
  926. go to Step 3
  927. endif
  928. else if RUNTEST record then
  929. pause tester for < TCK value > microseconds
  930. store <TCK value> as <current pause time>
  931. end if
  932. */