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