Preliminary support for Freescale i.MX53
[openocd/ellerodev.git] / src / svf / svf.c
blob64be127013d6d623940abe54244ec4dc3b78499c
1 /*
2 * Copyright (C) 2009 by Simon Qian
3 * SimonQian@SimonQian.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* The specification for SVF is available here:
22 * http://www.asset-intertech.com/support/svf.pdf
23 * Below, this document is refered to as the "SVF spec".
25 * The specification for XSVF is available here:
26 * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
27 * Below, this document is refered to as the "XSVF spec".
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include <jtag/jtag.h>
35 #include "svf.h"
36 #include <helper/time_support.h>
39 // SVF command
40 typedef enum
42 ENDDR,
43 ENDIR,
44 FREQUENCY,
45 HDR,
46 HIR,
47 PIO,
48 PIOMAP,
49 RUNTEST,
50 SDR,
51 SIR,
52 STATE,
53 TDR,
54 TIR,
55 TRST,
56 }svf_command_t;
58 static const char *svf_command_name[14] =
60 "ENDDR",
61 "ENDIR",
62 "FREQUENCY",
63 "HDR",
64 "HIR",
65 "PIO",
66 "PIOMAP",
67 "RUNTEST",
68 "SDR",
69 "SIR",
70 "STATE",
71 "TDR",
72 "TIR",
73 "TRST"
76 typedef enum
78 TRST_ON,
79 TRST_OFF,
80 TRST_Z,
81 TRST_ABSENT
82 }trst_mode_t;
84 static const char *svf_trst_mode_name[4] =
86 "ON",
87 "OFF",
88 "Z",
89 "ABSENT"
92 struct svf_statemove
94 tap_state_t from;
95 tap_state_t to;
96 uint32_t num_of_moves;
97 tap_state_t paths[8];
101 * These paths are from the SVF specification for the STATE command, to be
102 * used when the STATE command only includes the final state. The first
103 * element of the path is the "from" (current) state, and the last one is
104 * the "to" (target) state.
106 * All specified paths are the shortest ones in the JTAG spec, and are thus
107 * not (!!) exact matches for the paths used elsewhere in OpenOCD. Note
108 * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
109 * which has specific effects on the various registers; they are not NOPs.
111 * Paths to RESET are disabled here. As elsewhere in OpenOCD, and in XSVF
112 * and many SVF implementations, we don't want to risk missing that state.
113 * To get to RESET, always we ignore the current state.
115 static const struct svf_statemove svf_statemoves[] =
117 // from to num_of_moves, paths[8]
118 // {TAP_RESET, TAP_RESET, 1, {TAP_RESET}},
119 {TAP_RESET, TAP_IDLE, 2, {TAP_RESET, TAP_IDLE}},
120 {TAP_RESET, TAP_DRPAUSE, 6, {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
121 {TAP_RESET, TAP_IRPAUSE, 7, {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
123 // {TAP_IDLE, TAP_RESET, 4, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
124 {TAP_IDLE, TAP_IDLE, 1, {TAP_IDLE}},
125 {TAP_IDLE, TAP_DRPAUSE, 5, {TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
126 {TAP_IDLE, TAP_IRPAUSE, 6, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
128 // {TAP_DRPAUSE, TAP_RESET, 6, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
129 {TAP_DRPAUSE, TAP_IDLE, 4, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE}},
130 {TAP_DRPAUSE, TAP_DRPAUSE, 7, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
131 {TAP_DRPAUSE, TAP_IRPAUSE, 8, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
133 // {TAP_IRPAUSE, TAP_RESET, 6, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
134 {TAP_IRPAUSE, TAP_IDLE, 4, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_IDLE}},
135 {TAP_IRPAUSE, TAP_DRPAUSE, 7, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
136 {TAP_IRPAUSE, TAP_IRPAUSE, 8, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}}
140 #define XXR_TDI (1 << 0)
141 #define XXR_TDO (1 << 1)
142 #define XXR_MASK (1 << 2)
143 #define XXR_SMASK (1 << 3)
144 struct svf_xxr_para
146 int len;
147 int data_mask;
148 uint8_t *tdi;
149 uint8_t *tdo;
150 uint8_t *mask;
151 uint8_t *smask;
154 struct svf_para
156 float frequency;
157 tap_state_t ir_end_state;
158 tap_state_t dr_end_state;
159 tap_state_t runtest_run_state;
160 tap_state_t runtest_end_state;
161 trst_mode_t trst_mode;
163 struct svf_xxr_para hir_para;
164 struct svf_xxr_para hdr_para;
165 struct svf_xxr_para tir_para;
166 struct svf_xxr_para tdr_para;
167 struct svf_xxr_para sir_para;
168 struct svf_xxr_para sdr_para;
171 static struct svf_para svf_para;
172 static const struct svf_para svf_para_init =
174 // frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode
175 0, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TRST_Z,
176 // hir_para
177 // {len, data_mask, tdi, tdo, mask, smask},
178 {0, 0, NULL, NULL, NULL, NULL},
179 // hdr_para
180 // {len, data_mask, tdi, tdo, mask, smask},
181 {0, 0, NULL, NULL, NULL, NULL},
182 // tir_para
183 // {len, data_mask, tdi, tdo, mask, smask},
184 {0, 0, NULL, NULL, NULL, NULL},
185 // tdr_para
186 // {len, data_mask, tdi, tdo, mask, smask},
187 {0, 0, NULL, NULL, NULL, NULL},
188 // sir_para
189 // {len, data_mask, tdi, tdo, mask, smask},
190 {0, 0, NULL, NULL, NULL, NULL},
191 // sdr_para
192 // {len, data_mask, tdi, tdo, mask, smask},
193 {0, 0, NULL, NULL, NULL, NULL},
196 struct svf_check_tdo_para
198 int line_num; // used to record line number of the check operation
199 // so more information could be printed
200 int enabled; // check is enabled or not
201 int buffer_offset; // buffer_offset to buffers
202 int bit_len; // bit length to check
205 #define SVF_CHECK_TDO_PARA_SIZE 1024
206 static struct svf_check_tdo_para *svf_check_tdo_para = NULL;
207 static int svf_check_tdo_para_index = 0;
209 static int svf_read_command_from_file(FILE * fd);
210 static int svf_check_tdo(void);
211 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
212 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
214 static FILE * svf_fd = NULL;
215 static char * svf_read_line = NULL;
216 static size_t svf_read_line_size = 0;
217 static char *svf_command_buffer = NULL;
218 static size_t svf_command_buffer_size = 0;
219 static int svf_line_number = 1;
220 static int svf_getline (char **lineptr, size_t *n, FILE *stream);
222 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (1024 * 1024)
223 static uint8_t *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
224 static int svf_buffer_index = 0, svf_buffer_size = 0;
225 static int svf_quiet = 0;
226 static int svf_nil = 0;
228 // Targetting particular tap
229 static int svf_tap_is_specified = 0;
230 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi);
232 // Progress Indicator
233 static int svf_progress_enabled = 0;
234 static long svf_total_lines = 0;
235 static int svf_percentage = 0;
236 static int svf_last_printed_percentage = -1;
238 static void svf_free_xxd_para(struct svf_xxr_para *para)
240 if (NULL != para)
242 if (para->tdi != NULL)
244 free(para->tdi);
245 para->tdi = NULL;
247 if (para->tdo != NULL)
249 free(para->tdo);
250 para->tdo = NULL;
252 if (para->mask != NULL)
254 free(para->mask);
255 para->mask = NULL;
257 if (para->smask != NULL)
259 free(para->smask);
260 para->smask = NULL;
265 static unsigned svf_get_mask_u32(int bitlen)
267 uint32_t bitmask;
269 if (bitlen < 0)
271 bitmask = 0;
273 else if (bitlen >= 32)
275 bitmask = 0xFFFFFFFF;
277 else
279 bitmask = (1 << bitlen) - 1;
282 return bitmask;
285 int svf_add_statemove(tap_state_t state_to)
287 tap_state_t state_from = cmd_queue_cur_state;
288 unsigned index_var;
290 /* when resetting, be paranoid and ignore current state */
291 if (state_to == TAP_RESET) {
292 if (svf_nil)
293 return ERROR_OK;
295 jtag_add_tlr();
296 return ERROR_OK;
299 for (index_var = 0; index_var < ARRAY_SIZE(svf_statemoves); index_var++)
301 if ((svf_statemoves[index_var].from == state_from)
302 && (svf_statemoves[index_var].to == state_to))
304 if (svf_nil)
306 continue;
308 /* recorded path includes current state ... avoid extra TCKs! */
309 if (svf_statemoves[index_var].num_of_moves > 1)
310 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves - 1,
311 svf_statemoves[index_var].paths + 1);
312 else
313 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
314 svf_statemoves[index_var].paths);
315 return ERROR_OK;
318 LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
319 return ERROR_FAIL;
322 COMMAND_HANDLER(handle_svf_command)
324 #define SVF_MIN_NUM_OF_OPTIONS 1
325 #define SVF_MAX_NUM_OF_OPTIONS 5
326 int command_num = 0;
327 int ret = ERROR_OK;
328 long long time_measure_ms;
329 int time_measure_s, time_measure_m;
331 /* use NULL to indicate a "plain" svf file which accounts for
332 any additional devices in the scan chain, otherwise the device
333 that should be affected
335 struct jtag_tap *tap = NULL;
337 if ((CMD_ARGC < SVF_MIN_NUM_OF_OPTIONS) || (CMD_ARGC > SVF_MAX_NUM_OF_OPTIONS))
339 return ERROR_COMMAND_SYNTAX_ERROR;
342 // parse command line
343 svf_quiet = 0;
344 svf_nil = 0;
345 for (unsigned int i = 0; i < CMD_ARGC; i++)
347 if (strcmp(CMD_ARGV[i], "-tap") == 0)
349 tap = jtag_tap_by_string(CMD_ARGV[i+1]);
350 if (!tap)
352 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i+1]);
353 return ERROR_FAIL;
355 i++;
357 else if ((strcmp(CMD_ARGV[i], "quiet") == 0) || (strcmp(CMD_ARGV[i], "-quiet") == 0))
359 svf_quiet = 1;
361 else if ((strcmp(CMD_ARGV[i], "nil") == 0) || (strcmp(CMD_ARGV[i], "-nil") == 0))
363 svf_nil = 1;
365 else if ((strcmp(CMD_ARGV[i], "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
367 svf_progress_enabled = 1;
369 else if ((svf_fd = fopen(CMD_ARGV[i], "r")) == NULL)
371 int err = errno;
372 command_print(CMD_CTX, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
373 // no need to free anything now
374 return ERROR_COMMAND_SYNTAX_ERROR;
376 else
378 LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
382 if (svf_fd == NULL)
384 return ERROR_COMMAND_SYNTAX_ERROR;
387 // get time
388 time_measure_ms = timeval_ms();
390 // init
391 svf_line_number = 1;
392 svf_command_buffer_size = 0;
394 svf_check_tdo_para_index = 0;
395 svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
396 if (NULL == svf_check_tdo_para)
398 LOG_ERROR("not enough memory");
399 ret = ERROR_FAIL;
400 goto free_all;
403 svf_buffer_index = 0;
404 // double the buffer size
405 // in case current command cannot be committed, and next command is a bit scan command
406 // here is 32K bits for this big scan command, it should be enough
407 // buffer will be reallocated if buffer size is not enough
408 svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
409 if (NULL == svf_tdi_buffer)
411 LOG_ERROR("not enough memory");
412 ret = ERROR_FAIL;
413 goto free_all;
415 svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
416 if (NULL == svf_tdo_buffer)
418 LOG_ERROR("not enough memory");
419 ret = ERROR_FAIL;
420 goto free_all;
422 svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
423 if (NULL == svf_mask_buffer)
425 LOG_ERROR("not enough memory");
426 ret = ERROR_FAIL;
427 goto free_all;
429 svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
431 memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
433 if (!svf_nil)
435 // TAP_RESET
436 jtag_add_tlr();
439 if (tap)
441 /* Tap is specified, set header/trailer paddings */
442 int header_ir_len = 0, header_dr_len = 0, trailer_ir_len = 0, trailer_dr_len = 0;
443 struct jtag_tap *check_tap;
445 svf_tap_is_specified = 1;
447 for (check_tap = jtag_all_taps(); check_tap; check_tap = check_tap->next_tap) {
448 if (check_tap->abs_chain_position < tap->abs_chain_position)
450 //Header
451 header_ir_len += check_tap->ir_length;
452 header_dr_len ++;
454 else if (check_tap->abs_chain_position > tap->abs_chain_position)
456 //Trailer
457 trailer_ir_len += check_tap->ir_length;
458 trailer_dr_len ++;
462 // HDR %d TDI (0)
463 if (ERROR_OK != svf_set_padding(&svf_para.hdr_para, header_dr_len, 0))
465 LOG_ERROR("failed to set data header");
466 return ERROR_FAIL;
469 // HIR %d TDI (0xFF)
470 if (ERROR_OK != svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF))
472 LOG_ERROR("failed to set instruction header");
473 return ERROR_FAIL;
476 // TDR %d TDI (0)
477 if (ERROR_OK != svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0))
479 LOG_ERROR("failed to set data trailer");
480 return ERROR_FAIL;
483 // TIR %d TDI (0xFF)
484 if (ERROR_OK != svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF))
486 LOG_ERROR("failed to set instruction trailer");
487 return ERROR_FAIL;
492 if (svf_progress_enabled)
494 // Count total lines in file.
495 while ( ! feof (svf_fd) )
497 svf_getline (&svf_command_buffer, &svf_command_buffer_size, svf_fd);
498 svf_total_lines++;
500 rewind(svf_fd);
502 while (ERROR_OK == svf_read_command_from_file(svf_fd))
504 // Log Output
505 if (svf_quiet)
507 if (svf_progress_enabled)
509 svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
510 if (svf_last_printed_percentage != svf_percentage)
512 LOG_USER_N("\r%d%% ", svf_percentage);
513 svf_last_printed_percentage = svf_percentage;
517 else
519 if (svf_progress_enabled)
521 svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
522 LOG_USER_N("%3d%% %s", svf_percentage, svf_read_line);
524 else
526 LOG_USER_N("%s",svf_read_line);
529 // Run Command
530 if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer))
532 LOG_ERROR("fail to run command at line %d", svf_line_number);
533 ret = ERROR_FAIL;
534 break;
536 command_num++;
539 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
541 ret = ERROR_FAIL;
543 else if (ERROR_OK != svf_check_tdo())
545 ret = ERROR_FAIL;
548 // print time
549 time_measure_ms = timeval_ms() - time_measure_ms;
550 time_measure_s = time_measure_ms / 1000;
551 time_measure_ms %= 1000;
552 time_measure_m = time_measure_s / 60;
553 time_measure_s %= 60;
554 if (time_measure_ms < 1000)
556 command_print(CMD_CTX, "\r\nTime used: %dm%ds%lldms ", time_measure_m, time_measure_s, time_measure_ms);
559 free_all:
561 fclose(svf_fd);
562 svf_fd = 0;
564 // free buffers
565 if (svf_command_buffer)
567 free(svf_command_buffer);
568 svf_command_buffer = NULL;
569 svf_command_buffer_size = 0;
571 if (svf_check_tdo_para)
573 free(svf_check_tdo_para);
574 svf_check_tdo_para = NULL;
575 svf_check_tdo_para_index = 0;
577 if (svf_tdi_buffer)
579 free(svf_tdi_buffer);
580 svf_tdi_buffer = NULL;
582 if (svf_tdo_buffer)
584 free(svf_tdo_buffer);
585 svf_tdo_buffer = NULL;
587 if (svf_mask_buffer)
589 free(svf_mask_buffer);
590 svf_mask_buffer = NULL;
592 svf_buffer_index = 0;
593 svf_buffer_size = 0;
595 svf_free_xxd_para(&svf_para.hdr_para);
596 svf_free_xxd_para(&svf_para.hir_para);
597 svf_free_xxd_para(&svf_para.tdr_para);
598 svf_free_xxd_para(&svf_para.tir_para);
599 svf_free_xxd_para(&svf_para.sdr_para);
600 svf_free_xxd_para(&svf_para.sir_para);
602 if (ERROR_OK == ret)
604 command_print(CMD_CTX, "svf file programmed successfully for %d commands", command_num);
606 else
608 command_print(CMD_CTX, "svf file programmed failed");
611 return ret;
614 static int svf_getline (char **lineptr, size_t *n, FILE *stream)
616 #define MIN_CHUNK 16 //Buffer is increased by this size each time as required
617 size_t i = 0;
619 if (*lineptr == NULL)
621 *n = MIN_CHUNK;
622 *lineptr = (char *)malloc (*n);
623 if (!*lineptr)
625 return -1;
629 (*lineptr)[0] = fgetc(stream);
630 while ((*lineptr)[i] != '\n')
632 (*lineptr)[++i] = fgetc(stream);
633 if (feof(stream))
635 (*lineptr)[0] = 0;
636 return -1;
638 if ((i + 2) > *n)
640 *n += MIN_CHUNK;
641 *lineptr = realloc(*lineptr, *n);
645 (*lineptr)[++i] = 0;
647 return sizeof(*lineptr);
650 #define SVFP_CMD_INC_CNT 1024
651 static int svf_read_command_from_file(FILE * fd)
653 unsigned char ch;
654 int i = 0;
655 size_t cmd_pos = 0;
656 int cmd_ok = 0, slash = 0, comment = 0;
658 if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
660 return ERROR_FAIL;
662 svf_line_number++;
663 ch = svf_read_line[0];
664 while (!cmd_ok && (ch != 0))
666 switch (ch)
668 case '!':
669 slash = 0;
670 if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
672 return ERROR_FAIL;
674 svf_line_number++;
675 i = -1;
676 break;
677 case '/':
678 if (++slash == 2)
680 slash = 0;
681 if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
683 return ERROR_FAIL;
685 svf_line_number++;
686 i = -1;
688 break;
689 case ';':
690 slash = 0;
691 cmd_ok = 1;
692 break;
693 case '\n':
694 svf_line_number++;
695 if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
697 return ERROR_FAIL;
699 i = -1;
700 case '\r':
701 slash = 0;
702 comment = 0;
703 /* Don't save '\r' and '\n' if no data is parsed */
704 if (!cmd_pos)
705 break;
706 default:
707 /* The parsing code currently expects a space
708 * before parentheses -- "TDI (123)". Also a
709 * space afterwards -- "TDI (123) TDO(456)".
710 * But such spaces are optional... instead of
711 * parser updates, cope with that by adding the
712 * spaces as needed.
714 * Ensure there are 3 bytes available, for:
715 * - current character
716 * - added space.
717 * - terminating NUL ('\0')
719 if ((cmd_pos + 2) >= svf_command_buffer_size)
721 svf_command_buffer = realloc(svf_command_buffer, (cmd_pos + 2));
722 if (svf_command_buffer == NULL)
724 LOG_ERROR("not enough memory");
725 return ERROR_FAIL;
729 /* insert a space before '(' */
730 if ('(' == ch)
731 svf_command_buffer[cmd_pos++] = ' ';
733 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
735 /* insert a space after ')' */
736 if (')' == ch)
737 svf_command_buffer[cmd_pos++] = ' ';
738 break;
740 ch = svf_read_line[++i];
743 if (cmd_ok)
745 svf_command_buffer[cmd_pos] = '\0';
746 return ERROR_OK;
748 else
750 return ERROR_FAIL;
754 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
756 int pos = 0, num = 0, space_found = 1, in_bracket = 0;
758 while (pos < len)
760 switch (str[pos])
762 case '!':
763 case '/':
764 LOG_ERROR("fail to parse svf command");
765 return ERROR_FAIL;
766 case '(':
767 in_bracket = 1;
768 goto parse_char;
769 case ')':
770 in_bracket = 0;
771 goto parse_char;
772 default:
773 parse_char:
774 if (!in_bracket && isspace((int) str[pos]))
776 space_found = 1;
777 str[pos] = '\0';
779 else if (space_found)
781 argus[num++] = &str[pos];
782 space_found = 0;
784 break;
786 pos++;
789 *num_of_argu = num;
791 return ERROR_OK;
794 bool svf_tap_state_is_stable(tap_state_t state)
796 return (TAP_RESET == state) || (TAP_IDLE == state)
797 || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
800 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
802 int i;
804 for (i = 0; i < num_of_element; i++)
806 if (!strcmp(str, strs[i]))
808 return i;
811 return 0xFF;
814 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
816 int new_byte_len = (new_bit_len + 7) >> 3;
818 if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
820 if (*arr != NULL)
822 free(*arr);
823 *arr = NULL;
825 *arr = (uint8_t*)malloc(new_byte_len);
826 if (NULL == *arr)
828 LOG_ERROR("not enough memory");
829 return ERROR_FAIL;
831 memset(*arr, 0, new_byte_len);
833 return ERROR_OK;
836 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
838 int error = ERROR_OK;
839 error |= svf_adjust_array_length(&para->tdi, para->len, len);
840 memset(para->tdi, tdi, (len + 7) >> 3);
841 error |= svf_adjust_array_length(&para->tdo, para->len, len);
842 error |= svf_adjust_array_length(&para->mask, para->len, len);
843 para->len = len;
844 para->data_mask = XXR_TDI;
846 return error;
849 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
851 int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
852 uint8_t ch = 0;
854 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
856 LOG_ERROR("fail to adjust length of array");
857 return ERROR_FAIL;
860 /* fill from LSB (end of str) to MSB (beginning of str) */
861 for (i = 0; i < str_hbyte_len; i++)
863 ch = 0;
864 while (str_len > 0)
866 ch = str[--str_len];
868 /* Skip whitespace. The SVF specification (rev E) is
869 * deficient in terms of basic lexical issues like
870 * where whitespace is allowed. Long bitstrings may
871 * require line ends for correctness, since there is
872 * a hard limit on line length.
874 if (!isspace(ch))
876 if ((ch >= '0') && (ch <= '9'))
878 ch = ch - '0';
879 break;
881 else if ((ch >= 'A') && (ch <= 'F'))
883 ch = ch - 'A' + 10;
884 break;
886 else
888 LOG_ERROR("invalid hex string");
889 return ERROR_FAIL;
893 ch = 0;
896 // write bin
897 if (i % 2)
899 // MSB
900 (*bin)[i / 2] |= ch << 4;
902 else
904 // LSB
905 (*bin)[i / 2] = 0;
906 (*bin)[i / 2] |= ch;
910 /* consume optional leading '0' MSBs or whitespace */
911 while (str_len > 0 && ((str[str_len - 1] == '0')
912 || isspace((int) str[str_len - 1])))
913 str_len--;
915 /* check validity: we must have consumed everything */
916 if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0)
918 LOG_ERROR("value execeeds length");
919 return ERROR_FAIL;
922 return ERROR_OK;
925 static int svf_check_tdo(void)
927 int i, len, index_var;
929 for (i = 0; i < svf_check_tdo_para_index; i++)
931 index_var = svf_check_tdo_para[i].buffer_offset;
932 len = svf_check_tdo_para[i].bit_len;
933 if ((svf_check_tdo_para[i].enabled)
934 && buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var], &svf_mask_buffer[index_var], len))
936 unsigned bitmask;
937 unsigned received, expected, tapmask;
938 bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
940 memcpy(&received, svf_tdi_buffer + index_var, sizeof(unsigned));
941 memcpy(&expected, svf_tdo_buffer + index_var, sizeof(unsigned));
942 memcpy(&tapmask, svf_mask_buffer + index_var, sizeof(unsigned));
943 LOG_ERROR("tdo check error at line %d",
944 svf_check_tdo_para[i].line_num);
945 LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
946 received & bitmask,
947 expected & bitmask,
948 tapmask & bitmask);
949 return ERROR_FAIL;
952 svf_check_tdo_para_index = 0;
954 return ERROR_OK;
957 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
959 if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
961 LOG_ERROR("toooooo many operation undone");
962 return ERROR_FAIL;
965 svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
966 svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
967 svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
968 svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
969 svf_check_tdo_para_index++;
971 return ERROR_OK;
974 static int svf_execute_tap(void)
976 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
978 return ERROR_FAIL;
980 else if (ERROR_OK != svf_check_tdo())
982 return ERROR_FAIL;
985 svf_buffer_index = 0;
987 return ERROR_OK;
990 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
992 char *argus[256], command;
993 int num_of_argu = 0, i;
995 // tmp variable
996 int i_tmp;
998 // for RUNTEST
999 int run_count;
1000 float min_time, max_time;
1001 // for XXR
1002 struct svf_xxr_para *xxr_para_tmp;
1003 uint8_t **pbuffer_tmp;
1004 struct scan_field field;
1005 // for STATE
1006 tap_state_t *path = NULL, state;
1007 // flag padding commands skipped due to -tap command
1008 int padding_command_skipped = 0;
1010 if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
1012 return ERROR_FAIL;
1015 /* NOTE: we're a bit loose here, because we ignore case in
1016 * TAP state names (instead of insisting on uppercase).
1019 command = svf_find_string_in_array(argus[0],
1020 (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
1021 switch (command)
1023 case ENDDR:
1024 case ENDIR:
1025 if (num_of_argu != 2)
1027 LOG_ERROR("invalid parameter of %s", argus[0]);
1028 return ERROR_FAIL;
1031 i_tmp = tap_state_by_name(argus[1]);
1033 if (svf_tap_state_is_stable(i_tmp))
1035 if (command == ENDIR)
1037 svf_para.ir_end_state = i_tmp;
1038 LOG_DEBUG("\tIR end_state = %s",
1039 tap_state_name(i_tmp));
1041 else
1043 svf_para.dr_end_state = i_tmp;
1044 LOG_DEBUG("\tDR end_state = %s",
1045 tap_state_name(i_tmp));
1048 else
1050 LOG_ERROR("%s: %s is not a stable state",
1051 argus[0], argus[1]);
1052 return ERROR_FAIL;
1054 break;
1055 case FREQUENCY:
1056 if ((num_of_argu != 1) && (num_of_argu != 3))
1058 LOG_ERROR("invalid parameter of %s", argus[0]);
1059 return ERROR_FAIL;
1061 if (1 == num_of_argu)
1063 // TODO: set jtag speed to full speed
1064 svf_para.frequency = 0;
1066 else
1068 if (strcmp(argus[2], "HZ"))
1070 LOG_ERROR("HZ not found in FREQUENCY command");
1071 return ERROR_FAIL;
1073 if (ERROR_OK != svf_execute_tap())
1075 return ERROR_FAIL;
1077 svf_para.frequency = atof(argus[1]);
1078 // TODO: set jtag speed to
1079 if (svf_para.frequency > 0)
1081 command_run_linef(cmd_ctx, "adapter_khz %d", (int)svf_para.frequency / 1000);
1082 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
1085 break;
1086 case HDR:
1087 if (svf_tap_is_specified)
1089 padding_command_skipped = 1;
1090 break;
1092 xxr_para_tmp = &svf_para.hdr_para;
1093 goto XXR_common;
1094 case HIR:
1095 if (svf_tap_is_specified)
1097 padding_command_skipped = 1;
1098 break;
1100 xxr_para_tmp = &svf_para.hir_para;
1101 goto XXR_common;
1102 case TDR:
1103 if (svf_tap_is_specified)
1105 padding_command_skipped = 1;
1106 break;
1108 xxr_para_tmp = &svf_para.tdr_para;
1109 goto XXR_common;
1110 case TIR:
1111 if (svf_tap_is_specified)
1113 padding_command_skipped = 1;
1114 break;
1116 xxr_para_tmp = &svf_para.tir_para;
1117 goto XXR_common;
1118 case SDR:
1119 xxr_para_tmp = &svf_para.sdr_para;
1120 goto XXR_common;
1121 case SIR:
1122 xxr_para_tmp = &svf_para.sir_para;
1123 goto XXR_common;
1124 XXR_common:
1125 // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
1126 if ((num_of_argu > 10) || (num_of_argu % 2))
1128 LOG_ERROR("invalid parameter of %s", argus[0]);
1129 return ERROR_FAIL;
1131 i_tmp = xxr_para_tmp->len;
1132 xxr_para_tmp->len = atoi(argus[1]);
1133 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
1134 xxr_para_tmp->data_mask = 0;
1135 for (i = 2; i < num_of_argu; i += 2)
1137 if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
1139 LOG_ERROR("data section error");
1140 return ERROR_FAIL;
1142 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
1143 // TDI, TDO, MASK, SMASK
1144 if (!strcmp(argus[i], "TDI"))
1146 // TDI
1147 pbuffer_tmp = &xxr_para_tmp->tdi;
1148 xxr_para_tmp->data_mask |= XXR_TDI;
1150 else if (!strcmp(argus[i], "TDO"))
1152 // TDO
1153 pbuffer_tmp = &xxr_para_tmp->tdo;
1154 xxr_para_tmp->data_mask |= XXR_TDO;
1156 else if (!strcmp(argus[i], "MASK"))
1158 // MASK
1159 pbuffer_tmp = &xxr_para_tmp->mask;
1160 xxr_para_tmp->data_mask |= XXR_MASK;
1162 else if (!strcmp(argus[i], "SMASK"))
1164 // SMASK
1165 pbuffer_tmp = &xxr_para_tmp->smask;
1166 xxr_para_tmp->data_mask |= XXR_SMASK;
1168 else
1170 LOG_ERROR("unknow parameter: %s", argus[i]);
1171 return ERROR_FAIL;
1173 if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
1175 LOG_ERROR("fail to parse hex value");
1176 return ERROR_FAIL;
1178 LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
1180 // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
1181 // the mask pattern used is all cares
1182 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
1184 // MASK not defined and length changed
1185 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
1187 LOG_ERROR("fail to adjust length of array");
1188 return ERROR_FAIL;
1190 buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
1192 // If TDO is absent, no comparison is needed, set the mask to 0
1193 if (!(xxr_para_tmp->data_mask & XXR_TDO))
1195 if (NULL == xxr_para_tmp->tdo)
1197 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
1199 LOG_ERROR("fail to adjust length of array");
1200 return ERROR_FAIL;
1203 if (NULL == xxr_para_tmp->mask)
1205 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
1207 LOG_ERROR("fail to adjust length of array");
1208 return ERROR_FAIL;
1211 memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
1213 // do scan if necessary
1214 if (SDR == command)
1216 // check buffer size first, reallocate if necessary
1217 i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
1218 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1220 #if 1
1221 // simply print error message
1222 LOG_ERROR("buffer is not enough, report to author");
1223 return ERROR_FAIL;
1224 #else
1225 uint8_t *buffer_tmp;
1227 // reallocate buffer
1228 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1229 if (NULL == buffer_tmp)
1231 LOG_ERROR("not enough memory");
1232 return ERROR_FAIL;
1234 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1235 // svf_tdi_buffer isn't NULL here
1236 free(svf_tdi_buffer);
1237 svf_tdi_buffer = buffer_tmp;
1239 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1240 if (NULL == buffer_tmp)
1242 LOG_ERROR("not enough memory");
1243 return ERROR_FAIL;
1245 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1246 // svf_tdo_buffer isn't NULL here
1247 free(svf_tdo_buffer);
1248 svf_tdo_buffer = buffer_tmp;
1250 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1251 if (NULL == buffer_tmp)
1253 LOG_ERROR("not enough memory");
1254 return ERROR_FAIL;
1256 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1257 // svf_mask_buffer isn't NULL here
1258 free(svf_mask_buffer);
1259 svf_mask_buffer = buffer_tmp;
1261 buffer_tmp = NULL;
1262 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1263 #endif
1266 // assemble dr data
1267 i = 0;
1268 buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1269 i += svf_para.hdr_para.len;
1270 buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1271 i += svf_para.sdr_para.len;
1272 buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1273 i += svf_para.tdr_para.len;
1275 // add check data
1276 if (svf_para.sdr_para.data_mask & XXR_TDO)
1278 // assemble dr mask data
1279 i = 0;
1280 buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1281 i += svf_para.hdr_para.len;
1282 buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1283 i += svf_para.sdr_para.len;
1284 buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1285 i += svf_para.tdr_para.len;
1286 // assemble dr check data
1287 i = 0;
1288 buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1289 i += svf_para.hdr_para.len;
1290 buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1291 i += svf_para.sdr_para.len;
1292 buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1293 i += svf_para.tdr_para.len;
1295 svf_add_check_para(1, svf_buffer_index, i);
1297 else
1299 svf_add_check_para(0, svf_buffer_index, i);
1301 field.num_bits = i;
1302 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1303 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1304 if (!svf_nil)
1306 /* NOTE: doesn't use SVF-specified state paths */
1307 jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, svf_para.dr_end_state);
1310 svf_buffer_index += (i + 7) >> 3;
1312 else if (SIR == command)
1314 // check buffer size first, reallocate if necessary
1315 i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
1316 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1318 #if 1
1319 // simply print error message
1320 LOG_ERROR("buffer is not enough, report to author");
1321 return ERROR_FAIL;
1322 #else
1323 uint8_t *buffer_tmp;
1325 // reallocate buffer
1326 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1327 if (NULL == buffer_tmp)
1329 LOG_ERROR("not enough memory");
1330 return ERROR_FAIL;
1332 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1333 // svf_tdi_buffer isn't NULL here
1334 free(svf_tdi_buffer);
1335 svf_tdi_buffer = buffer_tmp;
1337 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1338 if (NULL == buffer_tmp)
1340 LOG_ERROR("not enough memory");
1341 return ERROR_FAIL;
1343 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1344 // svf_tdo_buffer isn't NULL here
1345 free(svf_tdo_buffer);
1346 svf_tdo_buffer = buffer_tmp;
1348 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1349 if (NULL == buffer_tmp)
1351 LOG_ERROR("not enough memory");
1352 return ERROR_FAIL;
1354 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1355 // svf_mask_buffer isn't NULL here
1356 free(svf_mask_buffer);
1357 svf_mask_buffer = buffer_tmp;
1359 buffer_tmp = NULL;
1360 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1361 #endif
1364 // assemble ir data
1365 i = 0;
1366 buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1367 i += svf_para.hir_para.len;
1368 buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1369 i += svf_para.sir_para.len;
1370 buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1371 i += svf_para.tir_para.len;
1373 // add check data
1374 if (svf_para.sir_para.data_mask & XXR_TDO)
1376 // assemble dr mask data
1377 i = 0;
1378 buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1379 i += svf_para.hir_para.len;
1380 buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1381 i += svf_para.sir_para.len;
1382 buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1383 i += svf_para.tir_para.len;
1384 // assemble dr check data
1385 i = 0;
1386 buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1387 i += svf_para.hir_para.len;
1388 buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1389 i += svf_para.sir_para.len;
1390 buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1391 i += svf_para.tir_para.len;
1393 svf_add_check_para(1, svf_buffer_index, i);
1395 else
1397 svf_add_check_para(0, svf_buffer_index, i);
1399 field.num_bits = i;
1400 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1401 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1402 if (!svf_nil)
1404 /* NOTE: doesn't use SVF-specified state paths */
1405 jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value,
1406 svf_para.ir_end_state);
1409 svf_buffer_index += (i + 7) >> 3;
1411 break;
1412 case PIO:
1413 case PIOMAP:
1414 LOG_ERROR("PIO and PIOMAP are not supported");
1415 return ERROR_FAIL;
1416 break;
1417 case RUNTEST:
1418 // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
1419 // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
1420 if ((num_of_argu < 3) && (num_of_argu > 11))
1422 LOG_ERROR("invalid parameter of %s", argus[0]);
1423 return ERROR_FAIL;
1425 // init
1426 run_count = 0;
1427 min_time = 0;
1428 max_time = 0;
1429 i = 1;
1431 // run_state
1432 i_tmp = tap_state_by_name(argus[i]);
1433 if (i_tmp != TAP_INVALID)
1435 if (svf_tap_state_is_stable(i_tmp))
1437 svf_para.runtest_run_state = i_tmp;
1439 /* When a run_state is specified, the new
1440 * run_state becomes the default end_state.
1442 svf_para.runtest_end_state = i_tmp;
1443 LOG_DEBUG("\trun_state = %s",
1444 tap_state_name(i_tmp));
1445 i++;
1447 else
1449 LOG_ERROR("%s: %s is not a stable state",
1450 argus[0], tap_state_name(i_tmp));
1451 return ERROR_FAIL;
1455 // run_count run_clk
1456 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
1458 if (!strcmp(argus[i + 1], "TCK"))
1460 // clock source is TCK
1461 run_count = atoi(argus[i]);
1462 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1464 else
1466 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1467 return ERROR_FAIL;
1469 i += 2;
1471 // min_time SEC
1472 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
1474 min_time = atof(argus[i]);
1475 LOG_DEBUG("\tmin_time = %fs", min_time);
1476 i += 2;
1478 // MAXIMUM max_time SEC
1479 if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
1481 max_time = atof(argus[i + 1]);
1482 LOG_DEBUG("\tmax_time = %fs", max_time);
1483 i += 3;
1485 // ENDSTATE end_state
1486 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
1488 i_tmp = tap_state_by_name(argus[i + 1]);
1490 if (svf_tap_state_is_stable(i_tmp))
1492 svf_para.runtest_end_state = i_tmp;
1493 LOG_DEBUG("\tend_state = %s",
1494 tap_state_name(i_tmp));
1496 else
1498 LOG_ERROR("%s: %s is not a stable state",
1499 argus[0], tap_state_name(i_tmp));
1500 return ERROR_FAIL;
1502 i += 2;
1505 // all parameter should be parsed
1506 if (i == num_of_argu)
1508 #if 1
1509 /* FIXME handle statemove failures */
1510 int retval;
1511 uint32_t min_usec = 1000000 * min_time;
1513 // enter into run_state if necessary
1514 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1516 retval = svf_add_statemove(svf_para.runtest_run_state);
1519 // add clocks and/or min wait
1520 if (run_count > 0) {
1521 if (!svf_nil)
1522 jtag_add_clocks(run_count);
1525 if (min_usec > 0) {
1526 if (!svf_nil)
1527 jtag_add_sleep(min_usec);
1530 // move to end_state if necessary
1531 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1533 retval = svf_add_statemove(svf_para.runtest_end_state);
1535 #else
1536 if (svf_para.runtest_run_state != TAP_IDLE)
1538 LOG_ERROR("cannot runtest in %s state",
1539 tap_state_name(svf_para.runtest_run_state));
1540 return ERROR_FAIL;
1543 if (!svf_nil)
1544 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1545 #endif
1547 else
1549 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
1550 return ERROR_FAIL;
1552 break;
1553 case STATE:
1554 // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
1555 if (num_of_argu < 2)
1557 LOG_ERROR("invalid parameter of %s", argus[0]);
1558 return ERROR_FAIL;
1560 if (num_of_argu > 2)
1562 // STATE pathstate1 ... stable_state
1563 path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1564 if (NULL == path)
1566 LOG_ERROR("not enough memory");
1567 return ERROR_FAIL;
1569 num_of_argu--; // num of path
1570 i_tmp = 1; /* path is from parameter 1 */
1571 for (i = 0; i < num_of_argu; i++, i_tmp++)
1573 path[i] = tap_state_by_name(argus[i_tmp]);
1574 if (path[i] == TAP_INVALID)
1576 LOG_ERROR("%s: %s is not a valid state",
1577 argus[0], argus[i_tmp]);
1578 free(path);
1579 return ERROR_FAIL;
1581 /* OpenOCD refuses paths containing TAP_RESET */
1582 if (TAP_RESET == path[i])
1584 /* FIXME last state MUST be stable! */
1585 if (i > 0)
1587 if (!svf_nil)
1588 jtag_add_pathmove(i, path);
1590 if (!svf_nil)
1591 jtag_add_tlr();
1592 num_of_argu -= i + 1;
1593 i = -1;
1596 if (num_of_argu > 0)
1598 // execute last path if necessary
1599 if (svf_tap_state_is_stable(path[num_of_argu - 1]))
1601 // last state MUST be stable state
1602 if (!svf_nil)
1603 jtag_add_pathmove(num_of_argu, path);
1604 LOG_DEBUG("\tmove to %s by path_move",
1605 tap_state_name(path[num_of_argu - 1]));
1607 else
1609 LOG_ERROR("%s: %s is not a stable state",
1610 argus[0],
1611 tap_state_name(path[num_of_argu - 1]));
1612 free(path);
1613 return ERROR_FAIL;
1617 free(path);
1618 path = NULL;
1620 else
1622 // STATE stable_state
1623 state = tap_state_by_name(argus[1]);
1624 if (svf_tap_state_is_stable(state))
1626 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1627 tap_state_name(state));
1628 /* FIXME handle statemove failures */
1629 svf_add_statemove(state);
1631 else
1633 LOG_ERROR("%s: %s is not a stable state",
1634 argus[0], tap_state_name(state));
1635 return ERROR_FAIL;
1638 break;
1639 case TRST:
1640 // TRST trst_mode
1641 if (num_of_argu != 2)
1643 LOG_ERROR("invalid parameter of %s", argus[0]);
1644 return ERROR_FAIL;
1646 if (svf_para.trst_mode != TRST_ABSENT)
1648 if (ERROR_OK != svf_execute_tap())
1650 return ERROR_FAIL;
1652 i_tmp = svf_find_string_in_array(argus[1],
1653 (char **)svf_trst_mode_name,
1654 ARRAY_SIZE(svf_trst_mode_name));
1655 switch (i_tmp)
1657 case TRST_ON:
1658 if (!svf_nil)
1659 jtag_add_reset(1, 0);
1660 break;
1661 case TRST_Z:
1662 case TRST_OFF:
1663 if (!svf_nil)
1664 jtag_add_reset(0, 0);
1665 break;
1666 case TRST_ABSENT:
1667 break;
1668 default:
1669 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1670 return ERROR_FAIL;
1672 svf_para.trst_mode = i_tmp;
1673 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1675 else
1677 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1678 return ERROR_FAIL;
1680 break;
1681 default:
1682 LOG_ERROR("invalid svf command: %s", argus[0]);
1683 return ERROR_FAIL;
1684 break;
1687 if (!svf_quiet)
1689 if (padding_command_skipped)
1691 LOG_USER("(Above Padding command skipped, as per -tap argument)");
1695 if (debug_level >= LOG_LVL_DEBUG)
1697 // for convenient debugging, execute tap if possible
1698 if ((svf_buffer_index > 0) && \
1699 (((command != STATE) && (command != RUNTEST)) || \
1700 ((command == STATE) && (num_of_argu == 2))))
1702 if (ERROR_OK != svf_execute_tap())
1704 return ERROR_FAIL;
1707 // output debug info
1708 if ((SIR == command) || (SDR == command))
1710 int read_value;
1711 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1712 // in debug mode, data is from index 0
1713 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1714 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1718 else
1720 // for fast executing, execute tap if necessary
1721 // half of the buffer is for the next command
1722 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1723 (((command != STATE) && (command != RUNTEST)) || \
1724 ((command == STATE) && (num_of_argu == 2))))
1726 return svf_execute_tap();
1730 return ERROR_OK;
1733 static const struct command_registration svf_command_handlers[] = {
1735 .name = "svf",
1736 .handler = handle_svf_command,
1737 .mode = COMMAND_EXEC,
1738 .help = "Runs a SVF file.",
1739 .usage = "svf [-tap device.tap] <file> [quiet] [nil] [progress]",
1741 COMMAND_REGISTRATION_DONE
1744 int svf_register_commands(struct command_context *cmd_ctx)
1746 return register_commands(cmd_ctx, NULL, svf_command_handlers);