15from writer
import VCDWriter
16from bitstring
import BitArray
22if sys.version_info[0] < 3:
23 from itertools
import izip
as zip
30def printBin(evt, wordwidth=8, linewidth=8, paraheight=4):
31 words = [evt[word:word + wordwidth].bin
for word
in range(0, len(evt), wordwidth)]
32 lines = ([
' '.join(words[n:n + linewidth])
for n
in range(0, len(words), linewidth)])
33 paras = ([
'\n'.join(lines[n:n + paraheight])
for n
in range(0, len(lines), paraheight)])
34 print(
'\n\n'.join(paras))
37def printHex(evt, wordwidth=32, linewidth=8, paraheight=4):
38 words = [evt[word:word + wordwidth].hex
for word
in range(0, len(evt), wordwidth)]
39 lines = ([
' '.join(words[n:n + linewidth])
for n
in range(0, len(words), linewidth)])
40 paras = ([
'\n'.join(lines[n:n + paraheight])
for n
in range(0, len(lines), paraheight)])
41 print(
'\n\n'.join(paras))
46dddd(15 downto 0) & cntr125M(15 downto 0) &
47hitMapTsf(191 downto 0) & valid_tracker(0 downto 0) &
49tracker_out[0](428 downto 210) & ccSelf(8 downto 0) &
50unamed (77 downto 36) &
51mergers[5](255 downto 236) & mergers[5](235 downto 0) &
52mergers[4](255 downto 236) & mergers[4](235 downto 0) &
53mergers[3](255 downto 236) & mergers[3](235 downto 0) &
54mergers[2](255 downto 236) & mergers[2](235 downto 0) &
55mergers[1](255 downto 236) & mergers[1](235 downto 0) &
56mergers[0](255 downto 236) & mergers[0](235 downto 0)
60ddd(15 downto 0) & cntr125M2D(15 downto 0) &
61"000" & TSF0_input(218 downto 210) &
62"000" & TSF2_input(218 downto 210) &
63"000" & TSF4_input(218 downto 210) &
64"000" & TSF6_input(218 downto 210) &
65"000" & TSF8_input(218 downto 210) &
68 ''.join([f
"""TSF{sl:d}_input({h:d} downto {h - 7:d}) &
69TSF{sl:d}_input({h - 8:d} downto {h - 20:d}) &
70""" for sl
in range(0, 9, 2)
for h
in range(209, 0, -21)]) + \
71 """unamed(901 downto 0)
74signalsall = signalsnk + signalstsf2
77operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
78 ast.Div: op.truediv, ast.Pow: op.pow, ast.USub: op.neg}
82 return eval_(ast.parse(expr, mode=
'eval').body)
86 if isinstance(node, ast.Num):
88 elif isinstance(node, ast.BinOp):
89 return operators[type(node.op)](eval_(node.left), eval_(node.right))
90 elif isinstance(node, ast.UnaryOp):
91 return operators[type(node.op)](eval_(node.operand))
96def makeAtlas(signals, evtsize):
98 Make bitmap from VHDL signal assignments.
99 input: string containing b2l signal designations.
100 output: list of lists
101 [ [ name, start, stop, pos, size, module ], ... ]
105 finesses = re.findall(r'(.+?)<=(.+?);', signals, re.DOTALL)
106 if len(finesses) != sum(1
if width > 0
else 0
for width
in evtsize):
107 raise Exception(
'Number of valid signal assignments does not match HSLB data dimension: ' + str(evtsize))
108 for finesse
in finesses:
109 for item
in re.split(
r'[\s\n]?&[\s\n]*', finesse[1].strip()):
110 item = re.sub(
r'--.+',
'', item)
111 dummy = re.match(
r'"([01]+)"', item)
113 sig = re.match(
r'(.+)\s*\((.+) downto (.+)\)', item.strip())
117 print(
'signal: ' + item.strip())
118 start = eval_expr(sig.group(2))
119 stop = eval_expr(sig.group(3))
120 size = start - stop + 1
122 raise ValueError(
'Vector size cannot be 0 or negative.')
125 size = len(dummy.group(1))
127 start, stop = size - 1, 0
128 atlas.append([name, start, stop, pos, size, finesse[0]])
130 if pos != sum(evtsize):
132 f
'Size of atlas:{pos} does not match event size:{evtsize}')
137 return signal[0] ==
"unamed"
140def unpack(meta, headers, triggers, atlas, writer, evtsize):
142 transform into VCD signals from clock-seperated data
and hitmap
144 unpackwith = ', '.join([f
'bin:{sig[4]}' for sig
in atlas])
145 vcdVars = [writer.register_var(
146 sig[5], sig[0] + f
'[{sig[1]}:{sig[2]}]',
'wire', size=sig[4])
if
147 not isUnamed(sig)
else None for sig
in atlas]
148 event = writer.register_var(
'm',
'event',
'event')
149 eventNo = writer.register_var(
'm',
'eventNo',
'integer')
150 subrun = writer.register_var(
'm',
'subrun',
'integer')
151 run = writer.register_var(
'm',
'run',
'integer')
152 delays = [writer.register_var(
'm', f
'delay{i}',
'wire', size=9)
153 for i
in range(len(evtsize))]
154 lastvalues = [
None] * len(atlas)
157 power = period.bit_length() - 1
158 print(
'converting to waveform ...')
162 change = writer.change
163 for trgInd, trg
in enumerate(triggers):
171 timestamp = iteration << power
172 writer.change(event, timestamp,
'1')
174 writer.change(eventNo, timestamp, meta[trgInd][0])
175 writer.change(run, timestamp, meta[trgInd][1])
176 writer.change(subrun, timestamp, meta[trgInd][2])
177 for i, d
in enumerate(delays):
178 if len(headers[trgInd][i]) <= 0:
180 writer.change(d, timestamp, headers[trgInd][i][64 + 11: 64 + 20].uint)
181 clocks = list(trg.cut(sum(evtsize)))
184 clocks = clocks[1:48] + [clocks[0]]
188 if timestamp & 1023 == 0:
189 print(f
'\r{str(timestamp)[:-3]} us converted', end=
"")
191 timestamp = iteration << power
193 values = clock.unpack(unpackwith)
194 for i, sig
in enumerate(atlas):
196 if dummycheck
and '1' in values[i]
and '0' in values[i]:
197 print(f
'[Warning] non-uniform dummy value detected at {timestamp}ns: {sig[3] % sum(evtsize)}')
200 if values[i] != lastvalues[i]:
201 change(vcdVars[i], timestamp, values[i])
215def writeVCD(meta, data, atlas, fname, evtsize, combine=False):
217 evt: 2D list of HSLB buffers
218 atlas: list of iterates
221 samples = len(list(filter(
None, data[0]))[0]) // list(filter(
None, evtsize))[0]
222 NullArray = BitArray([])
227 clocks.append(BitArray([]).join(
228 itertools.chain.from_iterable(zip(
230 *[evt[buf][32 * 3:].cut(evtsize[buf])
231 if evtsize[buf] > 0
else [NullArray] * samples
233 for buf
in range(len(evtsize))]))))
234 headers.append([evt[buf][:32 * 3]
for buf
in range(len(evtsize))])
235 with open(fname,
'w')
as fout:
236 with VCDWriter(fout, timescale=
'1 ns', date=
'today')
as writer:
239 raise Exception(
'Combine has not been updated. Use literal instead.')
242 unpack(meta, headers, clocks, atlas, writer, evtsize)
245if __name__ ==
"__main__":
247 parser = argparse.ArgumentParser()
248 parser.add_argument(
"-i",
"--pickle", help=
"input pickle file")
249 parser.add_argument(
"-s",
"--signal", help=
"input signal file")
250 parser.add_argument(
"-o",
"--output", help=
"output VCD file")
251 parser.add_argument(
"-nc",
"--nocheck", help=
"disable dummy variable check",
253 args = parser.parse_args()
255 with open(args.signal)
as fin:
256 evtsize = [int(width)
for width
in fin.readline().split()]
257 print(
'interpreting B2L data with dimension ' + str(evtsize))
258 atlas = makeAtlas(fin.read(), evtsize)
260 atlas = makeAtlas(signalsall, evtsize)
263 rfp = open(pica,
'rb')
264 data = pickle.load(rfp)
265 meta = pickle.load(rfp)
270 fname = args.output
if args.output
else pica[:pica.rfind(
'.')] +
'.vcd'
271 writeVCD(meta, data, atlas, fname, evtsize)