add first auto-generated interface_def (io_interface_def)
[pinmux.git] / src / interface_decl.py
1
2 class Pin(object):
3 """ pin interface declaration.
4 * name is the name of the pin
5 * ready, enabled and io all create a (* .... *) prefix
6 * action changes it to an "in" if true
7 """
8
9 def __init__(self, name,
10 ready=True,
11 enabled=True,
12 io=False,
13 action=False):
14 self.name = name
15 self.ready = ready
16 self.enabled = enabled
17 self.io = io
18 self.action = action
19
20 def ifacefmt(self):
21 res = ' '
22 status = []
23 if self.ready:
24 status.append('always_ready')
25 if self.enabled:
26 status.append('always_enabled')
27 if self.io:
28 status.append('result="io"')
29 if status:
30 res += '(*'
31 res += ','.join(status)
32 res += '*)'
33 res += " method "
34 if self.io:
35 res += "\n "
36 if self.action:
37 res += " Action "
38 res += self.name
39 res += ' (Bit#(1) in)'
40 else:
41 res += " Bit#(1) "
42 res += self.name
43 res += ";"
44 return res
45
46 def ifacedef(self, fmtoutfn=None, fmtinfn=None):
47 res = ' method '
48 if self.action:
49 fmtname = fmtinfn(self.name) if fmtinfn else self.name
50 res += "Action "
51 res += self.name
52 res += '(Bit#(1) in);\n'
53 res += ' %s<=in;\n' % fmtname
54 res += ' endmethod'
55 else:
56 fmtname = fmtoutfn(self.name) if fmtoutfn else self.name
57 res += "%s=%s;" % (self.name, fmtname)
58 return res
59
60
61 class Interface(object):
62 """ create an interface from a list of pinspecs.
63 each pinspec is a dictionary, see Pin class arguments
64 """
65
66 def __init__(self, pinspecs):
67 self.pins = []
68 for p in pinspecs:
69 if p.get('outen') is True: # special case, generate 3 pins
70 _p = {}
71 _p.update(p)
72 del _p['outen']
73 for psuffix in ['out', 'outen', 'in']:
74 _p['name'] = "%s_%s" % (p['name'], psuffix)
75 _p['action'] = psuffix != 'in'
76 self.pins.append(Pin(**_p))
77 else:
78 self.pins.append(Pin(**p))
79
80 def ifacefmt(self, i):
81 return '\n'+'\n'.join(map(lambda x:x.ifacefmt(), self.pins)).format(i)
82
83 def ifacefmtoutfn(self, name):
84 return name
85
86 def ifacefmtinfn(self, name):
87 return "wr%s" % name
88
89 def ifacefmtpin(self, pin):
90 return pin.ifacedef(self.ifacefmtoutfn, self.ifacefmtinfn)
91
92 def ifacedef(self, i):
93 res = '\n'.join(map(self.ifacefmtpin, self.pins)).format(i)
94 return '\n' + res + '\n'
95
96
97 class IOInterface(Interface):
98
99 def ifacefmtoutfn(self, name):
100 return "cell{0}_out.%s" % (name[3:-4])
101
102 def ifacefmtinfn(self, name):
103 return "cell{0}_in"
104
105
106 # ========= Interface declarations ================ #
107
108 mux_interface = '''
109 method Action cell{0}_mux(Bit#({1}) in);'''
110
111 io_interface = IOInterface([{'name': 'io_outputval_{0}', 'enabled': False},
112 {'name': 'io_output_en_{0}', 'enabled': False},
113 {'name': 'io_input_en_{0}', 'enabled': False},
114 {'name': 'io_pullup_en_{0}', 'enabled': False},
115 {'name': 'io_pulldown_en_{0}', 'enabled': False},
116 {'name': 'io_drivestrength_{0}', 'enabled': False},
117 {'name': 'io_pushpull_en_{0}', 'enabled': False},
118 {'name': 'io_opendrain_en_{0}', 'enabled': False},
119 {'name': 'io_inputval_{0}', 'action': True, 'io': True},
120 ])
121
122 # == Peripheral Interface definitions == #
123 # these are the interface of the peripherals to the pin mux
124 # Outputs from the peripherals will be inputs to the pinmux
125 # module. Hence the change in direction for most pins
126
127 uartinterface_decl = Interface([{'name': 'tx_{0}', 'action': True},
128 {'name': 'rx_{0}'},
129 ])
130
131 spiinterface_decl = Interface([{'name': 'sclk_{0}', 'action': True},
132 {'name': 'mosi_{0}', 'action': True},
133 {'name': 'ss_{0}', 'action': True},
134 {'name': 'miso_{0}'},
135 ])
136
137 twiinterface_decl = Interface([{'name': 'sda{0}', 'outen': True},
138 {'name': 'scl{0}', 'outen': True},
139 ])
140
141 sdinterface_decl = Interface([{'name': 'sd{0}_clk', 'action': True},
142 {'name': 'sd{0}_cmd', 'action': True},
143 {'name': 'sd{0}_d0', 'outen': True},
144 {'name': 'sd{0}_d1', 'outen': True},
145 {'name': 'sd{0}_d2', 'outen': True},
146 {'name': 'sd{0}_d3', 'outen': True}
147 ])
148
149 jtaginterface_decl = Interface([{'name': 'jtag{0}_tdi'},
150 {'name': 'jtag{0}_tms'},
151 {'name': 'jtag{0}_tclk'},
152 {'name': 'jtag{0}_trst'},
153 {'name': 'jtag{0}_tdo', 'action': True}])
154
155 pwminterface_decl = Interface([{'name': "pwm{0}", 'action': True}])
156
157 # ======================================= #
158
159 # basic test
160 if __name__ == '__main__':
161
162 def _pinmunge(p, sep, repl, dedupe=True):
163 """ munges the text so it's easier to compare.
164 splits by separator, strips out blanks, re-joins.
165 """
166 p = p.strip()
167 p = p.split(sep)
168 if dedupe:
169 p = filter(lambda x: x, p) # filter out blanks
170 return repl.join(p)
171
172 def pinmunge(p):
173 """ munges the text so it's easier to compare.
174 """
175 # first join lines by semicolons, strip out returns
176 p = p.split(";")
177 p = map(lambda x: x.replace('\n', ''), p)
178 p = '\n'.join(p)
179 # now split first by brackets, then spaces (deduping on spaces)
180 p = _pinmunge(p, "(", " ( ", False)
181 p = _pinmunge(p, ")", " ) ", False)
182 p = _pinmunge(p, " ", " ")
183 return p
184
185 from interface_def import io_interface_def
186 print io_interface_def.format(0)
187 print io_interface.ifacedef(0)