build.dsl: Add optional name_suffix to Resource.family.
authorWilliam D. Jones <thor0505@comcast.net>
Wed, 10 Jul 2019 15:29:09 +0000 (11:29 -0400)
committerwhitequark <cz@m-labs.hk>
Wed, 10 Jul 2019 15:41:23 +0000 (15:41 +0000)
nmigen/build/dsl.py
nmigen/test/test_build_dsl.py

index d5d181ec632e3e6c7f8e0b5eaf3817ebce11b2fd..f080e9b28d8eaf470650002f5afa9c3efd398d08 100644 (file)
@@ -182,7 +182,7 @@ class Subsignal:
 
 class Resource(Subsignal):
     @classmethod
-    def family(cls, name_or_number, number=None, *, ios, default_name):
+    def family(cls, name_or_number, number=None, *, ios, default_name, name_suffix=""):
         # This constructor accepts two different forms:
         #  1. Number-only form:
         #       Resource.family(0, default_name="name", ios=[Pins("A0 A1")])
@@ -190,10 +190,15 @@ class Resource(Subsignal):
         #       Resource.family("override", 0, default_name="name", ios=...)
         # This makes it easier to build abstractions for resources, e.g. an SPIResource abstraction
         # could simply delegate to `Resource.family(*args, default_name="spi", ios=ios)`.
+        # The name_suffix argument is meant to support creating resources with
+        # similar names, such as spi_flash, spi_flash_2x, etc.
+        if name_suffix:  # Only add "_" if we actually have a suffix.
+            name_suffix = "_" + name_suffix
+
         if number is None: # name_or_number is number
-            return cls(default_name, name_or_number, *ios)
+            return cls(default_name + name_suffix, name_or_number, *ios)
         else: # name_or_number is name
-            return cls(name_or_number, number, *ios)
+            return cls(name_or_number + name_suffix, number, *ios)
 
     def __init__(self, name, number, *args):
         super().__init__(name, *args)
index d7c73b9325393ff33ed7d1856044531c1afdd00d..e6d37adfc7faa3e391ded635c337bcb828964156 100644 (file)
@@ -229,10 +229,16 @@ class ResourceTestCase(FHDLTestCase):
         ios = [Subsignal("clk", Pins("A0", dir="o"))]
         r1  = Resource.family(0, default_name="spi", ios=ios)
         r2  = Resource.family("spi_flash", 0, default_name="spi", ios=ios)
+        r3  = Resource.family("spi_flash", 0, default_name="spi", ios=ios, name_suffix="4x")
+        r4  = Resource.family(0, default_name="spi", ios=ios, name_suffix="2x")
         self.assertEqual(r1.name, "spi")
         self.assertEqual(r1.ios, ios)
         self.assertEqual(r2.name, "spi_flash")
         self.assertEqual(r2.ios, ios)
+        self.assertEqual(r3.name, "spi_flash_4x")
+        self.assertEqual(r3.ios, ios)
+        self.assertEqual(r4.name, "spi_2x")
+        self.assertEqual(r4.ios, ios)
 
 
 class ConnectorTestCase(FHDLTestCase):