AddingPeripherals.mdwn
[pinmux.git] / src / bsv / interface_decl.py
index 1840ba19986803cf4b1fec3d08efc1db1c380b8e..83ba313e350522cf4bba37df2227cfe2755fc905 100644 (file)
@@ -154,7 +154,95 @@ class Pin(object):
         return (name, res)
 
 
-class Interface(PeripheralIface):
+class InterfaceFmt(object):
+
+    def ifacepfmtdecpin(self, pin):
+        return pin.ifacepfmt(self.ifacepfmtdecfn)
+
+    def ifacepfmtdecfn(self, name):
+        return name
+
+    def ifacefmtoutfn(self, name):
+        return "wr%s" % name  # like wruart
+
+    def ifacefmtdecfn2(self, name):
+        return name  # like: uart
+
+    def ifacefmtdecfn3(self, name):
+        """ HACK! """
+        return "%s_outen" % name  # like uart_outen
+
+    def ifacefmtinfn(self, name):
+        return "wr%s" % name
+
+    def ifacedef2pin(self, pin):
+        decfn = self.ifacefmtdecfn2
+        outfn = self.ifacefmtoutfn
+        # print pin, pin.outenmode
+        if pin.outenmode:
+            decfn = self.ifacefmtdecfn3
+            outfn = self.ifacefmtoutenfn
+        return pin.ifacedef2(outfn, self.ifacefmtinfn,
+                             decfn)
+
+    def vectorifacedef2(self, pins, count, names, bitfmt, *args):
+        tput = []
+        tget = []
+        tputen = []
+        if len(pins) == 0:
+            return ''
+        # XXX HACK! assume in, out and inout, create set of indices
+        # that are repeated three times.
+        plens = []
+        # ARG even worse hack for LCD *sigh*...
+        if names[1] is None and names[2] is None:
+            plens = range(len(pins))
+        else:
+            for i in range(0, len(pins), 3):
+                plens += [i / 3, i / 3, i / 3]
+        for (typ, txt) in map(self.ifacedef3pin, plens, pins):
+            if typ == 'tput':
+                tput.append(txt)
+            elif typ == 'tget':
+                tget.append(txt)
+            elif typ == 'tputen':
+                tputen.append(txt)
+        tput = '\n'.join(tput).format(*args)
+        tget = '\n'.join(tget).format(*args)
+        tputen = '\n'.join(tputen).format(*args)
+        bitfmt = bitfmt.format(count)
+        template = ["""\
+              interface {3} = interface Put#({0})
+                 method Action put({2} in);
+{1}
+                 endmethod
+               endinterface;
+""",
+                    """\
+               interface {3} = interface Put#({0})
+                 method Action put({2} in);
+{1}
+                 endmethod
+               endinterface;
+""",
+                    """\
+               interface {3} = interface Get#({0})
+                 method ActionValue#({2}) get;
+                   {2} tget;
+{1}
+                   return tget;
+                 endmethod
+               endinterface;
+"""]
+        res = ''
+        tlist = [tput, tputen, tget]
+        for i, n in enumerate(names):
+            if n:
+                res += template[i].format(count, tlist[i], bitfmt, n)
+        return '\n' + res + '\n'
+
+
+class Interface(PeripheralIface, InterfaceFmt):
     """ create an interface from a list of pinspecs.
         each pinspec is a dictionary, see Pin class arguments
         single indicates that there is only one of these, and
@@ -170,6 +258,7 @@ class Interface(PeripheralIface):
 
     def __init__(self, ifacename, pinspecs, ganged=None, single=False):
         PeripheralIface.__init__(self, ifacename)
+        InterfaceFmt.__init__(self)
         self.ifacename = ifacename
         self.ganged = ganged or {}
         self.pins = []  # a list of instances of class Pin
@@ -288,32 +377,13 @@ class Interface(PeripheralIface):
         res = '\n'.join(map(self.ifacefmtdecpin, self.pins)).format(*args)
         return '\n' + res  # pins is a list
 
-    def ifacepfmtdecfn(self, name):
-        return name
-
     def ifacefmtdecfn(self, name):
         return name  # like: uart
 
-    def ifacefmtdecfn2(self, name):
-        return name  # like: uart
-
-    def ifacefmtdecfn3(self, name):
-        """ HACK! """
-        return "%s_outen" % name  # like uart_outen
-
-    def ifacefmtoutfn(self, name):
-        return "wr%s" % name  # like wruart
-
-    def ifacefmtinfn(self, name):
-        return "wr%s" % name
-
     def wirefmtpin(self, pin):
         return pin.wirefmt(self.ifacefmtoutfn, self.ifacefmtinfn,
                            self.ifacefmtdecfn2)
 
-    def ifacepfmtdecpin(self, pin):
-        return pin.ifacepfmt(self.ifacepfmtdecfn)
-
     def ifacefmtdecpin(self, pin):
         return pin.ifacefmt(self.ifacefmtdecfn)
 
@@ -327,16 +397,6 @@ class Interface(PeripheralIface):
         return pin.ifacedef(outfn, self.ifacefmtinfn,
                             decfn)
 
-    def ifacedef2pin(self, pin):
-        decfn = self.ifacefmtdecfn2
-        outfn = self.ifacefmtoutfn
-        # print pin, pin.outenmode
-        if pin.outenmode:
-            decfn = self.ifacefmtdecfn3
-            outfn = self.ifacefmtoutenfn
-        return pin.ifacedef2(outfn, self.ifacefmtinfn,
-                             decfn)
-
     def ifacedef(self, *args):
         res = '\n'.join(map(self.ifacefmtpin, self.pins))
         res = res.format(*args)
@@ -347,60 +407,6 @@ class Interface(PeripheralIface):
         res = res.format(*args)
         return '\n' + res + '\n'
 
-    def vectorifacedef2(self, pins, count, names, bitfmt, *args):
-        tput = []
-        tget = []
-        tputen = []
-        # XXX HACK! assume in, out and inout, create set of indices
-        # that are repeated three times.
-        plens = []
-        # ARG even worse hack for LCD *sigh*...
-        if names[1] is None and names[2] is None:
-            plens = range(len(pins))
-        else:
-            for i in range(0, len(pins), 3):
-                plens += [i/3, i/3, i/3]
-        for (typ, txt) in map(self.ifacedef3pin, plens, pins):
-            if typ == 'tput':
-                tput.append(txt)
-            elif typ == 'tget':
-                tget.append(txt)
-            elif typ == 'tputen':
-                tputen.append(txt)
-        tput = '\n'.join(tput).format(*args)
-        tget = '\n'.join(tget).format(*args)
-        tputen = '\n'.join(tputen).format(*args)
-        bitfmt = bitfmt.format(count)
-        template = ["""\
-              interface {3} = interface Put#({0})
-                 method Action put({2} in);
-{1}
-                 endmethod
-               endinterface;
-""",
-"""\
-               interface {3} = interface Put#({0})
-                 method Action put({2} in);
-{1}
-                 endmethod
-               endinterface;
-""",
-"""\
-               interface {3} = interface Get#({0})
-                 method ActionValue#({2}) get;
-                   {2} tget;
-{1}
-                   return tget;
-                 endmethod
-               endinterface;
-"""]
-        res = ''
-        tlist = [tput, tputen, tget]
-        for i, n in enumerate(names):
-            if n:
-                res += template[i].format(count, tlist[i], bitfmt, n)
-        return '\n' + res + '\n'
-
 
 class MuxInterface(Interface):
 
@@ -424,18 +430,28 @@ class IOInterface(Interface):
         return generic_io.format(*args)
 
 
-class InterfaceBus(object):
+class InterfaceBus(InterfaceFmt):
 
-    def __init__(self, namelist, bitspec, filterbus):
+    def __init__(self, pins, is_inout, namelist, bitspec, filterbus):
+        InterfaceFmt.__init__(self)
         self.namelist = namelist
         self.bitspec = bitspec
-        self.fbus = filterbus # filter identifying which are bus pins
+        self.fbus = filterbus  # filter identifying which are bus pins
+        self.pins_ = pins
+        self.is_inout = is_inout
+        self.buspins = filter(lambda x: x.name_.startswith(self.fbus),
+                              self.pins_)
+        self.nonbuspins = filter(lambda x: not x.name_.startswith(self.fbus),
+                                 self.pins_)
 
     def get_nonbuspins(self):
-        return filter(lambda x: not x.name_.startswith(self.fbus), self.pins)
+        return self.nonbuspins
 
     def get_buspins(self):
-        return filter(lambda x: x.name_.startswith(self.fbus), self.pins)
+        return self.buspins
+
+    def get_n_iopinsdiv(self):
+        return 3 if self.is_inout else 1
 
     def ifacepfmt(self, *args):
         pins = self.get_nonbuspins()
@@ -443,7 +459,7 @@ class InterfaceBus(object):
         res = res.format(*args)
 
         pins = self.get_buspins()
-        plen = self.get_n_iopins(pins)
+        plen = len(pins) / self.get_n_iopinsdiv()
 
         res += '\n'
         template = "          interface {1}#(%s) {2};\n" % self.bitspec
@@ -461,10 +477,13 @@ class InterfaceBus(object):
         res = res.format(*args)
 
         pins = self.get_buspins()
-        plen = self.get_n_iopins(pins)
+        plen = len(pins) / self.get_n_iopinsdiv()
+        for pin in pins:
+            print "ifbus pins", pin.name_, plen
         bitspec = self.bitspec.format(plen)
-        return '\n' + res + self.vectorifacedef2(pins, plen,
-                                    self.namelist, bitspec, *args) + '\n'
+        print self
+        return '\n' + res + self.vectorifacedef2(
+            pins, plen, self.namelist, bitspec, *args)
 
     def ifacedef3pin(self, idx, pin):
         decfn = self.ifacefmtdecfn2
@@ -477,42 +496,105 @@ class InterfaceBus(object):
                              decfn)
 
 
+class InterfaceMultiBus(object):
+
+    def __init__(self, pins):
+        self.multibus_specs = []
+        self.nonbuspins = pins
+        self.nonb = self.add_bus(False, [], '', "xxxxxxxnofilter")
+
+    def add_bus(self, is_inout, namelist, bitspec, filterbus):
+        pins = self.nonbuspins
+        buspins = filter(lambda x: x.name_.startswith(filterbus), pins)
+        nbuspins = filter(lambda x: not x.name_.startswith(filterbus), pins)
+        self.nonbuspins = nbuspins
+        b = InterfaceBus(buspins, is_inout,
+                         namelist, bitspec, filterbus)
+        print is_inout, namelist, filterbus, buspins
+        self.multibus_specs.append(b)
+        self.multibus_specs[0].pins_ = nbuspins
+        self.multibus_specs[0].nonbuspins = nbuspins
+
+    def ifacepfmt(self, *args):
+        res = ''
+        #res = Interface.ifacepfmt(self, *args)
+        for b in self.multibus_specs:
+            res += b.ifacepfmt(*args)
+        return res
+
+    def ifacedef2(self, *args):
+        res = ''
+        #res = Interface.ifacedef2(self, *args)
+        for b in self.multibus_specs:
+            res += b.ifacedef2(*args)
+        return res
+
+
 class InterfaceLCD(InterfaceBus, Interface):
 
     def __init__(self, *args):
-        InterfaceBus.__init__(self, ['data_out', None, None],
-                              "Bit#({0})", "out")
         Interface.__init__(self, *args)
+        InterfaceBus.__init__(self, self.pins, False, ['data_out', None, None],
+                              "Bit#({0})", "out")
+
 
-    def get_n_iopins(self, pins): # HACK! assume in/out/outen so div by 3
-        return len(pins) 
+class InterfaceSDRAM(InterfaceMultiBus, Interface):
+
+    def __init__(self, ifacename, pinspecs, ganged=None, single=False):
+        Interface.__init__(self, ifacename, pinspecs, ganged, single)
+        InterfaceMultiBus.__init__(self, self.pins)
+        self.add_bus(False, ['dqm', None, None],
+                     "Bit#({0})", "sdrdqm")
+        self.add_bus(True, ['d_out', 'd_out_en', 'd_in'],
+                     "Bit#({0})", "sdrd")
+        self.add_bus(False, ['ad', None, None],
+                     "Bit#({0})", "sdrad")
+        self.add_bus(False, ['ba', None, None],
+                     "Bit#({0})", "sdrba")
+
+    def ifacedef2(self, *args):
+        return InterfaceMultiBus.ifacedef2(self, *args)
+
+
+class InterfaceFlexBus(InterfaceMultiBus, Interface):
+
+    def __init__(self, ifacename, pinspecs, ganged=None, single=False):
+        Interface.__init__(self, ifacename, pinspecs, ganged, single)
+        InterfaceMultiBus.__init__(self, self.pins)
+        self.add_bus(True, ['ad_out', 'ad_out_en', 'ad_in'],
+                     "Bit#({0})", "ad")
+        self.add_bus(False, ['bwe', None, None],
+                     "Bit#({0})", "bwe")
+        self.add_bus(False, ['tsiz', None, None],
+                     "Bit#({0})", "tsiz")
+        self.add_bus(False, ['cs', None, None],
+                     "Bit#({0})", "cs")
+
+    def ifacedef2(self, *args):
+        return InterfaceMultiBus.ifacedef2(self, *args)
 
 
 class InterfaceSD(InterfaceBus, Interface):
 
     def __init__(self, *args):
-        InterfaceBus.__init__(self, ['io_out', 'io_out_en', 'io_in'],
-                              "Bit#({0})", "d")
         Interface.__init__(self, *args)
-
-    def get_n_iopins(self, pins): # HACK! assume in/out/outen so div by 3
-        return len(pins) / 3
+        InterfaceBus.__init__(self, self.pins, True, ['out', 'out_en', 'in'],
+                              "Bit#({0})", "d")
 
 
 class InterfaceNSPI(InterfaceBus, Interface):
 
     def __init__(self, *args):
-        InterfaceBus.__init__(self, ['io_out', 'io_out_en', 'io_in'],
-                              "Bit#({0})", "io")
         Interface.__init__(self, *args)
-
-    def get_n_iopins(self, pins): # HACK! assume in/out/outen so div by 3
-        return len(pins) / 3
+        InterfaceBus.__init__(self, self.pins, True,
+                              ['io_out', 'io_out_en', 'io_in'],
+                              "Bit#({0})", "io")
 
 
 class InterfaceEINT(Interface):
     """ uses old-style (non-get/put) for now
     """
+
     def ifacepfmt(self, *args):
         res = '\n'.join(map(self.ifacefmtdecpin, self.pins)).format(*args)
         return '\n' + res  # pins is a list
@@ -521,19 +603,15 @@ class InterfaceEINT(Interface):
         return self.ifacedef(*args)
 
 
-
 class InterfaceGPIO(InterfaceBus, Interface):
     """ note: the busfilter cuts out everything as the entire set of pins
         is a bus, but it's less code.  get_nonbuspins returns empty list.
     """
 
     def __init__(self, ifacename, pinspecs, ganged=None, single=False):
-        InterfaceBus.__init__(self, ['out', 'out_en', 'in'],
-                              "Vector#({0},Bit#(1))", ifacename[-1])
         Interface.__init__(self, ifacename, pinspecs, ganged, single)
-
-    def get_n_iopins(self, pins): # HACK! assume in/out/outen so div by 3
-        return len(pins) / 3
+        InterfaceBus.__init__(self, self.pins, True, ['out', 'out_en', 'in'],
+                              "Vector#({0},Bit#(1))", ifacename[-1])
 
 
 class Interfaces(InterfacesBase, PeripheralInterfaces):
@@ -544,9 +622,13 @@ class Interfaces(InterfacesBase, PeripheralInterfaces):
         InterfacesBase.__init__(self, Interface, pth,
                                 {'gpio': InterfaceGPIO,
                                  'spi': InterfaceNSPI,
+                                 'mspi': InterfaceNSPI,
                                  'lcd': InterfaceLCD,
                                  'sd': InterfaceSD,
+                                 'fb': InterfaceFlexBus,
+                                 'sdr': InterfaceSDRAM,
                                  'qspi': InterfaceNSPI,
+                                 'mqspi': InterfaceNSPI,
                                  'eint': InterfaceEINT})
         PeripheralInterfaces.__init__(self)