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