remove write_ptp, add bitwidths.txt file
[pinmux.git] / src / spec / gen.py
1 import os
2 import os.path
3 from spec.interfaces import Pinouts
4
5
6 def specgen(of, pth, pinouts, bankspec, pinbanks, fixedpins, fastbus):
7 """ generates a specification of pinouts (tsv files)
8 for reading in by pinmux.
9
10 files generated:
11 * interfaces.txt - contains name and number of interfaces
12 * {interfacename}.txt - contains name of pin, type, and bus
13
14 type may be in, out or inout.
15 if type is "inout" then a THIRD optional parameter of type
16 "bus" indicates whether the bus is ganged together. in
17 future this may be "bus1", "bus2" and so on if an interface
18 contains more than one ganged group.
19
20 basically if the function needs to control whether a group
21 of pins shall be switched from input to output (as opposed
22 to the *pinmux* via user control deciding that), bus is
23 the way to indicate it.
24 """
25 pth = pth or ''
26 #print bankspec.keys()
27 #print fixedpins.keys()
28 #print pinouts.ganged.items()
29 if not os.path.exists(pth):
30 os.makedirs(pth)
31 with open(os.path.join(pth, 'bankwidths.txt'), 'w') as f:
32 for k, v in pinouts.muxwidths.items():
33 f.write("%s\t%d\n" % (k, v))
34 with open(os.path.join(pth, 'interfaces.txt'), 'w') as f:
35 for k in pinouts.fnspec.keys():
36 s = pinouts.fnspec[k]
37 line = [k.lower(), str(len(s))]
38 for b in fastbus:
39 if b.startswith(k.lower()):
40 line.append(b)
41 line = '\t'.join(line)
42 f.write("%s\n" % line)
43 s0 = s[list(s.keys())[0]] # hack, take first
44 gangedgroup = pinouts.ganged[k]
45 with open(os.path.join(pth, '%s.txt' % k.lower()), 'w') as g:
46 if len(s0.pingroup) == 1: # only one function, grouped higher
47 for ks in s.keys(): # grouped by interface
48 assert False, "TODO, single-function"
49 fntype = 'inout' # XXX TODO
50 k = s[ks].suffix
51 k_ = k.lower()
52 g.write("%s\t%s\n" % (k_, fntype))
53 else:
54 for pinname in s0.pingroup:
55 fntype = s0.fntype.get(pinname, 'inout')
56 pn = pinname.lower()
57 g.write("%s\t%s" % (pn, fntype))
58 if fntype == 'inout' and pinname in gangedgroup:
59 g.write("\tbus")
60 g.write("\n")
61
62 pks = sorted(pinouts.keys())
63
64 # truly dreadful way to work out the max mux size...
65 muxsz = 0
66 for k in pks:
67 for m in pinouts[k].keys():
68 muxsz = max(muxsz, m + 1)
69
70 # write out the mux...
71 with open(os.path.join(pth, 'pinmap.txt'), 'w') as g:
72 for k in pks:
73 res = [str(k)]
74 # append pin mux
75 for midx in range(muxsz):
76 if midx in pinouts[k]:
77 fname = pinouts[k][midx][0]
78 else:
79 fname = ''
80 res.append(fname.lower())
81 g.write('\t'.join(res) + '\n')
82
83 # ... and the dedicated pins
84 with open(os.path.join(pth, 'fixedpins.txt'), 'w') as g:
85 for p in fixedpins:
86 p = map(str, p)
87 p = map(str.lower, p)
88 g.write('\t'.join(p) + '\n')
89
90 # lists bankspec, shows where the pin-numbers *start*
91 of.write("# Pin Bank starting points and lengths\n\n")
92 with open(os.path.join(pth, 'pinspec.txt'), 'w') as g:
93 keys = sorted(bankspec.keys())
94 for bank in keys:
95 pinstart = bankspec[bank]
96 of.write("* %s %d %d\n" % (bank, pinstart, pinbanks[bank]))
97 g.write("%s\t%d\t%d\n" % (bank, pinstart, pinbanks[bank]))