yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
setundef.cc
Go to the documentation of this file.
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/celltypes.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/rtlil.h"
24 #include "kernel/log.h"
25 
28 
30 {
32  uint32_t next_bit_state;
33 
35  {
36  if (next_bit_mode == 0)
37  return RTLIL::State::S0;
38 
39  if (next_bit_mode == 1)
40  return RTLIL::State::S1;
41 
42  // xorshift32
47 
48  return ((next_bit_state >> (next_bit_state & 15)) & 16) ? RTLIL::State::S0 : RTLIL::State::S1;
49  }
50 
52  {
53  for (auto &bit : sig)
54  if (bit.wire == NULL && bit.data > RTLIL::State::S1)
55  bit = next_bit();
56  }
57 };
58 
59 struct SetundefPass : public Pass {
60  SetundefPass() : Pass("setundef", "replace undef values with defined constants") { }
61  virtual void help()
62  {
63  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
64  log("\n");
65  log(" setundef [options] [selection]\n");
66  log("\n");
67  log("This command replaced undef (x) constants with defined (0/1) constants.\n");
68  log("\n");
69  log(" -undriven\n");
70  log(" also set undriven nets to constant values\n");
71  log("\n");
72  log(" -zero\n");
73  log(" replace with bits cleared (0)\n");
74  log("\n");
75  log(" -one\n");
76  log(" replace with bits set (1)\n");
77  log("\n");
78  log(" -random <seed>\n");
79  log(" replace with random bits using the specified integer als seed\n");
80  log(" value for the random number generator.\n");
81  log("\n");
82  }
83  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
84  {
85  bool got_value = false;
86  bool undriven_mode = false;
87  SetundefWorker worker;
88 
89  size_t argidx;
90  for (argidx = 1; argidx < args.size(); argidx++)
91  {
92  if (args[argidx] == "-undriven") {
93  undriven_mode = true;
94  continue;
95  }
96  if (args[argidx] == "-zero") {
97  got_value = true;
98  worker.next_bit_mode = 0;
99  continue;
100  }
101  if (args[argidx] == "-one") {
102  got_value = true;
103  worker.next_bit_mode = 1;
104  continue;
105  }
106  if (args[argidx] == "-random" && !got_value && argidx+1 < args.size()) {
107  got_value = true;
108  worker.next_bit_mode = 2;
109  worker.next_bit_state = atoi(args[++argidx].c_str()) + 1;
110  for (int i = 0; i < 10; i++)
111  worker.next_bit();
112  continue;
113  }
114  break;
115  }
116  extra_args(args, argidx, design);
117 
118  if (!got_value)
119  log_cmd_error("One of the options -zero, -one, or -random <seed> must be specified.\n");
120 
121  for (auto &mod_it : design->modules_)
122  {
123  RTLIL::Module *module = mod_it.second;
124  if (!design->selected(module))
125  continue;
126 
127  if (undriven_mode)
128  {
129  if (!module->processes.empty())
130  log_error("The 'setundef' command can't operate in -undriven mode on modules with processes. Run 'proc' first.\n");
131 
132  SigMap sigmap(module);
133  SigPool undriven_signals;
134 
135  for (auto &it : module->wires_)
136  if (!it.second->port_input)
137  undriven_signals.add(sigmap(it.second));
138 
139  CellTypes ct(design);
140  for (auto &it : module->cells_)
141  for (auto &conn : it.second->connections())
142  if (!ct.cell_known(it.second->type) || ct.cell_output(it.second->type, conn.first))
143  undriven_signals.del(sigmap(conn.second));
144 
145  RTLIL::SigSpec sig = undriven_signals.export_all();
146  for (auto &c : sig.chunks()) {
147  RTLIL::SigSpec bits;
148  for (int i = 0; i < c.width; i++)
149  bits.append(worker.next_bit());
150  module->connect(RTLIL::SigSig(c, bits));
151  }
152  }
153 
154  module->rewrite_sigspecs(worker);
155  }
156  }
157 } SetundefPass;
158 
bool selected(T1 *module) const
Definition: rtlil.h:551
CellTypes ct
Definition: opt_clean.cc:33
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
void rewrite_sigspecs(T functor)
Definition: rtlil.h:1166
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: setundef.cc:83
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
void connect(const RTLIL::SigSig &conn)
Definition: rtlil.cc:1278
SetundefPass SetundefPass
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::SigSpec export_all()
Definition: sigtools.h:123
int next_bit_mode
Definition: setundef.cc:31
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
void log_cmd_error(const char *format,...)
Definition: log.cc:211
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
void add(RTLIL::SigSpec sig)
Definition: sigtools.h:41
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
virtual void help()
Definition: setundef.cc:61
void log(const char *format,...)
Definition: log.cc:180
RTLIL::State next_bit()
Definition: setundef.cc:34
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
State
Definition: rtlil.h:29
void del(RTLIL::SigSpec sig)
Definition: sigtools.h:54
uint32_t next_bit_state
Definition: setundef.cc:32
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
void operator()(RTLIL::SigSpec &sig)
Definition: setundef.cc:51
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
const std::vector< RTLIL::SigChunk > & chunks() const
Definition: rtlil.h:1016