2 This work is funded through NLnet under Grant 2019-02-012
8 from vcd
.gtkw
import GTKWSave
, GTKWColor
12 def write_gtkw(gtkw_name
, vcd_name
, gtkw_dom
, gtkw_style
=None,
13 module
=None, loc
=None, color
=None, base
=None,
14 zoom
=None, marker
=-1, clk_period
=1e-6,
15 time_resolution_unit
="ps"):
16 """ Write a GTKWave document according to the supplied style and DOM.
18 :param gtkw_name: name of the generated GTKWave document
19 :param vcd_name: name of the waveform file
20 :param gtkw_dom: DOM style description for the trace pane
21 :param gtkw_style: style for signals, classes and groups
22 :param module: default module
23 :param color: default trace color
24 :param base: default numerical base
25 :param loc: source code location to include as a comment
26 :param zoom: initial zoom level, in GTKWave format. Can also be "formal"
27 when the file comes from a formal engine
28 :param marker: initial location of a marker
29 :param clk_period: clock period in seconds, helping
30 to set a reasonable initial zoom level.
31 Use together with ``time_resolution_unit``.
32 :param time_resolution_unit: use "ps" or "ns". Derived from the units of
33 the "timescale" on the VCD file. Used with
34 "clk_period" to set a default zoom level
38 Syntax: ``{selector: {attribute: value, ...}, ...}``
40 "selector" can be a signal, class or group
42 Signal groups propagate most attributes to their children
46 * module: absolute path of the current module
47 * submodule: same as above, but relative
49 * base: numerical base for value display
50 * display: alternate text to display in the signal pane
51 * comment: comment to display in the signal pane
52 * bit: select a bit from a wide signal. MSB is zero, unfortunately
56 Syntax: ``[signal, (signal, class), (group, [children]), comment, ...]``
58 The DOM is a list of nodes.
60 Nodes are signals, signal groups or comments.
62 * signals are strings, or tuples: ``(signal name, class, class, ...)``
63 * signal groups are tuples: ``(group name, class, class, ..., [nodes])``
64 * comments are: ``{'comment': 'comment string'}``
66 In place of a class name, an inline class description can be used.
67 ``(signal, {attribute: value, ...}, ...)``
69 An anonymous group can be used to apply a style to a group of signals.
70 ``({attribute: value}, [signal, signal, ...])``
73 'blue': GTKWColor
.blue
,
74 'cycle': GTKWColor
.cycle
,
75 'green': GTKWColor
.green
,
76 'indigo': GTKWColor
.indigo
,
77 'normal': GTKWColor
.normal
,
78 'orange': GTKWColor
.orange
,
80 'violet': GTKWColor
.violet
,
81 'yellow': GTKWColor
.yellow
,
84 with
open(gtkw_name
, "wt") as gtkw_file
:
85 gtkw
= GTKWSave(gtkw_file
)
87 gtkw
.comment("Auto-generated by " + loc
)
88 gtkw
.dumpfile(vcd_name
)
89 # set a reasonable zoom level
90 # also, move the marker to an interesting place
92 # output from formal engines looks good at this zoom level
95 zoom
= -42.8 - log2(clk_period
)
96 # base zoom level is affected by time resolution units
97 if time_resolution_unit
== "ns":
98 zoom
= zoom
+ log2(1e3
)
99 gtkw
.zoom_markers(zoom
, marker
)
101 # create an empty style, if needed
102 if gtkw_style
is None:
105 # create an empty root selector, if needed
106 root_style
= gtkw_style
.get('', dict())
108 # apply styles to the root selector, if provided
109 if module
is not None:
110 root_style
['module'] = module
111 if color
is not None:
112 root_style
['color'] = color
114 root_style
['base'] = base
115 # base cannot be None, use 'hex' by default
116 if root_style
.get('base') is None:
117 root_style
['base'] = 'hex'
119 # recursively walk the DOM
120 def walk(dom
, style
):
124 # copy the style from the parent
125 node_style
= style
.copy()
126 # node is a signal name string
127 if isinstance(node
, str):
129 # apply style from node name, if specified
130 if node_name
in gtkw_style
:
131 node_style
.update(gtkw_style
[node_name
])
133 # could be a signal or a group
134 elif isinstance(node
, tuple):
136 # collect styles from the selectors
137 # order goes from the most specific to most generic
138 # which means earlier selectors override later ones
139 for selector
in reversed(node
):
140 # update the node style from the selector
141 if isinstance(selector
, str):
142 if selector
in gtkw_style
:
143 node_style
.update(gtkw_style
[selector
])
144 # apply an inline style description
145 elif isinstance(selector
, dict):
146 node_style
.update(selector
)
147 # node is a group if it has a child list
148 if isinstance(node
[-1], list):
151 elif isinstance(node
, dict):
152 if 'comment' in node
:
153 gtkw
.blank(node
['comment'])
154 # merge the submodule into the module path
155 if 'submodule' in node_style
:
156 node_module
= node_style
['submodule']
157 if 'module' in node_style
:
158 node_top_module
= node_style
['module']
159 node_module
= node_top_module
+ '.' + node_module
160 # update the module path
161 node_style
['module'] = node_module
162 # don't propagate this attribute to children
163 del node_style
['submodule']
164 # emit the group delimiters and walk over the child list
165 if children
is not None:
166 # only emit a group if it has a name
167 if isinstance(node_name
, str):
168 gtkw
.begin_group(node_name
)
169 # pass on the group style to its children
170 walk(children
, node_style
)
171 if isinstance(node_name
, str):
172 gtkw
.end_group(node_name
)
173 # emit a trace, if the node is a signal
174 elif node_name
is not None:
175 signal_name
= node_name
176 # prepend module name to signal
177 if 'module' in node_style
:
178 node_module
= node_style
['module']
179 if node_module
is not None:
180 signal_name
= node_module
+ '.' + signal_name
181 node_color
= colors
.get(node_style
.get('color'))
182 node_base
= node_style
.get('base')
183 display
= node_style
.get('display')
184 if 'bit' in node_style
:
185 bit
= node_style
['bit']
186 signal_name
= f
'({bit}){signal_name}'
187 gtkw
.trace(signal_name
, color
=node_color
,
188 datafmt
=node_base
, alias
=display
)
190 walk(gtkw_dom
, root_style
)