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".
36 #include "time_support.h"
58 const char *svf_command_name
[14] =
84 const char *svf_trst_mode_name
[4] =
96 uint32_t num_of_moves
;
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 svf_statemove_t 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
}}
139 char *svf_tap_state_name
[TAP_NUM_STATES
];
141 #define XXR_TDI (1 << 0)
142 #define XXR_TDO (1 << 1)
143 #define XXR_MASK (1 << 2)
144 #define XXR_SMASK (1 << 3)
158 tap_state_t ir_end_state
;
159 tap_state_t dr_end_state
;
160 tap_state_t runtest_run_state
;
161 tap_state_t runtest_end_state
;
162 trst_mode_t trst_mode
;
164 svf_xxr_para_t hir_para
;
165 svf_xxr_para_t hdr_para
;
166 svf_xxr_para_t tir_para
;
167 svf_xxr_para_t tdr_para
;
168 svf_xxr_para_t sir_para
;
169 svf_xxr_para_t sdr_para
;
173 const svf_para_t svf_para_init
=
175 // frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode
176 0, TAP_IDLE
, TAP_IDLE
, TAP_IDLE
, TAP_IDLE
, TRST_Z
,
178 // {len, data_mask, tdi, tdo, mask, smask},
179 {0, 0, NULL
, NULL
, NULL
, NULL
},
181 // {len, data_mask, tdi, tdo, mask, smask},
182 {0, 0, NULL
, NULL
, NULL
, NULL
},
184 // {len, data_mask, tdi, tdo, mask, smask},
185 {0, 0, NULL
, NULL
, NULL
, NULL
},
187 // {len, data_mask, tdi, tdo, mask, smask},
188 {0, 0, NULL
, NULL
, NULL
, NULL
},
190 // {len, data_mask, tdi, tdo, mask, smask},
191 {0, 0, NULL
, NULL
, NULL
, NULL
},
193 // {len, data_mask, tdi, tdo, mask, smask},
194 {0, 0, NULL
, NULL
, NULL
, NULL
},
199 int line_num
; // used to record line number of the check operation
200 // so more information could be printed
201 int enabled
; // check is enabled or not
202 int buffer_offset
; // buffer_offset to buffers
203 int bit_len
; // bit length to check
204 }svf_check_tdo_para_t
;
206 #define SVF_CHECK_TDO_PARA_SIZE 1024
207 static svf_check_tdo_para_t
*svf_check_tdo_para
= NULL
;
208 static int svf_check_tdo_para_index
= 0;
210 #define dimof(a) (sizeof(a) / sizeof((a)[0]))
212 static int svf_read_command_from_file(int fd
);
213 static int svf_check_tdo(void);
214 static int svf_add_check_para(uint8_t enabled
, int buffer_offset
, int bit_len
);
215 static int svf_run_command(struct command_context_s
*cmd_ctx
, char *cmd_str
);
216 static int handle_svf_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
218 static int svf_fd
= 0;
219 static char *svf_command_buffer
= NULL
;
220 static int svf_command_buffer_size
= 0;
221 static int svf_line_number
= 1;
223 static jtag_tap_t
*tap
= NULL
;
225 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (4 * 1024)
226 static uint8_t *svf_tdi_buffer
= NULL
, *svf_tdo_buffer
= NULL
, *svf_mask_buffer
= NULL
;
227 static int svf_buffer_index
= 0, svf_buffer_size
= 0;
228 static int svf_quiet
= 0;
231 int svf_register_commands(struct command_context_s
*cmd_ctx
)
233 register_command(cmd_ctx
, NULL
, "svf", handle_svf_command
,
234 COMMAND_EXEC
, "run svf <file>");
239 void svf_free_xxd_para(svf_xxr_para_t
*para
)
243 if (para
->tdi
!= NULL
)
248 if (para
->tdo
!= NULL
)
253 if (para
->mask
!= NULL
)
258 if (para
->smask
!= NULL
)
266 unsigned svf_get_mask_u32(int bitlen
)
274 else if (bitlen
>= 32)
276 bitmask
= 0xFFFFFFFF;
280 bitmask
= (1 << bitlen
) - 1;
286 static const char* tap_state_svf_name(tap_state_t state
)
292 case TAP_RESET
: ret
= "RESET"; break;
293 case TAP_IDLE
: ret
= "IDLE"; break;
294 case TAP_DRSELECT
: ret
= "DRSELECT"; break;
295 case TAP_DRCAPTURE
: ret
= "DRCAPTURE"; break;
296 case TAP_DRSHIFT
: ret
= "DRSHIFT"; break;
297 case TAP_DREXIT1
: ret
= "DREXIT1"; break;
298 case TAP_DRPAUSE
: ret
= "DRPAUSE"; break;
299 case TAP_DREXIT2
: ret
= "DREXIT2"; break;
300 case TAP_DRUPDATE
: ret
= "DRUPDATE"; break;
301 case TAP_IRSELECT
: ret
= "IRSELECT"; break;
302 case TAP_IRCAPTURE
: ret
= "IRCAPTURE"; break;
303 case TAP_IRSHIFT
: ret
= "IRSHIFT"; break;
304 case TAP_IREXIT1
: ret
= "IREXIT1"; break;
305 case TAP_IRPAUSE
: ret
= "IRPAUSE"; break;
306 case TAP_IREXIT2
: ret
= "IREXIT2"; break;
307 case TAP_IRUPDATE
: ret
= "IRUPDATE"; break;
308 default: ret
= "???"; break;
314 static int svf_add_statemove(tap_state_t state_to
)
316 tap_state_t state_from
= cmd_queue_cur_state
;
319 for (index
= 0; index
< dimof(svf_statemoves
); index
++)
321 if ((svf_statemoves
[index
].from
== state_from
)
322 && (svf_statemoves
[index
].to
== state_to
))
324 if (TAP_RESET
== state_from
)
327 if (svf_statemoves
[index
].num_of_moves
> 1)
329 jtag_add_pathmove(svf_statemoves
[index
].num_of_moves
- 1, svf_statemoves
[index
].paths
+ 1);
334 if (svf_statemoves
[index
].num_of_moves
> 0)
336 jtag_add_pathmove(svf_statemoves
[index
].num_of_moves
, svf_statemoves
[index
].paths
);
342 LOG_ERROR("can not move to %s", tap_state_svf_name(state_to
));
346 static int handle_svf_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
348 #define SVF_NUM_OF_OPTIONS 1
349 int command_num
= 0, i
;
353 if ((argc
< 1) || (argc
> (1 + SVF_NUM_OF_OPTIONS
)))
355 command_print(cmd_ctx
, "usage: svf <file> [quiet]");
361 for (i
= 1; i
< argc
; i
++)
363 if (!strcmp(args
[i
], "quiet"))
369 LOG_ERROR("unknown variant for svf: %s", args
[i
]);
371 // no need to free anything now
376 if ((svf_fd
= open(args
[0], O_RDONLY
)) < 0)
378 command_print(cmd_ctx
, "file \"%s\" not found", args
[0]);
380 // no need to free anything now
384 LOG_USER("svf processing file: \"%s\"", args
[0]);
387 time_ago
= timeval_ms();
391 svf_command_buffer_size
= 0;
393 svf_check_tdo_para_index
= 0;
394 svf_check_tdo_para
= malloc(sizeof(svf_check_tdo_para_t
) * SVF_CHECK_TDO_PARA_SIZE
);
395 if (NULL
== svf_check_tdo_para
)
397 LOG_ERROR("not enough memory");
402 svf_buffer_index
= 0;
403 // double the buffer size
404 // in case current command cannot be commited, and next command is a bit scan command
405 // here is 32K bits for this big scan command, it should be enough
406 // buffer will be reallocated if buffer size is not enough
407 svf_tdi_buffer
= (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT
);
408 if (NULL
== svf_tdi_buffer
)
410 LOG_ERROR("not enough memory");
414 svf_tdo_buffer
= (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT
);
415 if (NULL
== svf_tdo_buffer
)
417 LOG_ERROR("not enough memory");
421 svf_mask_buffer
= (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT
);
422 if (NULL
== svf_mask_buffer
)
424 LOG_ERROR("not enough memory");
428 svf_buffer_size
= 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT
;
430 memcpy(&svf_para
, &svf_para_init
, sizeof(svf_para
));
431 for (i
= 0; i
< (int)dimof(svf_tap_state_name
); i
++)
433 svf_tap_state_name
[i
] = (char *)tap_state_svf_name(i
);
439 while (ERROR_OK
== svf_read_command_from_file(svf_fd
))
441 if (ERROR_OK
!= svf_run_command(cmd_ctx
, svf_command_buffer
))
443 LOG_ERROR("fail to run command at line %d", svf_line_number
);
449 if (ERROR_OK
!= jtag_execute_queue())
453 else if (ERROR_OK
!= svf_check_tdo())
459 command_print(cmd_ctx
, "%lld ms used", timeval_ms() - time_ago
);
467 if (svf_command_buffer
)
469 free(svf_command_buffer
);
470 svf_command_buffer
= NULL
;
471 svf_command_buffer_size
= 0;
473 if (svf_check_tdo_para
)
475 free(svf_check_tdo_para
);
476 svf_check_tdo_para
= NULL
;
477 svf_check_tdo_para_index
= 0;
481 free(svf_tdi_buffer
);
482 svf_tdi_buffer
= NULL
;
486 free(svf_tdo_buffer
);
487 svf_tdo_buffer
= NULL
;
491 free(svf_mask_buffer
);
492 svf_mask_buffer
= NULL
;
494 svf_buffer_index
= 0;
497 svf_free_xxd_para(&svf_para
.hdr_para
);
498 svf_free_xxd_para(&svf_para
.hir_para
);
499 svf_free_xxd_para(&svf_para
.tdr_para
);
500 svf_free_xxd_para(&svf_para
.tir_para
);
501 svf_free_xxd_para(&svf_para
.sdr_para
);
502 svf_free_xxd_para(&svf_para
.sir_para
);
506 command_print(cmd_ctx
, "svf file programmed successfully for %d commands", command_num
);
510 command_print(cmd_ctx
, "svf file programmed failed");
516 #define SVFP_CMD_INC_CNT 1024
517 static int svf_read_command_from_file(int fd
)
519 char ch
, *tmp_buffer
= NULL
;
520 int cmd_pos
= 0, cmd_ok
= 0, slash
= 0, comment
= 0;
522 while (!cmd_ok
&& (read(fd
, &ch
, 1) > 0))
552 if (cmd_pos
>= svf_command_buffer_size
- 1)
554 tmp_buffer
= (char*)malloc(svf_command_buffer_size
+ SVFP_CMD_INC_CNT
); // 1 more byte for '\0'
555 if (NULL
== tmp_buffer
)
557 LOG_ERROR("not enough memory");
560 if (svf_command_buffer_size
> 0)
562 memcpy(tmp_buffer
, svf_command_buffer
, svf_command_buffer_size
);
564 if (svf_command_buffer
!= NULL
)
566 free(svf_command_buffer
);
568 svf_command_buffer
= tmp_buffer
;
569 svf_command_buffer_size
+= SVFP_CMD_INC_CNT
;
572 svf_command_buffer
[cmd_pos
++] = (char)toupper(ch
);
580 svf_command_buffer
[cmd_pos
] = '\0';
589 static int svf_parse_cmd_string(char *str
, int len
, char **argus
, int *num_of_argu
)
591 int pos
= 0, num
= 0, space_found
= 1;
601 LOG_ERROR("fail to parse svf command");
611 argus
[num
++] = &str
[pos
];
624 static int svf_tap_state_is_stable(tap_state_t state
)
626 return ((TAP_RESET
== state
) || (TAP_IDLE
== state
) || (TAP_DRPAUSE
== state
) || (TAP_IRPAUSE
== state
));
629 static int svf_tap_state_is_valid(tap_state_t state
)
631 return state
>= 0 && state
< TAP_NUM_STATES
;
634 static int svf_find_string_in_array(char *str
, char **strs
, int num_of_element
)
638 for (i
= 0; i
< num_of_element
; i
++)
640 if (!strcmp(str
, strs
[i
]))
648 static int svf_adjust_array_length(uint8_t **arr
, int orig_bit_len
, int new_bit_len
)
650 int new_byte_len
= (new_bit_len
+ 7) >> 3;
652 if ((NULL
== *arr
) || (((orig_bit_len
+ 7) >> 3) < ((new_bit_len
+ 7) >> 3)))
659 *arr
= (uint8_t*)malloc(new_byte_len
);
662 LOG_ERROR("not enough memory");
665 memset(*arr
, 0, new_byte_len
);
670 static int svf_copy_hexstring_to_binary(char *str
, uint8_t **bin
, int orig_bit_len
, int bit_len
)
672 int i
, str_len
= strlen(str
), str_hbyte_len
= (bit_len
+ 3) >> 2;
675 if (ERROR_OK
!= svf_adjust_array_length(bin
, orig_bit_len
, bit_len
))
677 LOG_ERROR("fail to adjust length of array");
681 for (i
= 0; i
< str_hbyte_len
; i
++)
690 if ((ch
>= '0') && (ch
<= '9'))
695 else if ((ch
>= 'A') && (ch
<= 'F'))
702 LOG_ERROR("invalid hex string");
714 (*bin
)[i
/ 2] |= ch
<< 4;
725 if (str_len
> 0 || (ch
& ~((1 << (4 - (bit_len
% 4))) - 1)) != 0)
727 LOG_ERROR("value execede length");
734 static int svf_check_tdo(void)
738 for (i
= 0; i
< svf_check_tdo_para_index
; i
++)
740 index
= svf_check_tdo_para
[i
].buffer_offset
;
741 len
= svf_check_tdo_para
[i
].bit_len
;
742 if ((svf_check_tdo_para
[i
].enabled
)
743 && buf_cmp_mask(&svf_tdi_buffer
[index
], &svf_tdo_buffer
[index
], &svf_mask_buffer
[index
], len
))
746 unsigned received
, expected
, tapmask
;
747 bitmask
= svf_get_mask_u32(svf_check_tdo_para
[i
].bit_len
);
749 memcpy(&received
, svf_tdi_buffer
+ index
, sizeof(unsigned));
750 memcpy(&expected
, svf_tdo_buffer
+ index
, sizeof(unsigned));
751 memcpy(&tapmask
, svf_mask_buffer
+ index
, sizeof(unsigned));
752 LOG_ERROR("tdo check error at line %d",
753 svf_check_tdo_para
[i
].line_num
);
754 LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
761 svf_check_tdo_para_index
= 0;
766 static int svf_add_check_para(uint8_t enabled
, int buffer_offset
, int bit_len
)
768 if (svf_check_tdo_para_index
>= SVF_CHECK_TDO_PARA_SIZE
)
770 LOG_ERROR("toooooo many operation undone");
774 svf_check_tdo_para
[svf_check_tdo_para_index
].line_num
= svf_line_number
;
775 svf_check_tdo_para
[svf_check_tdo_para_index
].bit_len
= bit_len
;
776 svf_check_tdo_para
[svf_check_tdo_para_index
].enabled
= enabled
;
777 svf_check_tdo_para
[svf_check_tdo_para_index
].buffer_offset
= buffer_offset
;
778 svf_check_tdo_para_index
++;
783 static int svf_execute_tap(void)
785 if (ERROR_OK
!= jtag_execute_queue())
789 else if (ERROR_OK
!= svf_check_tdo())
794 svf_buffer_index
= 0;
799 static int svf_run_command(struct command_context_s
*cmd_ctx
, char *cmd_str
)
801 char *argus
[256], command
;
802 int num_of_argu
= 0, i
;
809 float min_time
, max_time
;
811 svf_xxr_para_t
*xxr_para_tmp
;
812 uint8_t **pbuffer_tmp
;
815 tap_state_t
*path
= NULL
, state
;
819 LOG_USER("%s", svf_command_buffer
);
822 if (ERROR_OK
!= svf_parse_cmd_string(cmd_str
, strlen(cmd_str
), argus
, &num_of_argu
))
827 command
= svf_find_string_in_array(argus
[0], (char **)svf_command_name
, dimof(svf_command_name
));
832 if (num_of_argu
!= 2)
834 LOG_ERROR("invalid parameter of %s", argus
[0]);
837 i_tmp
= svf_find_string_in_array(argus
[1], (char **)svf_tap_state_name
, dimof(svf_tap_state_name
));
838 if (svf_tap_state_is_stable(i_tmp
))
840 if (command
== ENDIR
)
842 svf_para
.ir_end_state
= i_tmp
;
843 LOG_DEBUG("\tir_end_state = %s", svf_tap_state_name
[svf_para
.ir_end_state
]);
847 svf_para
.dr_end_state
= i_tmp
;
848 LOG_DEBUG("\tdr_end_state = %s", svf_tap_state_name
[svf_para
.dr_end_state
]);
853 LOG_ERROR("%s is not valid state", argus
[1]);
858 if ((num_of_argu
!= 1) && (num_of_argu
!= 3))
860 LOG_ERROR("invalid parameter of %s", argus
[0]);
863 if (1 == num_of_argu
)
865 // TODO: set jtag speed to full speed
866 svf_para
.frequency
= 0;
870 if (strcmp(argus
[2], "HZ"))
872 LOG_ERROR("HZ not found in FREQUENCY command");
875 if (ERROR_OK
!= svf_execute_tap())
879 svf_para
.frequency
= atof(argus
[1]);
880 // TODO: set jtag speed to
881 if (svf_para
.frequency
> 0)
883 command_run_linef(cmd_ctx
, "jtag_khz %d", (int)svf_para
.frequency
/ 1000);
884 LOG_DEBUG("\tfrequency = %f", svf_para
.frequency
);
889 xxr_para_tmp
= &svf_para
.hdr_para
;
892 xxr_para_tmp
= &svf_para
.hir_para
;
895 xxr_para_tmp
= &svf_para
.tdr_para
;
898 xxr_para_tmp
= &svf_para
.tir_para
;
901 xxr_para_tmp
= &svf_para
.sdr_para
;
904 xxr_para_tmp
= &svf_para
.sir_para
;
907 // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
908 if ((num_of_argu
> 10) || (num_of_argu
% 2))
910 LOG_ERROR("invalid parameter of %s", argus
[0]);
913 i_tmp
= xxr_para_tmp
->len
;
914 xxr_para_tmp
->len
= atoi(argus
[1]);
915 LOG_DEBUG("\tlength = %d", xxr_para_tmp
->len
);
916 xxr_para_tmp
->data_mask
= 0;
917 for (i
= 2; i
< num_of_argu
; i
+= 2)
919 if ((strlen(argus
[i
+ 1]) < 3) || (argus
[i
+ 1][0] != '(') || (argus
[i
+ 1][strlen(argus
[i
+ 1]) - 1] != ')'))
921 LOG_ERROR("data section error");
924 argus
[i
+ 1][strlen(argus
[i
+ 1]) - 1] = '\0';
925 // TDI, TDO, MASK, SMASK
926 if (!strcmp(argus
[i
], "TDI"))
929 pbuffer_tmp
= &xxr_para_tmp
->tdi
;
930 xxr_para_tmp
->data_mask
|= XXR_TDI
;
932 else if (!strcmp(argus
[i
], "TDO"))
935 pbuffer_tmp
= &xxr_para_tmp
->tdo
;
936 xxr_para_tmp
->data_mask
|= XXR_TDO
;
938 else if (!strcmp(argus
[i
], "MASK"))
941 pbuffer_tmp
= &xxr_para_tmp
->mask
;
942 xxr_para_tmp
->data_mask
|= XXR_MASK
;
944 else if (!strcmp(argus
[i
], "SMASK"))
947 pbuffer_tmp
= &xxr_para_tmp
->smask
;
948 xxr_para_tmp
->data_mask
|= XXR_SMASK
;
952 LOG_ERROR("unknow parameter: %s", argus
[i
]);
955 if (ERROR_OK
!= svf_copy_hexstring_to_binary(&argus
[i
+ 1][1], pbuffer_tmp
, i_tmp
, xxr_para_tmp
->len
))
957 LOG_ERROR("fail to parse hex value");
960 LOG_DEBUG("\t%s = 0x%X", argus
[i
], (**(int**)pbuffer_tmp
) & svf_get_mask_u32(xxr_para_tmp
->len
));
962 // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
963 // the mask pattern used is all cares
964 if (!(xxr_para_tmp
->data_mask
& XXR_MASK
) && (i_tmp
!= xxr_para_tmp
->len
))
966 // MASK not defined and length changed
967 if (ERROR_OK
!= svf_adjust_array_length(&xxr_para_tmp
->mask
, i_tmp
, xxr_para_tmp
->len
))
969 LOG_ERROR("fail to adjust length of array");
972 buf_set_ones(xxr_para_tmp
->mask
, xxr_para_tmp
->len
);
974 // If TDO is absent, no comparison is needed, set the mask to 0
975 if (!(xxr_para_tmp
->data_mask
& XXR_TDO
))
977 if (NULL
== xxr_para_tmp
->tdo
)
979 if (ERROR_OK
!= svf_adjust_array_length(&xxr_para_tmp
->tdo
, i_tmp
, xxr_para_tmp
->len
))
981 LOG_ERROR("fail to adjust length of array");
985 if (NULL
== xxr_para_tmp
->mask
)
987 if (ERROR_OK
!= svf_adjust_array_length(&xxr_para_tmp
->mask
, i_tmp
, xxr_para_tmp
->len
))
989 LOG_ERROR("fail to adjust length of array");
993 memset(xxr_para_tmp
->mask
, 0, (xxr_para_tmp
->len
+ 7) >> 3);
995 // do scan if necessary
998 // check buffer size first, reallocate if necessary
999 i
= svf_para
.hdr_para
.len
+ svf_para
.sdr_para
.len
+ svf_para
.tdr_para
.len
;
1000 if ((svf_buffer_size
- svf_buffer_index
) < ((i
+ 7) >> 3))
1003 // simply print error message
1004 LOG_ERROR("buffer is not enough, report to author");
1007 uint8_t *buffer_tmp
;
1009 // reallocate buffer
1010 buffer_tmp
= (uint8_t *)malloc(svf_buffer_index
+ ((i
+ 7) >> 3));
1011 if (NULL
== buffer_tmp
)
1013 LOG_ERROR("not enough memory");
1016 memcpy(buffer_tmp
, svf_tdi_buffer
, svf_buffer_index
);
1017 // svf_tdi_buffer isn't NULL here
1018 free(svf_tdi_buffer
);
1019 svf_tdi_buffer
= buffer_tmp
;
1021 buffer_tmp
= (uint8_t *)malloc(svf_buffer_index
+ ((i
+ 7) >> 3));
1022 if (NULL
== buffer_tmp
)
1024 LOG_ERROR("not enough memory");
1027 memcpy(buffer_tmp
, svf_tdo_buffer
, svf_buffer_index
);
1028 // svf_tdo_buffer isn't NULL here
1029 free(svf_tdo_buffer
);
1030 svf_tdo_buffer
= buffer_tmp
;
1032 buffer_tmp
= (uint8_t *)malloc(svf_buffer_index
+ ((i
+ 7) >> 3));
1033 if (NULL
== buffer_tmp
)
1035 LOG_ERROR("not enough memory");
1038 memcpy(buffer_tmp
, svf_mask_buffer
, svf_buffer_index
);
1039 // svf_mask_buffer isn't NULL here
1040 free(svf_mask_buffer
);
1041 svf_mask_buffer
= buffer_tmp
;
1044 svf_buffer_size
= svf_buffer_index
+ ((i
+ 7) >> 3);
1050 buf_set_buf(svf_para
.hdr_para
.tdi
, 0, &svf_tdi_buffer
[svf_buffer_index
], i
, svf_para
.hdr_para
.len
);
1051 i
+= svf_para
.hdr_para
.len
;
1052 buf_set_buf(svf_para
.sdr_para
.tdi
, 0, &svf_tdi_buffer
[svf_buffer_index
], i
, svf_para
.sdr_para
.len
);
1053 i
+= svf_para
.sdr_para
.len
;
1054 buf_set_buf(svf_para
.tdr_para
.tdi
, 0, &svf_tdi_buffer
[svf_buffer_index
], i
, svf_para
.tdr_para
.len
);
1055 i
+= svf_para
.tdr_para
.len
;
1058 if (svf_para
.sdr_para
.data_mask
& XXR_TDO
)
1060 // assemble dr mask data
1062 buf_set_buf(svf_para
.hdr_para
.mask
, 0, &svf_mask_buffer
[svf_buffer_index
], i
, svf_para
.hdr_para
.len
);
1063 i
+= svf_para
.hdr_para
.len
;
1064 buf_set_buf(svf_para
.sdr_para
.mask
, 0, &svf_mask_buffer
[svf_buffer_index
], i
, svf_para
.sdr_para
.len
);
1065 i
+= svf_para
.sdr_para
.len
;
1066 buf_set_buf(svf_para
.tdr_para
.mask
, 0, &svf_mask_buffer
[svf_buffer_index
], i
, svf_para
.tdr_para
.len
);
1067 i
+= svf_para
.tdr_para
.len
;
1068 // assemble dr check data
1070 buf_set_buf(svf_para
.hdr_para
.tdo
, 0, &svf_tdo_buffer
[svf_buffer_index
], i
, svf_para
.hdr_para
.len
);
1071 i
+= svf_para
.hdr_para
.len
;
1072 buf_set_buf(svf_para
.sdr_para
.tdo
, 0, &svf_tdo_buffer
[svf_buffer_index
], i
, svf_para
.sdr_para
.len
);
1073 i
+= svf_para
.sdr_para
.len
;
1074 buf_set_buf(svf_para
.tdr_para
.tdo
, 0, &svf_tdo_buffer
[svf_buffer_index
], i
, svf_para
.tdr_para
.len
);
1075 i
+= svf_para
.tdr_para
.len
;
1077 svf_add_check_para(1, svf_buffer_index
, i
);
1081 svf_add_check_para(0, svf_buffer_index
, i
);
1085 field
.out_value
= &svf_tdi_buffer
[svf_buffer_index
];
1086 field
.in_value
= &svf_tdi_buffer
[svf_buffer_index
];
1087 jtag_add_plain_dr_scan(1, &field
, svf_para
.dr_end_state
);
1089 svf_buffer_index
+= (i
+ 7) >> 3;
1091 else if (SIR
== command
)
1093 // check buffer size first, reallocate if necessary
1094 i
= svf_para
.hir_para
.len
+ svf_para
.sir_para
.len
+ svf_para
.tir_para
.len
;
1095 if ((svf_buffer_size
- svf_buffer_index
) < ((i
+ 7) >> 3))
1098 // simply print error message
1099 LOG_ERROR("buffer is not enough, report to author");
1102 uint8_t *buffer_tmp
;
1104 // reallocate buffer
1105 buffer_tmp
= (uint8_t *)malloc(svf_buffer_index
+ ((i
+ 7) >> 3));
1106 if (NULL
== buffer_tmp
)
1108 LOG_ERROR("not enough memory");
1111 memcpy(buffer_tmp
, svf_tdi_buffer
, svf_buffer_index
);
1112 // svf_tdi_buffer isn't NULL here
1113 free(svf_tdi_buffer
);
1114 svf_tdi_buffer
= buffer_tmp
;
1116 buffer_tmp
= (uint8_t *)malloc(svf_buffer_index
+ ((i
+ 7) >> 3));
1117 if (NULL
== buffer_tmp
)
1119 LOG_ERROR("not enough memory");
1122 memcpy(buffer_tmp
, svf_tdo_buffer
, svf_buffer_index
);
1123 // svf_tdo_buffer isn't NULL here
1124 free(svf_tdo_buffer
);
1125 svf_tdo_buffer
= buffer_tmp
;
1127 buffer_tmp
= (uint8_t *)malloc(svf_buffer_index
+ ((i
+ 7) >> 3));
1128 if (NULL
== buffer_tmp
)
1130 LOG_ERROR("not enough memory");
1133 memcpy(buffer_tmp
, svf_mask_buffer
, svf_buffer_index
);
1134 // svf_mask_buffer isn't NULL here
1135 free(svf_mask_buffer
);
1136 svf_mask_buffer
= buffer_tmp
;
1139 svf_buffer_size
= svf_buffer_index
+ ((i
+ 7) >> 3);
1145 buf_set_buf(svf_para
.hir_para
.tdi
, 0, &svf_tdi_buffer
[svf_buffer_index
], i
, svf_para
.hir_para
.len
);
1146 i
+= svf_para
.hir_para
.len
;
1147 buf_set_buf(svf_para
.sir_para
.tdi
, 0, &svf_tdi_buffer
[svf_buffer_index
], i
, svf_para
.sir_para
.len
);
1148 i
+= svf_para
.sir_para
.len
;
1149 buf_set_buf(svf_para
.tir_para
.tdi
, 0, &svf_tdi_buffer
[svf_buffer_index
], i
, svf_para
.tir_para
.len
);
1150 i
+= svf_para
.tir_para
.len
;
1153 if (svf_para
.sir_para
.data_mask
& XXR_TDO
)
1155 // assemble dr mask data
1157 buf_set_buf(svf_para
.hir_para
.mask
, 0, &svf_mask_buffer
[svf_buffer_index
], i
, svf_para
.hir_para
.len
);
1158 i
+= svf_para
.hir_para
.len
;
1159 buf_set_buf(svf_para
.sir_para
.mask
, 0, &svf_mask_buffer
[svf_buffer_index
], i
, svf_para
.sir_para
.len
);
1160 i
+= svf_para
.sir_para
.len
;
1161 buf_set_buf(svf_para
.tir_para
.mask
, 0, &svf_mask_buffer
[svf_buffer_index
], i
, svf_para
.tir_para
.len
);
1162 i
+= svf_para
.tir_para
.len
;
1163 // assemble dr check data
1165 buf_set_buf(svf_para
.hir_para
.tdo
, 0, &svf_tdo_buffer
[svf_buffer_index
], i
, svf_para
.hir_para
.len
);
1166 i
+= svf_para
.hir_para
.len
;
1167 buf_set_buf(svf_para
.sir_para
.tdo
, 0, &svf_tdo_buffer
[svf_buffer_index
], i
, svf_para
.sir_para
.len
);
1168 i
+= svf_para
.sir_para
.len
;
1169 buf_set_buf(svf_para
.tir_para
.tdo
, 0, &svf_tdo_buffer
[svf_buffer_index
], i
, svf_para
.tir_para
.len
);
1170 i
+= svf_para
.tir_para
.len
;
1172 svf_add_check_para(1, svf_buffer_index
, i
);
1176 svf_add_check_para(0, svf_buffer_index
, i
);
1180 field
.out_value
= &svf_tdi_buffer
[svf_buffer_index
];
1181 field
.in_value
= &svf_tdi_buffer
[svf_buffer_index
];
1182 jtag_add_plain_ir_scan(1, &field
, svf_para
.ir_end_state
);
1184 svf_buffer_index
+= (i
+ 7) >> 3;
1189 LOG_ERROR("PIO and PIOMAP are not supported");
1193 // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
1194 // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
1195 if ((num_of_argu
< 3) && (num_of_argu
> 11))
1197 LOG_ERROR("invalid parameter of %s", argus
[0]);
1206 i_tmp
= svf_find_string_in_array(argus
[i
], (char **)svf_tap_state_name
, dimof(svf_tap_state_name
));
1207 if (svf_tap_state_is_valid(i_tmp
))
1209 if (svf_tap_state_is_stable(i_tmp
))
1211 svf_para
.runtest_run_state
= i_tmp
;
1213 // When a run_state is specified, the new run_state becomes the default end_state
1214 svf_para
.runtest_end_state
= i_tmp
;
1215 LOG_DEBUG("\trun_state = %s", svf_tap_state_name
[svf_para
.runtest_run_state
]);
1220 LOG_ERROR("%s is not valid state", svf_tap_state_name
[i_tmp
]);
1224 // run_count run_clk
1225 if (((i
+ 2) <= num_of_argu
) && strcmp(argus
[i
+ 1], "SEC"))
1227 if (!strcmp(argus
[i
+ 1], "TCK"))
1229 // clock source is TCK
1230 run_count
= atoi(argus
[i
]);
1231 LOG_DEBUG("\trun_count@TCK = %d", run_count
);
1235 LOG_ERROR("%s not supported for clock", argus
[i
+ 1]);
1241 if (((i
+ 2) <= num_of_argu
) && !strcmp(argus
[i
+ 1], "SEC"))
1243 min_time
= atof(argus
[i
]);
1244 LOG_DEBUG("\tmin_time = %fs", min_time
);
1247 // MAXIMUM max_time SEC
1248 if (((i
+ 3) <= num_of_argu
) && !strcmp(argus
[i
], "MAXIMUM") && !strcmp(argus
[i
+ 2], "SEC"))
1250 max_time
= atof(argus
[i
+ 1]);
1251 LOG_DEBUG("\tmax_time = %fs", max_time
);
1254 // ENDSTATE end_state
1255 if (((i
+ 2) <= num_of_argu
) && !strcmp(argus
[i
], "ENDSTATE"))
1257 i_tmp
= svf_find_string_in_array(argus
[i
+ 1], (char **)svf_tap_state_name
, dimof(svf_tap_state_name
));
1258 if (svf_tap_state_is_stable(i_tmp
))
1260 svf_para
.runtest_end_state
= i_tmp
;
1261 LOG_DEBUG("\tend_state = %s", svf_tap_state_name
[svf_para
.runtest_end_state
]);
1265 LOG_ERROR("%s is not valid state", svf_tap_state_name
[i_tmp
]);
1270 // calculate run_count
1271 if ((0 == run_count
) && (min_time
> 0))
1273 run_count
= min_time
* svf_para
.frequency
;
1275 // all parameter should be parsed
1276 if (i
== num_of_argu
)
1280 // run_state and end_state is checked to be stable state
1283 // enter into run_state if necessary
1284 if (cmd_queue_cur_state
!= svf_para
.runtest_run_state
)
1286 svf_add_statemove(svf_para
.runtest_run_state
);
1289 // call jtag_add_clocks
1290 jtag_add_clocks(run_count
);
1292 // move to end_state if necessary
1293 if (svf_para
.runtest_end_state
!= svf_para
.runtest_run_state
)
1295 svf_add_statemove(svf_para
.runtest_end_state
);
1298 if (svf_para
.runtest_run_state
!= TAP_IDLE
)
1300 // RUNTEST can only executed in TAP_IDLE
1301 LOG_ERROR("cannot runtest in %s state", svf_tap_state_name
[svf_para
.runtest_run_state
]);
1305 jtag_add_runtest(run_count
, svf_para
.runtest_end_state
);
1311 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i
, num_of_argu
);
1316 // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
1317 if (num_of_argu
< 2)
1319 LOG_ERROR("invalid parameter of %s", argus
[0]);
1322 if (num_of_argu
> 2)
1324 // STATE pathstate1 ... stable_state
1325 path
= (tap_state_t
*)malloc((num_of_argu
- 1) * sizeof(tap_state_t
));
1328 LOG_ERROR("not enough memory");
1331 num_of_argu
--; // num of path
1332 i_tmp
= 1; // path is from patameter 1
1333 for (i
= 0; i
< num_of_argu
; i
++)
1335 path
[i
] = svf_find_string_in_array(argus
[i_tmp
++], (char **)svf_tap_state_name
, dimof(svf_tap_state_name
));
1336 if (!svf_tap_state_is_valid(path
[i
]))
1338 LOG_ERROR("%s is not valid state", svf_tap_state_name
[path
[i
]]);
1342 if (TAP_RESET
== path
[i
])
1346 jtag_add_pathmove(i
, path
);
1349 num_of_argu
-= i
+ 1;
1353 if (num_of_argu
> 0)
1355 // execute last path if necessary
1356 if (svf_tap_state_is_stable(path
[num_of_argu
- 1]))
1358 // last state MUST be stable state
1359 // TODO: call path_move
1360 jtag_add_pathmove(num_of_argu
, path
);
1361 LOG_DEBUG("\tmove to %s by path_move", svf_tap_state_name
[path
[num_of_argu
- 1]]);
1365 LOG_ERROR("%s is not valid state", svf_tap_state_name
[path
[num_of_argu
- 1]]);
1370 // no need to keep this memory, in jtag_add_pathmove, path will be duplicated
1379 // STATE stable_state
1380 state
= svf_find_string_in_array(argus
[1], (char **)svf_tap_state_name
, dimof(svf_tap_state_name
));
1381 if (svf_tap_state_is_stable(state
))
1383 // TODO: move to state
1384 svf_add_statemove(state
);
1386 LOG_DEBUG("\tmove to %s by svf_add_statemove", svf_tap_state_name
[state
]);
1390 LOG_ERROR("%s is not valid state", svf_tap_state_name
[state
]);
1397 if (num_of_argu
!= 2)
1399 LOG_ERROR("invalid parameter of %s", argus
[0]);
1402 if (svf_para
.trst_mode
!= TRST_ABSENT
)
1404 if (ERROR_OK
!= svf_execute_tap())
1408 i_tmp
= svf_find_string_in_array(argus
[1], (char **)svf_trst_mode_name
, dimof(svf_trst_mode_name
));
1412 jtag_add_reset(1, 0);
1416 jtag_add_reset(0, 0);
1421 LOG_ERROR("unknown TRST mode: %s", argus
[1]);
1424 svf_para
.trst_mode
= i_tmp
;
1425 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name
[svf_para
.trst_mode
]);
1429 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1434 LOG_ERROR("invalid svf command: %s", argus
[0]);
1439 if (debug_level
>= LOG_LVL_DEBUG
)
1441 // for convenient debugging, execute tap if possible
1442 if ((svf_buffer_index
> 0) && \
1443 (((command
!= STATE
) && (command
!= RUNTEST
)) || \
1444 ((command
== STATE
) && (num_of_argu
== 2))))
1446 if (ERROR_OK
!= svf_execute_tap())
1451 // output debug info
1452 if ((SIR
== command
) || (SDR
== command
))
1455 memcpy(&read_value
, svf_tdi_buffer
, sizeof(int));
1456 // in debug mode, data is from index 0
1457 int read_mask
= svf_get_mask_u32(svf_check_tdo_para
[0].bit_len
);
1458 LOG_DEBUG("\tTDO read = 0x%X", read_value
& read_mask
);
1464 // for fast executing, execute tap if necessary
1465 // half of the buffer is for the next command
1466 if (((svf_buffer_index
>= SVF_MAX_BUFFER_SIZE_TO_COMMIT
) || (svf_check_tdo_para_index
>= SVF_CHECK_TDO_PARA_SIZE
/ 2)) && \
1467 (((command
!= STATE
) && (command
!= RUNTEST
)) || \
1468 ((command
== STATE
) && (num_of_argu
== 2))))
1470 return svf_execute_tap();