2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
21 #include "kernel/sigtools.h"
24 PRIVATE_NAMESPACE_BEGIN
28 virtual ~ShregmapTech() { }
29 virtual bool analyze(vector
<int> &taps
) = 0;
30 virtual bool fixup(Cell
*cell
, dict
<int, SigBit
> &taps
) = 0;
33 struct ShregmapOptions
36 int keep_before
, keep_after
;
37 bool zinit
, init
, params
, ffe
;
38 dict
<IdString
, pair
<IdString
, IdString
>> ffcells
;
55 struct ShregmapTechGreenpak4
: ShregmapTech
57 bool analyze(vector
<int> &taps
)
59 if (GetSize(taps
) > 2 && taps
[0] == 0 && taps
[2] < 17) {
64 if (GetSize(taps
) > 2)
67 if (taps
.back() > 16) return false;
72 bool fixup(Cell
*cell
, dict
<int, SigBit
> &taps
)
74 auto D
= cell
->getPort(ID(D
));
75 auto C
= cell
->getPort(ID(C
));
77 auto newcell
= cell
->module
->addCell(NEW_ID
, ID(GP_SHREG
));
78 newcell
->setPort(ID(nRST
), State::S1
);
79 newcell
->setPort(ID(CLK
), C
);
80 newcell
->setPort(ID(IN
), D
);
83 for (auto tap
: taps
) {
84 newcell
->setPort(i
? ID(OUTB
) : ID(OUTA
), tap
.second
);
85 newcell
->setParam(i
? ID(OUTB_TAP
) : ID(OUTA_TAP
), tap
.first
+ 1);
89 cell
->setParam(ID(OUTA_INVERT
), 0);
99 const ShregmapOptions
&opts
;
100 int dff_count
, shreg_count
;
102 pool
<Cell
*> remove_cells
;
103 pool
<SigBit
> remove_init
;
105 dict
<SigBit
, bool> sigbit_init
;
106 dict
<SigBit
, Cell
*> sigbit_chain_next
;
107 dict
<SigBit
, Cell
*> sigbit_chain_prev
;
108 pool
<SigBit
> sigbit_with_non_chain_users
;
109 pool
<Cell
*> chain_start_cells
;
111 void make_sigbit_chain_next_prev()
113 for (auto wire
: module
->wires())
115 if (wire
->port_output
|| wire
->get_bool_attribute(ID::keep
)) {
116 for (auto bit
: sigmap(wire
))
117 sigbit_with_non_chain_users
.insert(bit
);
120 if (wire
->attributes
.count(ID(init
))) {
121 SigSpec initsig
= sigmap(wire
);
122 Const initval
= wire
->attributes
.at(ID(init
));
123 for (int i
= 0; i
< GetSize(initsig
) && i
< GetSize(initval
); i
++)
124 if (initval
[i
] == State::S0
&& !opts
.zinit
)
125 sigbit_init
[initsig
[i
]] = false;
126 else if (initval
[i
] == State::S1
)
127 sigbit_init
[initsig
[i
]] = true;
131 for (auto cell
: module
->cells())
133 if (opts
.ffcells
.count(cell
->type
) && !cell
->get_bool_attribute(ID::keep
))
135 IdString d_port
= opts
.ffcells
.at(cell
->type
).first
;
136 IdString q_port
= opts
.ffcells
.at(cell
->type
).second
;
138 SigBit d_bit
= sigmap(cell
->getPort(d_port
).as_bit());
139 SigBit q_bit
= sigmap(cell
->getPort(q_port
).as_bit());
141 if (opts
.init
|| sigbit_init
.count(q_bit
) == 0)
143 auto r
= sigbit_chain_next
.insert(std::make_pair(d_bit
, cell
));
145 // Insertion not successful means that d_bit is already
146 // connected to another register, thus mark it as a
147 // non chain user ...
148 sigbit_with_non_chain_users
.insert(d_bit
);
149 // ... and clone d_bit into another wire, and use that
150 // wire as a different key in the d_bit-to-cell dictionary
151 // so that it can be identified as another chain
152 // (omitting this common flop)
153 // Link: https://github.com/YosysHQ/yosys/pull/1085
154 // NB: This relies on us not updating sigmap with this
155 // alias otherwise it would think they are the same
157 Wire
*wire
= module
->addWire(NEW_ID
);
158 module
->connect(wire
, d_bit
);
159 sigbit_chain_next
.insert(std::make_pair(wire
, cell
));
162 sigbit_chain_prev
[q_bit
] = cell
;
167 for (auto conn
: cell
->connections())
168 if (cell
->input(conn
.first
))
169 for (auto bit
: sigmap(conn
.second
))
170 sigbit_with_non_chain_users
.insert(bit
);
174 void find_chain_start_cells()
176 for (auto it
: sigbit_chain_next
)
178 if (opts
.tech
== nullptr && sigbit_with_non_chain_users
.count(it
.first
))
181 if (sigbit_chain_prev
.count(it
.first
) != 0)
183 Cell
*c1
= sigbit_chain_prev
.at(it
.first
);
184 Cell
*c2
= it
.second
;
186 if (c1
->type
!= c2
->type
)
189 if (c1
->parameters
!= c2
->parameters
)
192 IdString d_port
= opts
.ffcells
.at(c1
->type
).first
;
193 IdString q_port
= opts
.ffcells
.at(c1
->type
).second
;
195 auto c1_conn
= c1
->connections();
196 auto c2_conn
= c1
->connections();
198 c1_conn
.erase(d_port
);
199 c1_conn
.erase(q_port
);
201 c2_conn
.erase(d_port
);
202 c2_conn
.erase(q_port
);
204 if (c1_conn
!= c2_conn
)
211 chain_start_cells
.insert(it
.second
);
215 vector
<Cell
*> create_chain(Cell
*start_cell
)
219 Cell
*c
= start_cell
;
224 IdString q_port
= opts
.ffcells
.at(c
->type
).second
;
225 SigBit q_bit
= sigmap(c
->getPort(q_port
).as_bit());
227 if (sigbit_chain_next
.count(q_bit
) == 0)
230 c
= sigbit_chain_next
.at(q_bit
);
231 if (chain_start_cells
.count(c
) != 0)
238 void process_chain(vector
<Cell
*> &chain
)
240 if (GetSize(chain
) < opts
.keep_before
+ opts
.minlen
+ opts
.keep_after
)
243 int cursor
= opts
.keep_before
;
244 while (cursor
< GetSize(chain
) - opts
.keep_after
)
246 int depth
= GetSize(chain
) - opts
.keep_after
- cursor
;
249 depth
= std::min(opts
.maxlen
, depth
);
251 Cell
*first_cell
= chain
[cursor
];
252 IdString q_port
= opts
.ffcells
.at(first_cell
->type
).second
;
253 dict
<int, SigBit
> taps_dict
;
257 vector
<SigBit
> qbits
;
260 for (int i
= 0; i
< depth
; i
++)
262 Cell
*cell
= chain
[cursor
+i
];
263 auto qbit
= sigmap(cell
->getPort(q_port
));
264 qbits
.push_back(qbit
);
266 if (sigbit_with_non_chain_users
.count(qbit
))
272 if (taps
.empty() || taps
.back() < depth
-1)
273 taps
.push_back(depth
-1);
275 if (opts
.tech
->analyze(taps
))
283 for (auto tap
: taps
) {
284 taps_dict
[tap
] = qbits
.at(tap
);
285 log_assert(depth
< tap
+1);
295 Cell
*last_cell
= chain
[cursor
+depth
-1];
297 log("Converting %s.%s ... %s.%s to a shift register with depth %d.\n",
298 log_id(module
), log_id(first_cell
), log_id(module
), log_id(last_cell
), depth
);
303 string shreg_cell_type_str
= "$__SHREG";
305 shreg_cell_type_str
+= "_";
307 if (first_cell
->type
[1] != '_')
308 shreg_cell_type_str
+= "_";
309 shreg_cell_type_str
+= first_cell
->type
.substr(1);
313 vector
<State
> initval
;
314 for (int i
= depth
-1; i
>= 0; i
--) {
315 SigBit bit
= sigmap(chain
[cursor
+i
]->getPort(q_port
).as_bit());
316 if (sigbit_init
.count(bit
) == 0)
317 initval
.push_back(State::Sx
);
318 else if (sigbit_init
.at(bit
))
319 initval
.push_back(State::S1
);
321 initval
.push_back(State::S0
);
322 remove_init
.insert(bit
);
324 first_cell
->setParam(ID(INIT
), initval
);
328 for (int i
= depth
-1; i
>= 0; i
--) {
329 SigBit bit
= sigmap(chain
[cursor
+i
]->getPort(q_port
).as_bit());
330 remove_init
.insert(bit
);
335 int param_clkpol
= -1;
338 if (first_cell
->type
== ID($_DFF_N_
)) param_clkpol
= 0;
339 if (first_cell
->type
== ID($_DFF_P_
)) param_clkpol
= 1;
341 if (first_cell
->type
== ID($_DFFE_NN_
)) param_clkpol
= 0, param_enpol
= 0;
342 if (first_cell
->type
== ID($_DFFE_NP_
)) param_clkpol
= 0, param_enpol
= 1;
343 if (first_cell
->type
== ID($_DFFE_PN_
)) param_clkpol
= 1, param_enpol
= 0;
344 if (first_cell
->type
== ID($_DFFE_PP_
)) param_clkpol
= 1, param_enpol
= 1;
346 log_assert(param_clkpol
>= 0);
347 first_cell
->setParam(ID(CLKPOL
), param_clkpol
);
348 if (opts
.ffe
) first_cell
->setParam(ID(ENPOL
), param_enpol
);
351 first_cell
->type
= shreg_cell_type_str
;
352 first_cell
->setPort(q_port
, last_cell
->getPort(q_port
));
353 first_cell
->setParam(ID(DEPTH
), depth
);
355 if (opts
.tech
!= nullptr && !opts
.tech
->fixup(first_cell
, taps_dict
))
356 remove_cells
.insert(first_cell
);
358 for (int i
= 1; i
< depth
; i
++)
359 remove_cells
.insert(chain
[cursor
+i
]);
366 for (auto cell
: remove_cells
)
367 module
->remove(cell
);
369 for (auto wire
: module
->wires())
371 if (wire
->attributes
.count(ID(init
)) == 0)
374 SigSpec initsig
= sigmap(wire
);
375 Const
&initval
= wire
->attributes
.at(ID(init
));
377 for (int i
= 0; i
< GetSize(initsig
) && i
< GetSize(initval
); i
++)
378 if (remove_init
.count(initsig
[i
]))
379 initval
[i
] = State::Sx
;
381 if (SigSpec(initval
).is_fully_undef())
382 wire
->attributes
.erase(ID(init
));
385 remove_cells
.clear();
386 sigbit_chain_next
.clear();
387 sigbit_chain_prev
.clear();
388 chain_start_cells
.clear();
391 ShregmapWorker(Module
*module
, const ShregmapOptions
&opts
) :
392 module(module
), sigmap(module
), opts(opts
), dff_count(0), shreg_count(0)
394 make_sigbit_chain_next_prev();
395 find_chain_start_cells();
397 for (auto c
: chain_start_cells
) {
398 vector
<Cell
*> chain
= create_chain(c
);
399 process_chain(chain
);
406 struct ShregmapPass
: public Pass
{
407 ShregmapPass() : Pass("shregmap", "map shift registers") { }
408 void help() YS_OVERRIDE
410 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
412 log(" shregmap [options] [selection]\n");
414 log("This pass converts chains of $_DFF_[NP]_ gates to target specific shift register\n");
415 log("primitives. The generated shift register will be of type $__SHREG_DFF_[NP]_ and\n");
416 log("will use the same interface as the original $_DFF_*_ cells. The cell parameter\n");
417 log("'DEPTH' will contain the depth of the shift register. Use a target-specific\n");
418 log("'techmap' map file to convert those cells to the actual target cells.\n");
421 log(" minimum length of shift register (default = 2)\n");
422 log(" (this is the length after -keep_before and -keep_after)\n");
425 log(" maximum length of shift register (default = no limit)\n");
426 log(" larger chains will be mapped to multiple shift register instances\n");
428 log(" -keep_before N\n");
429 log(" number of DFFs to keep before the shift register (default = 0)\n");
431 log(" -keep_after N\n");
432 log(" number of DFFs to keep after the shift register (default = 0)\n");
434 log(" -clkpol pos|neg|any\n");
435 log(" limit match to only positive or negative edge clocks. (default = any)\n");
437 log(" -enpol pos|neg|none|any_or_none|any\n");
438 log(" limit match to FFs with the specified enable polarity. (default = none)\n");
440 log(" -match <cell_type>[:<d_port_name>:<q_port_name>]\n");
441 log(" match the specified cells instead of $_DFF_N_ and $_DFF_P_. If\n");
442 log(" ':<d_port_name>:<q_port_name>' is omitted then 'D' and 'Q' is used\n");
443 log(" by default. E.g. the option '-clkpol pos' is just an alias for\n");
444 log(" '-match $_DFF_P_', which is an alias for '-match $_DFF_P_:D:Q'.\n");
447 log(" instead of encoding the clock and enable polarity in the cell name by\n");
448 log(" deriving from the original cell name, simply name all generated cells\n");
449 log(" $__SHREG_ and use CLKPOL and ENPOL parameters. An ENPOL value of 2 is\n");
450 log(" used to denote cells without enable input. The ENPOL parameter is\n");
451 log(" omitted when '-enpol none' (or no -enpol option) is passed.\n");
454 log(" assume the shift register is automatically zero-initialized, so it\n");
455 log(" becomes legal to merge zero initialized FFs into the shift register.\n");
458 log(" map initialized registers to the shift reg, add an INIT parameter to\n");
459 log(" generated cells with the initialization value. (first bit to shift out\n");
460 log(" in LSB position)\n");
462 log(" -tech greenpak4\n");
463 log(" map to greenpak4 shift registers.\n");
466 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
468 ShregmapOptions opts
;
469 string clkpol
, enpol
;
471 log_header(design
, "Executing SHREGMAP pass (map shift registers).\n");
474 for (argidx
= 1; argidx
< args
.size(); argidx
++)
476 if (args
[argidx
] == "-clkpol" && argidx
+1 < args
.size()) {
477 clkpol
= args
[++argidx
];
480 if (args
[argidx
] == "-enpol" && argidx
+1 < args
.size()) {
481 enpol
= args
[++argidx
];
484 if (args
[argidx
] == "-match" && argidx
+1 < args
.size()) {
485 vector
<string
> match_args
= split_tokens(args
[++argidx
], ":");
486 if (GetSize(match_args
) < 2)
487 match_args
.push_back("D");
488 if (GetSize(match_args
) < 3)
489 match_args
.push_back("Q");
490 IdString
id_cell_type(RTLIL::escape_id(match_args
[0]));
491 IdString
id_d_port_name(RTLIL::escape_id(match_args
[1]));
492 IdString
id_q_port_name(RTLIL::escape_id(match_args
[2]));
493 opts
.ffcells
[id_cell_type
] = make_pair(id_d_port_name
, id_q_port_name
);
496 if (args
[argidx
] == "-minlen" && argidx
+1 < args
.size()) {
497 opts
.minlen
= atoi(args
[++argidx
].c_str());
500 if (args
[argidx
] == "-maxlen" && argidx
+1 < args
.size()) {
501 opts
.maxlen
= atoi(args
[++argidx
].c_str());
504 if (args
[argidx
] == "-keep_before" && argidx
+1 < args
.size()) {
505 opts
.keep_before
= atoi(args
[++argidx
].c_str());
508 if (args
[argidx
] == "-keep_after" && argidx
+1 < args
.size()) {
509 opts
.keep_after
= atoi(args
[++argidx
].c_str());
512 if (args
[argidx
] == "-tech" && argidx
+1 < args
.size() && opts
.tech
== nullptr) {
513 string tech
= args
[++argidx
];
514 if (tech
== "greenpak4") {
517 opts
.tech
= new ShregmapTechGreenpak4
;
524 if (args
[argidx
] == "-zinit") {
528 if (args
[argidx
] == "-init") {
532 if (args
[argidx
] == "-params") {
538 extra_args(args
, argidx
, design
);
540 if (opts
.zinit
&& opts
.init
)
541 log_cmd_error("Options -zinit and -init are exclusive!\n");
543 if (opts
.ffcells
.empty())
545 bool clk_pos
= clkpol
== "" || clkpol
== "pos" || clkpol
== "any";
546 bool clk_neg
= clkpol
== "" || clkpol
== "neg" || clkpol
== "any";
548 bool en_none
= enpol
== "" || enpol
== "none" || enpol
== "any_or_none";
549 bool en_pos
= enpol
== "pos" || enpol
== "any" || enpol
== "any_or_none";
550 bool en_neg
= enpol
== "neg" || enpol
== "any" || enpol
== "any_or_none";
552 if (clk_pos
&& en_none
)
553 opts
.ffcells
[ID($_DFF_P_
)] = make_pair(IdString(ID(D
)), IdString(ID(Q
)));
554 if (clk_neg
&& en_none
)
555 opts
.ffcells
[ID($_DFF_N_
)] = make_pair(IdString(ID(D
)), IdString(ID(Q
)));
557 if (clk_pos
&& en_pos
)
558 opts
.ffcells
[ID($_DFFE_PP_
)] = make_pair(IdString(ID(D
)), IdString(ID(Q
)));
559 if (clk_pos
&& en_neg
)
560 opts
.ffcells
[ID($_DFFE_PN_
)] = make_pair(IdString(ID(D
)), IdString(ID(Q
)));
562 if (clk_neg
&& en_pos
)
563 opts
.ffcells
[ID($_DFFE_NP_
)] = make_pair(IdString(ID(D
)), IdString(ID(Q
)));
564 if (clk_neg
&& en_neg
)
565 opts
.ffcells
[ID($_DFFE_NN_
)] = make_pair(IdString(ID(D
)), IdString(ID(Q
)));
567 if (en_pos
|| en_neg
)
573 log_cmd_error("Options -clkpol and -match are exclusive!\n");
575 log_cmd_error("Options -enpol and -match are exclusive!\n");
577 log_cmd_error("Options -params and -match are exclusive!\n");
583 for (auto module
: design
->selected_modules()) {
584 ShregmapWorker
worker(module
, opts
);
585 dff_count
+= worker
.dff_count
;
586 shreg_count
+= worker
.shreg_count
;
589 log("Converted %d dff cells into %d shift registers.\n", dff_count
, shreg_count
);
591 if (opts
.tech
!= nullptr) {
598 PRIVATE_NAMESPACE_END