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)
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 # ========================================================================
18 # default module imports
25 # project module imports
26 from bsv
.interface_decl
import Interfaces
, mux_interface
, io_interface
27 from parse
import Parse
28 from bsv
.actual_pinmux
import init
29 from bsv
.bus_transactors
import axi4_lite
33 This BSV file has been generated by the PinMux tool available at:
34 https://bitbucket.org/casl/pinmux.
36 Authors: Neel Gala, Luke
37 Date of generation: ''' + time
.strftime("%c") + '''
40 header
= copyright
+ '''
43 // FunctionType: contains the active wires of a function. That INCLUDES
44 // GPIO (as GPIO is also a "Function"). These are what get muxed.
45 // However, only GPIO "Functions" will end up with Register SRAMs.
47 Bit#(1) outputval; // output from function to pad bit2
48 Bit#(1) inputval; // input from pad to function bit1
49 Bit#(1) output_en; // output enable from core to pad bit0
50 } FunctionType deriving(Eq,Bits,FShow);
52 // PadCharacteristicsType: this is the controllable characteristics
53 // of the I/O cell. They are NOT multiplexed. Registers are to be
54 // created which manage these characteristics
56 Bit#(1) pullup_en; // pullup enable from core to io_cell bit4
57 Bit#(1) pulldown_en; // pulldown enable from core to io_cell bit3
58 Bit#(1) drivestrength; // drivestrength from core to io_cell bit2
59 Bit#(1) pushpull_en; // pushpull enable from core to io_cell bit1
60 Bit#(1) opendrain_en; // opendrain enable form core to io_cell bit0
61 } PadCharacteristicsType deriving(Eq,Bits,FShow);
63 // TODO: leave this in for now, needs replacing with the above
65 Bit#(1) outputval; // output from core to pad bit7
66 Bit#(1) output_en; // output enable from core to pad bit6
67 Bit#(1) input_en; // input enable from core to io_cell bit5
68 Bit#(1) pullup_en; // pullup enable from core to io_cell bit4
69 Bit#(1) pulldown_en; // pulldown enable from core to io_cell bit3
70 Bit#(1) drivestrength; // drivestrength from core to io_cell bit2
71 Bit#(1) pushpull_en; // pushpull enable from core to io_cell bit1
72 Bit#(1) opendrain_en; // opendrain enable form core to io_cell bit0
73 } GenericIOType deriving(Eq,Bits,FShow);
83 def pinmuxgen(pth
=None, verify
=True):
84 """ populating the file with the code
87 p
= Parse(pth
, verify
)
88 ifaces
= Interfaces(pth
)
89 ifaces
.ifaceadd('io', p
.N_IO
, io_interface
, 0)
94 bp
= os
.path
.join(pth
, bp
)
95 if not os
.path
.exists(bp
):
97 bl
= os
.path
.join(bp
, 'bsv_lib')
98 if not os
.path
.exists(bl
):
101 cwd
= os
.path
.split(__file__
)[0]
103 # copy over template and library files
104 shutil
.copyfile(os
.path
.join(cwd
, 'Makefile.template'),
105 os
.path
.join(bp
, 'Makefile'))
106 cwd
= os
.path
.join(cwd
, 'bsv_lib')
107 for fname
in ['AXI4_Lite_Types.bsv', 'Semi_FIFOF.bsv']:
108 shutil
.copyfile(os
.path
.join(cwd
, fname
),
109 os
.path
.join(bl
, fname
))
111 bus
= os
.path
.join(bp
, 'busenable.bsv')
112 pmp
= os
.path
.join(bp
, 'pinmux.bsv')
113 ptp
= os
.path
.join(bp
, 'PinTop.bsv')
114 bvp
= os
.path
.join(bp
, 'bus.bsv')
116 write_pmp(pmp
, p
, ifaces
)
117 write_ptp(ptp
, p
, ifaces
)
118 write_bvp(bvp
, p
, ifaces
)
119 write_bus(bus
, p
, ifaces
)
122 def write_bus(bus
, p
, ifaces
):
123 # package and interface declaration followed by
124 # the generic io_cell definition
125 with
open(bus
, "w") as bsv_file
:
126 ifaces
.busfmt(bsv_file
)
129 def get_cell_bit_width(p
):
131 for cell
in p
.muxed_cells
:
132 max_num_cells
= max(len(cell
) - 1, max_num_cells
)
133 return int(math
.log(max_num_cells
, 2))
136 def write_pmp(pmp
, p
, ifaces
):
137 # package and interface declaration followed by
138 # the generic io_cell definition
139 with
open(pmp
, "w") as bsv_file
:
140 bsv_file
.write(header
)
143 interface MuxSelectionLines;
145 // declare the method which will capture the user pin-mux
146 // selection values.The width of the input is dependent on the number
147 // of muxes happening per IO. For now we have a generalized width
148 // where each IO will have the same number of muxes.''')
150 for cell
in p
.muxed_cells
:
151 cnum
= 'Bit#(' + str(int(math
.log(len(cell
) - 1, 2))) + ')'
152 bsv_file
.write(mux_interface
.ifacefmt(cell
[0], cnum
))
157 interface PeripheralSide;
158 // declare the interface to the IO cells.
159 // Each IO cell will have 8 input field (output from pin mux
160 // and on output field (input to pinmux)''')
161 # ==============================================================
163 # == create method definitions for all peripheral interfaces ==#
164 ifaces
.ifacefmt(bsv_file
)
166 # ==============================================================
168 # ===== finish interface definition and start module definition=======
172 interface Ifc_pinmux;
173 interface MuxSelectionLines mux_lines;
174 interface PeripheralSide peripheral_side;
177 module mkpinmux(Ifc_pinmux);
179 # ====================================================================
181 # ======================= create wire and registers =================#
183 // the followins wires capture the pin-mux selection
184 // values for each mux assigned to a CELL
186 cell_bit_width
= 'Bit#(%d)' % get_cell_bit_width(p
)
187 for cell
in p
.muxed_cells
:
188 bsv_file
.write(mux_interface
.wirefmt(
189 cell
[0], cell_bit_width
))
191 ifaces
.wirefmt(bsv_file
)
194 # ====================================================================
195 # ========================= Actual pinmuxing ========================#
197 /*====== This where the muxing starts for each io-cell======*/
199 bsv_file
.write(p
.pinmux
)
201 /*============================================================*/
203 # ====================================================================
204 # ================= interface definitions for each method =============#
206 interface mux_lines = interface MuxSelectionLines
208 for cell
in p
.muxed_cells
:
210 mux_interface
.ifacedef(
211 cell
[0], cell_bit_width
))
214 interface peripheral_side = interface PeripheralSide
216 ifaces
.ifacedef(bsv_file
)
217 bsv_file
.write(footer
)
218 print("BSV file successfully generated: bsv_src/pinmux.bsv")
219 # ======================================================================
222 def write_ptp(ptp
, p
, ifaces
):
223 with
open(ptp
, 'w') as bsv_file
:
224 bsv_file
.write(copyright
+ '''
227 interface Ifc_PintTop;
228 method ActionValue#(Bool) write(Bit#({0}) addr, Bit#({1}) data);
229 method Tuple2#(Bool,Bit#({1})) read(Bit#({0}) addr);
230 interface PeripheralSide peripheral_side;
233 module mkPinTop(Ifc_PintTop);
234 // instantiate the pin-mux module here
235 Ifc_pinmux pinmux <-mkpinmux;
237 // declare the registers which will be used to mux the IOs
238 '''.format(p
.ADDR_WIDTH
, p
.DATA_WIDTH
))
240 cell_bit_width
= str(get_cell_bit_width(p
))
241 for cell
in p
.muxed_cells
:
243 Reg#(Bit#({0})) rg_muxio_{1} <-mkReg(0);'''.format(
244 cell_bit_width
, cell
[0]))
247 // rule to connect the registers to the selection lines of the
249 rule connect_selection_registers;''')
251 for cell
in p
.muxed_cells
:
253 pinmux.mux_lines.cell{0}_mux(rg_muxio_{0});'''.format(cell
[0]))
257 // method definitions for the write user interface
258 method ActionValue#(Bool) write(Bit#({2}) addr, Bit#({3}) data);
260 case (addr[{0}:{1}])'''.format(p
.upper_offset
, p
.lower_offset
,
261 p
.ADDR_WIDTH
, p
.DATA_WIDTH
))
263 for cell
in p
.muxed_cells
:
265 {0}: rg_muxio_{1}<=truncate(data);'''.format(index
, cell
[0]))
275 // method definitions for the read user interface
276 method Tuple2#(Bool,Bit#({3})) read(Bit#({2}) addr);
279 case (addr[{0}:{1}])'''.format(p
.upper_offset
, p
.lower_offset
,
280 p
.ADDR_WIDTH
, p
.DATA_WIDTH
))
282 for cell
in p
.muxed_cells
:
284 {0}: data=zeroExtend(rg_muxio_{1});'''.format(index
, cell
[0]))
290 return tuple2(err,data);
292 interface peripheral_side=pinmux.peripheral_side;
298 def write_bvp(bvp
, p
, ifaces
):
299 # ######## Generate bus transactors ################
300 with
open(bvp
, 'w') as bsv_file
:
301 bsv_file
.write(axi4_lite
.format(p
.ADDR_WIDTH
, p
.DATA_WIDTH
))
302 # ##################################################