de275874f6fb11c3287e974f36404db49f362340
[yosys.git] / passes / cmds / splitnets.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/register.h"
21 #include "kernel/celltypes.h"
22 #include "kernel/rtlil.h"
23 #include "kernel/log.h"
24
25 USING_YOSYS_NAMESPACE
26 PRIVATE_NAMESPACE_BEGIN
27
28 struct SplitnetsWorker
29 {
30 std::map<RTLIL::Wire*, std::vector<RTLIL::SigBit>> splitmap;
31
32 void append_wire(RTLIL::Module *module, RTLIL::Wire *wire, int offset, int width, std::string format)
33 {
34 std::string new_wire_name = wire->name.str();
35
36 if (format.size() > 0)
37 new_wire_name += format.substr(0, 1);
38
39 if (width > 1) {
40 if (wire->upto)
41 new_wire_name += stringf("%d", wire->start_offset+wire->width-(offset+width)-1);
42 else
43 new_wire_name += stringf("%d", wire->start_offset+offset+width-1);
44 if (format.size() > 2)
45 new_wire_name += format.substr(2, 1);
46 else
47 new_wire_name += ":";
48 }
49
50 if (wire->upto)
51 new_wire_name += stringf("%d", wire->start_offset+wire->width-offset-1);
52 else
53 new_wire_name += stringf("%d", wire->start_offset+offset);
54
55 if (format.size() > 1)
56 new_wire_name += format.substr(1, 1);
57
58 RTLIL::Wire *new_wire = module->addWire(module->uniquify(new_wire_name), width);
59 new_wire->port_id = wire->port_id ? wire->port_id + offset : 0;
60 new_wire->port_input = wire->port_input;
61 new_wire->port_output = wire->port_output;
62 new_wire->start_offset = wire->start_offset + offset;
63
64 if (wire->attributes.count(ID::src))
65 new_wire->attributes[ID::src] = wire->attributes.at(ID::src);
66
67 if (wire->attributes.count(ID::hdlname))
68 new_wire->attributes[ID::hdlname] = wire->attributes.at(ID::hdlname);
69
70 if (wire->attributes.count(ID::keep))
71 new_wire->attributes[ID::keep] = wire->attributes.at(ID::keep);
72
73 if (wire->attributes.count(ID::init)) {
74 Const old_init = wire->attributes.at(ID::init), new_init;
75 for (int i = offset; i < offset+width; i++)
76 new_init.bits.push_back(i < GetSize(old_init) ? old_init.bits.at(i) : State::Sx);
77 new_wire->attributes[ID::init] = new_init;
78 }
79
80 std::vector<RTLIL::SigBit> sigvec = RTLIL::SigSpec(new_wire).to_sigbit_vector();
81 splitmap[wire].insert(splitmap[wire].end(), sigvec.begin(), sigvec.end());
82 }
83
84 void operator()(RTLIL::SigSpec &sig)
85 {
86 for (auto &bit : sig)
87 if (splitmap.count(bit.wire) > 0)
88 bit = splitmap.at(bit.wire).at(bit.offset);
89 }
90 };
91
92 struct SplitnetsPass : public Pass {
93 SplitnetsPass() : Pass("splitnets", "split up multi-bit nets") { }
94 void help() YS_OVERRIDE
95 {
96 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
97 log("\n");
98 log(" splitnets [options] [selection]\n");
99 log("\n");
100 log("This command splits multi-bit nets into single-bit nets.\n");
101 log("\n");
102 log(" -format char1[char2[char3]]\n");
103 log(" the first char is inserted between the net name and the bit index, the\n");
104 log(" second char is appended to the netname. e.g. -format () creates net\n");
105 log(" names like 'mysignal(42)'. the 3rd character is the range separation\n");
106 log(" character when creating multi-bit wires. the default is '[]:'.\n");
107 log("\n");
108 log(" -ports\n");
109 log(" also split module ports. per default only internal signals are split.\n");
110 log("\n");
111 log(" -driver\n");
112 log(" don't blindly split nets in individual bits. instead look at the driver\n");
113 log(" and split nets so that no driver drives only part of a net.\n");
114 log("\n");
115 }
116 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
117 {
118 bool flag_ports = false;
119 bool flag_driver = false;
120 std::string format = "[]:";
121
122 log_header(design, "Executing SPLITNETS pass (splitting up multi-bit signals).\n");
123
124 size_t argidx;
125 for (argidx = 1; argidx < args.size(); argidx++)
126 {
127 if (args[argidx] == "-format" && argidx+1 < args.size()) {
128 format = args[++argidx];
129 continue;
130 }
131 if (args[argidx] == "-ports") {
132 flag_ports = true;
133 continue;
134 }
135 if (args[argidx] == "-driver") {
136 flag_driver = true;
137 continue;
138 }
139 break;
140 }
141 extra_args(args, argidx, design);
142
143 // module_ports_db[module_name][old_port_name] = new_port_name_list
144 dict<IdString, dict<IdString, vector<IdString>>> module_ports_db;
145
146 for (auto module : design->selected_modules())
147 {
148 if (module->has_processes_warn())
149 continue;
150
151 SplitnetsWorker worker;
152
153 if (flag_ports)
154 {
155 int normalized_port_factor = 0;
156
157 for (auto wire : module->wires())
158 if (wire->port_id != 0) {
159 normalized_port_factor = max(normalized_port_factor, wire->port_id+1);
160 normalized_port_factor = max(normalized_port_factor, GetSize(wire)+1);
161 }
162
163 for (auto wire : module->wires())
164 wire->port_id *= normalized_port_factor;
165 }
166
167 if (flag_driver)
168 {
169 CellTypes ct(design);
170
171 std::map<RTLIL::Wire*, std::set<int>> split_wires_at;
172
173 for (auto &c : module->cells_)
174 for (auto &p : c.second->connections())
175 {
176 if (!ct.cell_known(c.second->type))
177 continue;
178 if (!ct.cell_output(c.second->type, p.first))
179 continue;
180
181 RTLIL::SigSpec sig = p.second;
182 for (auto &chunk : sig.chunks()) {
183 if (chunk.wire == NULL)
184 continue;
185 if (chunk.wire->port_id == 0 || flag_ports) {
186 if (chunk.offset != 0)
187 split_wires_at[chunk.wire].insert(chunk.offset);
188 if (chunk.offset + chunk.width < chunk.wire->width)
189 split_wires_at[chunk.wire].insert(chunk.offset + chunk.width);
190 }
191 }
192 }
193
194 for (auto &it : split_wires_at) {
195 int cursor = 0;
196 for (int next_cursor : it.second) {
197 worker.append_wire(module, it.first, cursor, next_cursor - cursor, format);
198 cursor = next_cursor;
199 }
200 worker.append_wire(module, it.first, cursor, it.first->width - cursor, format);
201 }
202 }
203 else
204 {
205 for (auto &w : module->wires_) {
206 RTLIL::Wire *wire = w.second;
207 if (wire->width > 1 && (wire->port_id == 0 || flag_ports) && design->selected(module, w.second))
208 worker.splitmap[wire] = std::vector<RTLIL::SigBit>();
209 }
210
211 for (auto &it : worker.splitmap)
212 for (int i = 0; i < it.first->width; i++)
213 worker.append_wire(module, it.first, i, 1, format);
214 }
215
216 module->rewrite_sigspecs(worker);
217
218 if (flag_ports)
219 {
220 for (auto wire : module->wires())
221 {
222 if (wire->port_id == 0)
223 continue;
224
225 SigSpec sig(wire);
226 worker(sig);
227
228 if (sig == wire)
229 continue;
230
231 vector<IdString> &new_ports = module_ports_db[module->name][wire->name];
232
233 for (SigSpec c : sig.chunks())
234 new_ports.push_back(c.as_wire()->name);
235 }
236 }
237
238 pool<RTLIL::Wire*> delete_wires;
239 for (auto &it : worker.splitmap)
240 delete_wires.insert(it.first);
241 module->remove(delete_wires);
242
243 if (flag_ports)
244 module->fixup_ports();
245 }
246
247 if (!module_ports_db.empty())
248 {
249 for (auto module : design->modules())
250 for (auto cell : module->cells())
251 {
252 if (module_ports_db.count(cell->type) == 0)
253 continue;
254
255 for (auto &it : module_ports_db.at(cell->type))
256 {
257 IdString port_id = it.first;
258 const auto &new_port_ids = it.second;
259
260 if (!cell->hasPort(port_id))
261 continue;
262
263 int offset = 0;
264 SigSpec sig = cell->getPort(port_id);
265
266 for (auto nid : new_port_ids)
267 {
268 int nlen = GetSize(design->module(cell->type)->wire(nid));
269 if (offset + nlen > GetSize(sig))
270 nlen = GetSize(sig) - offset;
271 if (nlen > 0)
272 cell->setPort(nid, sig.extract(offset, nlen));
273 offset += nlen;
274 }
275
276 cell->unsetPort(port_id);
277 }
278 }
279 }
280 }
281 } SplitnetsPass;
282
283 PRIVATE_NAMESPACE_END