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.
 
 
 
 
 
 

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