-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_harness.py
351 lines (290 loc) · 9.65 KB
/
generate_harness.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# ❯ python generate_harness.py --pnr-io-collateral pointwise.io.json --bitstream pointwise.bs && astyle harness.cpp
import argparse
import json
parser = argparse.ArgumentParser(description='Test the cgra')
# parser.add_argument('--IO', metavar='<IO_FILE>', help='File containing mapping between IO ports and files', dest="pnr_io_collateral")
parser.add_argument('--pnr-io-collateral', metavar='<collateral_file>.io.json', help='Collateral file generated by SMT-PNR', required=True)
parser.add_argument('--bitstream', metavar='<BITSTREAM_FILE>', help='Bitstream file containing the CGRA configuration', required=True)
parser.add_argument('--trace-file', help='Trace file', default=None)
parser.add_argument('--max-clock-cycles', help='Max number of clock cyles to run', default=40, type=int)
parser.add_argument('--wrapper-module-name', help='Name of the wrapper module', default='top')
parser.add_argument('--chunk-size', help="Size in bits of the data in the input/output files", default=8, type=int)
parser.add_argument('--output-file-name', help="Name of the generated harness file", default="harness.cpp")
parser.add_argument('--use-jtag', help="Should this test harness use JTAG to write config", default=False, action="store_true")
parser.add_argument('--verify-config', help="Should this test harness read back all the config after writing", default=False, action="store_true")
parser.add_argument('--trace', action="store_true", help="Dump a .vcd using verilator")
parser.add_argument('--trace-file-name', default="top_tb.vcd")
parser.add_argument('--quiet', action="store_true", help="Silence cycle counter")
args = parser.parse_args()
config_data_arr = []
config_addr_arr = []
with open(args.bitstream, "r") as bitstream_file:
for line in bitstream_file:
if line[0] == "#" or line == "\n":
continue # Skip comment or empty lines from serpent bistream
config_addr, config_data = line.split()
config_addr_arr.append(f"0x{config_addr}")
config_data_arr.append(f"0x{config_data}")
config_data_arr_str = "{" + ", ".join(config_data_arr) + "}"
config_addr_arr_str = "{" + ", ".join(config_addr_arr) + "}"
with open(args.pnr_io_collateral, "r") as pnr_collateral:
io_collateral = json.load(pnr_collateral)
# with open(args.IO, "r") as IO_file:
# """
# {
# "<module_name>": "<file_name>",
# "<module_name>": "<file_name>",
# "<module_name>": "<file_name>"
# ...
# }
# """
# IOs = json.load(IO_file)
includes = ""
file_setup = ""
jtag_setup = ""
chip_init = ""
chip_reset = ""
run_config = ""
verify_config = ""
run_test = ""
clk_switch = ""
stall = ""
unstall = ""
input_body = ""
output_body = ""
file_close = ""
wrapper_name = args.wrapper_module_name
includes = f"""
#include "V{wrapper_name}.h"
#include "verilated.h"
#include <iostream>
#include "stdint.h"
#include <fstream>
#include "printf.h"
"""
if args.trace:
step_command = f"step({wrapper_name}, time_step, tfp);"
else:
step_command = f"step({wrapper_name});"
if args.trace:
next_command = f"next({wrapper_name}, time_step, tfp);"
else:
next_command = f"next({wrapper_name});"
trace_setup = ""
if args.trace:
includes += "#include \"verilated_vcd_c.h\"\n"
trace_setup += f"""
Verilated::traceEverOn(true);
VerilatedVcdC* tfp = new VerilatedVcdC;
top->trace(tfp, 99); // What is 99? I don't know! FIXME
tfp->open(\"{args.trace_file_name}\");
uint32_t time_step = 0;
"""
file_close += "tfp->close();\n"
if (args.use_jtag):
includes += '#include "jtagdriver.h"\n'
if args.trace:
jtag_setup += f"""
JTAGDriver jtag({wrapper_name}, tfp, &time_step);
"""
else:
jtag_setup += f"""
JTAGDriver jtag({wrapper_name});
"""
chip_init += f"""
{wrapper_name}->clk_in = 0;
{wrapper_name}->reset_in = 0;
"""
if (args.use_jtag):
chip_init += f"""
jtag.init();
"""
else:
chip_init += f"""
{wrapper_name}->config_addr_in = 0;
{wrapper_name}->config_data_in = 0;
"""
chip_init += f"""
{wrapper_name}->eval();
"""
chip_reset = f"""
{wrapper_name}->reset_in = 1;
{wrapper_name}->eval();
{wrapper_name}->reset_in = 0;
{wrapper_name}->eval();
"""
if (args.use_jtag):
chip_reset += f"""
jtag.reset();
jtag.tck_bringup();
"""
if (args.use_jtag):
stall += f"""
jtag.stall();
"""
else:
stall += f"""
{wrapper_name}->pad_S3_T0_in = 1;
"""
if (args.use_jtag):
unstall += f"""
jtag.unstall();
"""
else:
unstall += f"""
{wrapper_name}->pad_S3_T0_in = 0;
"""
if (args.use_jtag):
run_config += f"""
jtag.write_config(config_addr_arr[i],config_data_arr[i]);
"""
else :
run_config += f"""
{wrapper_name}->config_data_in = config_data_arr[i];
{wrapper_name}->config_addr_in = config_addr_arr[i];
{next_command}
"""
if (args.use_jtag and args.verify_config):
verify_config += f"""
std::cout << "reading configuration" << std::endl;
bool config_error = false;
for (int i = 0; i < {len(config_data_arr)}; i++) {{
uint32_t read_data = jtag.read_config(config_addr_arr[i]);
if (read_data != config_data_arr[i]) {{
printf("ERROR - Iteration=%d, read_data=0x%08x, config_data_arr[i]=0x%08x, config_addr_arr[i]=0x%08x\\n", i, read_data, config_data_arr[i], config_addr_arr[i]);
config_error = true;
}};
}}
if (config_error) {{
std::cout << "error in configuration" << std::endl;
// FIXME: 1-bit IO pads are flipped (bit0 and bit1) causing an error
// exit(1);
}}
"""
if (args.use_jtag):
clk_switch += f"""
jtag.switch_to_fast();
{wrapper_name}->clk_in = 1;
{wrapper_name}->eval();
for (int i = 0; i < 5; i++) {{
{next_command}
}}
"""
# for entry in IOs:
for module in io_collateral:
file_name = f"{module}.raw"
mode = io_collateral[module]["mode"]
if mode == "inout":
raise NotImplementedError()
file_setup += f"""
std::fstream {module}_file("{file_name}", ios::{mode} | ios::binary);
if (!{module}_file.is_open()) {{
std::cout << "Could not open file {file_name}" << std::endl;
return 1;
}}
uint{args.chunk_size}_t {module}_{mode} = 0;
"""
if mode == 'in':
input_body += f"""
{module}_file.read((char *)&{module}_in, sizeof(uint{args.chunk_size}_t));
if ({module}_file.eof()) {{
std::cout << "Reached end of file {file_name}" << std::endl;
break;
}}
"""
for bit, pad in io_collateral[module]["bits"].items():
input_body += f"""
{wrapper_name}->{pad}_in = get_bit({bit}, {module}_in);
"""
else:
output_body += f"{module}_out = 0;\n"
for bit, pad in io_collateral[module]["bits"].items():
output_body += f"""
set_bit({wrapper_name}->{pad}_out, {bit}, {module}_out);
"""
output_body += f"""
{module}_file.write((char *)&{module}_out, sizeof(uint{args.chunk_size}_t));
"""
file_close += f"""
{module}_file.close();
"""
step_trace_args = ""
step_trace_body = ""
if args.trace:
step_trace_args = ", uint32_t &time_step, VerilatedVcdC* tfp"
step_trace_body = f"""
tfp->dump(time_step);
time_step++;
"""
step_def = f"""\
void step(V{wrapper_name} *{wrapper_name}{step_trace_args}) {{
{wrapper_name}->clk_in ^= 1;
{wrapper_name}->eval();
{step_trace_body}
}}
"""
next_step_args = ""
next_step_params = ""
if args.trace:
next_step_args = ", (time_step), (tfp)"
next_step_params = ", time_step, tfp"
log = ""
if not args.quiet:
log = "if (i % 10 == 0) std::cout << \"Cycle: \" << i << std::endl;\n"
next_def = f"""\
#define next(circuit{next_step_params}) \\
do {{ step((circuit){next_step_args}); step((circuit){next_step_args}); }} while (0)
"""
harness = f"""\
{includes}
{next_def}
static const uint32_t config_data_arr[] = {config_data_arr_str};
static const uint32_t config_addr_arr[] = {config_addr_arr_str};
// TODO: How many cycles do we actually need to hold reset down?
static const uint32_t NUM_RESET_CYCLES = 5;
{step_def}
uint8_t get_bit(uint8_t bit_position, uint{args.chunk_size}_t bit_vector) {{
return (bit_vector >> bit_position) & 1;
}}
void set_bit(uint8_t value, uint8_t bit_position, uint{args.chunk_size}_t &bit_vector) {{
bit_vector |= (value << bit_position);
}}
int main(int argc, char **argv) {{
Verilated::commandArgs(argc, argv);
V{wrapper_name}* {wrapper_name} = new V{wrapper_name};
{trace_setup}
{file_setup}
//Intialize jtag driver
{jtag_setup}
//Initialize all inputs to known values
{chip_init}
//Reset the chip
{chip_reset}
std::cout << "Done resetting" << std::endl;
{stall}
// Start clock at 1
{wrapper_name}->clk_in = 1;
{wrapper_name}->eval();
std::cout << "Beginning configuration" << std::endl;
for (int i = 0; i < {len(config_data_arr)}; i++) {{
{run_config}
}}
{verify_config}
std::cout << "Done configuring" << std::endl;
{clk_switch}
{unstall}
std::cout << "Running test" << std::endl;
for (int i = 0; i < {args.max_clock_cycles}; i++) {{
{input_body}
{step_command} // clk_in = 0
{output_body}
{step_command} // clk_in = 1
{log}
}}
std::cout << "Done testing" << std::endl;
{file_close}
delete {wrapper_name};
}}
"""
with open(args.output_file_name, "w") as harness_file:
harness_file.write(harness)