fail svinterfaces testcases on yosys error exit
[yosys.git] / tests / tools / txt2tikztiming.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import fileinput
5 import sys
6
7 parser = argparse.ArgumentParser(description='Convert vcd2txt output to tikz-timing line.')
8 parser.add_argument('filename', metavar='FILE', help='input txt file')
9 parser.add_argument('signame', metavar='SIG', help='Signal name')
10 parser.add_argument('-s', metavar='scale', default=1.0, type=float, help='Scale all time spans with this factor')
11 parser.add_argument('-l', action='store_true', help='Logic signal (high/low)')
12 parser.add_argument('-b', action='store_true', help='Display binary value')
13 parser.add_argument('-x', action='store_true', help='Display hex value')
14 parser.add_argument('-d', action='store_true', help='Display decimal value')
15 args = parser.parse_args()
16
17 start_time = None
18 stop_time = None
19 time_val = { }
20
21 def value_to_logic(value):
22 found_x = False
23 for char in value:
24 if char == '1':
25 return "H"
26 if char == 'x':
27 found_x = True
28 return "U" if found_x else "L"
29
30 def value_to_binary(value):
31 return "D{%s}" % value
32
33 def value_to_hex(value):
34 hex_string = ""
35 found_def = False
36 while len(value) % 4 != 0:
37 value = "0" + value
38 while len(value) != 0:
39 bin_digits = value[0:4]
40 hex_digit = 0
41 value = value[4:]
42 for b in bin_digits:
43 if b == '0':
44 hex_digit = hex_digit * 2
45 elif b == '1':
46 hex_digit = hex_digit * 2 + 1
47 else:
48 hex_digit += 100
49 if hex_digit > 15:
50 hex_string += "x"
51 else:
52 found_def = True
53 hex_string += "0123456789abcdef"[hex_digit]
54 if not found_def:
55 return "U";
56 return "D{%s}" % hex_string
57
58 def value_to_decimal(value):
59 val = 0
60 found_def = False
61 found_undef = False
62 for digit in value:
63 if digit == 'x':
64 found_undef = True
65 else:
66 val = val*2 + int(digit)
67 found_def = True
68 if found_def:
69 if found_undef:
70 return "D{X}"
71 else:
72 return "D{%d}" % val
73 return "U"
74
75 for line in fileinput.input(args.filename):
76 (node, time, name, value) = line.strip().split('\t')
77 time = int(time)
78 if start_time is None or start_time > time:
79 start_time = time
80 if stop_time is None or stop_time < time:
81 stop_time = time
82 if name == args.signame:
83 if args.l:
84 time_val[+time] = value_to_logic(value)
85 elif args.b:
86 time_val[+time] = value_to_binary(value)
87 elif args.x:
88 time_val[+time] = value_to_hex(value)
89 elif args.d:
90 time_val[+time] = value_to_decimal(value)
91 else:
92 time_val[+time] = value
93
94 if start_time not in time_val:
95 time_val[start_time] = "S"
96
97 last_time = None
98 last_value = None
99 for t in sorted(time_val.keys()):
100 if last_time is not None:
101 print("%f%s" % ((t - last_time)*args.s, last_value), end='')
102 (last_time, last_value) = (t, time_val[t])
103 if last_time < stop_time:
104 print("%f%s" % ((stop_time - last_time)*args.s, last_value), end='')
105 print('')
106