04d3cf98981e45b77d4b9e0a044671e491239e58
[pinmux.git] / src / pinmux_generator.py
1 # ================================== Steps to add peripherals ============
2 # Step-1: create interface declaration for the peripheral to be added.
3 # Remember these are interfaces defined for the pinmux and hence
4 # will be opposite to those defined at the peripheral.
5 # For eg. the output TX from the UART will be input (method Action)
6 # for the pinmux.
7 # These changes will have to be done in interface_decl.py
8 # Step-2 define the wires that will be required to transfer data from the
9 # peripheral interface to the IO cell and vice-versa. Create a
10 # mkDWire for each input/output between the peripheral and the
11 # pinmux. Also create an implicit wire of GenericIOType for each cell
12 # that can be connected to a each bit from the peripheral.
13 # These changes will have to be done in wire_def.py
14 # Step-3: create the definitions for each of the methods defined above.
15 # These changes will have to be done in interface_decl.py
16 # ========================================================================
17
18 # default module imports
19 import getopt
20 import os.path
21 import sys
22 import json
23 from spec import modules, specgen, dummytest
24
25
26 def printhelp():
27 print ('''pinmux_generator.py [-o outputdir] [-v|--validate] [-h|--help]
28 [-t outputtype] [-s|--spec spec]
29 -s | spec : generate from spec (python module)
30 -t | outputtype : outputtype, defaults to bsv
31 -o outputdir : defaults to bsv_src. also location for reading pinmux.txt
32 interfaces.txt and *.txt
33 -v | --validate : runs some validation on the pinmux
34 -h | --help : this help message
35 ''')
36
37
38 if __name__ == '__main__':
39 try:
40 options, remainder = getopt.getopt(
41 sys.argv[1:],
42 'o:vht:s:',
43 ['output=',
44 'validate',
45 'test',
46 'outputtype=',
47 'spec=',
48 'help',
49 'version=',
50 ])
51 except getopt.GetoptError as err:
52 print ("ERROR: %s" % str(err))
53 printhelp()
54 sys.exit(1)
55
56 output_type = 'bsv'
57 output_dir = None
58 validate = False
59 spec = None
60 pinspec = None
61 testing = False
62 for opt, arg in options:
63 if opt in ('-o', '--output'):
64 output_dir = arg
65 elif opt in ('-s', '--spec'):
66 pinspec = arg
67 elif opt in ('-t', '--outputtype'):
68 output_type = arg
69 elif opt in ('-v', '--validate'):
70 validate = True
71 elif opt in ('--test',):
72 testing = True
73 elif opt in ('-h', '--help'):
74 printhelp()
75 sys.exit(0)
76
77 if pinspec:
78 if pinspec not in modules:
79 print ("ERROR: spec type '%s' does not exist" % pinspec)
80 printhelp()
81 sys.exit(1)
82 module = modules[pinspec]
83
84 fname = os.path.join(output_dir or '', "%s.mdwn" % pinspec)
85 pyname = os.path.join(output_dir or '', "%s_pins.py" % pinspec)
86 d = os.path.split(fname)[0]
87 if not os.path.exists(d):
88 os.makedirs(d)
89 with open(fname, "w") as of:
90 with open(pyname, "w") as pyf:
91 ps = module.pinspec()
92 pm, chip = module.pinparse(ps, pinspec)
93 litexmap = ps.pywrite(pyf, pm)
94 pinout, bankspec, pin_spec, fixedpins = ps.write(of)
95 chip['litex.map'] = litexmap
96 chip = json.dumps(chip)
97 with open("%s/litex_pinpads.json" % pinspec, "w") as f:
98 f.write(chip)
99
100 if testing:
101 dummytest(ps, output_dir, output_type)
102 else:
103 specgen(of, output_dir, pinout,
104 bankspec, ps.muxwidths, pin_spec, fixedpins,
105 ps.fastbus)
106 else:
107 if output_type == 'bsv':
108 from bsv.pinmux_generator import pinmuxgen as gentypes
109 elif output_type == 'myhdl':
110 from myhdlgen.pinmux_generator import pinmuxgen as gentypes
111 else:
112 print ("ERROR: output type '%s' does not exist" % output_type)
113 printhelp()
114 sys.exit(0)
115
116 gentypes(output_dir, validate)