1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
31
32
33
34
35
36
37
38
39
40
41
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
98
99
100
101
102
103
104
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
137
138
139
140
141
142
149
150
154
155
160
166
167
168
169
170
171
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jtag/jtag.h>
#include <transport/transport.h>
#include "commands.h"
struct cmd_queue_page {
struct cmd_queue_page *next;
void *address;
size_t used;
...};
#define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
static struct cmd_queue_page *cmd_queue_pages;
static struct cmd_queue_page *cmd_queue_pages_tail;
static struct jtag_command *jtag_command_queue;
static struct jtag_command **next_command_pointer = &jtag_command_queue;
void jtag_queue_command(struct jtag_command *cmd)
{
if (!transport_is_jtag()) {
/* ... */
LOG_ERROR("JTAG API jtag_queue_command() called on non JTAG interface");
}if (!transport_is_jtag()) { ... }
cmd->next = NULL;
struct jtag_command **last_cmd = next_command_pointer;
assert(last_cmd);
assert(!*last_cmd);
*last_cmd = cmd;
next_command_pointer = &cmd->next;
}{ ... }
void *cmd_queue_alloc(size_t size)
{
struct cmd_queue_page **p_page = &cmd_queue_pages;
int offset;
uint8_t *t;
/* ... */
union worse_case_align {
int i;
long l;
float f;
void *v;
...};
#define ALIGN_SIZE (sizeof(union worse_case_align))
size = (size + ALIGN_SIZE - 1) & (~(ALIGN_SIZE - 1));
if (*p_page) {
p_page = &cmd_queue_pages_tail;
if (CMD_QUEUE_PAGE_SIZE < (*p_page)->used + size)
p_page = &((*p_page)->next);
}if (*p_page) { ... }
if (!*p_page) {
*p_page = malloc(sizeof(struct cmd_queue_page));
(*p_page)->used = 0;
size_t alloc_size = (size < CMD_QUEUE_PAGE_SIZE) ?
CMD_QUEUE_PAGE_SIZE : size;
(*p_page)->address = malloc(alloc_size);
(*p_page)->next = NULL;
cmd_queue_pages_tail = *p_page;
}if (!*p_page) { ... }
offset = (*p_page)->used;
(*p_page)->used += size;
t = (*p_page)->address;
return t + offset;
}{ ... }
static void cmd_queue_free(void)
{
struct cmd_queue_page *page = cmd_queue_pages;
while (page) {
struct cmd_queue_page *last = page;
free(page->address);
page = page->next;
free(last);
}while (page) { ... }
cmd_queue_pages = NULL;
cmd_queue_pages_tail = NULL;
}{ ... }
void jtag_command_queue_reset(void)
{
cmd_queue_free();
jtag_command_queue = NULL;
next_command_pointer = &jtag_command_queue;
}{ ... }
struct jtag_command *jtag_command_queue_get(void)
{
return jtag_command_queue;
}{ ... }
/* ... */
void jtag_scan_field_clone(struct scan_field *dst, const struct scan_field *src)
{
dst->num_bits = src->num_bits;
dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(DIV_ROUND_UP(src->num_bits, 8)), src->num_bits);
dst->in_value = src->in_value;
}{ ... }
enum scan_type jtag_scan_type(const struct scan_command *cmd)
{
int type = 0;
for (unsigned int i = 0; i < cmd->num_fields; i++) {
if (cmd->fields[i].in_value)
type |= SCAN_IN;
if (cmd->fields[i].out_value)
type |= SCAN_OUT;
}for (unsigned int i = 0; i < cmd->num_fields; i++) { ... }
return type;
}{ ... }
unsigned int jtag_scan_size(const struct scan_command *cmd)
{
unsigned int bit_count = 0;
for (unsigned int i = 0; i < cmd->num_fields; i++)
bit_count += cmd->fields[i].num_bits;
return bit_count;
}{ ... }
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
{
unsigned int bit_count = jtag_scan_size(cmd);
*buffer = calloc(1, DIV_ROUND_UP(bit_count, 8));
bit_count = 0;
LOG_DEBUG_IO("%s num_fields: %u",
cmd->ir_scan ? "IRSCAN" : "DRSCAN",
cmd->num_fields);
for (unsigned int i = 0; i < cmd->num_fields; i++) {
if (cmd->fields[i].out_value) {
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) {
char *char_buf = buf_to_hex_str(cmd->fields[i].out_value,
(cmd->fields[i].num_bits > DEBUG_JTAG_IOZ)
? DEBUG_JTAG_IOZ
: cmd->fields[i].num_bits);
LOG_DEBUG("fields[%u].out_value[%u]: 0x%s", i,
cmd->fields[i].num_bits, char_buf);
free(char_buf);
}if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) { ... }
buf_set_buf(cmd->fields[i].out_value, 0, *buffer,
bit_count, cmd->fields[i].num_bits);
}if (cmd->fields[i].out_value) { ... } else {
LOG_DEBUG_IO("fields[%u].out_value[%u]: NULL",
i, cmd->fields[i].num_bits);
}else { ... }
bit_count += cmd->fields[i].num_bits;
}for (unsigned int i = 0; i < cmd->num_fields; i++) { ... }
return bit_count;
}{ ... }
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
{
int bit_count = 0;
int retval;
retval = ERROR_OK;
for (unsigned int i = 0; i < cmd->num_fields; i++) {
/* ... */
if (cmd->fields[i].in_value) {
const unsigned int num_bits = cmd->fields[i].num_bits;
uint8_t *captured = buf_set_buf(buffer, bit_count,
malloc(DIV_ROUND_UP(num_bits, 8)), 0, num_bits);
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) {
char *char_buf = buf_to_hex_str(captured,
(num_bits > DEBUG_JTAG_IOZ)
? DEBUG_JTAG_IOZ
: num_bits);
LOG_DEBUG("fields[%u].in_value[%u]: 0x%s",
i, num_bits, char_buf);
free(char_buf);
}if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) { ... }
if (cmd->fields[i].in_value)
buf_cpy(captured, cmd->fields[i].in_value, num_bits);
free(captured);
}if (cmd->fields[i].in_value) { ... }
bit_count += cmd->fields[i].num_bits;
}for (unsigned int i = 0; i < cmd->num_fields; i++) { ... }
return retval;
}{ ... }