whitespace cleanup (autopep8)
[pinmux.git] / src / interface_decl.py
index a74e812fd005a0358f9b7cf0a9f8670934db8a94..8e2066846ceed87138c3aef8b1852c8e6e3cbbe7 100644 (file)
@@ -70,23 +70,27 @@ pwminterface_decl = '''
 '''
 # ======================================= #
 
+
 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 = '    (*'
+        res = '    '
         status = []
         if self.ready:
             status.append('always_ready')
@@ -94,16 +98,45 @@ 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.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
 
+
+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:
+            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 __str__(self):
+        return '\n'.join(map(str, self.pins))
+
+
 # basic test
 if __name__ == '__main__':
 
@@ -114,7 +147,7 @@ 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):
@@ -125,7 +158,27 @@ if __name__ == '__main__':
         p = _pinmunge(p, " ", " ")
         return p
 
-    pwm = Pin("pwm{0}", True, True, True, True)
-    print pinmunge(str(pwm))
-    print pinmunge(pwminterface_decl)
+    pwm = Interface([{'name': "pwm{0}", 'action': True}])
+    print pwm
+    print
     assert pinmunge(str(pwm)) == pinmunge(pwminterface_decl)
+
+    jtag = Interface([{'name': 'jtag{0}_tdi'},
+                      {'name': 'jtag{0}_tms'},
+                      {'name': 'jtag{0}_tclk'},
+                      {'name': 'jtag{0}_trst'},
+                      {'name': 'jtag{0}_tdo', 'action': True}])
+    print jtag
+    print
+    assert pinmunge(str(jtag)) == pinmunge(jtaginterface_decl)
+
+    sd = 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}
+                    ])
+    print sd
+    print
+    assert pinmunge(str(sd)) == pinmunge(sdinterface_decl)