2015-03-02 16:30:58 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2015-03-02 16:30:58 -06: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
|
|
|
*
|
2015-03-02 16:30:58 -06: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"
|
2015-06-10 01:13:56 -05:00
|
|
|
#include "kernel/cellaigs.h"
|
2015-03-02 16:30:58 -06:00
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct JsonWriter
|
|
|
|
{
|
|
|
|
std::ostream &f;
|
|
|
|
bool use_selection;
|
2015-06-10 01:13:56 -05:00
|
|
|
bool aig_mode;
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
bool compat_int_mode;
|
2015-03-02 16:30:58 -06:00
|
|
|
|
|
|
|
Design *design;
|
|
|
|
Module *module;
|
|
|
|
|
|
|
|
SigMap sigmap;
|
|
|
|
int sigidcounter;
|
|
|
|
dict<SigBit, string> sigids;
|
2015-06-10 01:13:56 -05:00
|
|
|
pool<Aig> aig_models;
|
2015-03-02 16:30:58 -06:00
|
|
|
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode) :
|
|
|
|
f(f), use_selection(use_selection), aig_mode(aig_mode),
|
|
|
|
compat_int_mode(compat_int_mode) { }
|
2015-03-02 16:30:58 -06:00
|
|
|
|
|
|
|
string get_string(string str)
|
|
|
|
{
|
2015-03-03 02:28:44 -06:00
|
|
|
string newstr = "\"";
|
|
|
|
for (char c : str) {
|
|
|
|
if (c == '\\')
|
|
|
|
newstr += c;
|
|
|
|
newstr += c;
|
|
|
|
}
|
|
|
|
return newstr + "\"";
|
2015-03-02 16:30:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
string get_name(IdString name)
|
|
|
|
{
|
|
|
|
return get_string(RTLIL::unescape_id(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
string get_bits(SigSpec sig)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
string str = "[";
|
|
|
|
for (auto bit : sigmap(sig)) {
|
|
|
|
str += first ? " " : ", ";
|
|
|
|
first = false;
|
|
|
|
if (sigids.count(bit) == 0) {
|
|
|
|
string &s = sigids[bit];
|
|
|
|
if (bit.wire == nullptr) {
|
|
|
|
if (bit == State::S0) s = "\"0\"";
|
2015-03-03 02:41:41 -06:00
|
|
|
else if (bit == State::S1) s = "\"1\"";
|
2015-03-02 16:30:58 -06:00
|
|
|
else if (bit == State::Sz) s = "\"z\"";
|
|
|
|
else s = "\"x\"";
|
|
|
|
} else
|
|
|
|
s = stringf("%d", sigidcounter++);
|
|
|
|
}
|
|
|
|
str += sigids[bit];
|
|
|
|
}
|
|
|
|
return str + " ]";
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:34:52 -05:00
|
|
|
void write_parameter_value(const Const &value)
|
|
|
|
{
|
|
|
|
if ((value.flags & RTLIL::ConstFlags::CONST_FLAG_STRING) != 0) {
|
|
|
|
string str = value.decode_string();
|
|
|
|
int state = 0;
|
|
|
|
for (char c : str) {
|
|
|
|
if (state == 0) {
|
|
|
|
if (c == '0' || c == '1' || c == 'x' || c == 'z')
|
|
|
|
state = 0;
|
|
|
|
else if (c == ' ')
|
|
|
|
state = 1;
|
|
|
|
else
|
|
|
|
state = 2;
|
|
|
|
} else if (state == 1 && c != ' ')
|
|
|
|
state = 2;
|
|
|
|
}
|
|
|
|
if (state < 2)
|
|
|
|
str += " ";
|
|
|
|
f << get_string(str);
|
2020-02-09 03:01:18 -06:00
|
|
|
} else if (compat_int_mode && GetSize(value) <= 32 && value.is_fully_def()) {
|
2019-08-01 05:34:52 -05:00
|
|
|
if ((value.flags & RTLIL::ConstFlags::CONST_FLAG_SIGNED) != 0)
|
|
|
|
f << stringf("%d", value.as_int());
|
|
|
|
else
|
|
|
|
f << stringf("%u", value.as_int());
|
|
|
|
} else {
|
|
|
|
f << get_string(value.as_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 01:32:04 -05:00
|
|
|
void write_parameters(const dict<IdString, Const> ¶meters, bool for_module=false)
|
2015-03-06 03:21:21 -06:00
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
for (auto ¶m : parameters) {
|
|
|
|
f << stringf("%s\n", first ? "" : ",");
|
2016-07-12 01:32:04 -05:00
|
|
|
f << stringf(" %s%s: ", for_module ? "" : " ", get_name(param.first).c_str());
|
2019-08-01 05:34:52 -05:00
|
|
|
write_parameter_value(param.second);
|
2015-03-06 03:21:21 -06:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:30:58 -06:00
|
|
|
void write_module(Module *module_)
|
|
|
|
{
|
|
|
|
module = module_;
|
|
|
|
log_assert(module->design == design);
|
|
|
|
sigmap.set(module);
|
|
|
|
sigids.clear();
|
|
|
|
|
|
|
|
// reserve 0 and 1 to avoid confusion with "0" and "1"
|
|
|
|
sigidcounter = 2;
|
|
|
|
|
2021-03-09 13:42:14 -06:00
|
|
|
if (module->has_processes()) {
|
2021-03-23 08:47:32 -05:00
|
|
|
log_error("Module %s contains processes, which are not supported by JSON backend (run `proc` first).\n", log_id(module));
|
2021-03-09 13:42:14 -06:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" %s: {\n", get_name(module->name).c_str());
|
|
|
|
|
2016-07-12 01:32:04 -05:00
|
|
|
f << stringf(" \"attributes\": {");
|
|
|
|
write_parameters(module->attributes, /*for_module=*/true);
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
|
2020-04-16 08:57:03 -05:00
|
|
|
if (module->parameter_default_values.size()) {
|
|
|
|
f << stringf(" \"parameter_default_values\": {");
|
|
|
|
write_parameters(module->parameter_default_values, /*for_module=*/true);
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" \"ports\": {");
|
|
|
|
bool first = true;
|
|
|
|
for (auto n : module->ports) {
|
|
|
|
Wire *w = module->wire(n);
|
|
|
|
if (use_selection && !module->selected(w))
|
|
|
|
continue;
|
|
|
|
f << stringf("%s\n", first ? "" : ",");
|
|
|
|
f << stringf(" %s: {\n", get_name(n).c_str());
|
|
|
|
f << stringf(" \"direction\": \"%s\",\n", w->port_input ? w->port_output ? "inout" : "input" : "output");
|
2019-06-21 12:47:25 -05:00
|
|
|
if (w->start_offset)
|
|
|
|
f << stringf(" \"offset\": %d,\n", w->start_offset);
|
|
|
|
if (w->upto)
|
|
|
|
f << stringf(" \"upto\": 1,\n");
|
2020-04-27 11:44:24 -05:00
|
|
|
if (w->is_signed)
|
|
|
|
f << stringf(" \"signed\": %d,\n", w->is_signed);
|
2019-06-21 13:01:40 -05:00
|
|
|
f << stringf(" \"bits\": %s\n", get_bits(w).c_str());
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" }");
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
|
|
|
|
f << stringf(" \"cells\": {");
|
|
|
|
first = true;
|
|
|
|
for (auto c : module->cells()) {
|
|
|
|
if (use_selection && !module->selected(c))
|
|
|
|
continue;
|
|
|
|
f << stringf("%s\n", first ? "" : ",");
|
|
|
|
f << stringf(" %s: {\n", get_name(c->name).c_str());
|
|
|
|
f << stringf(" \"hide_name\": %s,\n", c->name[0] == '$' ? "1" : "0");
|
|
|
|
f << stringf(" \"type\": %s,\n", get_name(c->type).c_str());
|
2015-06-10 01:13:56 -05:00
|
|
|
if (aig_mode) {
|
|
|
|
Aig aig(c);
|
|
|
|
if (!aig.name.empty()) {
|
|
|
|
f << stringf(" \"model\": \"%s\",\n", aig.name.c_str());
|
|
|
|
aig_models.insert(aig);
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" \"parameters\": {");
|
2015-03-06 03:21:21 -06:00
|
|
|
write_parameters(c->parameters);
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
f << stringf(" \"attributes\": {");
|
|
|
|
write_parameters(c->attributes);
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf("\n },\n");
|
2015-04-05 18:49:58 -05:00
|
|
|
if (c->known()) {
|
|
|
|
f << stringf(" \"port_directions\": {");
|
|
|
|
bool first2 = true;
|
|
|
|
for (auto &conn : c->connections()) {
|
|
|
|
string direction = "output";
|
|
|
|
if (c->input(conn.first))
|
|
|
|
direction = c->output(conn.first) ? "inout" : "input";
|
|
|
|
f << stringf("%s\n", first2 ? "" : ",");
|
|
|
|
f << stringf(" %s: \"%s\"", get_name(conn.first).c_str(), direction.c_str());
|
|
|
|
first2 = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
}
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" \"connections\": {");
|
2015-03-06 03:21:21 -06:00
|
|
|
bool first2 = true;
|
2015-03-02 16:30:58 -06:00
|
|
|
for (auto &conn : c->connections()) {
|
|
|
|
f << stringf("%s\n", first2 ? "" : ",");
|
|
|
|
f << stringf(" %s: %s", get_name(conn.first).c_str(), get_bits(conn.second).c_str());
|
|
|
|
first2 = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n }\n");
|
|
|
|
f << stringf(" }");
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
|
2021-03-09 13:42:14 -06:00
|
|
|
if (!module->memories.empty()) {
|
|
|
|
f << stringf(" \"memories\": {");
|
|
|
|
first = true;
|
|
|
|
for (auto &it : module->memories) {
|
|
|
|
if (use_selection && !module->selected(it.second))
|
|
|
|
continue;
|
|
|
|
f << stringf("%s\n", first ? "" : ",");
|
|
|
|
f << stringf(" %s: {\n", get_name(it.second->name).c_str());
|
|
|
|
f << stringf(" \"hide_name\": %s,\n", it.second->name[0] == '$' ? "1" : "0");
|
|
|
|
f << stringf(" \"attributes\": {");
|
|
|
|
write_parameters(it.second->attributes);
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
f << stringf(" \"width\": %d,\n", it.second->width);
|
|
|
|
f << stringf(" \"start_offset\": %d,\n", it.second->start_offset);
|
|
|
|
f << stringf(" \"size\": %d\n", it.second->size);
|
|
|
|
f << stringf(" }");
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n },\n");
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" \"netnames\": {");
|
|
|
|
first = true;
|
|
|
|
for (auto w : module->wires()) {
|
|
|
|
if (use_selection && !module->selected(w))
|
|
|
|
continue;
|
|
|
|
f << stringf("%s\n", first ? "" : ",");
|
|
|
|
f << stringf(" %s: {\n", get_name(w->name).c_str());
|
|
|
|
f << stringf(" \"hide_name\": %s,\n", w->name[0] == '$' ? "1" : "0");
|
2015-03-06 03:21:21 -06:00
|
|
|
f << stringf(" \"bits\": %s,\n", get_bits(w).c_str());
|
2019-06-21 08:22:17 -05:00
|
|
|
if (w->start_offset)
|
|
|
|
f << stringf(" \"offset\": %d,\n", w->start_offset);
|
|
|
|
if (w->upto)
|
|
|
|
f << stringf(" \"upto\": 1,\n");
|
2020-04-27 11:44:24 -05:00
|
|
|
if (w->is_signed)
|
|
|
|
f << stringf(" \"signed\": %d,\n", w->is_signed);
|
2015-03-06 03:21:21 -06:00
|
|
|
f << stringf(" \"attributes\": {");
|
|
|
|
write_parameters(w->attributes);
|
|
|
|
f << stringf("\n }\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
f << stringf(" }");
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n }\n");
|
|
|
|
|
|
|
|
f << stringf(" }");
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_design(Design *design_)
|
|
|
|
{
|
|
|
|
design = design_;
|
2017-07-10 06:17:38 -05:00
|
|
|
design->sort();
|
|
|
|
|
2015-03-03 02:28:44 -06:00
|
|
|
f << stringf("{\n");
|
|
|
|
f << stringf(" \"creator\": %s,\n", get_string(yosys_version_str).c_str());
|
|
|
|
f << stringf(" \"modules\": {\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
vector<Module*> modules = use_selection ? design->selected_modules() : design->modules();
|
|
|
|
bool first_module = true;
|
|
|
|
for (auto mod : modules) {
|
|
|
|
if (!first_module)
|
|
|
|
f << stringf(",\n");
|
|
|
|
write_module(mod);
|
|
|
|
first_module = false;
|
|
|
|
}
|
2015-06-10 01:13:56 -05:00
|
|
|
f << stringf("\n }");
|
|
|
|
if (!aig_models.empty()) {
|
|
|
|
f << stringf(",\n \"models\": {\n");
|
|
|
|
bool first_model = true;
|
|
|
|
for (auto &aig : aig_models) {
|
|
|
|
if (!first_model)
|
|
|
|
f << stringf(",\n");
|
|
|
|
f << stringf(" \"%s\": [\n", aig.name.c_str());
|
2015-06-11 03:48:16 -05:00
|
|
|
int node_idx = 0;
|
2015-06-10 01:13:56 -05:00
|
|
|
for (auto &node : aig.nodes) {
|
2015-06-11 03:48:16 -05:00
|
|
|
if (node_idx != 0)
|
2015-06-10 01:13:56 -05:00
|
|
|
f << stringf(",\n");
|
2015-06-11 03:48:16 -05:00
|
|
|
f << stringf(" /* %3d */ [ ", node_idx);
|
2015-06-10 01:13:56 -05:00
|
|
|
if (node.portbit >= 0)
|
|
|
|
f << stringf("\"%sport\", \"%s\", %d", node.inverter ? "n" : "",
|
|
|
|
log_id(node.portname), node.portbit);
|
|
|
|
else if (node.left_parent < 0 && node.right_parent < 0)
|
2015-06-10 16:00:12 -05:00
|
|
|
f << stringf("\"%s\"", node.inverter ? "true" : "false");
|
2015-06-10 01:13:56 -05:00
|
|
|
else
|
|
|
|
f << stringf("\"%s\", %d, %d", node.inverter ? "nand" : "and", node.left_parent, node.right_parent);
|
|
|
|
for (auto &op : node.outports)
|
|
|
|
f << stringf(", \"%s\", %d", log_id(op.first), op.second);
|
|
|
|
f << stringf(" ]");
|
2015-06-11 03:48:16 -05:00
|
|
|
node_idx++;
|
2015-06-10 01:13:56 -05:00
|
|
|
}
|
|
|
|
f << stringf("\n ]");
|
|
|
|
first_model = false;
|
|
|
|
}
|
|
|
|
f << stringf("\n }");
|
|
|
|
}
|
|
|
|
f << stringf("\n}\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JsonBackend : public Backend {
|
|
|
|
JsonBackend() : Backend("json", "write design to a JSON file") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2015-03-02 16:30:58 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" write_json [options] [filename]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Write a JSON netlist of the current design.\n");
|
|
|
|
log("\n");
|
2015-06-10 01:13:56 -05:00
|
|
|
log(" -aig\n");
|
2015-06-11 03:48:16 -05:00
|
|
|
log(" include AIG models for the different gate types\n");
|
2015-06-10 01:13:56 -05:00
|
|
|
log("\n");
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
log(" -compat-int\n");
|
2020-02-09 03:01:18 -06:00
|
|
|
log(" emit 32-bit or smaller fully-defined parameter values directly\n");
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
log(" as JSON numbers (for compatibility with old parsers)\n");
|
|
|
|
log("\n");
|
2015-06-10 01:13:56 -05:00
|
|
|
log("\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("The general syntax of the JSON output created by this command is as follows:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"creator\": \"Yosys <version info>\",\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"modules\": {\n");
|
|
|
|
log(" <module_name>: {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"attributes\": {\n");
|
|
|
|
log(" <attribute_name>: <attribute_value>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2020-04-16 08:57:03 -05:00
|
|
|
log(" \"parameter_default_values\": {\n");
|
|
|
|
log(" <parameter_name>: <parameter_value>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"ports\": {\n");
|
|
|
|
log(" <port_name>: <port_details>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" \"cells\": {\n");
|
|
|
|
log(" <cell_name>: <cell_details>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2021-03-09 13:42:14 -06:00
|
|
|
log(" \"memories\": {\n");
|
|
|
|
log(" <memory_name>: <memory_details>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"netnames\": {\n");
|
|
|
|
log(" <net_name>: <net_details>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
2015-06-11 03:48:16 -05:00
|
|
|
log(" },\n");
|
|
|
|
log(" \"models\": {\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" }\n");
|
|
|
|
log("\n");
|
|
|
|
log("Where <port_details> is:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" {\n");
|
|
|
|
log(" \"direction\": <\"input\" | \"output\" | \"inout\">,\n");
|
|
|
|
log(" \"bits\": <bit_vector>\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"offset\": <the lowest bit index in use, if non-0>\n");
|
|
|
|
log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" }\n");
|
|
|
|
log("\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log("The \"offset\" and \"upto\" fields are skipped if their value would be 0.");
|
|
|
|
log("They don't affect connection semantics, and are only used to preserve original");
|
|
|
|
log("HDL bit indexing.");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("And <cell_details> is:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" {\n");
|
|
|
|
log(" \"hide_name\": <1 | 0>,\n");
|
|
|
|
log(" \"type\": <cell_type>,\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"model\": <AIG model name, if -aig option used>,\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"parameters\": {\n");
|
|
|
|
log(" <parameter_name>: <parameter_value>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" \"attributes\": {\n");
|
|
|
|
log(" <attribute_name>: <attribute_value>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2015-04-05 18:49:58 -05:00
|
|
|
log(" \"port_directions\": {\n");
|
|
|
|
log(" <port_name>: <\"input\" | \"output\" | \"inout\">,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"connections\": {\n");
|
|
|
|
log(" <port_name>: <bit_vector>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" }\n");
|
|
|
|
log("\n");
|
2021-03-09 13:42:14 -06:00
|
|
|
log("And <memory_details> is:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" {\n");
|
|
|
|
log(" \"hide_name\": <1 | 0>,\n");
|
|
|
|
log(" \"attributes\": {\n");
|
|
|
|
log(" <attribute_name>: <attribute_value>,\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" \"width\": <memory width>\n");
|
|
|
|
log(" \"start_offset\": <the lowest valid memory address>\n");
|
|
|
|
log(" \"size\": <memory size>\n");
|
|
|
|
log(" }\n");
|
|
|
|
log("\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("And <net_details> is:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" {\n");
|
|
|
|
log(" \"hide_name\": <1 | 0>,\n");
|
|
|
|
log(" \"bits\": <bit_vector>\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"offset\": <the lowest bit index in use, if non-0>\n");
|
|
|
|
log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" }\n");
|
|
|
|
log("\n");
|
|
|
|
log("The \"hide_name\" fields are set to 1 when the name of this cell or net is\n");
|
|
|
|
log("automatically created and is likely not of interest for a regular user.\n");
|
|
|
|
log("\n");
|
2015-04-05 18:49:58 -05:00
|
|
|
log("The \"port_directions\" section is only included for cells for which the\n");
|
|
|
|
log("interface is known.\n");
|
|
|
|
log("\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("Module and cell ports and nets can be single bit wide or vectors of multiple\n");
|
|
|
|
log("bits. Each individual signal bit is assigned a unique integer. The <bit_vector>\n");
|
|
|
|
log("values referenced above are vectors of this integers. Signal bits that are\n");
|
2019-08-01 05:34:52 -05:00
|
|
|
log("connected to a constant driver are denoted as string \"0\", \"1\", \"x\", or\n");
|
|
|
|
log("\"z\" instead of a number.\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("\n");
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
log("Bit vectors (including integers) are written as string holding the binary");
|
|
|
|
log("representation of the value. Strings are written as strings, with an appended");
|
|
|
|
log("blank in cases of strings of the form /[01xz]* */.\n");
|
2016-12-29 05:13:29 -06:00
|
|
|
log("\n");
|
2015-08-14 15:23:01 -05:00
|
|
|
log("For example the following Verilog code:\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("\n");
|
|
|
|
log(" module test(input x, y);\n");
|
|
|
|
log(" (* keep *) foo #(.P(42), .Q(1337))\n");
|
|
|
|
log(" foo_inst (.A({x, y}), .B({y, x}), .C({4'd10, {4{x}}}));\n");
|
|
|
|
log(" endmodule\n");
|
|
|
|
log("\n");
|
|
|
|
log("Translates to the following JSON output:\n");
|
|
|
|
log("\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"creator\": \"Yosys 0.9+2406 (git sha1 fb1168d8, clang 9.0.1 -fPIC -Os)\",\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"modules\": {\n");
|
|
|
|
log(" \"test\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"attributes\": {\n");
|
|
|
|
log(" \"cells_not_processed\": \"00000000000000000000000000000001\",\n");
|
|
|
|
log(" \"src\": \"test.v:1.1-4.10\"\n");
|
|
|
|
log(" },\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"ports\": {\n");
|
|
|
|
log(" \"x\": {\n");
|
|
|
|
log(" \"direction\": \"input\",\n");
|
|
|
|
log(" \"bits\": [ 2 ]\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" \"y\": {\n");
|
|
|
|
log(" \"direction\": \"input\",\n");
|
|
|
|
log(" \"bits\": [ 3 ]\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" \"cells\": {\n");
|
|
|
|
log(" \"foo_inst\": {\n");
|
|
|
|
log(" \"hide_name\": 0,\n");
|
|
|
|
log(" \"type\": \"foo\",\n");
|
|
|
|
log(" \"parameters\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"P\": \"00000000000000000000000000101010\",\n");
|
|
|
|
log(" \"Q\": \"00000000000000000000010100111001\"\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" },\n");
|
|
|
|
log(" \"attributes\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"keep\": \"00000000000000000000000000000001\",\n");
|
|
|
|
log(" \"module_not_derived\": \"00000000000000000000000000000001\",\n");
|
|
|
|
log(" \"src\": \"test.v:3.1-3.55\"\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" },\n");
|
|
|
|
log(" \"connections\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"A\": [ 3, 2 ],\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"B\": [ 2, 3 ],\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"C\": [ 2, 2, 2, 2, \"0\", \"1\", \"0\", \"1\" ]\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" },\n");
|
|
|
|
log(" \"netnames\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"x\": {\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"hide_name\": 0,\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"bits\": [ 2 ],\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"attributes\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"src\": \"test.v:1.19-1.20\"\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" }\n");
|
|
|
|
log(" },\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"y\": {\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"hide_name\": 0,\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"bits\": [ 3 ],\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" \"attributes\": {\n");
|
2020-04-14 15:57:37 -05:00
|
|
|
log(" \"src\": \"test.v:1.22-1.23\"\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
|
|
|
log(" }\n");
|
|
|
|
log("\n");
|
2015-06-11 03:48:16 -05:00
|
|
|
log("The models are given as And-Inverter-Graphs (AIGs) in the following form:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" \"models\": {\n");
|
|
|
|
log(" <model_name>: [\n");
|
|
|
|
log(" /* 0 */ [ <node-spec> ],\n");
|
|
|
|
log(" /* 1 */ [ <node-spec> ],\n");
|
|
|
|
log(" /* 2 */ [ <node-spec> ],\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" ],\n");
|
|
|
|
log(" ...\n");
|
|
|
|
log(" },\n");
|
|
|
|
log("\n");
|
|
|
|
log("The following node-types may be used:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" [ \"port\", <portname>, <bitindex>, <out-list> ]\n");
|
|
|
|
log(" - the value of the specified input port bit\n");
|
|
|
|
log("\n");
|
|
|
|
log(" [ \"nport\", <portname>, <bitindex>, <out-list> ]\n");
|
|
|
|
log(" - the inverted value of the specified input port bit\n");
|
|
|
|
log("\n");
|
|
|
|
log(" [ \"and\", <node-index>, <node-index>, <out-list> ]\n");
|
2016-02-28 16:14:01 -06:00
|
|
|
log(" - the ANDed value of the specified nodes\n");
|
2015-06-11 03:48:16 -05:00
|
|
|
log("\n");
|
|
|
|
log(" [ \"nand\", <node-index>, <node-index>, <out-list> ]\n");
|
2016-02-28 16:14:01 -06:00
|
|
|
log(" - the inverted ANDed value of the specified nodes\n");
|
2015-06-11 03:48:16 -05:00
|
|
|
log("\n");
|
|
|
|
log(" [ \"true\", <out-list> ]\n");
|
|
|
|
log(" - the constant value 1\n");
|
|
|
|
log("\n");
|
|
|
|
log(" [ \"false\", <out-list> ]\n");
|
|
|
|
log(" - the constant value 0\n");
|
|
|
|
log("\n");
|
|
|
|
log("All nodes appear in topological order. I.e. only nodes with smaller indices\n");
|
|
|
|
log("are referenced by \"and\" and \"nand\" nodes.\n");
|
|
|
|
log("\n");
|
|
|
|
log("The optional <out-list> at the end of a node specification is a list of\n");
|
|
|
|
log("output portname and bitindex pairs, specifying the outputs driven by this node.\n");
|
|
|
|
log("\n");
|
|
|
|
log("For example, the following is the model for a 3-input 3-output $reduce_and cell\n");
|
|
|
|
log("inferred by the following code:\n");
|
|
|
|
log("\n");
|
|
|
|
log(" module test(input [2:0] in, output [2:0] out);\n");
|
|
|
|
log(" assign in = &out;\n");
|
|
|
|
log(" endmodule\n");
|
|
|
|
log("\n");
|
|
|
|
log(" \"$reduce_and:3U:3\": [\n");
|
|
|
|
log(" /* 0 */ [ \"port\", \"A\", 0 ],\n");
|
|
|
|
log(" /* 1 */ [ \"port\", \"A\", 1 ],\n");
|
|
|
|
log(" /* 2 */ [ \"and\", 0, 1 ],\n");
|
|
|
|
log(" /* 3 */ [ \"port\", \"A\", 2 ],\n");
|
|
|
|
log(" /* 4 */ [ \"and\", 2, 3, \"Y\", 0 ],\n");
|
|
|
|
log(" /* 5 */ [ \"false\", \"Y\", 1, \"Y\", 2 ]\n");
|
|
|
|
log(" ]\n");
|
|
|
|
log("\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("Future version of Yosys might add support for additional fields in the JSON\n");
|
2016-02-28 16:14:01 -06:00
|
|
|
log("format. A program processing this format must ignore all unknown fields.\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
}
|
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
|
2015-03-02 16:30:58 -06:00
|
|
|
{
|
2015-06-10 01:13:56 -05:00
|
|
|
bool aig_mode = false;
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
bool compat_int_mode = false;
|
2015-06-10 01:13:56 -05:00
|
|
|
|
2015-03-02 16:30:58 -06:00
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
2015-06-10 01:13:56 -05:00
|
|
|
if (args[argidx] == "-aig") {
|
|
|
|
aig_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
if (args[argidx] == "-compat-int") {
|
|
|
|
compat_int_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-02 16:30:58 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(f, filename, args, argidx);
|
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing JSON backend.\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
JsonWriter json_writer(*f, false, aig_mode, compat_int_mode);
|
2015-03-02 16:30:58 -06:00
|
|
|
json_writer.write_design(design);
|
|
|
|
}
|
|
|
|
} JsonBackend;
|
|
|
|
|
|
|
|
struct JsonPass : public Pass {
|
2015-03-06 03:21:21 -06:00
|
|
|
JsonPass() : Pass("json", "write design in JSON format") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2015-03-02 16:30:58 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log(" json [options] [selection]\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
log("\n");
|
|
|
|
log("Write a JSON netlist of all selected objects.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -o <filename>\n");
|
|
|
|
log(" write to the specified file.\n");
|
|
|
|
log("\n");
|
2015-06-10 01:13:56 -05:00
|
|
|
log(" -aig\n");
|
|
|
|
log(" also include AIG models for the different gate types\n");
|
|
|
|
log("\n");
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
log(" -compat-int\n");
|
2020-02-09 03:01:18 -06:00
|
|
|
log(" emit 32-bit or smaller fully-defined parameter values directly\n");
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
log(" as JSON numbers (for compatibility with old parsers)\n");
|
|
|
|
log("\n");
|
2015-03-06 03:21:21 -06:00
|
|
|
log("See 'help write_json' for a description of the JSON format used.\n");
|
|
|
|
log("\n");
|
2015-03-02 16:30:58 -06:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2015-03-02 16:30:58 -06:00
|
|
|
{
|
|
|
|
std::string filename;
|
2015-06-10 01:13:56 -05:00
|
|
|
bool aig_mode = false;
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
bool compat_int_mode = false;
|
2015-03-02 16:30:58 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-o" && argidx+1 < args.size()) {
|
|
|
|
filename = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-10 01:13:56 -05:00
|
|
|
if (args[argidx] == "-aig") {
|
|
|
|
aig_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
if (args[argidx] == "-compat-int") {
|
|
|
|
compat_int_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-02 16:30:58 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
std::ostream *f;
|
|
|
|
std::stringstream buf;
|
|
|
|
|
|
|
|
if (!filename.empty()) {
|
2019-06-17 16:45:48 -05:00
|
|
|
rewrite_filename(filename);
|
2015-03-02 16:30:58 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
json: remove the 32-bit parameter special case
Before, the rules for encoding parameters in JSON were as follows:
- if the parameter is not a string:
- if it is exactly 32 bits long and there are no z or x bits, emit it
as an int
- otherwise, emit it as a string made of 0/1/x/z characters
- if the parameter is a string:
- if it contains only 0/1/x/z characters, append a space at the end
to distinguish it from a non-string
- otherwise, emit it directly
However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values. Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
2020-02-01 03:21:19 -06:00
|
|
|
JsonWriter json_writer(*f, true, aig_mode, compat_int_mode);
|
2015-03-02 16:30:58 -06:00
|
|
|
json_writer.write_design(design);
|
|
|
|
|
|
|
|
if (!filename.empty()) {
|
|
|
|
delete f;
|
|
|
|
} else {
|
|
|
|
log("%s", buf.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} JsonPass;
|
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|