Add color style
[soc.git] / src / soc / experiment / alu_fsm.py
index f493983aba690003c7792d7519921fd424b2a559..0079bbd5360a4005aec0bc2edfd1f6802b12b392 100644 (file)
@@ -251,10 +251,61 @@ def write_gtkw_v1(base_name, top_dut_name, loc):
             gtkw.trace(dut + "n_ready_i", color=style_input)
 
 
-# write a GTKWave document according to the supplied style and DOM
-# TODO: Apply styles
 def write_gtkw(gtkw_name, vcd_name, gtkw_style, gtkw_dom,
                loc=None, zoom=-22.9, marker=-1):
+    """ Write a GTKWave document according to the supplied style and DOM.
+
+    :param gtkw_name: name of the generated GTKWave document
+    :param vcd_name: name of the waveform file
+    :param gtkw_style: style for signals, classes and groups
+    :param gtkw_dom: DOM style description for the trace pane
+    :param loc: source code location to include as a comment
+    :param zoom: initial zoom level, in GTKWave format
+    :param marker: initial location of a marker
+
+    **gtkw_style format**
+
+    Syntax: ``{selector: {attribute: value, ...}, ...}``
+
+    "selector" can be a signal, class or group
+
+    Signal groups propagate most attributes to their children
+
+    Attribute choices:
+
+    * module: instance path, for prepending to the signal name
+    * 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
+
+    **gtkw_dom format**
+
+    Syntax: ``[signal, (signal, class), (group, [children]), comment, ...]``
+
+    The DOM is a list of nodes.
+
+    Nodes are signals, signal groups or comments.
+
+    * signals are strings, or tuples: ``(signal name, class, class, ...)``
+    * signal groups are tuples: ``(group name, class, class, ..., [nodes])``
+    * comments are: ``{'comment': 'comment string'}``
+
+    In place of a class name, an inline class description can be used.
+    ``(signal, {attribute: value, ...}, ...)``
+    """
+    colors = {
+        'blue': GTKWColor.blue,
+        'cycle': GTKWColor.cycle,
+        'green': GTKWColor.green,
+        'indigo': GTKWColor.indigo,
+        'normal': GTKWColor.normal,
+        'orange': GTKWColor.orange,
+        'red': GTKWColor.red,
+        'violet': GTKWColor.violet,
+        'yellow': GTKWColor.yellow,
+    }
+
     with open(gtkw_name, "wt") as gtkw_file:
         gtkw = GTKWSave(gtkw_file)
         if loc is not None:
@@ -264,11 +315,18 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_style, gtkw_dom,
         # also, move the marker to an interesting place
         gtkw.zoom_markers(zoom, marker)
 
+        if '' in gtkw_style:
+            root_style = gtkw_style['']
+        else:
+            root_style = dict()
+
         # recursively walk the DOM
-        def walk(dom):
+        def walk(dom, style):
             for node in dom:
                 node_name = None
                 children = None
+                # copy the style from the parent
+                node_style = style.copy()
                 # node is a signal name string
                 if isinstance(node, str):
                     node_name = node
@@ -276,19 +334,38 @@ def write_gtkw(gtkw_name, vcd_name, gtkw_style, gtkw_dom,
                 # could be a signal or a group
                 elif isinstance(node, tuple):
                     node_name = node[0]
+                    # collect styles from the selectors
+                    # order goes from the most specific to most generic
+                    # which means earlier selectors override later ones
+                    for selector in reversed(node):
+                        # update the node style from the selector
+                        if isinstance(selector, str):
+                            if selector in gtkw_style:
+                                node_style.update(gtkw_style[selector])
+                        # apply an inline style description
+                        elif isinstance(selector, dict):
+                            node_style.update(selector)
                     # node is a group if it has a child list
                     if isinstance(node[-1], list):
                         children = node[-1]
                 # emit the group delimiters and walk over the child list
                 if children is not None:
                     gtkw.begin_group(node_name)
-                    walk(children)
+                    # pass on the group style to its children
+                    walk(children, node_style)
                     gtkw.end_group(node_name)
                 # emit a trace, if the node is a signal
                 elif node_name is not None:
-                    gtkw.trace(node_name)
+                    signal_name = node_name
+                    # prepend module name to signal
+                    if 'module' in node_style:
+                        signal_name = node_style['module'] + '.' + signal_name
+                    color = None
+                    if 'color' in node_style:
+                        color = colors[node_style['color']]
+                    gtkw.trace(signal_name, color=color)
 
-        walk(gtkw_dom)
+        walk(gtkw_dom, root_style)
 
 
 def test_shifter():
@@ -307,20 +384,8 @@ def test_shifter():
     write_gtkw_v1("test_shifter", "top.shf", __file__)
 
     # Describe a GTKWave document
-    # Uses a split CSS + DOM approach, where style is separated from
-    # content.
 
     # Style for signals, classes and groups
-    # Syntax: {selector: {attribute: value, ...}, ...}
-    # "selector" can be a signal, class or group
-    # signal groups propagate most attributes to their children
-    # attribute choices:
-    # - module: instance path, for prepending to the signal name
-    # - 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
-
     gtkwave_style = {
         # Root selector. Gives default attributes for every signal.
         '': {'module': 'top.shf', 'base': 'dec'},
@@ -335,15 +400,6 @@ def test_shifter():
     }
 
     # DOM style description for the trace pane
-    # Syntax: [signal, (signal, class), (group, [children]), comment, ...]
-    # The DOM is a list of nodes.
-    # Nodes are signals, signal groups or comments.
-    # - signals are strings, or tuples: (signal name, class, class, ...)
-    # - signal groups are tuples: (group name, class, class, ..., [nodes])
-    # - comments are: {'comment': 'comment string'}
-    # In place of a class name, an inline class description can be used.
-    # (signal, {attribute: value, ...}, ...)
-
     gtkwave_desc = [
         # simple signal, without a class
         # even so, it inherits the top-level root attributes