Renamed extend() to extend_xx(), changed most users to extend_u0()
[yosys.git] / passes / techmap / simplemap.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/sigtools.h"
22 #include "kernel/log.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 USING_YOSYS_NAMESPACE
28 PRIVATE_NAMESPACE_BEGIN
29
30 static void simplemap_not(RTLIL::Module *module, RTLIL::Cell *cell)
31 {
32 RTLIL::SigSpec sig_a = cell->getPort("\\A");
33 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
34
35 sig_a.extend_u0(GetSize(sig_y), cell->parameters.at("\\A_SIGNED").as_bool());
36
37 for (int i = 0; i < GetSize(sig_y); i++) {
38 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
39 gate->setPort("\\A", sig_a[i]);
40 gate->setPort("\\Y", sig_y[i]);
41 }
42 }
43
44 static void simplemap_pos(RTLIL::Module *module, RTLIL::Cell *cell)
45 {
46 RTLIL::SigSpec sig_a = cell->getPort("\\A");
47 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
48
49 sig_a.extend_u0(GetSize(sig_y), cell->parameters.at("\\A_SIGNED").as_bool());
50
51 module->connect(RTLIL::SigSig(sig_y, sig_a));
52 }
53
54 static void simplemap_bitop(RTLIL::Module *module, RTLIL::Cell *cell)
55 {
56 RTLIL::SigSpec sig_a = cell->getPort("\\A");
57 RTLIL::SigSpec sig_b = cell->getPort("\\B");
58 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
59
60 sig_a.extend_u0(GetSize(sig_y), cell->parameters.at("\\A_SIGNED").as_bool());
61 sig_b.extend_u0(GetSize(sig_y), cell->parameters.at("\\B_SIGNED").as_bool());
62
63 if (cell->type == "$xnor")
64 {
65 RTLIL::SigSpec sig_t = module->addWire(NEW_ID, GetSize(sig_y));
66
67 for (int i = 0; i < GetSize(sig_y); i++) {
68 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
69 gate->setPort("\\A", sig_t[i]);
70 gate->setPort("\\Y", sig_y[i]);
71 }
72
73 sig_y = sig_t;
74 }
75
76 std::string gate_type;
77 if (cell->type == "$and") gate_type = "$_AND_";
78 if (cell->type == "$or") gate_type = "$_OR_";
79 if (cell->type == "$xor") gate_type = "$_XOR_";
80 if (cell->type == "$xnor") gate_type = "$_XOR_";
81 log_assert(!gate_type.empty());
82
83 for (int i = 0; i < GetSize(sig_y); i++) {
84 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
85 gate->setPort("\\A", sig_a[i]);
86 gate->setPort("\\B", sig_b[i]);
87 gate->setPort("\\Y", sig_y[i]);
88 }
89 }
90
91 static void simplemap_reduce(RTLIL::Module *module, RTLIL::Cell *cell)
92 {
93 RTLIL::SigSpec sig_a = cell->getPort("\\A");
94 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
95
96 if (sig_y.size() == 0)
97 return;
98
99 if (sig_a.size() == 0) {
100 if (cell->type == "$reduce_and") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(1, sig_y.size())));
101 if (cell->type == "$reduce_or") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(0, sig_y.size())));
102 if (cell->type == "$reduce_xor") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(0, sig_y.size())));
103 if (cell->type == "$reduce_xnor") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(1, sig_y.size())));
104 if (cell->type == "$reduce_bool") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(0, sig_y.size())));
105 return;
106 }
107
108 if (sig_y.size() > 1) {
109 module->connect(RTLIL::SigSig(sig_y.extract(1, sig_y.size()-1), RTLIL::SigSpec(0, sig_y.size()-1)));
110 sig_y = sig_y.extract(0, 1);
111 }
112
113 std::string gate_type;
114 if (cell->type == "$reduce_and") gate_type = "$_AND_";
115 if (cell->type == "$reduce_or") gate_type = "$_OR_";
116 if (cell->type == "$reduce_xor") gate_type = "$_XOR_";
117 if (cell->type == "$reduce_xnor") gate_type = "$_XOR_";
118 if (cell->type == "$reduce_bool") gate_type = "$_OR_";
119 log_assert(!gate_type.empty());
120
121 RTLIL::Cell *last_output_cell = NULL;
122
123 while (sig_a.size() > 1)
124 {
125 RTLIL::SigSpec sig_t = module->addWire(NEW_ID, sig_a.size() / 2);
126
127 for (int i = 0; i < sig_a.size(); i += 2)
128 {
129 if (i+1 == sig_a.size()) {
130 sig_t.append(sig_a[i]);
131 continue;
132 }
133
134 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
135 gate->setPort("\\A", sig_a[i]);
136 gate->setPort("\\B", sig_a[i+1]);
137 gate->setPort("\\Y", sig_t[i/2]);
138 last_output_cell = gate;
139 }
140
141 sig_a = sig_t;
142 }
143
144 if (cell->type == "$reduce_xnor") {
145 RTLIL::SigSpec sig_t = module->addWire(NEW_ID);
146 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
147 gate->setPort("\\A", sig_a);
148 gate->setPort("\\Y", sig_t);
149 last_output_cell = gate;
150 sig_a = sig_t;
151 }
152
153 if (last_output_cell == NULL) {
154 module->connect(RTLIL::SigSig(sig_y, sig_a));
155 } else {
156 last_output_cell->setPort("\\Y", sig_y);
157 }
158 }
159
160 static void logic_reduce(RTLIL::Module *module, RTLIL::SigSpec &sig)
161 {
162 while (sig.size() > 1)
163 {
164 RTLIL::SigSpec sig_t = module->addWire(NEW_ID, sig.size() / 2);
165
166 for (int i = 0; i < sig.size(); i += 2)
167 {
168 if (i+1 == sig.size()) {
169 sig_t.append(sig[i]);
170 continue;
171 }
172
173 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_OR_");
174 gate->setPort("\\A", sig[i]);
175 gate->setPort("\\B", sig[i+1]);
176 gate->setPort("\\Y", sig_t[i/2]);
177 }
178
179 sig = sig_t;
180 }
181
182 if (sig.size() == 0)
183 sig = RTLIL::SigSpec(0, 1);
184 }
185
186 static void simplemap_lognot(RTLIL::Module *module, RTLIL::Cell *cell)
187 {
188 RTLIL::SigSpec sig_a = cell->getPort("\\A");
189 logic_reduce(module, sig_a);
190
191 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
192
193 if (sig_y.size() == 0)
194 return;
195
196 if (sig_y.size() > 1) {
197 module->connect(RTLIL::SigSig(sig_y.extract(1, sig_y.size()-1), RTLIL::SigSpec(0, sig_y.size()-1)));
198 sig_y = sig_y.extract(0, 1);
199 }
200
201 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
202 gate->setPort("\\A", sig_a);
203 gate->setPort("\\Y", sig_y);
204 }
205
206 static void simplemap_logbin(RTLIL::Module *module, RTLIL::Cell *cell)
207 {
208 RTLIL::SigSpec sig_a = cell->getPort("\\A");
209 logic_reduce(module, sig_a);
210
211 RTLIL::SigSpec sig_b = cell->getPort("\\B");
212 logic_reduce(module, sig_b);
213
214 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
215
216 if (sig_y.size() == 0)
217 return;
218
219 if (sig_y.size() > 1) {
220 module->connect(RTLIL::SigSig(sig_y.extract(1, sig_y.size()-1), RTLIL::SigSpec(0, sig_y.size()-1)));
221 sig_y = sig_y.extract(0, 1);
222 }
223
224 std::string gate_type;
225 if (cell->type == "$logic_and") gate_type = "$_AND_";
226 if (cell->type == "$logic_or") gate_type = "$_OR_";
227 log_assert(!gate_type.empty());
228
229 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
230 gate->setPort("\\A", sig_a);
231 gate->setPort("\\B", sig_b);
232 gate->setPort("\\Y", sig_y);
233 }
234
235 static void simplemap_mux(RTLIL::Module *module, RTLIL::Cell *cell)
236 {
237 RTLIL::SigSpec sig_a = cell->getPort("\\A");
238 RTLIL::SigSpec sig_b = cell->getPort("\\B");
239 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
240
241 for (int i = 0; i < GetSize(sig_y); i++) {
242 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_MUX_");
243 gate->setPort("\\A", sig_a[i]);
244 gate->setPort("\\B", sig_b[i]);
245 gate->setPort("\\S", cell->getPort("\\S"));
246 gate->setPort("\\Y", sig_y[i]);
247 }
248 }
249
250 static void simplemap_slice(RTLIL::Module *module, RTLIL::Cell *cell)
251 {
252 int offset = cell->parameters.at("\\OFFSET").as_int();
253 RTLIL::SigSpec sig_a = cell->getPort("\\A");
254 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
255 module->connect(RTLIL::SigSig(sig_y, sig_a.extract(offset, sig_y.size())));
256 }
257
258 static void simplemap_concat(RTLIL::Module *module, RTLIL::Cell *cell)
259 {
260 RTLIL::SigSpec sig_ab = cell->getPort("\\A");
261 sig_ab.append(cell->getPort("\\B"));
262 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
263 module->connect(RTLIL::SigSig(sig_y, sig_ab));
264 }
265
266 static void simplemap_sr(RTLIL::Module *module, RTLIL::Cell *cell)
267 {
268 int width = cell->parameters.at("\\WIDTH").as_int();
269 char set_pol = cell->parameters.at("\\SET_POLARITY").as_bool() ? 'P' : 'N';
270 char clr_pol = cell->parameters.at("\\CLR_POLARITY").as_bool() ? 'P' : 'N';
271
272 RTLIL::SigSpec sig_s = cell->getPort("\\SET");
273 RTLIL::SigSpec sig_r = cell->getPort("\\CLR");
274 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
275
276 std::string gate_type = stringf("$_SR_%c%c_", set_pol, clr_pol);
277
278 for (int i = 0; i < width; i++) {
279 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
280 gate->setPort("\\S", sig_s[i]);
281 gate->setPort("\\R", sig_r[i]);
282 gate->setPort("\\Q", sig_q[i]);
283 }
284 }
285
286 static void simplemap_dff(RTLIL::Module *module, RTLIL::Cell *cell)
287 {
288 int width = cell->parameters.at("\\WIDTH").as_int();
289 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
290
291 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
292 RTLIL::SigSpec sig_d = cell->getPort("\\D");
293 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
294
295 std::string gate_type = stringf("$_DFF_%c_", clk_pol);
296
297 for (int i = 0; i < width; i++) {
298 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
299 gate->setPort("\\C", sig_clk);
300 gate->setPort("\\D", sig_d[i]);
301 gate->setPort("\\Q", sig_q[i]);
302 }
303 }
304
305 static void simplemap_dffe(RTLIL::Module *module, RTLIL::Cell *cell)
306 {
307 int width = cell->parameters.at("\\WIDTH").as_int();
308 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
309 char en_pol = cell->parameters.at("\\EN_POLARITY").as_bool() ? 'P' : 'N';
310
311 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
312 RTLIL::SigSpec sig_en = cell->getPort("\\EN");
313 RTLIL::SigSpec sig_d = cell->getPort("\\D");
314 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
315
316 std::string gate_type = stringf("$_DFFE_%c%c_", clk_pol, en_pol);
317
318 for (int i = 0; i < width; i++) {
319 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
320 gate->setPort("\\C", sig_clk);
321 gate->setPort("\\E", sig_en);
322 gate->setPort("\\D", sig_d[i]);
323 gate->setPort("\\Q", sig_q[i]);
324 }
325 }
326
327 static void simplemap_dffsr(RTLIL::Module *module, RTLIL::Cell *cell)
328 {
329 int width = cell->parameters.at("\\WIDTH").as_int();
330 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
331 char set_pol = cell->parameters.at("\\SET_POLARITY").as_bool() ? 'P' : 'N';
332 char clr_pol = cell->parameters.at("\\CLR_POLARITY").as_bool() ? 'P' : 'N';
333
334 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
335 RTLIL::SigSpec sig_s = cell->getPort("\\SET");
336 RTLIL::SigSpec sig_r = cell->getPort("\\CLR");
337 RTLIL::SigSpec sig_d = cell->getPort("\\D");
338 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
339
340 std::string gate_type = stringf("$_DFFSR_%c%c%c_", clk_pol, set_pol, clr_pol);
341
342 for (int i = 0; i < width; i++) {
343 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
344 gate->setPort("\\C", sig_clk);
345 gate->setPort("\\S", sig_s[i]);
346 gate->setPort("\\R", sig_r[i]);
347 gate->setPort("\\D", sig_d[i]);
348 gate->setPort("\\Q", sig_q[i]);
349 }
350 }
351
352 static void simplemap_adff(RTLIL::Module *module, RTLIL::Cell *cell)
353 {
354 int width = cell->parameters.at("\\WIDTH").as_int();
355 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
356 char rst_pol = cell->parameters.at("\\ARST_POLARITY").as_bool() ? 'P' : 'N';
357
358 std::vector<RTLIL::State> rst_val = cell->parameters.at("\\ARST_VALUE").bits;
359 while (int(rst_val.size()) < width)
360 rst_val.push_back(RTLIL::State::S0);
361
362 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
363 RTLIL::SigSpec sig_rst = cell->getPort("\\ARST");
364 RTLIL::SigSpec sig_d = cell->getPort("\\D");
365 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
366
367 std::string gate_type_0 = stringf("$_DFF_%c%c0_", clk_pol, rst_pol);
368 std::string gate_type_1 = stringf("$_DFF_%c%c1_", clk_pol, rst_pol);
369
370 for (int i = 0; i < width; i++) {
371 RTLIL::Cell *gate = module->addCell(NEW_ID, rst_val.at(i) == RTLIL::State::S1 ? gate_type_1 : gate_type_0);
372 gate->setPort("\\C", sig_clk);
373 gate->setPort("\\R", sig_rst);
374 gate->setPort("\\D", sig_d[i]);
375 gate->setPort("\\Q", sig_q[i]);
376 }
377 }
378
379 static void simplemap_dlatch(RTLIL::Module *module, RTLIL::Cell *cell)
380 {
381 int width = cell->parameters.at("\\WIDTH").as_int();
382 char en_pol = cell->parameters.at("\\EN_POLARITY").as_bool() ? 'P' : 'N';
383
384 RTLIL::SigSpec sig_en = cell->getPort("\\EN");
385 RTLIL::SigSpec sig_d = cell->getPort("\\D");
386 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
387
388 std::string gate_type = stringf("$_DLATCH_%c_", en_pol);
389
390 for (int i = 0; i < width; i++) {
391 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
392 gate->setPort("\\E", sig_en);
393 gate->setPort("\\D", sig_d[i]);
394 gate->setPort("\\Q", sig_q[i]);
395 }
396 }
397
398 PRIVATE_NAMESPACE_END
399 YOSYS_NAMESPACE_BEGIN
400
401 extern void simplemap_get_mappers(std::map<RTLIL::IdString, void(*)(RTLIL::Module*, RTLIL::Cell*)> &mappers);
402
403 void simplemap_get_mappers(std::map<RTLIL::IdString, void(*)(RTLIL::Module*, RTLIL::Cell*)> &mappers)
404 {
405 mappers["$not"] = simplemap_not;
406 mappers["$pos"] = simplemap_pos;
407 mappers["$and"] = simplemap_bitop;
408 mappers["$or"] = simplemap_bitop;
409 mappers["$xor"] = simplemap_bitop;
410 mappers["$xnor"] = simplemap_bitop;
411 mappers["$reduce_and"] = simplemap_reduce;
412 mappers["$reduce_or"] = simplemap_reduce;
413 mappers["$reduce_xor"] = simplemap_reduce;
414 mappers["$reduce_xnor"] = simplemap_reduce;
415 mappers["$reduce_bool"] = simplemap_reduce;
416 mappers["$logic_not"] = simplemap_lognot;
417 mappers["$logic_and"] = simplemap_logbin;
418 mappers["$logic_or"] = simplemap_logbin;
419 mappers["$mux"] = simplemap_mux;
420 mappers["$slice"] = simplemap_slice;
421 mappers["$concat"] = simplemap_concat;
422 mappers["$sr"] = simplemap_sr;
423 mappers["$dff"] = simplemap_dff;
424 mappers["$dffe"] = simplemap_dffe;
425 mappers["$dffsr"] = simplemap_dffsr;
426 mappers["$adff"] = simplemap_adff;
427 mappers["$dlatch"] = simplemap_dlatch;
428 }
429
430 YOSYS_NAMESPACE_END
431 PRIVATE_NAMESPACE_BEGIN
432
433 struct SimplemapPass : public Pass {
434 SimplemapPass() : Pass("simplemap", "mapping simple coarse-grain cells") { }
435 virtual void help()
436 {
437 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
438 log("\n");
439 log(" simplemap [selection]\n");
440 log("\n");
441 log("This pass maps a small selection of simple coarse-grain cells to yosys gate\n");
442 log("primitives. The following internal cell types are mapped by this pass:\n");
443 log("\n");
444 log(" $not, $pos, $and, $or, $xor, $xnor\n");
445 log(" $reduce_and, $reduce_or, $reduce_xor, $reduce_xnor, $reduce_bool\n");
446 log(" $logic_not, $logic_and, $logic_or, $mux\n");
447 log(" $sr, $dff, $dffsr, $adff, $dlatch\n");
448 log("\n");
449 }
450 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
451 {
452 log_header("Executing SIMPLEMAP pass (map simple cells to gate primitives).\n");
453 extra_args(args, 1, design);
454
455 std::map<RTLIL::IdString, void(*)(RTLIL::Module*, RTLIL::Cell*)> mappers;
456 simplemap_get_mappers(mappers);
457
458 for (auto mod : design->modules()) {
459 if (!design->selected(mod))
460 continue;
461 std::vector<RTLIL::Cell*> cells = mod->cells();
462 for (auto cell : cells) {
463 if (mappers.count(cell->type) == 0)
464 continue;
465 if (!design->selected(mod, cell))
466 continue;
467 log("Mapping %s.%s (%s).\n", log_id(mod), log_id(cell), log_id(cell->type));
468 mappers.at(cell->type)(mod, cell);
469 mod->remove(cell);
470 }
471 }
472 }
473 } SimplemapPass;
474
475 PRIVATE_NAMESPACE_END