2013-09-14 04:23:45 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-09-14 04:23:45 -05:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-09-14 04:23:45 -05:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <string>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2016-03-02 05:02:59 -06:00
|
|
|
static string spice_id2str(IdString id)
|
|
|
|
{
|
2016-05-20 09:43:13 -05:00
|
|
|
static const char *escape_chars = "$\\[]()<>=";
|
2016-03-02 05:02:59 -06:00
|
|
|
string s = RTLIL::unescape_id(id);
|
|
|
|
|
|
|
|
for (auto &ch : s)
|
|
|
|
if (strchr(escape_chars, ch) != nullptr) ch = '_';
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static string spice_id2str(IdString id, bool use_inames, idict<IdString, 1> &inums)
|
|
|
|
{
|
|
|
|
if (!use_inames && *id.c_str() == '$')
|
|
|
|
return stringf("%d", inums(id));
|
|
|
|
return spice_id2str(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
2014-07-24 15:47:57 -05:00
|
|
|
if (s.wire) {
|
2016-03-02 05:02:59 -06:00
|
|
|
if (s.wire->port_id)
|
|
|
|
use_inames = true;
|
2014-07-24 15:47:57 -05:00
|
|
|
if (s.wire->width > 1)
|
2016-03-02 05:02:59 -06:00
|
|
|
f << stringf(" %s.%d", spice_id2str(s.wire->name, use_inames, inums).c_str(), s.offset);
|
2013-09-14 04:23:45 -05:00
|
|
|
else
|
2016-03-02 05:02:59 -06:00
|
|
|
f << stringf(" %s", spice_id2str(s.wire->name, use_inames, inums).c_str());
|
2013-09-14 04:23:45 -05:00
|
|
|
} else {
|
2014-07-24 15:47:57 -05:00
|
|
|
if (s == RTLIL::State::S0)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" %s", neg.c_str());
|
2014-07-24 15:47:57 -05:00
|
|
|
else if (s == RTLIL::State::S1)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" %s", pos.c_str());
|
2013-09-14 04:23:45 -05:00
|
|
|
else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" %s%d", ncpf.c_str(), nc_counter++);
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:24:28 -06:00
|
|
|
static void print_spice_module(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, std::string &neg, std::string &pos, std::string &buf, std::string &ncpf, bool big_endian, bool use_inames)
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
|
|
|
SigMap sigmap(module);
|
2016-03-02 05:02:59 -06:00
|
|
|
idict<IdString, 1> inums;
|
2013-09-14 04:23:45 -05:00
|
|
|
int cell_counter = 0, conn_counter = 0, nc_counter = 0;
|
|
|
|
|
2020-03-31 23:56:52 -05:00
|
|
|
for (auto cell : module->cells())
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("X%d", cell_counter++);
|
2013-09-14 04:23:45 -05:00
|
|
|
|
|
|
|
std::vector<RTLIL::SigSpec> port_sigs;
|
|
|
|
|
2020-03-31 23:56:52 -05:00
|
|
|
if (design->module(cell->type) == nullptr)
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
2014-11-09 03:44:23 -06:00
|
|
|
log_warning("no (blackbox) module for cell type `%s' (%s.%s) found! Guessing order of ports.\n",
|
2016-03-02 05:02:59 -06:00
|
|
|
log_id(cell->type), log_id(module), log_id(cell));
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &conn : cell->connections()) {
|
2013-09-14 04:23:45 -05:00
|
|
|
RTLIL::SigSpec sig = sigmap(conn.second);
|
|
|
|
port_sigs.push_back(sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-31 23:56:52 -05:00
|
|
|
RTLIL::Module *mod = design->module(cell->type);
|
2013-09-14 04:23:45 -05:00
|
|
|
|
|
|
|
std::vector<RTLIL::Wire*> ports;
|
2020-03-31 23:56:52 -05:00
|
|
|
for (auto wire : mod->wires()) {
|
2013-09-14 04:23:45 -05:00
|
|
|
if (wire->port_id == 0)
|
|
|
|
continue;
|
|
|
|
while (int(ports.size()) < wire->port_id)
|
|
|
|
ports.push_back(NULL);
|
|
|
|
ports.at(wire->port_id-1) = wire;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RTLIL::Wire *wire : ports) {
|
|
|
|
log_assert(wire != NULL);
|
|
|
|
RTLIL::SigSpec sig(RTLIL::State::Sz, wire->width);
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->hasPort(wire->name)) {
|
|
|
|
sig = sigmap(cell->getPort(wire->name));
|
2014-12-24 02:51:17 -06:00
|
|
|
sig.extend_u0(wire->width, false);
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
port_sigs.push_back(sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &sig : port_sigs) {
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < sig.size(); i++) {
|
|
|
|
RTLIL::SigSpec s = sig.extract(big_endian ? sig.size() - 1 - i : i, 1);
|
2016-03-02 05:02:59 -06:00
|
|
|
print_spice_net(f, s, neg, pos, ncpf, nc_counter, use_inames, inums);
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 05:02:59 -06:00
|
|
|
f << stringf(" %s\n", spice_id2str(cell->type).c_str());
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &conn : module->connections())
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < conn.first.size(); i++) {
|
2021-01-13 10:24:28 -06:00
|
|
|
f << (buf == "DC" ? stringf("V%d", conn_counter++) : stringf("X%d", cell_counter++));
|
2016-03-02 05:02:59 -06:00
|
|
|
print_spice_net(f, conn.second.extract(i, 1), neg, pos, ncpf, nc_counter, use_inames, inums);
|
2021-01-13 10:24:28 -06:00
|
|
|
print_spice_net(f, conn.first.extract(i, 1), neg, pos, ncpf, nc_counter, use_inames, inums);
|
|
|
|
f << (buf == "DC" ? " DC 0\n" : stringf(" %s\n", buf.c_str()));
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SpiceBackend : public Backend {
|
|
|
|
SpiceBackend() : Backend("spice", "write design to SPICE netlist file") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" write_spice [options] [filename]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Write the current design to an SPICE netlist file.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -big_endian\n");
|
2015-07-02 04:14:30 -05:00
|
|
|
log(" generate multi-bit ports in MSB first order\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
log(" (default is LSB first)\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -neg net_name\n");
|
|
|
|
log(" set the net name for constant 0 (default: Vss)\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -pos net_name\n");
|
|
|
|
log(" set the net name for constant 1 (default: Vdd)\n");
|
|
|
|
log("\n");
|
2021-01-13 10:24:28 -06:00
|
|
|
log(" -buf DC|subckt_name\n");
|
|
|
|
log(" set the name for jumper element (default: DC)\n");
|
|
|
|
log(" (used to connect different nets)\n");
|
|
|
|
log("\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
log(" -nc_prefix\n");
|
|
|
|
log(" prefix for not-connected nets (default: _NC)\n");
|
|
|
|
log("\n");
|
2016-03-02 05:02:59 -06:00
|
|
|
log(" -inames\n");
|
|
|
|
log(" include names of internal ($-prefixed) nets in outputs\n");
|
|
|
|
log(" (default is to use net numbers instead)\n");
|
|
|
|
log("\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
log(" -top top_module\n");
|
|
|
|
log(" set the specified module as design top module\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
|
|
|
std::string top_module_name;
|
2013-11-09 05:01:50 -06:00
|
|
|
RTLIL::Module *top_module = NULL;
|
2016-03-02 05:02:59 -06:00
|
|
|
bool big_endian = false, use_inames = false;
|
2021-01-13 10:24:28 -06:00
|
|
|
std::string neg = "Vss", pos = "Vdd", ncpf = "_NC", buf = "DC";
|
2013-09-14 04:23:45 -05:00
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing SPICE backend.\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-big_endian") {
|
|
|
|
big_endian = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-02 05:02:59 -06:00
|
|
|
if (args[argidx] == "-inames") {
|
|
|
|
use_inames = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-15 05:19:06 -05:00
|
|
|
if (args[argidx] == "-neg" && argidx+1 < args.size()) {
|
|
|
|
neg = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-pos" && argidx+1 < args.size()) {
|
|
|
|
pos = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-13 10:24:28 -06:00
|
|
|
if (args[argidx] == "-buf" && argidx+1 < args.size()) {
|
|
|
|
buf = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-15 05:19:06 -05:00
|
|
|
if (args[argidx] == "-nc_prefix" && argidx+1 < args.size()) {
|
|
|
|
ncpf = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-top" && argidx+1 < args.size()) {
|
|
|
|
top_module_name = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-14 04:23:45 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(f, filename, args, argidx);
|
|
|
|
|
2013-11-23 22:03:43 -06:00
|
|
|
if (top_module_name.empty())
|
2020-03-31 23:56:52 -05:00
|
|
|
for (auto module : design->modules())
|
2020-03-12 14:57:01 -05:00
|
|
|
if (module->get_bool_attribute(ID::top))
|
2020-03-31 23:56:52 -05:00
|
|
|
top_module_name = module->name.str();
|
2013-11-23 22:03:43 -06:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf("* SPICE netlist generated by %s\n", yosys_version_str);
|
|
|
|
*f << stringf("\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
|
2020-03-31 23:56:52 -05:00
|
|
|
for (auto module : design->modules())
|
2013-09-14 04:23:45 -05:00
|
|
|
{
|
2019-04-18 10:42:12 -05:00
|
|
|
if (module->get_blackbox_attribute())
|
2013-09-14 04:23:45 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (module->processes.size() != 0)
|
2016-03-02 05:02:59 -06:00
|
|
|
log_error("Found unmapped processes in module %s: unmapped processes are not supported in SPICE backend!\n", log_id(module));
|
2013-09-14 04:23:45 -05:00
|
|
|
if (module->memories.size() != 0)
|
2016-04-05 01:18:21 -05:00
|
|
|
log_error("Found unmapped memories in module %s: unmapped memories are not supported in SPICE backend!\n", log_id(module));
|
2013-09-14 04:23:45 -05:00
|
|
|
|
|
|
|
if (module->name == RTLIL::escape_id(top_module_name)) {
|
|
|
|
top_module = module;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RTLIL::Wire*> ports;
|
2020-03-31 23:56:52 -05:00
|
|
|
for (auto wire : module->wires()) {
|
2013-09-14 04:23:45 -05:00
|
|
|
if (wire->port_id == 0)
|
|
|
|
continue;
|
|
|
|
while (int(ports.size()) < wire->port_id)
|
|
|
|
ports.push_back(NULL);
|
|
|
|
ports.at(wire->port_id-1) = wire;
|
|
|
|
}
|
|
|
|
|
2016-03-02 05:02:59 -06:00
|
|
|
*f << stringf(".SUBCKT %s", spice_id2str(module->name).c_str());
|
2013-09-14 04:23:45 -05:00
|
|
|
for (RTLIL::Wire *wire : ports) {
|
|
|
|
log_assert(wire != NULL);
|
|
|
|
if (wire->width > 1) {
|
|
|
|
for (int i = 0; i < wire->width; i++)
|
2016-03-02 05:02:59 -06:00
|
|
|
*f << stringf(" %s.%d", spice_id2str(wire->name).c_str(), big_endian ? wire->width - 1 - i : i);
|
2013-09-14 04:23:45 -05:00
|
|
|
} else
|
2016-03-02 05:02:59 -06:00
|
|
|
*f << stringf(" %s", spice_id2str(wire->name).c_str());
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf("\n");
|
2021-01-13 10:24:28 -06:00
|
|
|
print_spice_module(*f, module, design, neg, pos, buf, ncpf, big_endian, use_inames);
|
2016-03-02 05:02:59 -06:00
|
|
|
*f << stringf(".ENDS %s\n\n", spice_id2str(module->name).c_str());
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!top_module_name.empty()) {
|
|
|
|
if (top_module == NULL)
|
|
|
|
log_error("Can't find top module `%s'!\n", top_module_name.c_str());
|
2021-01-13 10:24:28 -06:00
|
|
|
print_spice_module(*f, top_module, design, neg, pos, buf, ncpf, big_endian, use_inames);
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf("\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf("************************\n");
|
|
|
|
*f << stringf("* end of SPICE netlist *\n");
|
|
|
|
*f << stringf("************************\n");
|
|
|
|
*f << stringf("\n");
|
2013-09-14 04:23:45 -05:00
|
|
|
}
|
|
|
|
} SpiceBackend;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|