yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
add.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/yosys.h"
21 
24 
25 static void add_wire(RTLIL::Design *design, RTLIL::Module *module, std::string name, int width, bool flag_input, bool flag_output, bool flag_global)
26 {
27  RTLIL::Wire *wire = NULL;
28  name = RTLIL::escape_id(name);
29 
30  if (module->count_id(name) != 0)
31  {
32  if (module->wires_.count(name) > 0)
33  wire = module->wires_.at(name);
34 
35  if (wire != NULL && wire->width != width)
36  wire = NULL;
37 
38  if (wire != NULL && wire->port_input != flag_input)
39  wire = NULL;
40 
41  if (wire != NULL && wire->port_output != flag_output)
42  wire = NULL;
43 
44  if (wire == NULL)
45  log_cmd_error("Found incompatible object with same name in module %s!\n", module->name.c_str());
46 
47  log("Module %s already has such an object.\n", module->name.c_str());
48  }
49  else
50  {
51  wire = module->addWire(name, width);
52  wire->port_input = flag_input;
53  wire->port_output = flag_output;
54 
55  if (flag_input || flag_output) {
56  wire->port_id = module->wires_.size();
57  module->fixup_ports();
58  }
59 
60  log("Added wire %s to module %s.\n", name.c_str(), module->name.c_str());
61  }
62 
63  if (!flag_global)
64  return;
65 
66  for (auto &it : module->cells_)
67  {
68  if (design->modules_.count(it.second->type) == 0)
69  continue;
70 
71  RTLIL::Module *mod = design->modules_.at(it.second->type);
72  if (!design->selected_whole_module(mod->name))
73  continue;
74  if (mod->get_bool_attribute("\\blackbox"))
75  continue;
76  if (it.second->hasPort(name))
77  continue;
78 
79  it.second->setPort(name, wire);
80  log("Added connection %s to cell %s.%s (%s).\n", name.c_str(), module->name.c_str(), it.first.c_str(), it.second->type.c_str());
81  }
82 }
83 
84 struct AddPass : public Pass {
85  AddPass() : Pass("add", "add objects to the design") { }
86  virtual void help()
87  {
88  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
89  log("\n");
90  log(" add <command> [selection]\n");
91  log("\n");
92  log("This command adds objects to the design. It operates on all fully selected\n");
93  log("modules. So e.g. 'add -wire foo' will add a wire foo to all selected modules.\n");
94  log("\n");
95  log("\n");
96  log(" add {-wire|-input|-inout|-output} <name> <width> [selection]\n");
97  log("\n");
98  log("Add a wire (input, inout, output port) with the given name and width. The\n");
99  log("command will fail if the object exists already and has different properties\n");
100  log("than the object to be created.\n");
101  log("\n");
102  log("\n");
103  log(" add -global_input <name> <width> [selection]\n");
104  log("\n");
105  log("Like 'add -input', but also connect the signal between instances of the\n");
106  log("selected modules.\n");
107  log("\n");
108  }
109  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
110  {
111  std::string command;
112  std::string arg_name;
113  bool arg_flag_input = false;
114  bool arg_flag_output = false;
115  bool arg_flag_global = false;
116  int arg_width = 0;
117 
118  size_t argidx;
119  for (argidx = 1; argidx < args.size(); argidx++)
120  {
121  std::string arg = args[argidx];
122  if (arg == "-wire" || arg == "-input" || arg == "-inout" || arg == "-output" || arg == "-global_input") {
123  if (argidx+2 >= args.size())
124  break;
125  command = "wire";
126  if (arg == "-input" || arg == "-inout" || arg == "-global_input")
127  arg_flag_input = true;
128  if (arg == "-output" || arg == "-inout")
129  arg_flag_output = true;
130  if (arg == "-global_input")
131  arg_flag_global = true;
132  arg_name = args[++argidx];
133  arg_width = atoi(args[++argidx].c_str());
134  continue;
135  }
136  break;
137  }
138  extra_args(args, argidx, design);
139 
140  for (auto &mod : design->modules_)
141  {
142  RTLIL::Module *module = mod.second;
143  if (!design->selected_whole_module(module->name))
144  continue;
145  if (module->get_bool_attribute("\\blackbox"))
146  continue;
147 
148  if (command == "wire")
149  add_wire(design, module, arg_name, arg_width, arg_flag_input, arg_flag_output, arg_flag_global);
150  }
151  }
152 } AddPass;
153 
const char * c_str() const
Definition: rtlil.h:178
virtual void help()
Definition: add.cc:86
AddPass AddPass
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN void add_wire(RTLIL::Design *design, RTLIL::Module *module, std::string name, int width, bool flag_input, bool flag_output, bool flag_global)
Definition: add.cc:25
bool port_input
Definition: rtlil.h:827
int width
Definition: rtlil.h:826
RTLIL::Module * module
Definition: abc.cc:94
int port_id
Definition: rtlil.h:826
static std::string escape_id(std::string str)
Definition: rtlil.h:251
bool port_output
Definition: rtlil.h:827
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: add.cc:109
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
void fixup_ports()
Definition: rtlil.cc:1312
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
RTLIL::IdString name
Definition: rtlil.h:599
bool selected_whole_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:388
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
void log_cmd_error(const char *format,...)
Definition: log.cc:211
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
virtual size_t count_id(RTLIL::IdString id)
Definition: rtlil.cc:472
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
void log(const char *format,...)
Definition: log.cc:180
AddPass()
Definition: add.cc:85
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
Definition: add.cc:84