-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect_cfpzoo_kernel.py
369 lines (306 loc) · 13.6 KB
/
select_cfpzoo_kernel.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# *******************************************************************************
# * Copyright 2016 -- 2022 IBM Corporation
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# ********************************************************************************
# @brief A script for editing the files needed to select the cFp_Zoo kernel to be implemented
# @author DID
# @date Nov. 2020
import pathlib
import os
import shutil
from re import search
import sys
def edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr):
f1 = open(full_file, 'r')
f2 = open(full_file+"_new", 'w')
replaced = 0
uaf_start_detected = taf_start_detected = 0
ddr_component_start_detected = ddr_component_end_detected = 0
ddr_uaf_start_detected = ddr_uaf_end_detected = 0
ddr_taf_start_detected = ddr_taf_end_detected = 0
ddr_Mp2_dummy_start_detected = ddr_Mp2_dummy_end_detected = 0
for s in f1:
new_s = s2 = s
if search("vhdl", full_file):
search_str = " component "
if search(search_str, s):
s2 = " component "+new_kernel+"Application is\n"
replaced = replaced + 1
search_str = " end component "
if search(search_str, s):
s2 = " end component "+new_kernel+"Application;\n"
replaced = replaced + 1
#######################################################################################
# Replacing role interface for DDR Mp0-1 in Role.vdl
# Check component start
search_str = "SHELL / Mem / Mp0 Interface / Start Component"
if search(search_str, s):
s2 = s
ddr_component_start_detected = 1
# Check component stop
search_str = "SHELL / Mem / Mp1 Interface / End Component"
if search(search_str, s):
s2 = s
ddr_component_end_detected = 1
# Replace ddr pattern in component declaration
if (ddr_component_start_detected):
excluding_pattern = "-- auto excluding component Mp0-Mp1 "
if (search(excluding_pattern, s2)):
if (ddr):
s2 = s2.replace(str(excluding_pattern), str(''))
replaced = replaced + 1
else:
if (not ddr):
s2 = excluding_pattern + s2
replaced = replaced + 1
if (ddr_component_end_detected):
ddr_component_start_detected = ddr_component_end_detected = 0;
# Check DDR in UAF start
search_str = "SHELL / Mem / Mp0 Interface / Start in UAF"
if search(search_str, s):
s2 = s
ddr_uaf_start_detected = 1
# Check DDR in UAF stop
search_str = "SHELL / Mem / Mp1 Interface / End in UAF"
if search(search_str, s):
s2 = s
ddr_uaf_end_detected = 1
# Replace ddr pattern in component declaration
if (ddr_uaf_start_detected):
excluding_pattern = "-- auto excluding Mp0-Mp1 in UAF "
if (search(excluding_pattern, s2)):
if (ddr):
s2 = s2.replace(str(excluding_pattern), str(''))
replaced = replaced + 1
else:
if (not ddr):
s2 = excluding_pattern + s2
replaced = replaced + 1
if (ddr_uaf_end_detected):
ddr_uaf_start_detected = ddr_uaf_end_detected = 0;
# Check DDR in TAF start
search_str = "SHELL / Mem / Mp0 Interface / Start in TAF"
if search(search_str, s):
s2 = s
ddr_taf_start_detected = 1
# Check DDR in TAF stop
search_str = "SHELL / Mem / Mp1 Interface / End in TAF"
if search(search_str, s):
s2 = s
ddr_taf_end_detected = 1
# Replace ddr pattern in component declaration
if (ddr_taf_start_detected):
excluding_pattern = "-- auto excluding Mp0-Mp1 in TAF "
if (search(excluding_pattern, s2)):
if (ddr):
s2 = s2.replace(str(excluding_pattern), str(''))
replaced = replaced + 1
else:
if (not ddr):
s2 = excluding_pattern + s2
replaced = replaced + 1
if (ddr_taf_end_detected):
ddr_taf_start_detected = ddr_taf_end_detected = 0;
# Check DDR in dummy start
search_str = "2nd Memory Port dummy connections Start"
if search(search_str, s):
s2 = s
ddr_Mp2_dummy_start_detected = 1
# Check DDR in dummy stop
search_str = "2nd Memory Port dummy connections End"
if search(search_str, s):
s2 = s
ddr_Mp2_dummy_end_detected = 1
# Replace ddr pattern in component declaration
if (ddr_Mp2_dummy_start_detected):
excluding_pattern = "-- auto excluding Mp2 open connections "
if (search(excluding_pattern, s2)):
if (not ddr):
s2 = s2.replace(str(excluding_pattern), str(''))
replaced = replaced + 1
else:
if (ddr):
s2 = excluding_pattern + s2
replaced = replaced + 1
if (ddr_Mp2_dummy_end_detected):
ddr_Mp2_dummy_start_detected = ddr_Mp2_dummy_end_detected = 0;
#######################################################################################
search_str = " UAF: "
if search(search_str, s):
s2 = " UAF: "+new_kernel+"Application\n"
uaf_start_detected = 1
replaced = replaced + 1
search_str = " TAF: "
if search(search_str, s):
s2 = " TAF: "+new_kernel+"Application\n"
taf_start_detected = 1
replaced = replaced + 1
# Checking if we have to comment out UAF
if (uaf_start_detected):
excluding_pattern = "-- auto excluding UAF "
if (search(excluding_pattern, s2)):
if (udp):
s2 = s2.replace(str(excluding_pattern), str(''))
replaced = replaced + 1
else:
if (not udp):
s2 = excluding_pattern + s2
replaced = replaced + 1
if (search(" \);", s)):
uaf_start_detected = 0;
# Checking if we have to comment out TAF
if (taf_start_detected):
excluding_pattern = "-- auto excluding TAF "
if (search(excluding_pattern, s2)):
if (tcp):
s2 = s2.replace(str(excluding_pattern), str(''))
replaced = replaced + 1
else:
if (not tcp):
s2 = excluding_pattern + s2
replaced = replaced + 1
if (search(" \);", s)):
taf_start_detected = 0;
new_s = s.replace(str(s), str(s2))
f2.write(new_s)
if search("tcl", full_file):
search_str = "# IBM-HSL-IP : "
if search(search_str, s):
s2 = "# IBM-HSL-IP : "+new_kernel+" Application Flash\n"
replaced = replaced + 1
search_str = "set ipModName"
if search(search_str, s):
s2 = "set ipModName \""+new_kernel+"Application\"\n"
replaced = replaced + 1
search_str = "set ipName"
if search(search_str, s):
s2 = "set ipName \""+new_kernel.lower()+"\"\n"
replaced = replaced + 1
new_s = s.replace(str(s), str(s2))
f2.write(new_s)
if search("Makefile", full_file):
search_str = ".PHONY: all clean mem_test_flash "
if search(search_str, s):
s2 = ".PHONY: all clean mem_test_flash "+new_kernel.lower()+"\n"
replaced = replaced + 1
search_str = "all: mem_test_flash "
if search(search_str, s):
s2 = "all: mem_test_flash "+new_kernel.lower()+"\n"
replaced = replaced + 1
new_s = s.replace(str(s), str(s2))
f2.write(new_s)
if search("config.h", full_file):
search_str1 = "#define NET_TYPE "
search_str2 = "#define PACK_SIZE "
if search(search_str1, s):
if (udp and tcp):
s2 = search_str1 + "udp //tcp\n"
elif (udp):
s2 = search_str1 + "udp\n"
elif (tcp):
s2 = search_str1 + "tcp\n"
replaced = replaced + 1
elif search(search_str2, s):
s2 = search_str2 + mtu + "\n"
replaced = replaced + 1
new_s = s.replace(str(s), str(s2))
f2.write(new_s)
if search(".hpp", full_file):
search_str1 = "#define DEFAULT_TX_PORT "
search_str2 = "#define DEFAULT_RX_PORT "
if search(search_str1, s):
s2 = search_str1 + port + "\n"
replaced = replaced + 1
elif search(search_str2, s):
s2 = search_str2 + port + "\n"
replaced = replaced + 1
new_s = s.replace(str(s), str(s2))
search_str3 = "#define ENABLE_DDR"
if search(search_str3, s):
if (ddr):
s2 = search_str3 + "\n"
else:
s2 = "// " + search_str3 + "\n"
replaced = replaced + 1
new_s = new_s.replace(str(s), str(s2))
f2.write(new_s)
print("INFO: Edits of file " + full_file + " : "+ str(replaced))
print("#################")
f1.close()
f2.close()
if (replaced != 0):
shutil.copyfile(full_file, full_file+"_backup")
shutil.move(full_file+"_new", full_file)
###################################################################################################
### Main script
###################################################################################################
kernels = ["Harris", "Median_Blur", "MCEuropeanEngine", "Uppercase", "Memtest", "Warp_Transform"]
#print("Available kernels:")
#print('\n'.join(kernels))
# Count the arguments
arguments = len(sys.argv) - 1
if arguments != 6:
print("ERROR: Invalid number of arguments. Expected 6 but provided " + str(arguments) + ". Aborting...")
exit(-1)
kernel_id = -1
for i in range(len(kernels)):
if (kernels[i] == sys.argv[3]):
kernel_id = i
break
if (kernel_id == -1):
print("ERROR: Kernel " + sys.argv[3] + " not found. Aborting...")
exit(-1)
else:
print("INFO: Kernel " + sys.argv[3] + " found at index " + str(kernel_id) + ". Continuing...")
# select tcp/udp role
udp = tcp = 0
if search('udp', sys.argv[1]):
udp = 1
if search('tcp', sys.argv[1]):
tcp = 1
mtu=sys.argv[4]
port=sys.argv[5]
# select ddr in role
if search('ddr_enabled', sys.argv[6]):
ddr = 1
elif search('ddr_disabled', sys.argv[6]):
ddr = 0
else:
print("ERROR: Invalid DDR option. Aborting...")
exit(-1)
if ((kernel_id < 0 ) or kernel_id >= len(kernels)):
print("ERROR: Invalid kernel id. Aborting...")
exit(-1)
new_kernel = kernels[kernel_id]
role = os.getenv('roleName1')
file = "ROLE/"+role+"/hdl/Role.vhdl"
full_file = str(pathlib.Path().absolute()) + '/' + str(file)
print("#################\n"+full_file+"\n----------------")
edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
file = "ROLE/"+role+"/tcl/create_ip_cores.tcl"
full_file = str(pathlib.Path().absolute()) + '/' + str(file)
print("#################\n"+full_file+"\n----------------")
edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
file = "ROLE/"+role+"/hls/Makefile"
full_file = str(pathlib.Path().absolute()) + '/' + str(file)
print("#################\n"+full_file+"\n----------------")
edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
file = "HOST/"+role+"/"+new_kernel.lower()+"/languages/cplusplus/include/config.h"
full_file = str(pathlib.Path().absolute()) + '/' + str(file)
print("#################\n"+full_file+"\n----------------")
edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
file = "ROLE/"+role+"/hls/"+new_kernel.lower()+"/include/"+new_kernel.lower()+".hpp"
full_file = str(pathlib.Path().absolute()) + '/' + str(file)
print("#################\n"+full_file+"\n----------------")
edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)