10 #include <unordered_map>
11 #include <unordered_set>
17 std::vector<const Net *> retval;
27 if (n) retval.push_back(n);
33 retval.push_back(mp->get_net());
41 return gate_pin_group->get_name();
42 return module_pin_group->get_name();
47 return (mSimulationSet.find(g) != mSimulationSet.end());
52 mSimulationSet.insert(gates.begin(), gates.end());
55 compute_output_nets();
56 compute_partial_nets();
61 return mSimulationSet;
66 for (
const Clock& c: m_clocks)
67 if (c.clock_net == n)
return true;
73 m_clocks.push_back(clk);
78 mSimulationSet.clear();
81 m_output_nets.clear();
82 m_partial_nets.clear();
88 return has_gates() && (!m_clocks.empty() || mNoClockUsed) && !m_input_nets.empty();
93 return !mSimulationSet.empty();
103 return m_output_nets;
108 return m_partial_nets;
113 return (m_input_nets.find(n) != m_input_nets.end());
129 if (!filename.empty())
130 if (!(of = fopen(filename.c_str(),
"w")))
132 log_warning(
"SimulationInput",
"cannot open simulation input dump file '{}'", filename);
136 fprintf(of,
"Gates:______________________________________\n");
137 for (
const Gate* g: mSimulationSet)
139 fprintf(of,
" %4d <%s>\n", g->get_id(), g->get_name().c_str());
141 fprintf(of,
"Clocks:_____________________________________\n");
142 for (
const Clock& clk: m_clocks)
144 fprintf(of,
" %4d <%s> \t period: %u \n", clk.clock_net->get_id(), clk.clock_net->get_name().c_str(), (
unsigned int) clk.period());
146 fprintf(of,
"Input nets:_________________________________\n");
147 for (
const Net* n: m_input_nets)
149 fprintf(of,
" %4d <%s>\n", n->get_id(), n->get_name().c_str());
151 fprintf(of,
"Output nets:________________________________\n");
152 for (
const Net* n: m_output_nets)
154 fprintf(of,
" %4d <%s>\n", n->get_id(), n->get_name().c_str());
156 fprintf(of,
"Partial netlist nets:________________________________\n");
157 for (
const Net* n: m_partial_nets)
159 fprintf(of,
" %4d <%s>\n", n->get_id(), n->get_name().c_str());
161 if (filename.empty())
167 void SimulationInput::compute_input_nets()
169 m_input_nets.clear();
170 for (
auto gate : mSimulationSet)
172 for (
auto net : gate->get_fan_in_nets())
175 if (
net->is_global_input_net())
177 m_input_nets.insert(
net);
181 int number_sources = 0;
182 for (
auto src :
net->get_sources())
187 m_input_nets.insert(
net);
192 m_input_nets.insert(
net);
198 void SimulationInput::compute_output_nets()
200 m_output_nets.clear();
201 for (
auto gate : mSimulationSet)
203 for (
auto net : gate->get_fan_out_nets())
206 if (
net->is_global_output_net())
208 m_output_nets.push_back(
net);
212 int number_destinations = 0;
213 for (
auto dst :
net->get_destinations())
215 ++number_destinations;
218 m_output_nets.push_back(
net);
222 if (!number_destinations)
223 m_output_nets.push_back(
net);
229 void SimulationInput::compute_partial_nets()
231 m_partial_nets.clear();
232 std::unordered_set<const Net*> found;
233 for (
const Gate* g : mSimulationSet)
235 for (
const Net* n : g->get_fan_in_nets())
237 if (found.find(n) == found.end())
240 m_partial_nets.push_back(n);
243 for (
const Net* n : g->get_fan_out_nets())
245 if (found.find(n) == found.end())
248 m_partial_nets.push_back(n);
257 struct NetGroupCandidate
259 SimulationInput::NetGroup group;
260 std::vector<const Net*> nets;
262 std::string sort_name;
268 SimulationInput::DirectionType group_direction(
const std::vector<const Net*>& nets,
const std::unordered_map<const Net*, SimulationInput::DirectionType>& simulated_nets)
271 for (
const Net* n : nets)
284 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c) { return std::tolower(c); });
294 std::unordered_map<const Net*, DirectionType> simulated_nets;
296 for (
const Net* n : m_partial_nets) simulated_nets[n] =
Undefined;
297 for (
const Net* n : m_input_nets) simulated_nets[n] =
InputOnly;
298 for (
const Net* n : m_output_nets) simulated_nets[n] =
OutputOnly;
301 std::unordered_set<const Module*> simulated_modules;
302 for (
const Gate* g : mSimulationSet)
304 const Module *m = g->get_module();
307 simulated_modules.insert(m);
312 std::vector<NetGroupCandidate> candidates;
314 for (
const Module* m : simulated_modules)
318 if (pg->size() < 2 || pg->size() > 31)
continue;
319 bool pin_group_simulated =
true;
320 bool is_source =
true;
321 std::vector<const Net*> nets;
327 const Net* n = mp->get_net();
329 if (simulated_nets.find(n) == simulated_nets.end())
332 pin_group_simulated =
false;
337 if (!pin_group_simulated || nets.empty())
continue;
339 NetGroupCandidate cand;
340 cand.group.module_pin_group = pg;
341 cand.group.direction = group_direction(nets, simulated_nets);
342 cand.group.ascending = pg->is_ascending();
344 cand.rank = is_source ? 0 : 1;
345 cand.name = pg->get_name();
346 cand.sort_name =
to_lower(cand.name);
347 cand.owner_id = m->get_id();
348 cand.pin_group_id = pg->get_id();
349 candidates.push_back(cand);
353 for (
const Gate* g : mSimulationSet)
357 if (pg->size() < 2 || pg->size() > 31)
continue;
358 bool pin_group_simulated =
true;
359 bool is_source =
true;
360 std::vector<const Net*> nets;
362 for (
GatePin* gp : pg->get_pins())
367 switch (gp->get_direction())
370 n = g->get_fan_in_net(gp);
373 n = g->get_fan_out_net(gp);
378 if (!n || simulated_nets.find(n) == simulated_nets.end())
380 pin_group_simulated =
false;
385 if (!pin_group_simulated)
continue;
387 NetGroupCandidate cand;
389 cand.group.gate_pin_group = pg;
390 cand.group.direction = group_direction(nets, simulated_nets);
391 cand.group.ascending = pg->is_ascending();
393 cand.rank = is_source ? 2 : 3;
394 cand.name = pg->get_name();
395 cand.sort_name =
to_lower(cand.name);
396 cand.owner_id = g->get_id();
397 cand.pin_group_id = pg->get_id();
398 candidates.push_back(cand);
403 std::sort(candidates.begin(), candidates.end(), [](
const NetGroupCandidate& a,
const NetGroupCandidate& b) {
404 return std::tie(a.rank, a.sort_name, a.name, a.owner_id, a.pin_group_id) < std::tie(b.rank, b.sort_name, b.name, b.owner_id, b.pin_group_id);
407 std::unordered_set<const Net*> grouped_nets;
408 for (
const NetGroupCandidate& cand : candidates)
410 if (std::any_of(cand.nets.begin(), cand.nets.end(), [&grouped_nets](
const Net* n) { return grouped_nets.find(n) != grouped_nets.end(); }))
412 grouped_nets.insert(cand.nets.begin(), cand.nets.end());
413 m_netgroups.push_back(cand.group);
Net * get_fan_in_net(const std::string &pin_name) const
Net * get_fan_out_net(const std::string &pin_name) const
Module * get_parent_module() const
#define log_warning(channel,...)