forked from nwuerfel/e1039vhdlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
road2vhdl.py
399 lines (329 loc) · 13.6 KB
/
road2vhdl.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# reads roads.tsv file and generates L1 trigger matrix for the new mapping
# Forhad enacted as of end of 2019
# shit its really hard to do a direct comparison of firmware because I have
# no idea what order the roads were sorted in the old version, and I don't
# care to figure it out, should just run pulser test to confirm full
# test....
# spacing from tab expand is dumb but oh well...
# N wuerfel
# ~ AP AP AP AP ~
pxbins = 12
nlevels = 9
import math
import sys
def roadIdToHodoWord(roadid):
hodoword = ''
h1 = str(int((abs(roadid)-1)/(16*16*16)) + 1)
h2 = str(int((abs(roadid)-1)/(16*16))%16 + 1)
h3 = str(int((abs(roadid)-1)/16)%16 + 1)
h4 = str(int(abs(roadid)-1)%16 + 1)
#print "roadid, h1, h2, h3, h4"
#print roadid
#print h1
#print h2
#print h3
#print h4
hodoword = h1.zfill(2)+h2.zfill(2)+h3.zfill(2)+h4.zfill(2)
#print hodoword
return hodoword
# generates array of [hodo,firmwarename] elements
# returns list
def readL1FirmwareMap(filename):
maplines = []
#print "reading mapping for " + filename
print("reading mapping for " + filename)
with open(filename) as fp:
mapping = fp.readlines()
mapping = mapping[1:]
for line in mapping:
line = line.strip().split(',')
maplines.append(line[1:4:2])
return maplines
# reads roadset and generates and generates array of roads as
# [roadid, hodo_pattern_word] elements
# returns roadset, array with num in each bin, indexed by bin
def readL1roadset(filename):
L1roadset=[]
#print"reading roadset for " + filename
print("reading roadset for " + filename)
with open(filename) as fp:
roads = fp.readlines()
for rawroad in roads:
hodo_pattern_word = ''
rawroad = rawroad.strip().split()
roadid = int(rawroad[0])
print(roadid)
hodo_pattern_word = roadIdToHodoWord(roadid)
PxBin = 1; # force it
road = [roadid, hodo_pattern_word, PxBin]
L1roadset.append(road)
binnumbers = [0]*pxbins
for road in L1roadset:
binnumbers[int(road[2])] = binnumbers[int(road[2])] + 1
L1roadset = sorted(L1roadset, key = lambda x: x[2])
return L1roadset,binnumbers
# returns string line of the form: "if(4 fold coincidence)then"
def road2firmwareline(mapping, hodoword, half):
firmwarewords=[]
for i in range(1,5):
searchword = "H"+str(i)+half
# god I hate the corner case of H4 having u and b
if i==4:
searchword = searchword + "u"
searchword = searchword+str(hodoword[2*(i-1):2*i].zfill(2))
for item in mapping:
found = 0
if str(item[0]) == searchword:
firmwarewords.append(item[1])
found = 1
break
if not found:
print("couldn't find this hit:")
print(searchword)
quit()
# constructing the line with stupid formatting
vhdline = " if("
for idx,word in enumerate(firmwarewords):
letter = word[0]
# stupid stripping formatting
# strip only one leading zero if it exists
num = word[1:]
if num[0] == '0':
num = num[-1]
vhdline = vhdline+str(letter)+\
"("+str(num).rjust(2)+\
")=\'1\'"
if idx < 3:
vhdline = vhdline+" AND "
else:
vhdline = vhdline+ " )then\n"
return vhdline
# takes set of up to 4 signals to or reduce
# produces a line like: "if(up to 4 fold OR)then"
def ORreduce(header,signals,level,pxbin):
if len(signals) < 1 or len(signals) > 4:
print("wrong length for ,levelsigset...")
print (signals)
quit()
line=" if("
for sigdx,sig in enumerate(signals):
line = line + header + "_temp_lv" + str(level) + "_" +\
str(pxbin).zfill(2) + "(" + str(sig).rjust(4) + ")='1' "
if sigdx < len(signals)-1:
line = line + "OR "
line = line + ")then\n"
print(line)
return line
def main(version):
if version == 460:
half = 'T'
halfname = "top"
else:
half = 'B'
halfname = "bot"
vhdlfilename = "e1039_vhdl/code_" + str(version) + ".vhd"
vhdlheaderfname = "e1039vhdtext/" + str(version) + "header.txt"
mappingfile = "e1039_TDC_mappings/" + str(version)+"mapping.txt"
hodomapping = readL1FirmwareMap(mappingfile)
print(hodomapping)
#proadfname = "rs_103/pos_"+halfname+".txt"
#mroadfname = "rs_103/neg_"+halfname+".txt"
proadfname = "/Users/ievgenlavrukhin/work/SpinQuest/Trigger_RoadSets/2024/136/pos_"+halfname+".txt"
mroadfname = "/Users/ievgenlavrukhin/work/SpinQuest/Trigger_RoadSets/2024/136/neg_"+halfname+".txt"
proads,ppxbins = readL1roadset(proadfname)
mroads,mpxbins = readL1roadset(mroadfname)
print (ppxbins)
print (mpxbins)
# multidim: [[numperbin_0..._i],....] indx is level
psiglens = []
msiglens = []
# somethings are nasty hardcoded headers...
with open(vhdlfilename,"w") as vfp:
# copy header over
vfp.write("--- VHLD for roadsets"+ proadfname+ " and "+ mroadfname + "\n")
with open(vhdlheaderfname,"r") as hfp:
lines = hfp.readlines()
for line in lines:
vfp.write(line)
# now write signals needed
# this take input about num roads -> 12 signals for each pxbin (can
# change later but for now just recreate old version)
# num signals in l1 is numroads in bin
#//TODO
# 8 levels for pos and 8 for min
# TODO change PF and NF to MF just recreating now
for header in ["PF","NF"]:
if header == "PF":
binlist = ppxbins
siglens = psiglens
else:
binlist = mpxbins
siglens = msiglens
#do reduction at each level
lastbinval=binlist[:]
for level in range(1,nlevels):
siglens.append(lastbinval[:])
for binno in range(0,pxbins):
# stupid like most things its all corner cases
if level == 1:
divisor = 1
else:
divisor = 4.0
siglen = int(math.ceil(lastbinval[binno]/divisor))
# take off 1 unless 0 in the bin...
if lastbinval[binno] != 0:
offset = 1
else:
offset = 0
line = " signal " + header + "_temp_lv" +\
str(level)
if level!=nlevels-1:
line = line + "_" + str(binno).zfill(2) + \
": std_logic_vector" + "(" + \
str(siglen - offset).rjust(4) + \
" downto 0);\n"
vfp.write(line)
# update lasbinvals
lastbinval[binno] = siglen
# other stupid corner case is last line, always same
line_final = " signal " + header + "_temp_lv8: " + \
"std_logic_vector( 11 downto 0);\n"
vfp.write(line_final)
# write the mapping that swaps channels around between sampling and
# the firmware values...
with open("e1039vhdtext/signalmapping.txt","r") as sigmapfp:
lines = sigmapfp.readlines()
for line in lines:
vfp.write(line)
# now write l0, doesn't change because it just combines u/d on
# vertical in ST4 and kills a clock for other signals
with open("e1039vhdtext/level0.txt","r") as l0fp:
lines = l0fp.readlines()
for line in lines:
vfp.write(line)
# L1 #
vfp.write("lookuptable_LV1 : process(c1)\nbegin\n")
vfp.write(" if c1'event and c1='1' then\n")
# we have num per bin, but its clunky to iterate based on bin
# instead, we sort roads when we read them in by bin indx, then we
# iterate the binidx as we write each road and check that it
# matches expectation and scream if it doesnt
for header in ["PF","NF"]:
if header == "PF":
roadlist = proads
binlist = ppxbins
else:
roadlist = mroads
binlist = mpxbins
binidx = 0
bincount = 0
for road in roadlist:
# check if we're in the next pxbin
# and check if we wrote the num expected for that bin to
# match signal length
if int(binidx) != int(road[2]):
if bincount != int(binlist[binidx]):
print("bincount written doesn't match sig len")
print("count: " + str(bincount))
print("siglen: " + str(binlist[binidx]))
quit()
binidx = binidx+1
bincount = 0
binidx = int(road[2])
coincidenceline = road2firmwareline(hodomapping,\
road[1],half)
signal = header + "_temp_lv1_" + str(binidx).zfill(2) +\
"(" + str(bincount).rjust(4) + ")"
# write this ugly guy
vfp.write(coincidenceline)
vfp.write(" "+signal+"<=\'1\';\n")
vfp.write(" else\n")
vfp.write(" "+signal+"<=\'0\';\n")
vfp.write(" end if;\n")
bincount = bincount + 1
## more stupid corner cases, assigning 0 to bins with no hits
for i in range(0,pxbins):
if int(binlist[i]) == 0:
vfp.write("\t\t " + header + "_temp_lv1_"+\
str(i).zfill(2)+"( 0)<='0';\n")
vfp.write(" end if;\nend process;\n")
# all except L0-1 from here just do or reduction...
# L2 - L7 #
for level in range(2,nlevels):
# header stuff for each level
vfp.write("lookuptable_LV"+str(level)+ " : process(c1)\n")
vfp.write("begin\n")
vfp.write(" if c1\'event and c1=\'1\' then\n")
for header in ["PF","NF"]:
if header == "PF":
binlist = psiglens
else:
binlist = msiglens
# doing reduction on sigs requires us to know how many sigs
# at each level in each bin...
nsigsforlevel = binlist[level-1]
# janky way to pick max four sigs to or reduce
sigset = []
for bindx, nsig in enumerate(nsigsforlevel):
# corner case on empty bins
# cheat...
if nsig == 0:
nsig = 1
for i in range(0,nsig):
# if we have 4 OR we're at the end of the set
# take up to 4 and turn into OR reduction
# then write to file
sigset.append(i)
if ((i+1) % 4 == 0) or (i==nsig-1):
print("hit module")
vhdlORreduceline = ORreduce(header,sigset,\
level-1,bindx)
# if A OR B OR C OR D then
vfp.write(vhdlORreduceline)
# set next sig to 1
# corner case on level 8....
if level == nlevels-1:
vfp.write("\t\t " + header+"_temp_lv"+\
str(level)+"("+\
str(bindx).rjust(4) +\
")<=\'1\';\n")
else:
vfp.write("\t\t " + header+"_temp_lv"+\
str(level)+ "_"+str(bindx).zfill(2) + "("+\
str(int(sigset[0]/4)).rjust(4) +\
")<=\'1\';\n")
# or
vfp.write("\t\t else\n")
# set next sig to 0
# again corner case on lastlev
if level == nlevels-1:
vfp.write("\t\t " + header+"_temp_lv"+\
str(level)+ "("+\
str(bindx).rjust(4) +\
")<=\'0\';\n")
else:
vfp.write("\t\t " + header+"_temp_lv"+\
str(level)+ "_"+str(bindx).zfill(2) + "("+\
str(int(sigset[0]/4)).rjust(4) +\
")<=\'0\';\n")
# wrap up this or reduction
vfp.write("\t\t end if;\n")
# clear sigs for next line
sigset[:] = []
# L8 is basically the same but
# only one sig rather than many bins
# trailers at each level
vfp.write(" end if;\n")
vfp.write("end process;\n")
# tail #
vfp.write("end rtl;")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: $> python road2vhdl.py <revision number>")
quit()
else:
version = int(sys.argv[1])
if version not in [460,470]:
print("please give revision 460 or 470")
quit()
main(version)