forked from fjullien/jtag_vpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jtagServer.cpp
314 lines (271 loc) · 6.85 KB
/
jtagServer.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2014, Franck Jullien <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and non-source forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in non-source form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS WORK IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* WORK, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <argp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include "jtagServer.h"
#include "jtag_common.h"
int VerilatorJtagServer::gen_clk(uint64_t t, int nb_period, uint8_t *tck, uint8_t tdo,
uint8_t *captured_tdo, int restart, int get_tdo)
{
static bool gen_clk_begin = 1;
static bool tck_low = 1;
static uint64_t time;
static int cur_period = 0;
static int capture_done = 0;
if (restart) {
cur_period = 0;
gen_clk_begin = 1;
tck_low = 1;
return DONE;
}
if (gen_clk_begin) {
gen_clk_begin = 0;
time = t;
}
if (cur_period < (nb_period * tck_period)) {
if (tck_low) {
capture_done = 0;
*tck = 0;
if ((t - time) > tck_period) {
time = t;
tck_low = 0;
}
return IN_PROGRESS;
} else {
*tck = 1;
if (get_tdo && !capture_done) {
capture_done = 1;
*captured_tdo = tdo;
}
if ((t - time) > tck_period) {
time = t;
tck_low = 1;
}
cur_period++;
return IN_PROGRESS;
}
}
capture_done = 0;
gen_clk_begin = 1;
return DONE;
}
void VerilatorJtagServer::gen_clk_restart()
{
gen_clk(0, 0, NULL, 0, NULL, 1, 0);
}
int VerilatorJtagServer::reset_tap(uint64_t t, uint8_t *tms, uint8_t *tck)
{
*tms = 1;
if (gen_clk(t, 5, tck, -1, NULL, 0, 0) == DONE) {
gen_clk_restart();
*tms = 0;
return DONE;
}
return IN_PROGRESS;
}
int VerilatorJtagServer::goto_run_test_idle(uint64_t t, uint8_t *tms, uint8_t *tck)
{
*tms = 0;
if (gen_clk(t, 1, tck, -1, NULL, 0, 0) == DONE) {
gen_clk_restart();
return DONE;
}
return IN_PROGRESS;
}
int VerilatorJtagServer::do_tms_seq(uint64_t t, int length, int nb_bits,
uint8_t *buffer, uint8_t *tms, uint8_t *tck)
{
static int i = 0;
static int j = 0;
static unsigned char data;
static int start = 1;
int nb_bits_rem;
int nb_bits_in_this_byte;
// Number of bits to send in the last byte
nb_bits_rem = nb_bits % 8;
while (i < length) {
// If we are in the last byte, we have to send only
// nb_bits_rem bits. If not, we send the whole byte.
nb_bits_in_this_byte = (i == (length - 1)) ? nb_bits_rem : 8;
if (start) {
data = buffer[i];
start = 0;
}
while (j < nb_bits_in_this_byte) {
*tms = 0;
if ((data & 1) == 1)
*tms = 1;
if (gen_clk(t, 1, tck, 0, NULL, 0, 0) == DONE) {
j++;
data = data >> 1;
gen_clk_restart();
}
return IN_PROGRESS;
}
j = 0;
i++;
start = 1;
return IN_PROGRESS;
}
*tms = 0;
i = 0;
j = 0;
start = 1;
return DONE;
}
int VerilatorJtagServer::do_scan_chain(uint64_t t, int length, int nb_bits,
unsigned char *buffer_out, unsigned char *buffer_in, uint8_t *tms,
uint8_t *tck, uint8_t *tdi, uint8_t tdo, int flip_tms)
{
static int i = 0;
static int j = 0;
static int index = 0;
static int start = 1;
static unsigned char data;
static uint8_t captured_tdo;
int nb_bits_rem;
int nb_bits_in_this_byte;
// Number of bits to send in the last byte
nb_bits_rem = nb_bits % 8;
while (i < length) {
// If we are in the last byte, we have to send only
// nb_bits_rem bits if it's not zero.
// If not, we send the whole byte.
nb_bits_in_this_byte = (i == (length - 1)) ? ((nb_bits_rem == 0) ? 8 : nb_bits_rem) : 8;
if (start) {
buffer_in[i] = 0;
if (buffer_out)
data = buffer_out[i];
else
data = 0;
start = 0;
}
while (j < nb_bits_in_this_byte) {
*tdi = 0;
if ((data & 1) == 1)
*tdi = 1;
// On the last bit, set TMS to '1'
if (((j == (nb_bits_in_this_byte - 1)) && (i == (length - 1))) && (flip_tms == 1))
*tms = 1;
if (gen_clk(t, 1, tck, tdo, &captured_tdo, 0, 1) == DONE) {
if (captured_tdo)
buffer_in[i] |= (1 << j);
else
buffer_in[i] &= ~(1 << j);
j++;
data = data >> 1;
gen_clk_restart();
}
return IN_PROGRESS;
}
j = 0;
i++;
start = 1;
return IN_PROGRESS;
}
*tdi = 0;
*tms = 0;
i = 0;
j = 0;
start = 1;
return DONE;
}
int VerilatorJtagServer::init_jtag_server(int port)
{
return ::init_jtag_server(port);
}
int VerilatorJtagServer::doJTAG(uint64_t t, uint8_t *tms, uint8_t *tdi, uint8_t *tck, uint8_t tdo)
{
if (!cmd_in_progress)
check_for_command(&packet);
switch (jtag_state) {
case CHECK_CMD:
switch (packet.cmd) {
case CMD_RESET:
cmd_in_progress = 1;
jtag_state = TAP_RESET;
break;
case CMD_TMS_SEQ:
cmd_in_progress = 1;
jtag_state = DO_TMS_SEQ;
break;
case CMD_SCAN_CHAIN:
cmd_in_progress = 1;
tms_flip = 0;
jtag_state = SCAN_CHAIN;
break;
case CMD_SCAN_CHAIN_FLIP_TMS:
cmd_in_progress = 1;
tms_flip = 1;
jtag_state = SCAN_CHAIN;
break;
default:
break;
}
break;
case TAP_RESET:
if (reset_tap(t, tms, tck) == DONE) {
jtag_state = GOTO_IDLE;
}
break;
case GOTO_IDLE:
if (goto_run_test_idle(t, tms, tck) == DONE) {
cmd_in_progress = 0;
jtag_state = CHECK_CMD;
}
break;
case DO_TMS_SEQ:
if (do_tms_seq(t, packet.length, packet.nb_bits, packet.buffer_out, tms, tck) == DONE) {
cmd_in_progress = 0;
jtag_state = CHECK_CMD;
}
break;
case SCAN_CHAIN:
*tms = 0;
if (do_scan_chain(t, packet.length, packet.nb_bits, packet.buffer_out,
packet.buffer_in, tms, tck, tdi, tdo, tms_flip) == DONE) {
cmd_in_progress = 0;
send_result_to_server(&packet);
jtag_state = CHECK_CMD;
}
break;
case FINISHED:
break;
}
return 0;
}
VerilatorJtagServer::VerilatorJtagServer(uint64_t period) {
tck_period = period;
jtag_state = CHECK_CMD;
cmd_in_progress = 0;
}
VerilatorJtagServer::~VerilatorJtagServer() {
}