0dfdef591043aeb94747804d7dc2e56d57483373
[pinmux.git] / src / myhdl / pinmux_generator.py
1 from parse import Parse
2 from ifacebase import InterfacesBase
3 try:
4 from string import maketrans
5 except ImportError:
6 maketrans = str.maketrans
7
8 digits = maketrans('0123456789', ' ' * 10) # delete space later
9
10 # XXX hmmm duplicated from src/bsc/actual_pinmux.py
11 def transfn(temp):
12 """ removes the number from the string of signal name.
13 """
14 temp = temp.split('_')
15 if len(temp) == 2:
16 temp[0] = temp[0].translate(digits)
17 temp[0] = temp[0] .replace(' ', '')
18 return '_'.join(temp)
19
20
21 class Pin(object):
22 """ pin interface declaration.
23 * name is the name of the pin
24 * ready, enabled and io all create a (* .... *) prefix
25 * action changes it to an "in" if true
26 """
27
28 def __init__(self, name,
29 ready=True,
30 enabled=True,
31 io=False,
32 action=False,
33 bitspec=None,
34 outenmode=False):
35 self.name = name
36 self.ready = ready
37 self.enabled = enabled
38 self.io = io
39 self.action = action
40 self.bitspec = bitspec if bitspec else 'Bit#(1)'
41 self.outenmode = outenmode
42
43
44 class Interface(object):
45 """ create an interface from a list of pinspecs.
46 each pinspec is a dictionary, see Pin class arguments
47 single indicates that there is only one of these, and
48 so the name must *not* be extended numerically (see pname)
49 """
50 # sample interface object:
51 """
52 twiinterface_decl = Interface('twi',
53 [{'name': 'sda', 'outen': True},
54 {'name': 'scl', 'outen': True},
55 ])
56 """
57
58 def __init__(self, ifacename, pinspecs, ganged=None, single=False):
59 self.ifacename = ifacename
60 self.ganged = ganged or {}
61 self.pins = [] # a list of instances of class Pin
62 self.pinspecs = pinspecs # a list of dictionary
63 self.single = single
64 for p in pinspecs:
65 _p = {}
66 _p.update(p)
67 if p.get('outen') is True: # special case, generate 3 pins
68 del _p['outen']
69 for psuffix in ['out', 'outen', 'in']:
70 # changing the name (like sda) to (twi_sda_out)
71 _p['name'] = "%s_%s" % (self.pname(p['name']), psuffix)
72 _p['action'] = psuffix != 'in'
73 self.pins.append(Pin(**_p))
74 # will look like {'name': 'twi_sda_out', 'action': True}
75 # {'name': 'twi_sda_outen', 'action': True}
76 #{'name': 'twi_sda_in', 'action': False}
77 # NOTice - outen key is removed
78 else:
79 _p['name'] = self.pname(p['name'])
80 self.pins.append(Pin(**_p))
81
82 def getifacetype(self, name):
83 for p in self.pinspecs:
84 fname = "%s_%s" % (self.ifacename, p['name'])
85 #print "search", self.ifacename, name, fname
86 if fname == name:
87 if p.get('action'):
88 return 'out'
89 elif p.get('outen'):
90 return 'inout'
91 return 'input'
92 return None
93
94 def pname(self, name):
95 """ generates the interface spec e.g. flexbus_ale
96 if there is only one flexbus interface, or
97 sd{0}_cmd if there are several. string format
98 function turns this into sd0_cmd, sd1_cmd as
99 appropriate. single mode stops the numerical extension.
100 """
101 if self.single:
102 return '%s_%s' % (self.ifacename, name)
103 return '%s{0}_%s' % (self.ifacename, name)
104
105
106
107 class Interfaces(InterfacesBase):
108 """ contains a list of interface definitions
109 """
110
111 def __init__(self, pth=None):
112 InterfacesBase.__init__(self, Interface, pth)
113
114
115 def init(p, ifaces):
116 for cell in p.muxed_cells:
117
118 for i in range(0, len(cell) - 1):
119 cname = cell[i + 1]
120 if not cname: # skip blank entries, no need to test
121 continue
122 temp = transfn(cname)
123 x = ifaces.getifacetype(temp)
124 print (cname, temp, x)
125
126
127 def pinmuxgen(pth=None, verify=True):
128 p = Parse(pth, verify)
129 print p, dir(p)
130 ifaces = Interfaces(pth)
131 init(p, ifaces)