remove hard-coded additions of interfaces, use Interfaces class
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 22 Mar 2018 10:15:11 +0000 (10:15 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 22 Mar 2018 10:15:11 +0000 (10:15 +0000)
src/interface_decl.py
src/pinmux_generator.py

index 42269997d3b240df8e99677d89761623c22dd6a8..963f2831c83b39599e1ac707ff9bf2f2f02b819e 100644 (file)
@@ -173,7 +173,7 @@ class Interfaces(UserDict):
 
     def __init__(self):
         self.ifacecount = []
-        ifaces = {}
+        UserDict.__init__(self, {})
         with open('interfaces.txt', 'r') as ifile:
             for l in ifile.readlines():
                 l = l.strip()
@@ -181,10 +181,12 @@ class Interfaces(UserDict):
                 print l
                 name = l[0]
                 count = int(l[1])
-                self.ifacecount.append((name, count))
                 spec = self.read_spec(name)
-                ifaces[name] = Interface(name, spec)
-        UserDict.__init__(self, ifaces)
+                self.ifaceadd(name, count, Interface(name, spec))
+                
+    def ifaceadd(self, name, count, iface):
+        self.ifacecount.append((name, count))
+        self[name] = iface
 
     def read_spec(self, name):
         spec = []
@@ -201,6 +203,30 @@ class Interfaces(UserDict):
                 spec.append(d)
         return spec
 
+    def ifacedef(self, f, *args):
+        for (name, count) in self.ifacecount:
+            for i in range(count):
+                f.write(self.data[name].ifacedef(i))
+
+    def ifacefmt(self, f, *args):
+        comment = '''
+          // interface declaration between %s-{0} and pinmux'''
+        for (name, count) in self.ifacecount:
+            for i in range(count):
+                c = comment % name.upper()
+                f.write(c.format(i))
+                f.write(self.data[name].ifacefmt(i))
+
+    def wirefmt(self, f, *args):
+        comment = '\n      // following wires capture signals ' \
+                  'to IO CELL if %s-{0} is\n' \
+                  '      // allotted to it'
+        for (name, count) in self.ifacecount:
+            for i in range(count):
+                c = comment % name
+                f.write(c.format(i))
+                f.write(self.data[name].wirefmt(i))
+
 
 # ========= Interface declarations ================ #
 
@@ -225,44 +251,6 @@ io_interface = IOInterface('io',
 # Outputs from the peripherals will be inputs to the pinmux
 # module. Hence the change in direction for most pins
 
-uartinterface_decl = Interface('uart',
-                            [{'name': 'rx'},
-                             {'name': 'tx', 'action': True},
-                            ])
-
-spiinterface_decl = Interface('spi',
-                            [{'name': 'sclk', 'action': True},
-                             {'name': 'mosi', 'action': True},
-                             {'name': 'nss', 'action': True},
-                             {'name': 'miso'},
-                            ])
-
-twiinterface_decl = Interface('twi',
-                            [{'name': 'sda', 'outen': True},
-                             {'name': 'scl', 'outen': True},
-                            ])
-
-sdinterface_decl = Interface('sd',
-                            [{'name': 'clk', 'action': True},
-                             {'name': 'cmd', 'action': True},
-                             {'name': 'd0', 'outen': True},
-                             {'name': 'd1', 'outen': True},
-                             {'name': 'd2', 'outen': True},
-                             {'name': 'd3', 'outen': True}
-                            ])
-
-jtaginterface_decl = Interface('jtag',
-                            [{'name': 'tdi'},
-                             {'name': 'tms'},
-                             {'name': 'tclk'},
-                             {'name': 'trst'},
-                             {'name': 'tdo', 'action': True}
-                            ])
-
-pwminterface_decl = Interface('pwm',
-                            [{'name': "pwm", 'action': True}
-                            ])
-
 ifaces = Interfaces()
 
 # ======================================= #
@@ -270,6 +258,44 @@ ifaces = Interfaces()
 # basic test
 if __name__ == '__main__':
 
+    uartinterface_decl = Interface('uart',
+                                [{'name': 'rx'},
+                                 {'name': 'tx', 'action': True},
+                                ])
+
+    spiinterface_decl = Interface('spi',
+                                [{'name': 'sclk', 'action': True},
+                                 {'name': 'mosi', 'action': True},
+                                 {'name': 'nss', 'action': True},
+                                 {'name': 'miso'},
+                                ])
+
+    twiinterface_decl = Interface('twi',
+                                [{'name': 'sda', 'outen': True},
+                                 {'name': 'scl', 'outen': True},
+                                ])
+
+    sdinterface_decl = Interface('sd',
+                                [{'name': 'clk', 'action': True},
+                                 {'name': 'cmd', 'action': True},
+                                 {'name': 'd0', 'outen': True},
+                                 {'name': 'd1', 'outen': True},
+                                 {'name': 'd2', 'outen': True},
+                                 {'name': 'd3', 'outen': True}
+                                ])
+
+    jtaginterface_decl = Interface('jtag',
+                                [{'name': 'tdi'},
+                                 {'name': 'tms'},
+                                 {'name': 'tclk'},
+                                 {'name': 'trst'},
+                                 {'name': 'tdo', 'action': True}
+                                ])
+
+    pwminterface_decl = Interface('pwm',
+                                [{'name': "pwm", 'action': True}
+                                ])
+
     def _pinmunge(p, sep, repl, dedupe=True):
         """ munges the text so it's easier to compare.
             splits by separator, strips out blanks, re-joins.
index d656326030a99e1c0755ee82481e03a8b2ef9482..5197859272857755d3d391b56f79a64a926aae53 100644 (file)
@@ -94,35 +94,8 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file:
     # ==============================================================
 
     # == create method definitions for all peripheral interfaces ==#
-    for i in range(0, N_UART):
-        bsv_file.write('''
-          // interface declaration between UART-{0} and pinmux'''.format(i))
-        bsv_file.write(uartinterface_decl.ifacefmt(i))
-
-    for i in range(0, N_SPI):
-        bsv_file.write('''
-          // interface declaration between SPI-{0} and pinmux'''.format(i))
-        bsv_file.write(spiinterface_decl.ifacefmt(i))
+    ifaces.ifacefmt(bsv_file)
 
-    for i in range(0, N_TWI):
-        bsv_file.write('''
-          // interface declaration between TWI-{0} and pinmux'''.format(i))
-        bsv_file.write(twiinterface_decl.ifacefmt(i))
-
-    for i in range(0, N_SD):
-        bsv_file.write('''
-          // interface declaration between SD-{0} and pinmux'''.format(i))
-        bsv_file.write(sdinterface_decl.ifacefmt(i))
-
-    for i in range(0, N_JTAG):
-        bsv_file.write('''
-          // interface declaration between JTAG-{0} and pinmux'''.format(i))
-        bsv_file.write(jtaginterface_decl.ifacefmt(i))
-
-    for i in range(0, N_PWM):
-        bsv_file.write('''
-          // interface declaration between PWM-{0} and pinmux'''.format(i))
-        bsv_file.write(pwminterface_decl.ifacefmt(i))
     # ==============================================================
 
     # ===== finish interface definition and start module definition=======
@@ -152,41 +125,8 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file:
     for i in range(0, N_IO):
         bsv_file.write(generic_io.format(i))
 
-    for i in range(0, N_UART):
-        bsv_file.write(
-        '''\n      // following wires capture signals to IO CELL if uart-{0} is
-      // allotted to it'''.format(i))
-        bsv_file.write(uartinterface_decl.wirefmt(i))
-
-    for i in range(0, N_SPI):
-        bsv_file.write(
-        '''\n      // following wires capture signals to IO CELL if spi-{0} is
-      // allotted to it'''.format(i))
-        bsv_file.write(spiinterface_decl.wirefmt(i))
-
-    for i in range(0, N_TWI):
-        bsv_file.write(
-        '''\n      // following wires capture signals to IO CELL if twi-{0} is
-      // allotted to it'''.format(i))
-        bsv_file.write(twiinterface_decl.wirefmt(i))
-
-    for i in range(0, N_SD):
-        bsv_file.write(
-        '''\n      // following wires capture signals to IO CELL if sd-{0} is
-      // allotted to it'''.format(i))
-        bsv_file.write(sdinterface_decl.wirefmt(i))
-
-    for i in range(0, N_JTAG):
-        bsv_file.write(
-        '''\n      // following wires capture signals to IO CELL if jtag-{0} is
-      // allotted to it'''.format(i))
-        bsv_file.write(jtaginterface_decl.wirefmt(i))
-
-    for i in range(0, N_PWM):
-        bsv_file.write(
-        '''\n      // following wires capture signals to IO CELL if pwm-{0} is
-      // allotted to it'''.format(i))
-        bsv_file.write(pwminterface_decl.wirefmt(i))
+    ifaces.wirefmt(bsv_file)
+
     bsv_file.write("\n")
     # ====================================================================
     # ========================= Actual pinmuxing ========================#
@@ -211,18 +151,7 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file:
 ''')
     for i in range(0, N_IO):
         bsv_file.write(io_interface.ifacedef(i))
-    for i in range(0, N_UART):
-        bsv_file.write(uartinterface_decl.ifacedef(i))
-    for i in range(0, N_SPI):
-        bsv_file.write(spiinterface_decl.ifacedef(i))
-    for i in range(0, N_TWI):
-        bsv_file.write(twiinterface_decl.ifacedef(i))
-    for i in range(0, N_SD):
-        bsv_file.write(sdinterface_decl.ifacedef(i))
-    for i in range(0, N_JTAG):
-        bsv_file.write(jtaginterface_decl.ifacedef(i))
-    for i in range(0, N_PWM):
-        bsv_file.write(pwminterface_decl.ifacedef(i))
+    ifaces.ifacedef(bsv_file)
     bsv_file.write(footer)
     print("BSV file successfully generated: bsv_src/pinmux.bsv")
     # ======================================================================