argh reorder functions to not be recursively dependent
[pinmux.git] / src / pinmux_generator.py
index b38ffcb5555e9d40a518bdc74c4a1d2cf8a3cf26..52267f4870cfe536f181d37287b5fa5f7b01a889 100644 (file)
@@ -19,9 +19,8 @@
 import getopt
 import os.path
 import sys
-from spec import modules, specgen
-
-from bsv.pinmux_generator import pinmuxgen as bsvgen
+import json
+from spec import modules, specgen, dummytest
 
 
 def printhelp():
@@ -43,13 +42,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)
 
@@ -57,6 +57,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
@@ -66,22 +68,49 @@ 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)
+        pyname = os.path.join(output_dir or '', "%s_pins.py" % pinspec)
+        d = os.path.split(fname)[0]
+        if not os.path.exists(d):
+            os.makedirs(d)
+        with open(fname, "w") as of:
+            with open(pyname, "w") as pyf:
+                ps = module.pinspec()
+                pinout, bankspec, pin_spec, fixedpins = ps.write(of)
+                #chip['litex.map'] = litexmap
+                if testing:
+                    dummytest(ps, output_dir, output_type)
+                else:
+                    specgen(of, output_dir, pinout,
+                            bankspec, ps.muxwidths, pin_spec, fixedpins,
+                            ps.fastbus)
+                pm, chip = module.pinparse(ps, pinspec)
+                litexmap = ps.pywrite(pyf, pm)
+                chip = json.dumps(chip)
+                with open("%s/litex_pinpads.json" % pinspec, "w") as f:
+                    f.write(chip)
+
     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)