0de361f180501b82218acb0ec2ea9f1050995c67
[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
23 # project module imports
24 from interface_decl import *
25 from interface_def import *
26 from params import *
27 from wire_def import *
28 from actual_pinmux import *
29
30 if not os.path.exists("bsv_src"):
31 os.makedirs("bsv_src")
32
33 bsv_file = open("./bsv_src/pinmux.bsv", "w")
34
35
36 header = '''
37 /*
38 This BSV file has been generated by the PinMux tool available at:
39 https://bitbucket.org/casl/pinmux.
40
41 Authors: Neel Gala, Luke
42 Date of generation: ''' + time.strftime("%c") + '''
43 */
44 package pinmux;
45
46 typedef struct{
47 Bit#(1) outputval; // output from core to pad bit7
48 Bit#(1) output_en; // output enable from core to pad bit6
49 Bit#(1) input_en; // input enable from core to io_cell bit5
50 Bit#(1) pullup_en; // pullup enable from core to io_cell bit4
51 Bit#(1) pulldown_en; // pulldown enable from core to io_cell bit3
52 Bit#(1) drivestrength; // drivestrength from core to io_cell bit2
53 Bit#(1) pushpull_en; // pushpull enable from core to io_cell bit1
54 Bit#(1) opendrain_en; // opendrain enable form core to io_cell bit0
55 } GenericIOType deriving(Eq,Bits,FShow);
56
57 interface Ifc_pinmux;
58 '''
59 footer = '''
60 endmodule
61 endpackage
62 '''
63 # ============================================#
64 # ==== populating the file with the code =====#
65 # ============================================#
66
67 # package and interface declaration followed by the generic io_cell definition
68 bsv_file.write(header)
69
70 bsv_file.write('''
71
72 // declare the method which will capture the user pin-mux
73 // selection values.The width of the input is dependent on the number
74 // of muxes happening per IO. For now we have a generalized width
75 // where each IO will have the same number of muxes.''')
76
77 for cell in muxed_cells:
78 bsv_file.write(mux_interface.format(cell[0], len(cell) - 1))
79
80 bsv_file.write('''
81
82 // declare the interface to the IO cells.
83 // Each IO cell will have 8 input field (output from pin mux
84 // and on output field (input to pinmux)''')
85 for i in range(0, N_IO):
86 bsv_file.write('''\n // interface for IO CEll-{0}''')
87 bsv_file.write(io_interface.format(i))
88 # ==============================================================
89
90 # == create method definitions for all peripheral interfaces ==#
91 for i in range(0, N_UART):
92 bsv_file.write('''
93 // interface declaration between UART-{0} and pinmux'''.format(i))
94 bsv_file.write(uartinterface_decl.format(i))
95
96 for i in range(0, N_SPI):
97 bsv_file.write('''
98 // interface declaration between SPI-{0} and pinmux'''.format(i))
99 bsv_file.write(spiinterface_decl.format(i))
100
101 for i in range(0, N_TWI):
102 bsv_file.write('''
103 // interface declaration between TWI-{0} and pinmux'''.format(i))
104 bsv_file.write(twiinterface_decl.format(i))
105 # ==============================================================
106
107 # ===== finish interface definition and start module definition=======
108 bsv_file.write('''
109 endinterface
110 (*synthesize*)
111 module mkpinmux(Ifc_pinmux);
112 ''')
113 # ====================================================================
114
115 # ======================= create wire and registers =================#
116 bsv_file.write('''
117 // the followins wires capture the pin-mux selection
118 // values for each mux assigned to a CELL
119 ''')
120 for cell in muxed_cells:
121 bsv_file.write(muxwire.format(cell[0], len(cell) - 1))
122
123
124 bsv_file.write(
125 '''\n // following wires capture the values sent to the IO Cell''')
126 for i in range(0, N_IO):
127 bsv_file.write(generic_io.format(i))
128
129 for i in range(0, N_UART):
130 bsv_file.write(
131 '''\n // following wires capture signals to IO CELL if uart-{0} is
132 // allotted to it'''.format(i))
133 bsv_file.write(uartwires.format(i))
134
135 for i in range(0, N_SPI):
136 bsv_file.write(
137 '''\n // following wires capture signals to IO CELL if spi-{0} is
138 // allotted to it'''.format(i))
139 bsv_file.write(spiwires.format(i))
140
141 for i in range(0, N_TWI):
142 bsv_file.write(
143 '''\n // following wires capture signals to IO CELL if twi-{0} is
144 // allotted to it'''.format(i))
145 bsv_file.write(twiwires.format(i))
146 bsv_file.write("\n")
147 # ====================================================================
148 # ========================= Actual pinmuxing ========================#
149 bsv_file.write('''
150 /*====== This where the muxing starts for each io-cell======*/
151 ''')
152 bsv_file.write(pinmux)
153 bsv_file.write('''
154 /*============================================================*/
155 ''')
156 # ====================================================================
157 # ================= interface definitions for each method =============#
158 for cell in muxed_cells:
159 bsv_file.write(mux_interface_def.format(cell[0], len(cell) - 1))
160 for i in range(0, N_IO):
161 bsv_file.write(io_interface_def.format(i))
162 for i in range(0, N_UART):
163 bsv_file.write(uartinterface_def.format(i))
164 for i in range(0, N_SPI):
165 bsv_file.write(spiinterface_def.format(i))
166 for i in range(0, N_TWI):
167 bsv_file.write(twiinterface_def.format(i))
168 bsv_file.write(footer)
169 print("BSV file successfully generated: bsv_src/pinmux.bsv")
170 # ======================================================================
171 bsv_file.close()