From d9ab99499881fcfc7c93c9c0b02809a474b39bc7 Mon Sep 17 00:00:00 2001
From: Thomas Kramer <code@tkramer.ch>
Date: Thu, 23 Feb 2023 08:50:13 +0100
Subject: [PATCH] compute costs of terminal nodes and apply them to the graph
 edges

---
 lclayout/router.py        | 26 ++++++++++++++++++++++++++
 lclayout/routing_graph.py | 30 ++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/lclayout/router.py b/lclayout/router.py
index 5b30982..de990f0 100644
--- a/lclayout/router.py
+++ b/lclayout/router.py
@@ -462,6 +462,32 @@ class DefaultRouter():
             else:
                 assert False, 'Routing graph is not connected.'
 
+        # Set via weights based on terminal costs.
+        regions_by_layer = {
+            # Convert Shapes objects into db.Region for fast 'inside' checks.
+            layer: db.Region(s) for layer, s in shapes.items()
+        }
+        for net, ts in terminals_by_net:
+            for terminal_node in ts:
+                layer, (x, y) = terminal_node
+                possible_via_layers = (data['layer'] for _, _, data in via_layers.edges(layer, data=True))
+
+                for via_layer in possible_via_layers:
+                    via_cost = compute_terminal_cost(
+                        via_layer,
+                        layer,
+                        (x, y),
+                        regions_by_layer[layer],
+                        tech
+                    )
+
+                    other_node = via_layer, (x, y) # Node on the other side of the via.
+
+                    if terminal_node in graph and other_node in graph:
+                        # Scale the via cost
+                        graph[terminal_node][other_node]['weight'] *= via_cost + 1
+
+
         self._routing_graph = graph
 
         # TODO: SPLIT HERE
diff --git a/lclayout/routing_graph.py b/lclayout/routing_graph.py
index 2354178..f1b8fe3 100644
--- a/lclayout/routing_graph.py
+++ b/lclayout/routing_graph.py
@@ -396,6 +396,36 @@ def _extract_terminal_nodes_from_shape(routing_nodes: Dict[Any, Set[Tuple[int, i
     logger.debug(f"routing_terminals: {routing_terminals}")
     return routing_terminals
 
+def compute_terminal_cost(
+    via_layer: str,
+    layer: str,
+    coord: Tuple[int, int],
+    layer_region: db.Region,
+    tech
+    ) -> int:
+    """
+    Compute the cost of placing a via on `via_layer` which connects to `layer` at `(x, y)`.
+    The cost is derived from the existing geometries on the layer.
+
+    TODO: Move to new module 'pin_access_analysis'
+
+    Returns an unscaled cost. 0 for vias which can be placed without extending the pin shape, 
+    1 for vias which would extend the pin shape.
+    """
+    
+    enc = tech.minimum_enclosure.get((layer, via_layer), 0)
+    via_size = tech.via_size[via_layer]
+    
+    d = enc + via_size // 2
+
+    # Test if the via would be completely enclosed in existing shapes.
+    is_enclosed = is_inside(coord, layer_region, d)
+
+    if is_enclosed:
+        # Via can be placed without extending the pin shape.
+        return 0
+    else:
+        return 1
 
 def extract_terminal_nodes_by_lvs(graph: nx.Graph,
                                   pin_shapes_by_net: Dict[str, List[List[Tuple[str, db.Polygon]]]],
-- 
GitLab