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/log.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/consteval.h"
24 #include "kernel/celltypes.h"
28 PRIVATE_NAMESPACE_BEGIN
30 static RTLIL::Module
*module
;
31 static SigMap assign_map
;
32 typedef std::pair
<RTLIL::Cell
*, RTLIL::IdString
> sig2driver_entry_t
;
33 static SigSet
<sig2driver_entry_t
> sig2driver
, sig2user
;
34 static std::set
<RTLIL::Cell
*> muxtree_cells
;
35 static SigPool sig_at_port
;
37 static bool check_state_mux_tree(RTLIL::SigSpec old_sig
, RTLIL::SigSpec sig
, SigPool
&recursion_monitor
)
39 if (sig_at_port
.check_any(assign_map(sig
)))
42 if (sig
.is_fully_const() || old_sig
== sig
)
45 if (recursion_monitor
.check_any(sig
)) {
46 log_warning("logic loop in mux tree at signal %s in module %s.\n",
47 log_signal(sig
), RTLIL::id2cstr(module
->name
));
51 recursion_monitor
.add(sig
);
53 std::set
<sig2driver_entry_t
> cellport_list
;
54 sig2driver
.find(sig
, cellport_list
);
55 for (auto &cellport
: cellport_list
) {
56 if ((cellport
.first
->type
!= "$mux" && cellport
.first
->type
!= "$pmux") || cellport
.second
!= "\\Y")
58 RTLIL::SigSpec sig_a
= assign_map(cellport
.first
->getPort("\\A"));
59 RTLIL::SigSpec sig_b
= assign_map(cellport
.first
->getPort("\\B"));
60 if (!check_state_mux_tree(old_sig
, sig_a
, recursion_monitor
))
62 for (int i
= 0; i
< sig_b
.size(); i
+= sig_a
.size())
63 if (!check_state_mux_tree(old_sig
, sig_b
.extract(i
, sig_a
.size()), recursion_monitor
))
65 muxtree_cells
.insert(cellport
.first
);
68 recursion_monitor
.del(sig
);
73 static bool check_state_users(RTLIL::SigSpec sig
)
75 if (sig_at_port
.check_any(assign_map(sig
)))
78 std::set
<sig2driver_entry_t
> cellport_list
;
79 sig2user
.find(sig
, cellport_list
);
80 for (auto &cellport
: cellport_list
) {
81 RTLIL::Cell
*cell
= cellport
.first
;
82 if (muxtree_cells
.count(cell
) > 0)
84 if (cellport
.second
!= "\\A" && cellport
.second
!= "\\B")
86 if (!cell
->hasPort("\\A") || !cell
->hasPort("\\B") || !cell
->hasPort("\\Y"))
88 for (auto &port_it
: cell
->connections())
89 if (port_it
.first
!= "\\A" && port_it
.first
!= "\\B" && port_it
.first
!= "\\Y")
91 if (assign_map(cell
->getPort("\\A")) == sig
&& cell
->getPort("\\B").is_fully_const())
93 if (assign_map(cell
->getPort("\\B")) == sig
&& cell
->getPort("\\A").is_fully_const())
101 static void detect_fsm(RTLIL::Wire
*wire
)
103 if (wire
->attributes
.count("\\fsm_encoding") > 0 || wire
->width
<= 1)
105 if (sig_at_port
.check_any(assign_map(RTLIL::SigSpec(wire
))))
108 std::set
<sig2driver_entry_t
> cellport_list
;
109 sig2driver
.find(RTLIL::SigSpec(wire
), cellport_list
);
110 for (auto &cellport
: cellport_list
) {
111 if ((cellport
.first
->type
!= "$dff" && cellport
.first
->type
!= "$adff") || cellport
.second
!= "\\Q")
113 muxtree_cells
.clear();
114 SigPool recursion_monitor
;
115 RTLIL::SigSpec sig_q
= assign_map(cellport
.first
->getPort("\\Q"));
116 RTLIL::SigSpec sig_d
= assign_map(cellport
.first
->getPort("\\D"));
117 if (sig_q
== RTLIL::SigSpec(wire
) && check_state_mux_tree(sig_q
, sig_d
, recursion_monitor
) && check_state_users(sig_q
)) {
118 log("Found FSM state register %s in module %s.\n", wire
->name
.c_str(), module
->name
.c_str());
119 wire
->attributes
["\\fsm_encoding"] = RTLIL::Const("auto");
125 struct FsmDetectPass
: public Pass
{
126 FsmDetectPass() : Pass("fsm_detect", "finding FSMs in design") { }
129 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
131 log(" fsm_detect [selection]\n");
133 log("This pass detects finite state machines by identifying the state signal.\n");
134 log("The state signal is then marked by setting the attribute 'fsm_encoding'\n");
135 log("on the state signal to \"auto\".\n");
137 log("Existing 'fsm_encoding' attributes are not changed by this pass.\n");
139 log("Signals can be protected from being detected by this pass by setting the\n");
140 log("'fsm_encoding' attribute to \"none\".\n");
143 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
145 log_header("Executing FSM_DETECT pass (finding FSMs in design).\n");
146 extra_args(args
, 1, design
);
149 ct
.setup_internals();
150 ct
.setup_internals_mem();
152 ct
.setup_stdcells_mem();
154 for (auto &mod_it
: design
->modules_
)
156 if (!design
->selected(mod_it
.second
))
159 module
= mod_it
.second
;
160 assign_map
.set(module
);
165 for (auto &cell_it
: module
->cells_
)
166 for (auto &conn_it
: cell_it
.second
->connections()) {
167 if (ct
.cell_output(cell_it
.second
->type
, conn_it
.first
) || !ct
.cell_known(cell_it
.second
->type
)) {
168 RTLIL::SigSpec sig
= conn_it
.second
;
169 assign_map
.apply(sig
);
170 sig2driver
.insert(sig
, sig2driver_entry_t(cell_it
.second
, conn_it
.first
));
172 if (!ct
.cell_known(cell_it
.second
->type
) || ct
.cell_input(cell_it
.second
->type
, conn_it
.first
)) {
173 RTLIL::SigSpec sig
= conn_it
.second
;
174 assign_map
.apply(sig
);
175 sig2user
.insert(sig
, sig2driver_entry_t(cell_it
.second
, conn_it
.first
));
179 for (auto &wire_it
: module
->wires_
)
180 if (wire_it
.second
->port_id
!= 0)
181 sig_at_port
.add(assign_map(RTLIL::SigSpec(wire_it
.second
)));
183 for (auto &wire_it
: module
->wires_
)
184 if (design
->selected(module
, wire_it
.second
))
185 detect_fsm(wire_it
.second
);
191 muxtree_cells
.clear();
195 PRIVATE_NAMESPACE_END