speed up ==, hash, <, >, <=, and >= for plain_data
[nmutil.git] / src / nmutil / gtkw.py
index 83a7db1722e3c3362d3c491a9f6824ffd368a883..f26fc00cf17b1e0be1977f1c699aff5a37eb35d7 100644 (file)
@@ -1,10 +1,18 @@
+"""
+    This work is funded through NLnet under Grant 2019-02-012
+
+    License: LGPLv3+
+
+
+"""
 from vcd.gtkw import GTKWSave, GTKWColor
 from math import log2
 
 
 def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
                module=None, loc=None, color=None, base=None,
-               zoom=None, marker=-1, clk_period=1e-6):
+               zoom=None, marker=-1, clk_period=1e-6,
+               time_resolution_unit="ps"):
     """ Write a GTKWave document according to the supplied style and DOM.
 
     :param gtkw_name: name of the generated GTKWave document
@@ -15,10 +23,15 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
     :param color: default trace color
     :param base: default numerical base
     :param loc: source code location to include as a comment
-    :param zoom: initial zoom level, in GTKWave format
+    :param zoom: initial zoom level, in GTKWave format. Can also be "formal"
+                 when the file comes from a formal engine
     :param marker: initial location of a marker
     :param clk_period: clock period in seconds, helping
-                       to set a reasonable initial zoom level
+                       to set a reasonable initial zoom level.
+                       Use together with ``time_resolution_unit``.
+    :param time_resolution_unit: use "ps" or "ns". Derived from the units of
+                                 the "timescale" on the VCD file. Used with
+                                 "clk_period" to set a default zoom level
 
     **gtkw_style format**
 
@@ -30,12 +43,14 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
 
     Attribute choices:
 
-    * module: instance path, for prepending to the signal name
+    * module: absolute path of the current module
+    * submodule: same as above, but relative
     * color: trace color
     * base: numerical base for value display
     * display: alternate text to display in the signal pane
     * comment: comment to display in the signal pane
     * bit: select a bit from a wide signal. MSB is zero, unfortunately
+    * closed (for groups): this group starts closed
 
     **gtkw_dom format**
 
@@ -51,6 +66,9 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
 
     In place of a class name, an inline class description can be used.
     ``(signal, {attribute: value, ...}, ...)``
+
+    An anonymous group can be used to apply a style to a group of signals.
+    ``({attribute: value}, [signal, signal, ...])``
     """
     colors = {
         'blue': GTKWColor.blue,
@@ -71,8 +89,14 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
         gtkw.dumpfile(vcd_name)
         # set a reasonable zoom level
         # also, move the marker to an interesting place
+        if zoom == "formal":
+            # output from formal engines looks good at this zoom level
+            zoom = -6.3
         if zoom is None:
             zoom = -42.8 - log2(clk_period)
+            # base zoom level is affected by time resolution units
+            if time_resolution_unit == "ns":
+                zoom = zoom + log2(1e3)
         gtkw.zoom_markers(zoom, marker)
 
         # create an empty style, if needed
@@ -128,12 +152,30 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
                 elif isinstance(node, dict):
                     if 'comment' in node:
                         gtkw.blank(node['comment'])
+                # merge the submodule into the module path
+                if 'submodule' in node_style:
+                    node_module = node_style['submodule']
+                    if 'module' in node_style:
+                        node_top_module = node_style['module']
+                        node_module = node_top_module + '.' + node_module
+                    # update the module path
+                    node_style['module'] = node_module
+                    # don't propagate this attribute to children
+                    del node_style['submodule']
                 # emit the group delimiters and walk over the child list
                 if children is not None:
-                    gtkw.begin_group(node_name)
+                    # see whether this group starts closed
+                    closed = False
+                    if 'closed' in node_style:
+                        closed = node_style['closed']
+                        del node_style['closed']  # do not inherit
+                    # only emit a group if it has a name
+                    if isinstance(node_name, str):
+                        gtkw.begin_group(node_name, closed)
                     # pass on the group style to its children
                     walk(children, node_style)
-                    gtkw.end_group(node_name)
+                    if isinstance(node_name, str):
+                        gtkw.end_group(node_name)
                 # emit a trace, if the node is a signal
                 elif node_name is not None:
                     signal_name = node_name
@@ -145,12 +187,10 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_dom, gtkw_style=None,
                     node_color = colors.get(node_style.get('color'))
                     node_base = node_style.get('base')
                     display = node_style.get('display')
-                    if 'bit' not in node_style:
-                        gtkw.trace(signal_name, color=node_color,
-                                   datafmt=node_base, alias=display)
-                    else:
+                    if 'bit' in node_style:
                         bit = node_style['bit']
-                        gtkw.trace_bit(bit, signal_name, color=node_color,
-                                       alias=display)
+                        signal_name = f'({bit}){signal_name}'
+                    gtkw.trace(signal_name, color=node_color,
+                               datafmt=node_base, alias=display)
 
         walk(gtkw_dom, root_style)