make mux_interface a Pin/Interface... getting complicated
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Wed, 21 Mar 2018 06:29:44 +0000 (06:29 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Wed, 21 Mar 2018 06:29:44 +0000 (06:29 +0000)
src/interface_decl.py
src/interface_def.py
src/pinmux_generator.py

index 712fb29ed15c96a23a21eb841e94167de5567a68..2dd0a6cd25928a2cfaf9098f3c6091e617899091 100644 (file)
@@ -10,14 +10,16 @@ class Pin(object):
                  ready=True,
                  enabled=True,
                  io=False,
-                 action=False):
+                 action=False,
+                 bitspec=None):
         self.name = name
         self.ready = ready
         self.enabled = enabled
         self.io = io
         self.action = action
+        self.bitspec = bitspec if bitspec else '1'
 
-    def ifacefmt(self):
+    def ifacefmt(self, fmtfn=None):
         res = '    '
         status = []
         if self.ready:
@@ -33,27 +35,28 @@ class Pin(object):
         res += " method "
         if self.io:
             res += "\n                      "
+        name = fmtfn(self.name)
         if self.action:
             res += " Action "
-            res += self.name
-            res += ' (Bit#(1) in)'
+            res += name
+            res += ' (Bit#(%s) in)' % self.bitspec
         else:
-            res += " Bit#(1) "
-            res += self.name
+            res += " Bit#(%s) " % self.bitspec
+            res += name
         res += ";"
         return res
 
-    def ifacedef(self, fmtoutfn=None, fmtinfn=None):
+    def ifacedef(self, fmtoutfn=None, fmtinfn=None, fmtdecfn=None):
         res = '      method '
         if self.action:
-            fmtname = fmtinfn(self.name) if fmtinfn else self.name
+            fmtname = fmtinfn(self.name)
             res += "Action  "
-            res += self.name
-            res += '(Bit#(1) in);\n'
+            res += fmtdecfn(self.name)
+            res += '(Bit#(%s) in);\n' % self.bitspec
             res += '         %s<=in;\n' % fmtname
             res += '      endmethod'
         else:
-            fmtname = fmtoutfn(self.name) if fmtoutfn else self.name
+            fmtname = fmtoutfn(self.name)
             res += "%s=%s;" % (self.name, fmtname)
         return res
 
@@ -77,8 +80,15 @@ class Interface(object):
             else:
                 self.pins.append(Pin(**p))
 
-    def ifacefmt(self, i):
-        return '\n'+'\n'.join(map(lambda x:x.ifacefmt(), self.pins)).format(i)
+    def ifacefmt(self, *args):
+        res = '\n'.join(map(self.ifacefmtdecpin, self.pins)).format(*args)
+        return '\n' + res
+
+    def ifacefmtdecfn(self, name):
+        return name
+
+    def ifacefmtdecfn2(self, name):
+        return name
 
     def ifacefmtoutfn(self, name):
         return name
@@ -86,16 +96,34 @@ class Interface(object):
     def ifacefmtinfn(self, name):
         return "wr%s" % name
 
+    def ifacefmtdecpin(self, pin):
+        return pin.ifacefmt(self.ifacefmtdecfn)
+
     def ifacefmtpin(self, pin):
-        return pin.ifacedef(self.ifacefmtoutfn, self.ifacefmtinfn)
+        return pin.ifacedef(self.ifacefmtoutfn, self.ifacefmtinfn,
+                            self.ifacefmtdecfn2)
 
-    def ifacedef(self, i):
-        res = '\n'.join(map(self.ifacefmtpin, self.pins)).format(i)
+    def ifacedef(self, *args):
+        res = '\n'.join(map(self.ifacefmtpin, self.pins)).format(*args)
         return '\n' + res + '\n'
 
 
+class MuxInterface(Interface):
+
+    def ifacefmtdecfn2(self, name):
+        return "cell{0}_mux"
+
+    def ifacefmtdecfn(self, name):
+        return "cell{0}_mux"
+
+    def ifacefmtinfn(self, name):
+        return "wrmux{0}"
+
 class IOInterface(Interface):
 
+    #def ifacefmtdecfn(self, name):
+    #    return "cell{0}_mux"
+
     def ifacefmtoutfn(self, name):
         return "cell{0}_out.%s" % (name[3:-4])
 
@@ -105,8 +133,9 @@ class IOInterface(Interface):
 
 # ========= Interface declarations ================ #
 
-mux_interface = '''
-      method Action cell{0}_mux(Bit#({1}) in);'''
+mux_interface = MuxInterface([{'name': 'cell{0}', 'ready':False,
+                      'enabled':False,
+                     'bitspec': '{1}', 'action': True}])
 
 io_interface = IOInterface([{'name': 'io_outputval_{0}', 'enabled': False},
                           {'name': 'io_output_en_{0}', 'enabled': False},
@@ -185,3 +214,14 @@ if __name__ == '__main__':
     from interface_def import io_interface_def
     print io_interface_def.format(0)
     print io_interface.ifacedef(0)
+    assert io_interface_def.format(0) == io_interface.ifacedef(0)
+
+    mux_interfacetest = '''
+          method Action cell{0}_mux(Bit#({1}) in);'''
+    print pinmunge(mux_interfacetest.format(0,1))
+    print pinmunge(mux_interface.ifacefmt(0, 1))
+    from interface_def import mux_interface_def
+    print repr(mux_interface_def.format(0, 1))
+    print repr(mux_interface.ifacedef(0, 1))
+    assert mux_interface_def.format(0,1) == mux_interface.ifacedef(0,1)
+
index 6c2039777138dbd13bb284339b9dbc7015a25ccf..e27ab68c8bee8c0d173402fe17ffcd55deeab98a 100644 (file)
@@ -1,6 +1,6 @@
 # === templates for interface definitions ====== #
 mux_interface_def = '''
-      method Action cell{0}_mux (Bit#({1}) in );
+      method Action  cell{0}_mux(Bit#({1}) in);
          wrmux{0}<=in;
       endmethod
 '''
index 64bb373004dba5d43090074bf01a1c3613390cd7..b496d229b16e0f13c399f39f7ac86a4a2e16ca68 100644 (file)
@@ -78,7 +78,7 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file:
       // where each IO will have the same number of muxes.''')
 
     for cell in muxed_cells:
-        bsv_file.write(mux_interface.format(cell[0],
+        bsv_file.write(mux_interface.ifacefmt(cell[0],
                                         int(math.log(len(cell) - 1, 2))))
 
     bsv_file.write('''
@@ -203,7 +203,7 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file:
     interface mux_lines = interface MuxSelectionLines
 ''')
     for cell in muxed_cells:
-        bsv_file.write(mux_interface_def.format(cell[0],
+        bsv_file.write(mux_interface.ifacedef(cell[0],
                                             int(math.log(len(cell) - 1, 2))))
     bsv_file.write('''
     endinterface;