whitespace cleanup (autopep8)
[pinmux.git] / src / interface_decl.py
index df54caf6834862ddfcd544a01dd10ec6a011f345..8e2066846ceed87138c3aef8b1852c8e6e3cbbe7 100644 (file)
@@ -69,3 +69,116 @@ 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):
+        self.name = name
+        self.ready = ready
+        self.enabled = enabled
+        self.io = io
+        self.action = action
+
+    def __str__(self):
+        res = '    '
+        status = []
+        if self.ready:
+            status.append('always_ready')
+        if self.enabled:
+            status.append('always_enabled')
+        if self.io:
+            status.append('result="io"')
+        if status:
+            res += '(*'
+            res += ','.join(status)
+            res += '*)'
+        res += " method "
+        if self.action:
+            res += " Action "
+            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__':
+
+    def _pinmunge(p, sep, repl, dedupe=True):
+        """ munges the text so it's easier to compare.
+            splits by separator, strips out blanks, re-joins.
+        """
+        p = p.strip()
+        p = p.split(sep)
+        if dedupe:
+            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.
+        """
+        p = _pinmunge(p, "(", " ( ", False)
+        p = _pinmunge(p, ")", " ) ", False)
+        p = _pinmunge(p, " ", " ")
+        return p
+
+    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)