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