8b25d9ecc769f7c83960b3662a7f80158eb23814
[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 Ifc_pinmux;
59 '''
60 footer = '''
61 endmodule
62 endpackage
63 '''
64 # ============================================#
65 # ==== populating the file with the code =====#
66 # ============================================#
67
68 # package and interface declaration followed by the generic io_cell definition
69 bsv_file.write(header)
70
71 bsv_file.write('''
72
73 // declare the method which will capture the user pin-mux
74 // selection values.The width of the input is dependent on the number
75 // of muxes happening per IO. For now we have a generalized width
76 // where each IO will have the same number of muxes.''')
77
78 for cell in muxed_cells:
79 bsv_file.write(mux_interface.format(cell[0],
80 int(math.log(len(cell) - 1, 2))))
81
82 bsv_file.write('''
83
84 // declare the interface to the IO cells.
85 // Each IO cell will have 8 input field (output from pin mux
86 // and on output field (input to pinmux)''')
87 for i in range(0, N_IO):
88 bsv_file.write('''\n // interface for IO CEll-{0}''')
89 bsv_file.write(io_interface.format(i))
90 # ==============================================================
91
92 # == create method definitions for all peripheral interfaces ==#
93 for i in range(0, N_UART):
94 bsv_file.write('''
95 // interface declaration between UART-{0} and pinmux'''.format(i))
96 bsv_file.write(uartinterface_decl.format(i))
97
98 for i in range(0, N_SPI):
99 bsv_file.write('''
100 // interface declaration between SPI-{0} and pinmux'''.format(i))
101 bsv_file.write(spiinterface_decl.format(i))
102
103 for i in range(0, N_TWI):
104 bsv_file.write('''
105 // interface declaration between TWI-{0} and pinmux'''.format(i))
106 bsv_file.write(twiinterface_decl.format(i))
107 # ==============================================================
108
109 # ===== finish interface definition and start module definition=======
110 bsv_file.write('''
111 endinterface
112 (*synthesize*)
113 module mkpinmux(Ifc_pinmux);
114 ''')
115 # ====================================================================
116
117 # ======================= create wire and registers =================#
118 bsv_file.write('''
119 // the followins wires capture the pin-mux selection
120 // values for each mux assigned to a CELL
121 ''')
122 for cell in muxed_cells:
123 bsv_file.write(muxwire.format(cell[0], int(math.log(len(cell) - 1, 2))))
124
125
126 bsv_file.write(
127 '''\n // following wires capture the values sent to the IO Cell''')
128 for i in range(0, N_IO):
129 bsv_file.write(generic_io.format(i))
130
131 for i in range(0, N_UART):
132 bsv_file.write(
133 '''\n // following wires capture signals to IO CELL if uart-{0} is
134 // allotted to it'''.format(i))
135 bsv_file.write(uartwires.format(i))
136
137 for i in range(0, N_SPI):
138 bsv_file.write(
139 '''\n // following wires capture signals to IO CELL if spi-{0} is
140 // allotted to it'''.format(i))
141 bsv_file.write(spiwires.format(i))
142
143 for i in range(0, N_TWI):
144 bsv_file.write(
145 '''\n // following wires capture signals to IO CELL if twi-{0} is
146 // allotted to it'''.format(i))
147 bsv_file.write(twiwires.format(i))
148 bsv_file.write("\n")
149 # ====================================================================
150 # ========================= Actual pinmuxing ========================#
151 bsv_file.write('''
152 /*====== This where the muxing starts for each io-cell======*/
153 ''')
154 bsv_file.write(pinmux)
155 bsv_file.write('''
156 /*============================================================*/
157 ''')
158 # ====================================================================
159 # ================= interface definitions for each method =============#
160 for cell in muxed_cells:
161 bsv_file.write(mux_interface_def.format(cell[0],
162 int(math.log(len(cell) - 1, 2))))
163 for i in range(0, N_IO):
164 bsv_file.write(io_interface_def.format(i))
165 for i in range(0, N_UART):
166 bsv_file.write(uartinterface_def.format(i))
167 for i in range(0, N_SPI):
168 bsv_file.write(spiinterface_def.format(i))
169 for i in range(0, N_TWI):
170 bsv_file.write(twiinterface_def.format(i))
171 bsv_file.write(footer)
172 print("BSV file successfully generated: bsv_src/pinmux.bsv")
173 # ======================================================================
174 bsv_file.close()