Use C++11 final/override keywords.
[yosys.git] / backends / spice / spice.cc
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
27 USING_YOSYS_NAMESPACE
28 PRIVATE_NAMESPACE_BEGIN
29
30 static string spice_id2str(IdString id)
31 {
32 static const char *escape_chars = "$\\[]()<>=";
33 string s = RTLIL::unescape_id(id);
34
35 for (auto &ch : s)
36 if (strchr(escape_chars, ch) != nullptr) ch = '_';
37
38 return s;
39 }
40
41 static string spice_id2str(IdString id, bool use_inames, idict<IdString, 1> &inums)
42 {
43 if (!use_inames && *id.c_str() == '$')
44 return stringf("%d", inums(id));
45 return spice_id2str(id);
46 }
47
48 static void print_spice_net(std::ostream &f, RTLIL::SigBit s, std::string &neg, std::string &pos, std::string &ncpf, int &nc_counter, bool use_inames, idict<IdString, 1> &inums)
49 {
50 if (s.wire) {
51 if (s.wire->port_id)
52 use_inames = true;
53 if (s.wire->width > 1)
54 f << stringf(" %s.%d", spice_id2str(s.wire->name, use_inames, inums).c_str(), s.offset);
55 else
56 f << stringf(" %s", spice_id2str(s.wire->name, use_inames, inums).c_str());
57 } else {
58 if (s == RTLIL::State::S0)
59 f << stringf(" %s", neg.c_str());
60 else if (s == RTLIL::State::S1)
61 f << stringf(" %s", pos.c_str());
62 else
63 f << stringf(" %s%d", ncpf.c_str(), nc_counter++);
64 }
65 }
66
67 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, bool use_inames)
68 {
69 SigMap sigmap(module);
70 idict<IdString, 1> inums;
71 int cell_counter = 0, conn_counter = 0, nc_counter = 0;
72
73 for (auto cell : module->cells())
74 {
75 f << stringf("X%d", cell_counter++);
76
77 std::vector<RTLIL::SigSpec> port_sigs;
78
79 if (design->module(cell->type) == nullptr)
80 {
81 log_warning("no (blackbox) module for cell type `%s' (%s.%s) found! Guessing order of ports.\n",
82 log_id(cell->type), log_id(module), log_id(cell));
83 for (auto &conn : cell->connections()) {
84 RTLIL::SigSpec sig = sigmap(conn.second);
85 port_sigs.push_back(sig);
86 }
87 }
88 else
89 {
90 RTLIL::Module *mod = design->module(cell->type);
91
92 std::vector<RTLIL::Wire*> ports;
93 for (auto wire : mod->wires()) {
94 if (wire->port_id == 0)
95 continue;
96 while (int(ports.size()) < wire->port_id)
97 ports.push_back(NULL);
98 ports.at(wire->port_id-1) = wire;
99 }
100
101 for (RTLIL::Wire *wire : ports) {
102 log_assert(wire != NULL);
103 RTLIL::SigSpec sig(RTLIL::State::Sz, wire->width);
104 if (cell->hasPort(wire->name)) {
105 sig = sigmap(cell->getPort(wire->name));
106 sig.extend_u0(wire->width, false);
107 }
108 port_sigs.push_back(sig);
109 }
110 }
111
112 for (auto &sig : port_sigs) {
113 for (int i = 0; i < sig.size(); i++) {
114 RTLIL::SigSpec s = sig.extract(big_endian ? sig.size() - 1 - i : i, 1);
115 print_spice_net(f, s, neg, pos, ncpf, nc_counter, use_inames, inums);
116 }
117 }
118
119 f << stringf(" %s\n", spice_id2str(cell->type).c_str());
120 }
121
122 for (auto &conn : module->connections())
123 for (int i = 0; i < conn.first.size(); i++) {
124 f << stringf("V%d", conn_counter++);
125 print_spice_net(f, conn.first.extract(i, 1), neg, pos, ncpf, nc_counter, use_inames, inums);
126 print_spice_net(f, conn.second.extract(i, 1), neg, pos, ncpf, nc_counter, use_inames, inums);
127 f << stringf(" DC 0\n");
128 }
129 }
130
131 struct SpiceBackend : public Backend {
132 SpiceBackend() : Backend("spice", "write design to SPICE netlist file") { }
133 void help() override
134 {
135 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
136 log("\n");
137 log(" write_spice [options] [filename]\n");
138 log("\n");
139 log("Write the current design to an SPICE netlist file.\n");
140 log("\n");
141 log(" -big_endian\n");
142 log(" generate multi-bit ports in MSB first order\n");
143 log(" (default is LSB first)\n");
144 log("\n");
145 log(" -neg net_name\n");
146 log(" set the net name for constant 0 (default: Vss)\n");
147 log("\n");
148 log(" -pos net_name\n");
149 log(" set the net name for constant 1 (default: Vdd)\n");
150 log("\n");
151 log(" -nc_prefix\n");
152 log(" prefix for not-connected nets (default: _NC)\n");
153 log("\n");
154 log(" -inames\n");
155 log(" include names of internal ($-prefixed) nets in outputs\n");
156 log(" (default is to use net numbers instead)\n");
157 log("\n");
158 log(" -top top_module\n");
159 log(" set the specified module as design top module\n");
160 log("\n");
161 }
162 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
163 {
164 std::string top_module_name;
165 RTLIL::Module *top_module = NULL;
166 bool big_endian = false, use_inames = false;
167 std::string neg = "Vss", pos = "Vdd", ncpf = "_NC";
168
169 log_header(design, "Executing SPICE backend.\n");
170
171 size_t argidx;
172 for (argidx = 1; argidx < args.size(); argidx++)
173 {
174 if (args[argidx] == "-big_endian") {
175 big_endian = true;
176 continue;
177 }
178 if (args[argidx] == "-inames") {
179 use_inames = true;
180 continue;
181 }
182 if (args[argidx] == "-neg" && argidx+1 < args.size()) {
183 neg = args[++argidx];
184 continue;
185 }
186 if (args[argidx] == "-pos" && argidx+1 < args.size()) {
187 pos = args[++argidx];
188 continue;
189 }
190 if (args[argidx] == "-nc_prefix" && argidx+1 < args.size()) {
191 ncpf = args[++argidx];
192 continue;
193 }
194 if (args[argidx] == "-top" && argidx+1 < args.size()) {
195 top_module_name = args[++argidx];
196 continue;
197 }
198 break;
199 }
200 extra_args(f, filename, args, argidx);
201
202 if (top_module_name.empty())
203 for (auto module : design->modules())
204 if (module->get_bool_attribute(ID::top))
205 top_module_name = module->name.str();
206
207 *f << stringf("* SPICE netlist generated by %s\n", yosys_version_str);
208 *f << stringf("\n");
209
210 for (auto module : design->modules())
211 {
212 if (module->get_blackbox_attribute())
213 continue;
214
215 if (module->processes.size() != 0)
216 log_error("Found unmapped processes in module %s: unmapped processes are not supported in SPICE backend!\n", log_id(module));
217 if (module->memories.size() != 0)
218 log_error("Found unmapped memories in module %s: unmapped memories are not supported in SPICE backend!\n", log_id(module));
219
220 if (module->name == RTLIL::escape_id(top_module_name)) {
221 top_module = module;
222 continue;
223 }
224
225 std::vector<RTLIL::Wire*> ports;
226 for (auto wire : module->wires()) {
227 if (wire->port_id == 0)
228 continue;
229 while (int(ports.size()) < wire->port_id)
230 ports.push_back(NULL);
231 ports.at(wire->port_id-1) = wire;
232 }
233
234 *f << stringf(".SUBCKT %s", spice_id2str(module->name).c_str());
235 for (RTLIL::Wire *wire : ports) {
236 log_assert(wire != NULL);
237 if (wire->width > 1) {
238 for (int i = 0; i < wire->width; i++)
239 *f << stringf(" %s.%d", spice_id2str(wire->name).c_str(), big_endian ? wire->width - 1 - i : i);
240 } else
241 *f << stringf(" %s", spice_id2str(wire->name).c_str());
242 }
243 *f << stringf("\n");
244 print_spice_module(*f, module, design, neg, pos, ncpf, big_endian, use_inames);
245 *f << stringf(".ENDS %s\n\n", spice_id2str(module->name).c_str());
246 }
247
248 if (!top_module_name.empty()) {
249 if (top_module == NULL)
250 log_error("Can't find top module `%s'!\n", top_module_name.c_str());
251 print_spice_module(*f, top_module, design, neg, pos, ncpf, big_endian, use_inames);
252 *f << stringf("\n");
253 }
254
255 *f << stringf("************************\n");
256 *f << stringf("* end of SPICE netlist *\n");
257 *f << stringf("************************\n");
258 *f << stringf("\n");
259 }
260 } SpiceBackend;
261
262 PRIVATE_NAMESPACE_END