from UserDict import UserDict
 
+from wire_def import generic_io # special case
 
 class Pin(object):
     """ pin interface declaration.
     def ifacefmtinfn(self, name):
         return "cell{0}_mux_in"
 
+    def wirefmt(self, *args):
+        return generic_io.format(*args)
+
 
 class Interfaces(UserDict):
     """ contains a list of interface definitions
                 spec = self.read_spec(name)
                 self.ifaceadd(name, count, Interface(name, spec))
 
-    def ifaceadd(self, name, count, iface):
-        self.ifacecount.append((name, count))
+    def ifaceadd(self, name, count, iface, at=None):
+        if at is None:
+            at = len(self.ifacecount)
+        self.ifacecount.insert(at, (name, count))
         self[name] = iface
 
     def read_spec(self, name):
 
 p = Parse()
 init(p)
 ifaces = Interfaces()
-#ifaces.ifaceadd('io', p.N_IO, io_interface)
+ifaces.ifaceadd('io', p.N_IO, io_interface, 0)
 
 if not os.path.exists("bsv_src"):
     os.makedirs("bsv_src")
       // declare the interface to the IO cells.
       // Each IO cell will have 8 input field (output from pin mux
       // and on output field (input to pinmux)''')
-    for i in range(0, p.N_IO):
-        bsv_file.write('''\n      // interface declaration between IO-{0} and pinmux'''.format(i))
-
-        bsv_file.write(io_interface.ifacefmt(i))
     # ==============================================================
 
     # == create method definitions for all peripheral interfaces ==#
         bsv_file.write(muxwire.format(
             cell[0], int(math.log(len(cell) - 1, 2))))
 
-    bsv_file.write(
-        '''\n      // following wires capture the values sent to the IO Cell''')
-    for i in range(0, p.N_IO):
-        bsv_file.write(generic_io.format(i))
-
     ifaces.wirefmt(bsv_file)
 
     bsv_file.write("\n")
     endinterface;
     interface peripheral_side = interface PeripheralSide
 ''')
-    for i in range(0, p.N_IO):
-        bsv_file.write(io_interface.ifacedef(i))
     ifaces.ifacedef(bsv_file)
     bsv_file.write(footer)
     print("BSV file successfully generated: bsv_src/pinmux.bsv")