remove repeated calls, use __call__
[pinmux.git] / src / pinmux_generator.py
index 8d36f1c113a4ebb80d69b68cb11f0d6596d2ec50..b95377b4bad5d00485b4ed2c9fac5d56c8305b0a 100644 (file)
 import getopt
 import os.path
 import sys
-from spec import modules, specgen
-
-from bsv.pinmux_generator import pinmuxgen as bsvgen
+from spec import modules, specgen, dummytest
 
 
 def printhelp():
     print ('''pinmux_generator.py [-o outputdir] [-v|--validate] [-h|--help]
-                                  [-t outputtype]
+                                  [-t outputtype] [-s|--spec spec]
+    -s | spec       : generate from spec (python module)
     -t | outputtype : outputtype, defaults to bsv
     -o outputdir    : defaults to bsv_src.  also location for reading pinmux.txt
                       interfaces.txt and *.txt
@@ -42,13 +41,14 @@ if __name__ == '__main__':
             'o:vht:s:',
             ['output=',
              'validate',
+             'test',
              'outputtype=',
              'spec=',
              'help',
              'version=',
              ])
     except getopt.GetoptError as err:
-        print "ERROR: %s" % str(err)
+        print ("ERROR: %s" % str(err))
         printhelp()
         sys.exit(1)
 
@@ -56,6 +56,8 @@ if __name__ == '__main__':
     output_dir = None
     validate = False
     spec = None
+    pinspec = None
+    testing = False
     for opt, arg in options:
         if opt in ('-o', '--output'):
             output_dir = arg
@@ -65,22 +67,39 @@ if __name__ == '__main__':
             output_type = arg
         elif opt in ('-v', '--validate'):
             validate = True
+        elif opt in ('--test',):
+            testing = True
         elif opt in ('-h', '--help'):
             printhelp()
             sys.exit(0)
 
     if pinspec:
-        if not modules.has_key(pinspec):
-            print "ERROR: spec type '%s' does not exist" % pinspec
+        if pinspec not in modules:
+            print ("ERROR: spec type '%s' does not exist" % pinspec)
             printhelp()
             sys.exit(1)
         module = modules[pinspec]
-        pinout, bankspec, fixedpins = module.pinspec()
-        specgen(output_dir, pinout, bankspec, fixedpins)
+
+        fname = os.path.join(output_dir or '', "%s.mdwn" % pinspec)
+        d = os.path.split(fname)[0]
+        if not os.path.exists(d):
+            os.makedirs(d)
+        with open(fname, "w") as of:
+            ps = module.pinspec()
+            pinout, bankspec, pinspec, fixedpins = ps.write(of)
+            if testing:
+                dummytest(ps, output_dir, output_type)
+            else:
+                specgen(of, output_dir, pinout,
+                        bankspec, ps.muxwidths, pinspec, fixedpins, ps.fastbus)
     else:
-        gentypes = {'bsv': bsvgen}
-        if not gentypes.has_key(output_type):
-            print "ERROR: output type '%s' does not exist" % output_type
+        if output_type == 'bsv':
+            from bsv.pinmux_generator import pinmuxgen as gentypes
+        elif output_type == 'myhdl':
+            from myhdlgen.pinmux_generator import pinmuxgen as gentypes
+        else:
+            print ("ERROR: output type '%s' does not exist" % output_type)
             printhelp()
             sys.exit(0)
-        gentypes[output_type](output_dir, validate)
+
+        gentypes(output_dir, validate)