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.
 
 
 
 
 
 

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