yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
spice.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/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/log.h"
25 #include <string>
26 
29 
30 static void print_spice_net(std::ostream &f, RTLIL::SigBit s, std::string &neg, std::string &pos, std::string &ncpf, int &nc_counter)
31 {
32  if (s.wire) {
33  if (s.wire->width > 1)
34  f << stringf(" %s[%d]", RTLIL::id2cstr(s.wire->name), s.offset);
35  else
36  f << stringf(" %s", RTLIL::id2cstr(s.wire->name));
37  } else {
38  if (s == RTLIL::State::S0)
39  f << stringf(" %s", neg.c_str());
40  else if (s == RTLIL::State::S1)
41  f << stringf(" %s", pos.c_str());
42  else
43  f << stringf(" %s%d", ncpf.c_str(), nc_counter++);
44  }
45 }
46 
47 static void print_spice_module(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, std::string &neg, std::string &pos, std::string &ncpf, bool big_endian)
48 {
49  SigMap sigmap(module);
50  int cell_counter = 0, conn_counter = 0, nc_counter = 0;
51 
52  for (auto &cell_it : module->cells_)
53  {
54  RTLIL::Cell *cell = cell_it.second;
55  f << stringf("X%d", cell_counter++);
56 
57  std::vector<RTLIL::SigSpec> port_sigs;
58 
59  if (design->modules_.count(cell->type) == 0)
60  {
61  log_warning("no (blackbox) module for cell type `%s' (%s.%s) found! Guessing order of ports.\n",
62  RTLIL::id2cstr(cell->type), RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name));
63  for (auto &conn : cell->connections()) {
64  RTLIL::SigSpec sig = sigmap(conn.second);
65  port_sigs.push_back(sig);
66  }
67  }
68  else
69  {
70  RTLIL::Module *mod = design->modules_.at(cell->type);
71 
72  std::vector<RTLIL::Wire*> ports;
73  for (auto wire_it : mod->wires_) {
74  RTLIL::Wire *wire = wire_it.second;
75  if (wire->port_id == 0)
76  continue;
77  while (int(ports.size()) < wire->port_id)
78  ports.push_back(NULL);
79  ports.at(wire->port_id-1) = wire;
80  }
81 
82  for (RTLIL::Wire *wire : ports) {
83  log_assert(wire != NULL);
84  RTLIL::SigSpec sig(RTLIL::State::Sz, wire->width);
85  if (cell->hasPort(wire->name)) {
86  sig = sigmap(cell->getPort(wire->name));
87  sig.extend(wire->width, false);
88  }
89  port_sigs.push_back(sig);
90  }
91  }
92 
93  for (auto &sig : port_sigs) {
94  for (int i = 0; i < sig.size(); i++) {
95  RTLIL::SigSpec s = sig.extract(big_endian ? sig.size() - 1 - i : i, 1);
96  print_spice_net(f, s, neg, pos, ncpf, nc_counter);
97  }
98  }
99 
100  f << stringf(" %s\n", RTLIL::id2cstr(cell->type));
101  }
102 
103  for (auto &conn : module->connections())
104  for (int i = 0; i < conn.first.size(); i++) {
105  f << stringf("V%d", conn_counter++);
106  print_spice_net(f, conn.first.extract(i, 1), neg, pos, ncpf, nc_counter);
107  print_spice_net(f, conn.second.extract(i, 1), neg, pos, ncpf, nc_counter);
108  f << stringf(" DC 0\n");
109  }
110 }
111 
112 struct SpiceBackend : public Backend {
113  SpiceBackend() : Backend("spice", "write design to SPICE netlist file") { }
114  virtual void help()
115  {
116  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
117  log("\n");
118  log(" write_spice [options] [filename]\n");
119  log("\n");
120  log("Write the current design to an SPICE netlist file.\n");
121  log("\n");
122  log(" -big_endian\n");
123  log(" generate multi-bit ports in MSB first order \n");
124  log(" (default is LSB first)\n");
125  log("\n");
126  log(" -neg net_name\n");
127  log(" set the net name for constant 0 (default: Vss)\n");
128  log("\n");
129  log(" -pos net_name\n");
130  log(" set the net name for constant 1 (default: Vdd)\n");
131  log("\n");
132  log(" -nc_prefix\n");
133  log(" prefix for not-connected nets (default: _NC)\n");
134  log("\n");
135  log(" -top top_module\n");
136  log(" set the specified module as design top module\n");
137  log("\n");
138  }
139  virtual void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
140  {
141  std::string top_module_name;
142  RTLIL::Module *top_module = NULL;
143  bool big_endian = false;
144  std::string neg = "Vss", pos = "Vdd", ncpf = "_NC";
145 
146  log_header("Executing SPICE backend.\n");
147 
148  size_t argidx;
149  for (argidx = 1; argidx < args.size(); argidx++)
150  {
151  if (args[argidx] == "-big_endian") {
152  big_endian = true;
153  continue;
154  }
155  if (args[argidx] == "-neg" && argidx+1 < args.size()) {
156  neg = args[++argidx];
157  continue;
158  }
159  if (args[argidx] == "-pos" && argidx+1 < args.size()) {
160  pos = args[++argidx];
161  continue;
162  }
163  if (args[argidx] == "-nc_prefix" && argidx+1 < args.size()) {
164  ncpf = args[++argidx];
165  continue;
166  }
167  if (args[argidx] == "-top" && argidx+1 < args.size()) {
168  top_module_name = args[++argidx];
169  continue;
170  }
171  break;
172  }
173  extra_args(f, filename, args, argidx);
174 
175  if (top_module_name.empty())
176  for (auto & mod_it:design->modules_)
177  if (mod_it.second->get_bool_attribute("\\top"))
178  top_module_name = mod_it.first.str();
179 
180  *f << stringf("* SPICE netlist generated by %s\n", yosys_version_str);
181  *f << stringf("\n");
182 
183  for (auto module_it : design->modules_)
184  {
185  RTLIL::Module *module = module_it.second;
186  if (module->get_bool_attribute("\\blackbox"))
187  continue;
188 
189  if (module->processes.size() != 0)
190  log_error("Found unmapped processes in module %s: unmapped processes are not supported in SPICE backend!\n", RTLIL::id2cstr(module->name));
191  if (module->memories.size() != 0)
192  log_error("Found munmapped emories in module %s: unmapped memories are not supported in SPICE backend!\n", RTLIL::id2cstr(module->name));
193 
194  if (module->name == RTLIL::escape_id(top_module_name)) {
195  top_module = module;
196  continue;
197  }
198 
199  std::vector<RTLIL::Wire*> ports;
200  for (auto wire_it : module->wires_) {
201  RTLIL::Wire *wire = wire_it.second;
202  if (wire->port_id == 0)
203  continue;
204  while (int(ports.size()) < wire->port_id)
205  ports.push_back(NULL);
206  ports.at(wire->port_id-1) = wire;
207  }
208 
209  *f << stringf(".SUBCKT %s", RTLIL::id2cstr(module->name));
210  for (RTLIL::Wire *wire : ports) {
211  log_assert(wire != NULL);
212  if (wire->width > 1) {
213  for (int i = 0; i < wire->width; i++)
214  *f << stringf(" %s[%d]", RTLIL::id2cstr(wire->name), big_endian ? wire->width - 1 - i : i);
215  } else
216  *f << stringf(" %s", RTLIL::id2cstr(wire->name));
217  }
218  *f << stringf("\n");
219  print_spice_module(*f, module, design, neg, pos, ncpf, big_endian);
220  *f << stringf(".ENDS %s\n\n", RTLIL::id2cstr(module->name));
221  }
222 
223  if (!top_module_name.empty()) {
224  if (top_module == NULL)
225  log_error("Can't find top module `%s'!\n", top_module_name.c_str());
226  print_spice_module(*f, top_module, design, neg, pos, ncpf, big_endian);
227  *f << stringf("\n");
228  }
229 
230  *f << stringf("************************\n");
231  *f << stringf("* end of SPICE netlist *\n");
232  *f << stringf("************************\n");
233  *f << stringf("\n");
234  }
235 } SpiceBackend;
236 
const char * yosys_version_str
RTLIL::Wire * wire
Definition: rtlil.h:907
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
void log_warning(const char *format,...)
Definition: log.cc:196
void log_header(const char *format,...)
Definition: log.cc:188
const std::vector< RTLIL::SigSig > & connections() const
Definition: rtlil.cc:1307
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
RTLIL::IdString name
Definition: rtlil.h:853
int width
Definition: rtlil.h:826
std::map< RTLIL::IdString, RTLIL::Memory * > memories
Definition: rtlil.h:601
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
int port_id
Definition: rtlil.h:826
SpiceBackend SpiceBackend
RTLIL::IdString type
Definition: rtlil.h:854
int offset
Definition: rtlil.h:910
static std::string escape_id(std::string str)
Definition: rtlil.h:251
void extra_args(std::ostream *&f, std::string &filename, std::vector< std::string > args, size_t argidx)
Definition: register.cc:439
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
SpiceBackend()
Definition: spice.cc:113
RTLIL_ATTRIBUTE_MEMBERS bool hasPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1766
virtual void help()
Definition: spice.cc:114
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
virtual void execute(std::ostream *&f, std::string filename, std::vector< std::string > args, RTLIL::Design *design)
Definition: spice.cc:139
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::IdString name
Definition: rtlil.h:599
static void print_spice_module(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, std::string &neg, std::string &pos, std::string &ncpf, bool big_endian)
Definition: spice.cc:47
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
RTLIL::IdString name
Definition: rtlil.h:825
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
#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
void log(const char *format,...)
Definition: log.cc:180
RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other=NULL) const
Definition: rtlil.cc:2414
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN void print_spice_net(std::ostream &f, RTLIL::SigBit s, std::string &neg, std::string &pos, std::string &ncpf, int &nc_counter)
Definition: spice.cc:30
void extend(int width, bool is_signed=false)
Definition: rtlil.cc:2593
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814