add first auto-generated interface_def (io_interface_def)
[pinmux.git] / src / interface_decl.py
index 79710d41433d687a53c425c60cf23446cda086e4..712fb29ed15c96a23a21eb841e94167de5567a68 100644 (file)
@@ -1,92 +1,24 @@
-# ========= Interface declarations ================ #
-mux_interface = '''
-      method Action cell{0}_mux(Bit#({1}) in);'''
-
-io_interface = '''
-      (*always_ready*)   method   Bit#(1) io_outputval_{0};
-      (*always_ready*)   method   Bit#(1) io_output_en_{0};
-      (*always_ready*)   method   Bit#(1) io_input_en_{0};
-      (*always_ready*)   method   Bit#(1) io_pullup_en_{0};
-      (*always_ready*)   method   Bit#(1) io_pulldown_en_{0};
-      (*always_ready*)   method   Bit#(1) io_drivestrength_{0};
-      (*always_ready*)   method   Bit#(1) io_pushpull_en_{0};
-      (*always_ready*)   method   Bit#(1) io_opendrain_en_{0};
-      (*always_ready,always_enabled,result="io"*)
-                 method   Action  io_inputval_{0}(Bit#(1) in);
-'''
-# == Peripheral Interface definitions == #
-# these are the interface of the peripherals to the pin mux
-# Outputs from the peripherals will be inputs to the pinmux
-# module. Hence the change in direction for most pins
-
-uartinterface_decl = '''
-      (*always_ready,always_enabled*) method Action tx_{0}(Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) rx_{0};
-'''
-
-spiinterface_decl = '''
-      (*always_ready,always_enabled*) method Action sclk_{0} (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action mosi_{0} (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action ss_{0}   (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) miso_{0};
-'''
-
-twiinterface_decl = '''
-      (*always_ready,always_enabled*) method Action sda{0}_out (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sda{0}_outen (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) sda{0}_in;
-      (*always_ready,always_enabled*) method Action scl{0}_out (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action scl{0}_outen (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) scl{0}_in;
-'''
-
-sdinterface_decl = '''
-      (*always_ready,always_enabled*) method Action sd{0}_clk (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sd{0}_cmd (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sd{0}_d0_out (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sd{0}_d0_outen (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) sd{0}_d0_in;
-      (*always_ready,always_enabled*) method Action sd{0}_d1_out (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sd{0}_d1_outen (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) sd{0}_d1_in;
-      (*always_ready,always_enabled*) method Action sd{0}_d2_out (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sd{0}_d2_outen (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) sd{0}_d2_in;
-      (*always_ready,always_enabled*) method Action sd{0}_d3_out (Bit#(1) in);
-      (*always_ready,always_enabled*) method Action sd{0}_d3_outen (Bit#(1) in);
-      (*always_ready,always_enabled*) method Bit#(1) sd{0}_d3_in;
-'''
-
-jtaginterface_decl = '''
-      (*always_ready,always_enabled*) method Bit#(1) jtag{0}_tdi;
-      (*always_ready,always_enabled*) method Bit#(1) jtag{0}_tms;
-      (*always_ready,always_enabled*) method Bit#(1) jtag{0}_tclk;
-      (*always_ready,always_enabled*) method Bit#(1) jtag{0}_trst;
-      (*always_ready,always_enabled*) method Action jtag{0}_tdo(Bit#(1) in);
-'''
-
-pwminterface_decl = '''
-      (*always_ready,always_enabled*) method Action pwm{0}(Bit#(1) in);
-'''
-# ======================================= #
 
 class Pin(object):
+    """ pin interface declaration.
+        * name is the name of the pin
+        * ready, enabled and io all create a (* .... *) prefix
+        * action changes it to an "in" if true
+    """
 
     def __init__(self, name,
-                       ready=True,
-                       enabled=True,
-                       io=False,
-                       action=False,
-                       inout=True):
+                 ready=True,
+                 enabled=True,
+                 io=False,
+                 action=False):
         self.name = name
         self.ready = ready
         self.enabled = enabled
         self.io = io
         self.action = action
-        self.inout = inout
 
-    def __str__(self):
-        res = '    (*'
+    def ifacefmt(self):
+        res = '    '
         status = []
         if self.ready:
             status.append('always_ready')
@@ -94,27 +26,135 @@ class Pin(object):
             status.append('always_enabled')
         if self.io:
             status.append('result="io"')
-        res += ','.join(status)
-        res += "*) method "
+        if status:
+            res += '(*'
+            res += ','.join(status)
+            res += '*)'
+        res += " method "
+        if self.io:
+            res += "\n                      "
         if self.action:
             res += " Action "
-        res += self.name
-        if self.inout:
+            res += self.name
             res += ' (Bit#(1) in)'
+        else:
+            res += " Bit#(1) "
+            res += self.name
         res += ";"
         return res
 
+    def ifacedef(self, fmtoutfn=None, fmtinfn=None):
+        res = '      method '
+        if self.action:
+            fmtname = fmtinfn(self.name) if fmtinfn else self.name
+            res += "Action  "
+            res += self.name
+            res += '(Bit#(1) in);\n'
+            res += '         %s<=in;\n' % fmtname
+            res += '      endmethod'
+        else:
+            fmtname = fmtoutfn(self.name) if fmtoutfn else self.name
+            res += "%s=%s;" % (self.name, fmtname)
+        return res
+
 
 class Interface(object):
+    """ create an interface from a list of pinspecs.
+        each pinspec is a dictionary, see Pin class arguments
+    """
 
     def __init__(self, pinspecs):
         self.pins = []
         for p in pinspecs:
-            self.pins.append(Pin(**p))
+            if p.get('outen') is True:  # special case, generate 3 pins
+                _p = {}
+                _p.update(p)
+                del _p['outen']
+                for psuffix in ['out', 'outen', 'in']:
+                    _p['name'] = "%s_%s" % (p['name'], psuffix)
+                    _p['action'] = psuffix != 'in'
+                    self.pins.append(Pin(**_p))
+            else:
+                self.pins.append(Pin(**p))
+
+    def ifacefmt(self, i):
+        return '\n'+'\n'.join(map(lambda x:x.ifacefmt(), self.pins)).format(i)
+
+    def ifacefmtoutfn(self, name):
+        return name
+
+    def ifacefmtinfn(self, name):
+        return "wr%s" % name
+
+    def ifacefmtpin(self, pin):
+        return pin.ifacedef(self.ifacefmtoutfn, self.ifacefmtinfn)
+
+    def ifacedef(self, i):
+        res = '\n'.join(map(self.ifacefmtpin, self.pins)).format(i)
+        return '\n' + res + '\n'
+
+
+class IOInterface(Interface):
 
-    def __str__(self):
-        return '\n'.join(map(str, self.pins))
+    def ifacefmtoutfn(self, name):
+        return "cell{0}_out.%s" % (name[3:-4])
 
+    def ifacefmtinfn(self, name):
+        return "cell{0}_in"
+
+
+# ========= Interface declarations ================ #
+
+mux_interface = '''
+      method Action cell{0}_mux(Bit#({1}) in);'''
+
+io_interface = IOInterface([{'name': 'io_outputval_{0}', 'enabled': False},
+                          {'name': 'io_output_en_{0}', 'enabled': False},
+                          {'name': 'io_input_en_{0}', 'enabled': False},
+                          {'name': 'io_pullup_en_{0}', 'enabled': False},
+                          {'name': 'io_pulldown_en_{0}', 'enabled': False},
+                          {'name': 'io_drivestrength_{0}', 'enabled': False},
+                          {'name': 'io_pushpull_en_{0}', 'enabled': False},
+                          {'name': 'io_opendrain_en_{0}', 'enabled': False},
+                          {'name': 'io_inputval_{0}', 'action': True, 'io': True},
+                          ])
+
+# == Peripheral Interface definitions == #
+# these are the interface of the peripherals to the pin mux
+# Outputs from the peripherals will be inputs to the pinmux
+# module. Hence the change in direction for most pins
+
+uartinterface_decl = Interface([{'name': 'tx_{0}', 'action': True},
+                                {'name': 'rx_{0}'},
+                                ])
+
+spiinterface_decl = Interface([{'name': 'sclk_{0}', 'action': True},
+                               {'name': 'mosi_{0}', 'action': True},
+                               {'name': 'ss_{0}', 'action': True},
+                               {'name': 'miso_{0}'},
+                               ])
+
+twiinterface_decl = Interface([{'name': 'sda{0}', 'outen': True},
+                               {'name': 'scl{0}', 'outen': True},
+                               ])
+
+sdinterface_decl = Interface([{'name': 'sd{0}_clk', 'action': True},
+                              {'name': 'sd{0}_cmd', 'action': True},
+                              {'name': 'sd{0}_d0', 'outen': True},
+                              {'name': 'sd{0}_d1', 'outen': True},
+                              {'name': 'sd{0}_d2', 'outen': True},
+                              {'name': 'sd{0}_d3', 'outen': True}
+                              ])
+
+jtaginterface_decl = Interface([{'name': 'jtag{0}_tdi'},
+                                {'name': 'jtag{0}_tms'},
+                                {'name': 'jtag{0}_tclk'},
+                                {'name': 'jtag{0}_trst'},
+                                {'name': 'jtag{0}_tdo', 'action': True}])
+
+pwminterface_decl = Interface([{'name': "pwm{0}", 'action': True}])
+
+# ======================================= #
 
 # basic test
 if __name__ == '__main__':
@@ -126,18 +166,22 @@ if __name__ == '__main__':
         p = p.strip()
         p = p.split(sep)
         if dedupe:
-            p = filter(lambda x: x, p) # filter out blanks
+            p = filter(lambda x: x, p)  # filter out blanks
         return repl.join(p)
 
     def pinmunge(p):
         """ munges the text so it's easier to compare.
         """
+        # first join lines by semicolons, strip out returns
+        p = p.split(";")
+        p = map(lambda x: x.replace('\n', ''), p)
+        p = '\n'.join(p)
+        # now split first by brackets, then spaces (deduping on spaces)
         p = _pinmunge(p, "(", " ( ", False)
         p = _pinmunge(p, ")", " ) ", False)
         p = _pinmunge(p, " ", " ")
         return p
 
-    pwm = Interface([{'name': "pwm{0}", 'action': True}])
-    print pwm
-    assert pinmunge(str(pwm)) == pinmunge(pwminterface_decl)
-
+    from interface_def import io_interface_def
+    print io_interface_def.format(0)
+    print io_interface.ifacedef(0)