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.
 
 
 
 
 
 

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