2021-11-16 11:30:23 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Aki "lethalbit" Van Ness <aki@yosyshq.com> <aki@lethalbit.net>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/cellaigs.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <string>
|
2022-02-17 10:35:45 -06:00
|
|
|
#include <algorithm>
|
2021-11-16 11:30:23 -06:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
2022-01-14 08:41:52 -06:00
|
|
|
struct JnyWriter
|
2021-11-16 11:30:23 -06:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::ostream &f;
|
|
|
|
bool _use_selection;
|
2022-02-24 09:39:30 -06:00
|
|
|
|
|
|
|
// XXX(aki): TODO: this needs to be updated to us
|
|
|
|
// dict<T, V> and then coalesce_cells needs to be updated
|
|
|
|
// but for now for the PoC this looks to be sufficient
|
2021-11-16 11:30:23 -06:00
|
|
|
std::unordered_map<std::string, std::vector<Cell*>> _cells{};
|
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
bool _include_connections;
|
|
|
|
bool _include_attributes;
|
|
|
|
bool _include_properties;
|
|
|
|
|
2022-02-17 10:35:45 -06:00
|
|
|
string escape_string(string str) {
|
|
|
|
std::string newstr;
|
|
|
|
|
|
|
|
auto itr = str.begin();
|
|
|
|
|
|
|
|
for(; itr != str.end(); ++itr) {
|
|
|
|
switch (*itr) {
|
|
|
|
case '\\': {
|
2022-03-10 10:03:51 -06:00
|
|
|
newstr += "\\\\";
|
2022-02-17 10:35:45 -06:00
|
|
|
break;
|
|
|
|
} case '\n': {
|
|
|
|
newstr += "\\n";
|
|
|
|
break;
|
|
|
|
} case '\f': {
|
|
|
|
newstr += "\\f";
|
|
|
|
break;
|
|
|
|
} case '\t': {
|
|
|
|
newstr += "\\t";
|
|
|
|
break;
|
|
|
|
} case '\r': {
|
|
|
|
newstr += "\\r";
|
|
|
|
break;
|
|
|
|
} case '\"': {
|
|
|
|
newstr += "\\\"";
|
|
|
|
break;
|
|
|
|
} case '\b': {
|
|
|
|
newstr += "\\b";
|
|
|
|
break;
|
|
|
|
} default: {
|
|
|
|
newstr += *itr;
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 11:30:23 -06:00
|
|
|
}
|
2022-02-17 10:35:45 -06:00
|
|
|
|
|
|
|
return newstr;
|
2021-11-16 11:30:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX(aki): I know this is far from ideal but i'm out of spoons and cant focus so
|
|
|
|
// it'll have to do for now,
|
|
|
|
void coalesce_cells(Module* mod)
|
|
|
|
{
|
2022-03-10 10:05:04 -06:00
|
|
|
_cells.clear();
|
2021-11-16 11:30:23 -06:00
|
|
|
for (auto cell : mod->cells()) {
|
2022-02-17 10:35:45 -06:00
|
|
|
const auto cell_type = escape_string(RTLIL::unescape_id(cell->type));
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
if (_cells.find(cell_type) == _cells.end())
|
|
|
|
_cells.emplace(cell_type, std::vector<Cell*>());
|
|
|
|
|
|
|
|
_cells.at(cell_type).push_back(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
// XXX(aki): this is a lazy way to do this i know,,,
|
|
|
|
std::string gen_indent(const uint16_t level)
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
for (uint16_t i = 0; i <= level; ++i)
|
|
|
|
{
|
|
|
|
s << " ";
|
|
|
|
}
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2021-11-16 11:30:23 -06:00
|
|
|
public:
|
2022-02-10 10:34:43 -06:00
|
|
|
JnyWriter(std::ostream &f, bool use_selection, bool connections, bool attributes, bool properties) noexcept:
|
2022-02-24 08:54:03 -06:00
|
|
|
f(f), _use_selection(use_selection),
|
|
|
|
_include_connections(connections), _include_attributes(attributes), _include_properties(properties)
|
2022-02-10 10:34:43 -06:00
|
|
|
{ }
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void write_metadata(Design *design, uint16_t indent_level = 0)
|
2021-11-16 11:30:23 -06:00
|
|
|
{
|
|
|
|
log_assert(design != nullptr);
|
|
|
|
|
|
|
|
design->sort();
|
|
|
|
|
2021-12-03 12:43:11 -06:00
|
|
|
f << "{\n";
|
2022-02-17 10:35:45 -06:00
|
|
|
f << stringf(" \"generator\": \"%s\",\n", escape_string(yosys_version_str).c_str());
|
2021-11-16 11:30:23 -06:00
|
|
|
// XXX(aki): Replace this with a proper version info eventually:tm:
|
2021-12-03 12:43:11 -06:00
|
|
|
f << " \"version\": \"0.0.0\",\n";
|
2022-02-10 10:34:43 -06:00
|
|
|
|
|
|
|
f << " \"features\": [";
|
|
|
|
|
|
|
|
size_t fnum{0};
|
|
|
|
if (_include_connections) {
|
|
|
|
++fnum;
|
|
|
|
f << "\"connections\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_include_attributes) {
|
|
|
|
if (fnum > 0)
|
|
|
|
f << ", ";
|
|
|
|
++fnum;
|
|
|
|
f << "\"attributes\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_include_properties) {
|
|
|
|
if (fnum > 0)
|
|
|
|
f << ", ";
|
|
|
|
++fnum;
|
|
|
|
f << "\"properties\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
f << "],\n";
|
|
|
|
|
2021-12-03 12:43:11 -06:00
|
|
|
f << " \"modules\": [\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
bool first{true};
|
|
|
|
for (auto mod : _use_selection ? design->selected_modules() : design->modules()) {
|
|
|
|
if (!first)
|
2021-12-03 12:43:11 -06:00
|
|
|
f << ",\n";
|
2022-02-03 04:04:45 -06:00
|
|
|
write_module(mod, indent_level + 2);
|
2021-11-16 11:30:23 -06:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
f << "\n";
|
2021-12-03 12:43:11 -06:00
|
|
|
f << " ]\n";
|
|
|
|
f << "}\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:11:58 -06:00
|
|
|
void write_sigspec(const RTLIL::SigSpec& sig, uint16_t indent_level = 0) {
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
|
|
|
|
|
|
|
f << _indent << " {\n";
|
|
|
|
f << _indent << " \"width\": \"" << sig.size() << "\",\n";
|
|
|
|
f << _indent << " \"type\": \"";
|
|
|
|
|
|
|
|
if (sig.is_wire()) {
|
|
|
|
f << "wire";
|
|
|
|
} else if (sig.is_chunk()) {
|
|
|
|
f << "chunk";
|
|
|
|
} else if (sig.is_bit()) {
|
|
|
|
f << "bit";
|
|
|
|
} else {
|
|
|
|
f << "unknown";
|
|
|
|
}
|
|
|
|
f << "\",\n";
|
|
|
|
|
|
|
|
f << _indent << " \"const\": ";
|
|
|
|
if (sig.has_const()) {
|
|
|
|
f << "true";
|
|
|
|
} else {
|
|
|
|
f << "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
f << "\n";
|
|
|
|
|
|
|
|
f << _indent << " }";
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_mod_conn(const std::pair<RTLIL::SigSpec, RTLIL::SigSpec>& conn, uint16_t indent_level = 0) {
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
|
|
|
f << _indent << " {\n";
|
|
|
|
f << _indent << " \"signals\": [\n";
|
|
|
|
|
|
|
|
write_sigspec(conn.first, indent_level + 2);
|
|
|
|
f << ",\n";
|
|
|
|
write_sigspec(conn.second, indent_level + 2);
|
|
|
|
f << "\n";
|
|
|
|
|
|
|
|
f << _indent << " ]\n";
|
|
|
|
f << _indent << " }";
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_cell_conn(const std::pair<RTLIL::IdString, RTLIL::SigSpec>& sig, uint16_t indent_level = 0) {
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
|
|
|
f << _indent << " {\n";
|
2022-02-17 10:35:45 -06:00
|
|
|
f << _indent << " \"name\": \"" << escape_string(RTLIL::unescape_id(sig.first)) << "\",\n";
|
2022-02-17 07:11:58 -06:00
|
|
|
f << _indent << " \"signals\": [\n";
|
|
|
|
|
|
|
|
write_sigspec(sig.second, indent_level + 2);
|
|
|
|
f << "\n";
|
|
|
|
|
|
|
|
f << _indent << " ]\n";
|
|
|
|
f << _indent << " }";
|
|
|
|
}
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void write_module(Module* mod, uint16_t indent_level = 0) {
|
2021-11-16 11:30:23 -06:00
|
|
|
log_assert(mod != nullptr);
|
|
|
|
|
|
|
|
coalesce_cells(mod);
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
const auto _indent = gen_indent(indent_level);
|
|
|
|
|
|
|
|
f << _indent << "{\n";
|
2022-02-17 10:35:45 -06:00
|
|
|
f << stringf(" %s\"name\": \"%s\",\n", _indent.c_str(), escape_string(RTLIL::unescape_id(mod->name)).c_str());
|
2022-02-03 04:04:45 -06:00
|
|
|
f << _indent << " \"cell_sorts\": [\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
bool first_sort{true};
|
|
|
|
for (auto& sort : _cells) {
|
|
|
|
if (!first_sort)
|
2021-12-03 12:43:11 -06:00
|
|
|
f << ",\n";
|
2022-02-03 04:04:45 -06:00
|
|
|
write_cell_sort(sort, indent_level + 2);
|
2021-11-16 11:30:23 -06:00
|
|
|
first_sort = false;
|
|
|
|
}
|
2021-12-03 12:43:11 -06:00
|
|
|
f << "\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
f << _indent << " ]";
|
|
|
|
if (_include_connections) {
|
2022-02-17 07:11:58 -06:00
|
|
|
f << ",\n" << _indent << " \"connections\": [\n";
|
|
|
|
|
|
|
|
bool first_conn{true};
|
|
|
|
for (const auto& conn : mod->connections()) {
|
|
|
|
if (!first_conn)
|
|
|
|
f << ",\n";
|
|
|
|
|
|
|
|
write_mod_conn(conn, indent_level + 2);
|
|
|
|
|
|
|
|
first_conn = false;
|
|
|
|
}
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
f << _indent << " ]";
|
|
|
|
}
|
|
|
|
if (_include_attributes) {
|
2022-02-17 07:11:58 -06:00
|
|
|
f << ",\n" << _indent << " \"attributes\": {\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
write_prams(mod->attributes, indent_level + 2);
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
f << "\n";
|
|
|
|
f << _indent << " }";
|
|
|
|
}
|
|
|
|
f << "\n" << _indent << "}";
|
2022-02-03 04:04:45 -06:00
|
|
|
}
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void write_cell_ports(RTLIL::Cell* port_cell, uint64_t indent_level = 0) {
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
bool first_port{true};
|
|
|
|
for (auto con : port_cell->connections()) {
|
|
|
|
if (!first_port)
|
2021-12-03 12:43:11 -06:00
|
|
|
f << ",\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
f << _indent << " {\n";
|
2022-02-17 10:35:45 -06:00
|
|
|
f << stringf(" %s\"name\": \"%s\",\n", _indent.c_str(), escape_string(RTLIL::unescape_id(con.first)).c_str());
|
2022-02-03 04:04:45 -06:00
|
|
|
f << _indent << " \"direction\": \"";
|
2021-11-16 11:30:23 -06:00
|
|
|
if (port_cell->input(con.first))
|
2021-12-03 12:43:11 -06:00
|
|
|
f << "i";
|
2021-11-16 11:30:23 -06:00
|
|
|
if (port_cell->input(con.first))
|
2021-12-03 12:43:11 -06:00
|
|
|
f << "o";
|
|
|
|
f << "\",\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
if (con.second.size() == 1)
|
2022-02-03 04:04:45 -06:00
|
|
|
f << _indent << " \"range\": [0, 0]\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
else
|
2022-02-03 04:04:45 -06:00
|
|
|
f << stringf(" %s\"range\": [%d, %d]\n", _indent.c_str(), con.second.size(), 0);
|
|
|
|
f << _indent << " }";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
first_port = false;
|
|
|
|
}
|
2021-12-03 12:43:11 -06:00
|
|
|
f << "\n";
|
2022-02-03 04:04:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void write_cell_sort(std::pair<const std::string, std::vector<Cell*>>& sort, uint16_t indent_level = 0) {
|
|
|
|
const auto port_cell = sort.second.front();
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
|
|
|
|
|
|
|
f << _indent << "{\n";
|
2022-03-10 10:04:44 -06:00
|
|
|
f << stringf(" %s\"type\": \"%s\",\n", _indent.c_str(), sort.first.c_str());
|
2022-02-03 04:04:45 -06:00
|
|
|
f << _indent << " \"ports\": [\n";
|
|
|
|
|
|
|
|
write_cell_ports(port_cell, indent_level + 2);
|
|
|
|
|
|
|
|
f << _indent << " ],\n" << _indent << " \"cells\": [\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
bool first_cell{true};
|
|
|
|
for (auto& cell : sort.second) {
|
|
|
|
if (!first_cell)
|
2021-12-03 12:43:11 -06:00
|
|
|
f << ",\n";
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
write_cell(cell, indent_level + 2);
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
first_cell = false;
|
|
|
|
}
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2021-12-03 12:43:11 -06:00
|
|
|
f << "\n";
|
2022-02-03 04:04:45 -06:00
|
|
|
f << _indent << " ]\n";
|
|
|
|
f << _indent << "}";
|
2021-12-03 12:43:11 -06:00
|
|
|
}
|
2021-12-03 12:44:09 -06:00
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void write_param_val(const Const& v) {
|
2021-12-03 12:44:09 -06:00
|
|
|
if ((v.flags & RTLIL::ConstFlags::CONST_FLAG_STRING) == RTLIL::ConstFlags::CONST_FLAG_STRING) {
|
|
|
|
const auto str = v.decode_string();
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
// XXX(aki): TODO, uh, yeah
|
2021-12-03 12:44:09 -06:00
|
|
|
|
2022-02-17 10:35:45 -06:00
|
|
|
f << "\"" << escape_string(str) << "\"";
|
2021-12-03 12:44:09 -06:00
|
|
|
} else if ((v.flags & RTLIL::ConstFlags::CONST_FLAG_SIGNED) == RTLIL::ConstFlags::CONST_FLAG_SIGNED) {
|
2022-02-17 07:15:48 -06:00
|
|
|
f << stringf("\"%dsd %d\"", v.size(), v.as_int(true));
|
2021-12-03 12:44:09 -06:00
|
|
|
} else if ((v.flags & RTLIL::ConstFlags::CONST_FLAG_REAL) == RTLIL::ConstFlags::CONST_FLAG_REAL) {
|
|
|
|
|
|
|
|
} else {
|
2022-02-17 10:35:45 -06:00
|
|
|
f << "\"" << escape_string(v.as_string()) << "\"";
|
2021-12-03 12:44:09 -06:00
|
|
|
}
|
2021-11-16 11:30:23 -06:00
|
|
|
}
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void write_prams(dict<RTLIL::IdString, RTLIL::Const>& params, uint16_t indent_level = 0) {
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
2021-11-16 14:25:14 -06:00
|
|
|
|
|
|
|
bool first_param{true};
|
2022-02-03 04:04:45 -06:00
|
|
|
for (auto& param : params) {
|
2021-11-16 14:25:14 -06:00
|
|
|
if (!first_param)
|
|
|
|
f << stringf(",\n");
|
2021-11-18 06:35:14 -06:00
|
|
|
const auto param_val = param.second;
|
2021-12-03 12:44:09 -06:00
|
|
|
if (!param_val.empty()) {
|
2022-02-17 10:35:45 -06:00
|
|
|
f << stringf(" %s\"%s\": ", _indent.c_str(), escape_string(RTLIL::unescape_id(param.first)).c_str());
|
2021-12-03 12:44:09 -06:00
|
|
|
write_param_val(param_val);
|
|
|
|
} else {
|
2022-02-17 10:35:45 -06:00
|
|
|
f << stringf(" %s\"%s\": true", _indent.c_str(), escape_string(RTLIL::unescape_id(param.first)).c_str());
|
2021-12-03 12:44:09 -06:00
|
|
|
}
|
2021-11-16 14:25:14 -06:00
|
|
|
|
|
|
|
first_param = false;
|
|
|
|
}
|
2022-02-03 04:04:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_cell(Cell* cell, uint16_t indent_level = 0) {
|
|
|
|
const auto _indent = gen_indent(indent_level);
|
|
|
|
log_assert(cell != nullptr);
|
|
|
|
|
|
|
|
f << _indent << " {\n";
|
2022-02-17 10:35:45 -06:00
|
|
|
f << stringf(" %s\"name\": \"%s\"", _indent.c_str(), escape_string(RTLIL::unescape_id(cell->name)).c_str());
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2022-02-17 07:11:58 -06:00
|
|
|
if (_include_connections) {
|
|
|
|
f << ",\n" << _indent << " \"connections\": [\n";
|
|
|
|
|
|
|
|
bool first_conn{true};
|
|
|
|
for (const auto& conn : cell->connections()) {
|
|
|
|
if (!first_conn)
|
|
|
|
f << ",\n";
|
|
|
|
|
|
|
|
write_cell_conn(conn, indent_level + 2);
|
|
|
|
|
|
|
|
first_conn = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
f << "\n";
|
|
|
|
f << _indent << " ]";
|
|
|
|
}
|
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
if (_include_attributes) {
|
2022-02-17 07:11:58 -06:00
|
|
|
f << ",\n" << _indent << " \"attributes\": {\n";
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
write_prams(cell->attributes, indent_level + 2);
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
f << "\n";
|
|
|
|
f << _indent << " }";
|
|
|
|
}
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
if (_include_properties) {
|
2022-02-17 07:11:58 -06:00
|
|
|
f << ",\n" << _indent << " \"parameters\": {\n";
|
2022-02-03 04:04:45 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
write_prams(cell->parameters, indent_level + 2);
|
|
|
|
|
|
|
|
f << "\n";
|
|
|
|
f << _indent << " }";
|
|
|
|
}
|
2021-11-16 14:25:14 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
f << "\n" << _indent << " }";
|
2021-11-16 11:30:23 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-14 08:41:52 -06:00
|
|
|
struct JnyBackend : public Backend {
|
|
|
|
JnyBackend() : Backend("jny", "generate design metadata") { }
|
2022-02-03 04:04:45 -06:00
|
|
|
void help() override {
|
2022-02-24 09:39:30 -06:00
|
|
|
// XXX(aki): TODO: explicitly document the JSON schema
|
2021-11-16 11:30:23 -06:00
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2022-01-14 08:41:52 -06:00
|
|
|
log(" jny [options] [selection]\n");
|
2021-11-16 11:30:23 -06:00
|
|
|
log("\n");
|
2022-03-17 06:26:14 -05:00
|
|
|
log(" -no-connections\n");
|
|
|
|
log(" Don't include connection information in the netlist output.\n");
|
2022-02-10 10:34:43 -06:00
|
|
|
log("\n");
|
2022-03-17 06:26:14 -05:00
|
|
|
log(" -no-attributes\n");
|
|
|
|
log(" Don't include attributed information in the netlist output.\n");
|
2022-02-10 10:34:43 -06:00
|
|
|
log("\n");
|
2022-03-17 06:26:14 -05:00
|
|
|
log(" -no-properties\n");
|
|
|
|
log(" Don't include property information in the netlist output.\n");
|
2022-02-10 10:34:43 -06:00
|
|
|
log("\n");
|
2021-11-16 11:30:23 -06:00
|
|
|
log("Write a JSON metadata for the current design\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override {
|
2022-02-10 10:34:43 -06:00
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
bool connections{true};
|
|
|
|
bool attributes{true};
|
|
|
|
bool properties{true};
|
2022-02-10 10:34:43 -06:00
|
|
|
|
2021-11-16 11:30:23 -06:00
|
|
|
size_t argidx{1};
|
2022-02-10 10:34:43 -06:00
|
|
|
for (; argidx < args.size(); argidx++) {
|
2022-03-17 06:26:14 -05:00
|
|
|
if (args[argidx] == "-no-connections") {
|
|
|
|
connections = false;
|
2022-02-10 10:34:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
if (args[argidx] == "-no-attributes") {
|
|
|
|
attributes = false;
|
2022-02-10 10:34:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
if (args[argidx] == "-no-properties") {
|
|
|
|
properties = false;
|
2022-02-10 10:34:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2021-11-16 11:30:23 -06:00
|
|
|
extra_args(f, filename, args, argidx);
|
|
|
|
|
2022-01-14 08:41:52 -06:00
|
|
|
log_header(design, "Executing jny backend.\n");
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
JnyWriter jny_writer(*f, false, connections, attributes, properties);
|
2022-01-14 08:41:52 -06:00
|
|
|
jny_writer.write_metadata(design);
|
2021-11-16 11:30:23 -06:00
|
|
|
}
|
|
|
|
|
2022-01-14 08:41:52 -06:00
|
|
|
} JnyBackend;
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
|
2022-01-14 08:41:52 -06:00
|
|
|
struct JnyPass : public Pass {
|
|
|
|
JnyPass() : Pass("jny", "write design and metadata") { }
|
2021-11-16 11:30:23 -06:00
|
|
|
|
2022-02-03 04:04:45 -06:00
|
|
|
void help() override {
|
2021-11-16 11:30:23 -06:00
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2022-01-14 08:41:52 -06:00
|
|
|
log(" jny [options] [selection]\n");
|
2021-11-16 11:30:23 -06:00
|
|
|
log("\n");
|
2022-01-14 08:41:52 -06:00
|
|
|
log("Write a JSON netlist metadata for the current design\n");
|
2021-11-16 11:30:23 -06:00
|
|
|
log("\n");
|
|
|
|
log(" -o <filename>\n");
|
|
|
|
log(" write to the specified file.\n");
|
|
|
|
log("\n");
|
2022-03-17 06:26:14 -05:00
|
|
|
log(" -no-connections\n");
|
|
|
|
log(" Don't include connection information in the netlist output.\n");
|
2022-02-10 10:34:43 -06:00
|
|
|
log("\n");
|
2022-03-17 06:26:14 -05:00
|
|
|
log(" -no-attributes\n");
|
|
|
|
log(" Don't include attributed information in the netlist output.\n");
|
2022-02-10 10:34:43 -06:00
|
|
|
log("\n");
|
2022-03-17 06:26:14 -05:00
|
|
|
log(" -no-properties\n");
|
|
|
|
log(" Don't include property information in the netlist output.\n");
|
2022-02-10 10:34:43 -06:00
|
|
|
log("\n");
|
2022-01-14 08:41:52 -06:00
|
|
|
log("See 'help write_jny' for a description of the JSON format used.\n");
|
2021-11-16 11:30:23 -06:00
|
|
|
log("\n");
|
|
|
|
}
|
2022-02-03 04:04:45 -06:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override {
|
2021-11-16 11:30:23 -06:00
|
|
|
std::string filename{};
|
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
bool connections{true};
|
|
|
|
bool attributes{true};
|
|
|
|
bool properties{true};
|
2022-02-10 10:34:43 -06:00
|
|
|
|
|
|
|
size_t argidx{1};
|
|
|
|
for (; argidx < args.size(); argidx++) {
|
2021-11-16 11:30:23 -06:00
|
|
|
if (args[argidx] == "-o" && argidx+1 < args.size()) {
|
|
|
|
filename = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-10 10:34:43 -06:00
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
if (args[argidx] == "-no-connections") {
|
|
|
|
connections = false;
|
2022-02-10 10:34:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
if (args[argidx] == "-no-attributes") {
|
|
|
|
attributes = false;
|
2022-02-10 10:34:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-17 06:26:14 -05:00
|
|
|
if (args[argidx] == "-no-properties") {
|
|
|
|
properties = false;
|
2022-02-10 10:34:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-16 11:30:23 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
std::ostream *f;
|
|
|
|
std::stringstream buf;
|
|
|
|
|
|
|
|
if (!filename.empty()) {
|
|
|
|
rewrite_filename(filename);
|
|
|
|
std::ofstream *ff = new std::ofstream;
|
|
|
|
ff->open(filename.c_str(), std::ofstream::trunc);
|
|
|
|
if (ff->fail()) {
|
|
|
|
delete ff;
|
|
|
|
log_error("Can't open file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
|
|
|
|
}
|
|
|
|
f = ff;
|
|
|
|
} else {
|
|
|
|
f = &buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-10 10:34:43 -06:00
|
|
|
JnyWriter jny_writer(*f, false, connections, attributes, properties);
|
2022-01-14 08:41:52 -06:00
|
|
|
jny_writer.write_metadata(design);
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
if (!filename.empty()) {
|
|
|
|
delete f;
|
|
|
|
} else {
|
|
|
|
log("%s", buf.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:41:52 -06:00
|
|
|
} JnyPass;
|
2021-11-16 11:30:23 -06:00
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|