decoupling interfaces for IO and memory mapped registers
[pinmux.git] / src / pinmux_generator.py
1 # ================================== Steps to add peripherals ============
2 # Step-1: create interface declaration for the peripheral to be added.
3 # Remember these are interfaces defined for the pinmux and hence
4 # will be opposite to those defined at the peripheral.
5 # For eg. the output TX from the UART will be input (method Action)
6 # for the pinmux.
7 # These changes will have to be done in interface_decl.py
8 # Step-2 define the wires that will be required to transfer data from the
9 # peripheral interface to the IO cell and vice-versa. Create a
10 # mkDWire for each input/output between the peripheral and the
11 # pinmux. Also create an implicit wire of GenericIOType for each cell
12 # that can be connected to a each bit from the peripheral.
13 # These changes will have to be done in wire_def.py
14 # Step-3: create the definitions for each of the methods defined above.
15 # These changes will have to be done in interface_decl.py
16 # ========================================================================
17
18 # default module imports
19 import os
20 import sys
21 import time
22 import math
23
24 # project module imports
25 from interface_decl import *
26 from interface_def import *
27 from parse import *
28 from wire_def import *
29 from actual_pinmux import *
30
31 if not os.path.exists("bsv_src"):
32 os.makedirs("bsv_src")
33
34 bsv_file = open("./bsv_src/pinmux.bsv", "w")
35
36
37 header = '''
38 /*
39 This BSV file has been generated by the PinMux tool available at:
40 https://bitbucket.org/casl/pinmux.
41
42 Authors: Neel Gala, Luke
43 Date of generation: ''' + time.strftime("%c") + '''
44 */
45 package pinmux;
46
47 typedef struct{
48 Bit#(1) outputval; // output from core to pad bit7
49 Bit#(1) output_en; // output enable from core to pad bit6
50 Bit#(1) input_en; // input enable from core to io_cell bit5
51 Bit#(1) pullup_en; // pullup enable from core to io_cell bit4
52 Bit#(1) pulldown_en; // pulldown enable from core to io_cell bit3
53 Bit#(1) drivestrength; // drivestrength from core to io_cell bit2
54 Bit#(1) pushpull_en; // pushpull enable from core to io_cell bit1
55 Bit#(1) opendrain_en; // opendrain enable form core to io_cell bit0
56 } GenericIOType deriving(Eq,Bits,FShow);
57
58 interface MuxSelectionLines;
59 '''
60 footer = '''
61 endinterface;
62 endmodule
63 endpackage
64 '''
65 # ============================================#
66 # ==== populating the file with the code =====#
67 # ============================================#
68
69 # package and interface declaration followed by the generic io_cell definition
70 bsv_file.write(header)
71
72 bsv_file.write('''
73
74 // declare the method which will capture the user pin-mux
75 // selection values.The width of the input is dependent on the number
76 // of muxes happening per IO. For now we have a generalized width
77 // where each IO will have the same number of muxes.''')
78
79 for cell in muxed_cells:
80 bsv_file.write(mux_interface.format(cell[0],
81 int(math.log(len(cell) - 1, 2))))
82
83 bsv_file.write('''
84 endinterface
85
86 interface PeripheralSide;
87 // declare the interface to the IO cells.
88 // Each IO cell will have 8 input field (output from pin mux
89 // and on output field (input to pinmux)''')
90 for i in range(0, N_IO):
91 bsv_file.write('''\n // interface for IO CEll-{0}''')
92 bsv_file.write(io_interface.format(i))
93 # ==============================================================
94
95 # == create method definitions for all peripheral interfaces ==#
96 for i in range(0, N_UART):
97 bsv_file.write('''
98 // interface declaration between UART-{0} and pinmux'''.format(i))
99 bsv_file.write(uartinterface_decl.format(i))
100
101 for i in range(0, N_SPI):
102 bsv_file.write('''
103 // interface declaration between SPI-{0} and pinmux'''.format(i))
104 bsv_file.write(spiinterface_decl.format(i))
105
106 for i in range(0, N_TWI):
107 bsv_file.write('''
108 // interface declaration between TWI-{0} and pinmux'''.format(i))
109 bsv_file.write(twiinterface_decl.format(i))
110
111 for i in range(0, N_SD):
112 bsv_file.write('''
113 // interface declaration between SD-{0} and pinmux'''.format(i))
114 bsv_file.write(sdinterface_decl.format(i))
115
116 for i in range(0, N_JTAG):
117 bsv_file.write('''
118 // interface declaration between JTAG-{0} and pinmux'''.format(i))
119 bsv_file.write(jtaginterface_decl.format(i))
120 # ==============================================================
121
122 # ===== finish interface definition and start module definition=======
123 bsv_file.write('''
124 endinterface
125
126 interface Ifc_pinmux;
127 interface MuxSelectionLines mux_lines;
128 interface PeripheralSide peripheral_side;
129 endinterface
130 (*synthesize*)
131 module mkpinmux(Ifc_pinmux);
132 ''')
133 # ====================================================================
134
135 # ======================= create wire and registers =================#
136 bsv_file.write('''
137 // the followins wires capture the pin-mux selection
138 // values for each mux assigned to a CELL
139 ''')
140 for cell in muxed_cells:
141 bsv_file.write(muxwire.format(cell[0], int(math.log(len(cell) - 1, 2))))
142
143
144 bsv_file.write(
145 '''\n // following wires capture the values sent to the IO Cell''')
146 for i in range(0, N_IO):
147 bsv_file.write(generic_io.format(i))
148
149 for i in range(0, N_UART):
150 bsv_file.write(
151 '''\n // following wires capture signals to IO CELL if uart-{0} is
152 // allotted to it'''.format(i))
153 bsv_file.write(uartwires.format(i))
154
155 for i in range(0, N_SPI):
156 bsv_file.write(
157 '''\n // following wires capture signals to IO CELL if spi-{0} is
158 // allotted to it'''.format(i))
159 bsv_file.write(spiwires.format(i))
160
161 for i in range(0, N_TWI):
162 bsv_file.write(
163 '''\n // following wires capture signals to IO CELL if twi-{0} is
164 // allotted to it'''.format(i))
165 bsv_file.write(twiwires.format(i))
166
167 for i in range(0, N_SD):
168 bsv_file.write(
169 '''\n // following wires capture signals to IO CELL if sd-{0} is
170 // allotted to it'''.format(i))
171 bsv_file.write(sdwires.format(i))
172
173 for i in range(0, N_JTAG):
174 bsv_file.write(
175 '''\n // following wires capture signals to IO CELL if jtag-{0} is
176 // allotted to it'''.format(i))
177 bsv_file.write(jtagwires.format(i))
178 bsv_file.write("\n")
179 # ====================================================================
180 # ========================= Actual pinmuxing ========================#
181 bsv_file.write('''
182 /*====== This where the muxing starts for each io-cell======*/
183 ''')
184 bsv_file.write(pinmux)
185 bsv_file.write('''
186 /*============================================================*/
187 ''')
188 # ====================================================================
189 # ================= interface definitions for each method =============#
190 bsv_file.write('''
191 interface mux_lines = interface MuxSelectionLines
192 ''')
193 for cell in muxed_cells:
194 bsv_file.write(mux_interface_def.format(cell[0],
195 int(math.log(len(cell) - 1, 2))))
196 bsv_file.write('''
197 endinterface;
198 interface peripheral_side = interface PeripheralSide
199 ''')
200 for i in range(0, N_IO):
201 bsv_file.write(io_interface_def.format(i))
202 for i in range(0, N_UART):
203 bsv_file.write(uartinterface_def.format(i))
204 for i in range(0, N_SPI):
205 bsv_file.write(spiinterface_def.format(i))
206 for i in range(0, N_TWI):
207 bsv_file.write(twiinterface_def.format(i))
208 for i in range(0, N_SD):
209 bsv_file.write(sdinterface_def.format(i))
210 for i in range(0, N_JTAG):
211 bsv_file.write(jtaginterface_def.format(i))
212 bsv_file.write(footer)
213 print("BSV file successfully generated: bsv_src/pinmux.bsv")
214 # ======================================================================
215 bsv_file.close()
216
217 bsv_file = open('bsv_src/PinTop.bsv','w')
218 bsv_file.write('''
219 package PinTop;
220 import pinmux::*;
221 interface Ifc_PintTop;
222 interface PeripheralSide peripheral_side;
223 endinterface
224
225 module mkPinTop(Ifc_PintTop);
226 Ifc_pinmux pinmux <-mkpinmux;
227 interface peripheral_side=pinmux.peripheral_side;
228 endmodule
229 endpackage
230 ''')
231 bsv_file.close