Added option for reduction of long clock sequences in wavedrom converter

Signed-off-by: Wojciech Gryncewicz <wgryncewicz@antmicro.com>
This commit is contained in:
Wojciech Gryncewicz 2020-12-01 15:26:39 +01:00
parent adea4260e7
commit c5bd980a84
1 changed files with 53 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import sys
import argparse import argparse
import pathlib import pathlib
import wavedrom import wavedrom
import re
from contextlib import contextmanager from contextlib import contextmanager
@ -83,7 +84,33 @@ def readVCD (file):
return vcd return vcd
def parsetowavedrom (file, savetofile = False):
def reduce_clock_sequences (wave) :
''' Remove clock seqnces longer than 2 cycles
not accompanied by other signals changes
Parameters:
wave - dictionary 'signal'->['list of states'] [dict]
'''
for v in wave:
sig = wave[v] # analized signal
other = [wave[i] for i in wave if i!=v] # list of other signals
other = [''.join(s) for s in zip(*other)] # list of concatenated states
other = [len(s.replace('.','')) for s in other] # list of state changes count
sig = [s if o==0 else ' ' for s,o in zip(sig,other)] # keep only when no changes in other
sig = "".join(sig)
cuts = []
for m in re.finditer("(10){2,}",sig):
cuts.append( (m.start()+1, m.end()-1) ) # area to be reduced, leave 1..0
cuts.reverse()
for cut in cuts:
for v,w in wave.items(): # reduce cuts from all signals
wave[v] = w[ :cut[0]] + w[cut[1]: ]
return wave
def parsetowavedrom (file, savetofile = False, reduce_clock = False):
''' Reads and simplifies VCD waveform ''' Reads and simplifies VCD waveform
Generates wavedrom notation. Generates wavedrom notation.
@ -140,7 +167,8 @@ def parsetowavedrom (file, savetofile = False):
if len(line)>=2: if len(line)>=2:
wave [ varsubst[line[1]] ][-1] = line[0] wave [ varsubst[line[1]] ][-1] = line[0]
# TODO: add "double interval support" if reduce_clock:
wave = reduce_clock_sequences(wave)
signals = [] signals = []
for v in wave.keys(): for v in wave.keys():
@ -160,6 +188,9 @@ def parsetowavedrom (file, savetofile = False):
def quoted_strings_wavedrom (wdr) : def quoted_strings_wavedrom (wdr) :
''' Convert wavedrom script to more restrictive ''' Convert wavedrom script to more restrictive
version of JSON with quoted keywords version of JSON with quoted keywords
Parameters:
wdr - wavedrom script [str]
''' '''
wdr = wdr.replace(' signal:',' "signal":') wdr = wdr.replace(' signal:',' "signal":')
wdr = wdr.replace(' name:',' "name":') wdr = wdr.replace(' name:',' "name":')
@ -190,6 +221,11 @@ def main():
"--savesvg", "--savesvg",
help="generate .svg image", help="generate .svg image",
action="store_true") action="store_true")
parser.add_argument(
"-r",
"--reduceclk",
help="reduce clock sequences",
action="store_true")
parser.add_argument( parser.add_argument(
"infile", "infile",
help="VCD waveform file", help="VCD waveform file",
@ -209,7 +245,7 @@ def main():
errors = 0 errors = 0
for f in infile: for f in infile:
try: try:
wdr = parsetowavedrom(f, args.wavedrom) wdr = parsetowavedrom(f, args.wavedrom, args.reduceclk)
if args.savesvg: if args.savesvg:
svg = wavedrom.render( quoted_strings_wavedrom(wdr) ) svg = wavedrom.render( quoted_strings_wavedrom(wdr) )
outfile = f.with_suffix(".svg") outfile = f.with_suffix(".svg")