Improved support for $sop cells
[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 "simplemap.h"
21 #include "kernel/sigtools.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 USING_YOSYS_NAMESPACE
27 YOSYS_NAMESPACE_BEGIN
28
29 void simplemap_not(RTLIL::Module *module, RTLIL::Cell *cell)
30 {
31 RTLIL::SigSpec sig_a = cell->getPort("\\A");
32 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
33
34 sig_a.extend_u0(GetSize(sig_y), cell->parameters.at("\\A_SIGNED").as_bool());
35
36 for (int i = 0; i < GetSize(sig_y); i++) {
37 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
38 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
39 gate->setPort("\\A", sig_a[i]);
40 gate->setPort("\\Y", sig_y[i]);
41 }
42 }
43
44 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 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->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
70 gate->setPort("\\A", sig_t[i]);
71 gate->setPort("\\Y", sig_y[i]);
72 }
73
74 sig_y = sig_t;
75 }
76
77 std::string gate_type;
78 if (cell->type == "$and") gate_type = "$_AND_";
79 if (cell->type == "$or") gate_type = "$_OR_";
80 if (cell->type == "$xor") gate_type = "$_XOR_";
81 if (cell->type == "$xnor") gate_type = "$_XOR_";
82 log_assert(!gate_type.empty());
83
84 for (int i = 0; i < GetSize(sig_y); i++) {
85 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
86 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
87 gate->setPort("\\A", sig_a[i]);
88 gate->setPort("\\B", sig_b[i]);
89 gate->setPort("\\Y", sig_y[i]);
90 }
91 }
92
93 void simplemap_reduce(RTLIL::Module *module, RTLIL::Cell *cell)
94 {
95 RTLIL::SigSpec sig_a = cell->getPort("\\A");
96 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
97
98 if (sig_y.size() == 0)
99 return;
100
101 if (sig_a.size() == 0) {
102 if (cell->type == "$reduce_and") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(1, sig_y.size())));
103 if (cell->type == "$reduce_or") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(0, sig_y.size())));
104 if (cell->type == "$reduce_xor") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(0, sig_y.size())));
105 if (cell->type == "$reduce_xnor") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(1, sig_y.size())));
106 if (cell->type == "$reduce_bool") module->connect(RTLIL::SigSig(sig_y, RTLIL::SigSpec(0, sig_y.size())));
107 return;
108 }
109
110 if (sig_y.size() > 1) {
111 module->connect(RTLIL::SigSig(sig_y.extract(1, sig_y.size()-1), RTLIL::SigSpec(0, sig_y.size()-1)));
112 sig_y = sig_y.extract(0, 1);
113 }
114
115 std::string gate_type;
116 if (cell->type == "$reduce_and") gate_type = "$_AND_";
117 if (cell->type == "$reduce_or") gate_type = "$_OR_";
118 if (cell->type == "$reduce_xor") gate_type = "$_XOR_";
119 if (cell->type == "$reduce_xnor") gate_type = "$_XOR_";
120 if (cell->type == "$reduce_bool") gate_type = "$_OR_";
121 log_assert(!gate_type.empty());
122
123 RTLIL::Cell *last_output_cell = NULL;
124
125 while (sig_a.size() > 1)
126 {
127 RTLIL::SigSpec sig_t = module->addWire(NEW_ID, sig_a.size() / 2);
128
129 for (int i = 0; i < sig_a.size(); i += 2)
130 {
131 if (i+1 == sig_a.size()) {
132 sig_t.append(sig_a[i]);
133 continue;
134 }
135
136 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
137 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
138 gate->setPort("\\A", sig_a[i]);
139 gate->setPort("\\B", sig_a[i+1]);
140 gate->setPort("\\Y", sig_t[i/2]);
141 last_output_cell = gate;
142 }
143
144 sig_a = sig_t;
145 }
146
147 if (cell->type == "$reduce_xnor") {
148 RTLIL::SigSpec sig_t = module->addWire(NEW_ID);
149 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
150 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
151 gate->setPort("\\A", sig_a);
152 gate->setPort("\\Y", sig_t);
153 last_output_cell = gate;
154 sig_a = sig_t;
155 }
156
157 if (last_output_cell == NULL) {
158 module->connect(RTLIL::SigSig(sig_y, sig_a));
159 } else {
160 last_output_cell->setPort("\\Y", sig_y);
161 }
162 }
163
164 static void logic_reduce(RTLIL::Module *module, RTLIL::SigSpec &sig, RTLIL::Cell *cell)
165 {
166 while (sig.size() > 1)
167 {
168 RTLIL::SigSpec sig_t = module->addWire(NEW_ID, sig.size() / 2);
169
170 for (int i = 0; i < sig.size(); i += 2)
171 {
172 if (i+1 == sig.size()) {
173 sig_t.append(sig[i]);
174 continue;
175 }
176
177 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_OR_");
178 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
179 gate->setPort("\\A", sig[i]);
180 gate->setPort("\\B", sig[i+1]);
181 gate->setPort("\\Y", sig_t[i/2]);
182 }
183
184 sig = sig_t;
185 }
186
187 if (sig.size() == 0)
188 sig = RTLIL::SigSpec(0, 1);
189 }
190
191 void simplemap_lognot(RTLIL::Module *module, RTLIL::Cell *cell)
192 {
193 RTLIL::SigSpec sig_a = cell->getPort("\\A");
194 logic_reduce(module, sig_a, cell);
195
196 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
197
198 if (sig_y.size() == 0)
199 return;
200
201 if (sig_y.size() > 1) {
202 module->connect(RTLIL::SigSig(sig_y.extract(1, sig_y.size()-1), RTLIL::SigSpec(0, sig_y.size()-1)));
203 sig_y = sig_y.extract(0, 1);
204 }
205
206 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_NOT_");
207 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
208 gate->setPort("\\A", sig_a);
209 gate->setPort("\\Y", sig_y);
210 }
211
212 void simplemap_logbin(RTLIL::Module *module, RTLIL::Cell *cell)
213 {
214 RTLIL::SigSpec sig_a = cell->getPort("\\A");
215 logic_reduce(module, sig_a, cell);
216
217 RTLIL::SigSpec sig_b = cell->getPort("\\B");
218 logic_reduce(module, sig_b, cell);
219
220 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
221
222 if (sig_y.size() == 0)
223 return;
224
225 if (sig_y.size() > 1) {
226 module->connect(RTLIL::SigSig(sig_y.extract(1, sig_y.size()-1), RTLIL::SigSpec(0, sig_y.size()-1)));
227 sig_y = sig_y.extract(0, 1);
228 }
229
230 std::string gate_type;
231 if (cell->type == "$logic_and") gate_type = "$_AND_";
232 if (cell->type == "$logic_or") gate_type = "$_OR_";
233 log_assert(!gate_type.empty());
234
235 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
236 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
237 gate->setPort("\\A", sig_a);
238 gate->setPort("\\B", sig_b);
239 gate->setPort("\\Y", sig_y);
240 }
241
242 void simplemap_eqne(RTLIL::Module *module, RTLIL::Cell *cell)
243 {
244 RTLIL::SigSpec sig_a = cell->getPort("\\A");
245 RTLIL::SigSpec sig_b = cell->getPort("\\B");
246 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
247 bool is_signed = cell->parameters.at("\\A_SIGNED").as_bool();
248 bool is_ne = cell->type == "$ne" || cell->type == "$nex";
249
250 RTLIL::SigSpec xor_out = module->addWire(NEW_ID, max(GetSize(sig_a), GetSize(sig_b)));
251 RTLIL::Cell *xor_cell = module->addXor(NEW_ID, sig_a, sig_b, xor_out, is_signed);
252 xor_cell->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
253 simplemap_bitop(module, xor_cell);
254 module->remove(xor_cell);
255
256 RTLIL::SigSpec reduce_out = is_ne ? sig_y : module->addWire(NEW_ID);
257 RTLIL::Cell *reduce_cell = module->addReduceOr(NEW_ID, xor_out, reduce_out);
258 reduce_cell->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
259 simplemap_reduce(module, reduce_cell);
260 module->remove(reduce_cell);
261
262 if (!is_ne) {
263 RTLIL::Cell *not_cell = module->addLogicNot(NEW_ID, reduce_out, sig_y);
264 not_cell->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
265 simplemap_lognot(module, not_cell);
266 module->remove(not_cell);
267 }
268 }
269
270 void simplemap_mux(RTLIL::Module *module, RTLIL::Cell *cell)
271 {
272 RTLIL::SigSpec sig_a = cell->getPort("\\A");
273 RTLIL::SigSpec sig_b = cell->getPort("\\B");
274 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
275
276 for (int i = 0; i < GetSize(sig_y); i++) {
277 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_MUX_");
278 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
279 gate->setPort("\\A", sig_a[i]);
280 gate->setPort("\\B", sig_b[i]);
281 gate->setPort("\\S", cell->getPort("\\S"));
282 gate->setPort("\\Y", sig_y[i]);
283 }
284 }
285
286 void simplemap_tribuf(RTLIL::Module *module, RTLIL::Cell *cell)
287 {
288 RTLIL::SigSpec sig_a = cell->getPort("\\A");
289 RTLIL::SigSpec sig_e = cell->getPort("\\EN");
290 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
291
292 for (int i = 0; i < GetSize(sig_y); i++) {
293 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_TBUF_");
294 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
295 gate->setPort("\\A", sig_a[i]);
296 gate->setPort("\\E", sig_e);
297 gate->setPort("\\Y", sig_y[i]);
298 }
299 }
300
301 void simplemap_lut(RTLIL::Module *module, RTLIL::Cell *cell)
302 {
303 SigSpec lut_ctrl = cell->getPort("\\A");
304 SigSpec lut_data = cell->getParam("\\LUT");
305 lut_data.extend_u0(1 << cell->getParam("\\WIDTH").as_int());
306
307 for (int idx = 0; GetSize(lut_data) > 1; idx++) {
308 SigSpec sig_s = lut_ctrl[idx];
309 SigSpec new_lut_data = module->addWire(NEW_ID, GetSize(lut_data)/2);
310 for (int i = 0; i < GetSize(lut_data); i += 2) {
311 RTLIL::Cell *gate = module->addCell(NEW_ID, "$_MUX_");
312 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
313 gate->setPort("\\A", lut_data[i]);
314 gate->setPort("\\B", lut_data[i+1]);
315 gate->setPort("\\S", lut_ctrl[idx]);
316 gate->setPort("\\Y", new_lut_data[i/2]);
317 }
318 lut_data = new_lut_data;
319 }
320
321 module->connect(cell->getPort("\\Y"), lut_data);
322 }
323
324 void simplemap_sop(RTLIL::Module *module, RTLIL::Cell *cell)
325 {
326 SigSpec ctrl = cell->getPort("\\A");
327 SigSpec table = cell->getParam("\\TABLE");
328
329 int width = cell->getParam("\\WIDTH").as_int();
330 int depth = cell->getParam("\\DEPTH").as_int();
331 table.extend_u0(2 * width * depth);
332
333 SigSpec products;
334
335 for (int i = 0; i < depth; i++) {
336 SigSpec in, pat;
337 for (int j = 0; j < width; j++) {
338 if (table[2*i*width + 2*j + 0] == State::S1) {
339 in.append(ctrl[j]);
340 pat.append(State::S0);
341 }
342 if (table[2*i*width + 2*j + 1] == State::S1) {
343 in.append(ctrl[j]);
344 pat.append(State::S1);
345 }
346 }
347
348 products.append(GetSize(in) > 0 ? module->Eq(NEW_ID, in, pat) : State::S1);
349 }
350
351 module->connect(cell->getPort("\\Y"), module->ReduceOr(NEW_ID, products));
352 }
353
354 void simplemap_slice(RTLIL::Module *module, RTLIL::Cell *cell)
355 {
356 int offset = cell->parameters.at("\\OFFSET").as_int();
357 RTLIL::SigSpec sig_a = cell->getPort("\\A");
358 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
359 module->connect(RTLIL::SigSig(sig_y, sig_a.extract(offset, sig_y.size())));
360 }
361
362 void simplemap_concat(RTLIL::Module *module, RTLIL::Cell *cell)
363 {
364 RTLIL::SigSpec sig_ab = cell->getPort("\\A");
365 sig_ab.append(cell->getPort("\\B"));
366 RTLIL::SigSpec sig_y = cell->getPort("\\Y");
367 module->connect(RTLIL::SigSig(sig_y, sig_ab));
368 }
369
370 void simplemap_sr(RTLIL::Module *module, RTLIL::Cell *cell)
371 {
372 int width = cell->parameters.at("\\WIDTH").as_int();
373 char set_pol = cell->parameters.at("\\SET_POLARITY").as_bool() ? 'P' : 'N';
374 char clr_pol = cell->parameters.at("\\CLR_POLARITY").as_bool() ? 'P' : 'N';
375
376 RTLIL::SigSpec sig_s = cell->getPort("\\SET");
377 RTLIL::SigSpec sig_r = cell->getPort("\\CLR");
378 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
379
380 std::string gate_type = stringf("$_SR_%c%c_", set_pol, clr_pol);
381
382 for (int i = 0; i < width; i++) {
383 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
384 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
385 gate->setPort("\\S", sig_s[i]);
386 gate->setPort("\\R", sig_r[i]);
387 gate->setPort("\\Q", sig_q[i]);
388 }
389 }
390
391 void simplemap_dff(RTLIL::Module *module, RTLIL::Cell *cell)
392 {
393 int width = cell->parameters.at("\\WIDTH").as_int();
394 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
395
396 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
397 RTLIL::SigSpec sig_d = cell->getPort("\\D");
398 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
399
400 std::string gate_type = stringf("$_DFF_%c_", clk_pol);
401
402 for (int i = 0; i < width; i++) {
403 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
404 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
405 gate->setPort("\\C", sig_clk);
406 gate->setPort("\\D", sig_d[i]);
407 gate->setPort("\\Q", sig_q[i]);
408 }
409 }
410
411 void simplemap_dffe(RTLIL::Module *module, RTLIL::Cell *cell)
412 {
413 int width = cell->parameters.at("\\WIDTH").as_int();
414 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
415 char en_pol = cell->parameters.at("\\EN_POLARITY").as_bool() ? 'P' : 'N';
416
417 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
418 RTLIL::SigSpec sig_en = cell->getPort("\\EN");
419 RTLIL::SigSpec sig_d = cell->getPort("\\D");
420 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
421
422 std::string gate_type = stringf("$_DFFE_%c%c_", clk_pol, en_pol);
423
424 for (int i = 0; i < width; i++) {
425 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
426 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
427 gate->setPort("\\C", sig_clk);
428 gate->setPort("\\E", sig_en);
429 gate->setPort("\\D", sig_d[i]);
430 gate->setPort("\\Q", sig_q[i]);
431 }
432 }
433
434 void simplemap_dffsr(RTLIL::Module *module, RTLIL::Cell *cell)
435 {
436 int width = cell->parameters.at("\\WIDTH").as_int();
437 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
438 char set_pol = cell->parameters.at("\\SET_POLARITY").as_bool() ? 'P' : 'N';
439 char clr_pol = cell->parameters.at("\\CLR_POLARITY").as_bool() ? 'P' : 'N';
440
441 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
442 RTLIL::SigSpec sig_s = cell->getPort("\\SET");
443 RTLIL::SigSpec sig_r = cell->getPort("\\CLR");
444 RTLIL::SigSpec sig_d = cell->getPort("\\D");
445 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
446
447 std::string gate_type = stringf("$_DFFSR_%c%c%c_", clk_pol, set_pol, clr_pol);
448
449 for (int i = 0; i < width; i++) {
450 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
451 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
452 gate->setPort("\\C", sig_clk);
453 gate->setPort("\\S", sig_s[i]);
454 gate->setPort("\\R", sig_r[i]);
455 gate->setPort("\\D", sig_d[i]);
456 gate->setPort("\\Q", sig_q[i]);
457 }
458 }
459
460 void simplemap_adff(RTLIL::Module *module, RTLIL::Cell *cell)
461 {
462 int width = cell->parameters.at("\\WIDTH").as_int();
463 char clk_pol = cell->parameters.at("\\CLK_POLARITY").as_bool() ? 'P' : 'N';
464 char rst_pol = cell->parameters.at("\\ARST_POLARITY").as_bool() ? 'P' : 'N';
465
466 std::vector<RTLIL::State> rst_val = cell->parameters.at("\\ARST_VALUE").bits;
467 while (int(rst_val.size()) < width)
468 rst_val.push_back(RTLIL::State::S0);
469
470 RTLIL::SigSpec sig_clk = cell->getPort("\\CLK");
471 RTLIL::SigSpec sig_rst = cell->getPort("\\ARST");
472 RTLIL::SigSpec sig_d = cell->getPort("\\D");
473 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
474
475 std::string gate_type_0 = stringf("$_DFF_%c%c0_", clk_pol, rst_pol);
476 std::string gate_type_1 = stringf("$_DFF_%c%c1_", clk_pol, rst_pol);
477
478 for (int i = 0; i < width; i++) {
479 RTLIL::Cell *gate = module->addCell(NEW_ID, rst_val.at(i) == RTLIL::State::S1 ? gate_type_1 : gate_type_0);
480 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
481 gate->setPort("\\C", sig_clk);
482 gate->setPort("\\R", sig_rst);
483 gate->setPort("\\D", sig_d[i]);
484 gate->setPort("\\Q", sig_q[i]);
485 }
486 }
487
488 void simplemap_dlatch(RTLIL::Module *module, RTLIL::Cell *cell)
489 {
490 int width = cell->parameters.at("\\WIDTH").as_int();
491 char en_pol = cell->parameters.at("\\EN_POLARITY").as_bool() ? 'P' : 'N';
492
493 RTLIL::SigSpec sig_en = cell->getPort("\\EN");
494 RTLIL::SigSpec sig_d = cell->getPort("\\D");
495 RTLIL::SigSpec sig_q = cell->getPort("\\Q");
496
497 std::string gate_type = stringf("$_DLATCH_%c_", en_pol);
498
499 for (int i = 0; i < width; i++) {
500 RTLIL::Cell *gate = module->addCell(NEW_ID, gate_type);
501 gate->add_strpool_attribute("\\src", cell->get_strpool_attribute("\\src"));
502 gate->setPort("\\E", sig_en);
503 gate->setPort("\\D", sig_d[i]);
504 gate->setPort("\\Q", sig_q[i]);
505 }
506 }
507
508 void simplemap_get_mappers(std::map<RTLIL::IdString, void(*)(RTLIL::Module*, RTLIL::Cell*)> &mappers)
509 {
510 mappers["$not"] = simplemap_not;
511 mappers["$pos"] = simplemap_pos;
512 mappers["$and"] = simplemap_bitop;
513 mappers["$or"] = simplemap_bitop;
514 mappers["$xor"] = simplemap_bitop;
515 mappers["$xnor"] = simplemap_bitop;
516 mappers["$reduce_and"] = simplemap_reduce;
517 mappers["$reduce_or"] = simplemap_reduce;
518 mappers["$reduce_xor"] = simplemap_reduce;
519 mappers["$reduce_xnor"] = simplemap_reduce;
520 mappers["$reduce_bool"] = simplemap_reduce;
521 mappers["$logic_not"] = simplemap_lognot;
522 mappers["$logic_and"] = simplemap_logbin;
523 mappers["$logic_or"] = simplemap_logbin;
524 mappers["$eq"] = simplemap_eqne;
525 mappers["$eqx"] = simplemap_eqne;
526 mappers["$ne"] = simplemap_eqne;
527 mappers["$nex"] = simplemap_eqne;
528 mappers["$mux"] = simplemap_mux;
529 mappers["$tribuf"] = simplemap_tribuf;
530 mappers["$lut"] = simplemap_lut;
531 mappers["$sop"] = simplemap_sop;
532 mappers["$slice"] = simplemap_slice;
533 mappers["$concat"] = simplemap_concat;
534 mappers["$sr"] = simplemap_sr;
535 mappers["$dff"] = simplemap_dff;
536 mappers["$dffe"] = simplemap_dffe;
537 mappers["$dffsr"] = simplemap_dffsr;
538 mappers["$adff"] = simplemap_adff;
539 mappers["$dlatch"] = simplemap_dlatch;
540 }
541
542 void simplemap(RTLIL::Module *module, RTLIL::Cell *cell)
543 {
544 static std::map<RTLIL::IdString, void(*)(RTLIL::Module*, RTLIL::Cell*)> mappers;
545 static bool initialized_mappers = false;
546
547 if (!initialized_mappers) {
548 simplemap_get_mappers(mappers);
549 initialized_mappers = true;
550 }
551
552 mappers.at(cell->type)(module, cell);
553 }
554
555 YOSYS_NAMESPACE_END
556 PRIVATE_NAMESPACE_BEGIN
557
558 struct SimplemapPass : public Pass {
559 SimplemapPass() : Pass("simplemap", "mapping simple coarse-grain cells") { }
560 virtual void help()
561 {
562 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
563 log("\n");
564 log(" simplemap [selection]\n");
565 log("\n");
566 log("This pass maps a small selection of simple coarse-grain cells to yosys gate\n");
567 log("primitives. The following internal cell types are mapped by this pass:\n");
568 log("\n");
569 log(" $not, $pos, $and, $or, $xor, $xnor\n");
570 log(" $reduce_and, $reduce_or, $reduce_xor, $reduce_xnor, $reduce_bool\n");
571 log(" $logic_not, $logic_and, $logic_or, $mux, $tribuf\n");
572 log(" $sr, $dff, $dffsr, $adff, $dlatch\n");
573 log("\n");
574 }
575 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
576 {
577 log_header(design, "Executing SIMPLEMAP pass (map simple cells to gate primitives).\n");
578 extra_args(args, 1, design);
579
580 std::map<RTLIL::IdString, void(*)(RTLIL::Module*, RTLIL::Cell*)> mappers;
581 simplemap_get_mappers(mappers);
582
583 for (auto mod : design->modules()) {
584 if (!design->selected(mod))
585 continue;
586 std::vector<RTLIL::Cell*> cells = mod->cells();
587 for (auto cell : cells) {
588 if (mappers.count(cell->type) == 0)
589 continue;
590 if (!design->selected(mod, cell))
591 continue;
592 log("Mapping %s.%s (%s).\n", log_id(mod), log_id(cell), log_id(cell->type));
593 mappers.at(cell->type)(mod, cell);
594 mod->remove(cell);
595 }
596 }
597 }
598 } SimplemapPass;
599
600 PRIVATE_NAMESPACE_END