2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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.
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.
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/cellaigs.h"
25 #include "kernel/log.h"
29 PRIVATE_NAMESPACE_BEGIN
43 dict
<SigBit
, string
> sigids
;
46 JsonWriter(std::ostream
&f
, bool use_selection
, bool aig_mode
, bool compat_int_mode
) :
47 f(f
), use_selection(use_selection
), aig_mode(aig_mode
),
48 compat_int_mode(compat_int_mode
) { }
50 string
get_string(string str
)
69 newstr
+= stringf("\\u%04X", c
);
76 string
get_name(IdString name
)
78 return get_string(RTLIL::unescape_id(name
));
81 string
get_bits(SigSpec sig
)
85 for (auto bit
: sigmap(sig
)) {
86 str
+= first
? " " : ", ";
88 if (sigids
.count(bit
) == 0) {
89 string
&s
= sigids
[bit
];
90 if (bit
.wire
== nullptr) {
91 if (bit
== State::S0
) s
= "\"0\"";
92 else if (bit
== State::S1
) s
= "\"1\"";
93 else if (bit
== State::Sz
) s
= "\"z\"";
96 s
= stringf("%d", sigidcounter
++);
103 void write_parameter_value(const Const
&value
)
105 if ((value
.flags
& RTLIL::ConstFlags::CONST_FLAG_STRING
) != 0) {
106 string str
= value
.decode_string();
110 if (c
== '0' || c
== '1' || c
== 'x' || c
== 'z')
116 } else if (state
== 1 && c
!= ' ')
121 f
<< get_string(str
);
122 } else if (compat_int_mode
&& GetSize(value
) <= 32 && value
.is_fully_def()) {
123 if ((value
.flags
& RTLIL::ConstFlags::CONST_FLAG_SIGNED
) != 0)
124 f
<< stringf("%d", value
.as_int());
126 f
<< stringf("%u", value
.as_int());
128 f
<< get_string(value
.as_string());
132 void write_parameters(const dict
<IdString
, Const
> ¶meters
, bool for_module
=false)
135 for (auto ¶m
: parameters
) {
136 f
<< stringf("%s\n", first
? "" : ",");
137 f
<< stringf(" %s%s: ", for_module
? "" : " ", get_name(param
.first
).c_str());
138 write_parameter_value(param
.second
);
143 void write_module(Module
*module_
)
146 log_assert(module
->design
== design
);
150 // reserve 0 and 1 to avoid confusion with "0" and "1"
153 if (module
->has_processes()) {
154 log_error("Module %s contains processes, which are not supported by JSON backend (run `proc` first).\n", log_id(module
));
157 f
<< stringf(" %s: {\n", get_name(module
->name
).c_str());
159 f
<< stringf(" \"attributes\": {");
160 write_parameters(module
->attributes
, /*for_module=*/true);
161 f
<< stringf("\n },\n");
163 if (module
->parameter_default_values
.size()) {
164 f
<< stringf(" \"parameter_default_values\": {");
165 write_parameters(module
->parameter_default_values
, /*for_module=*/true);
166 f
<< stringf("\n },\n");
169 f
<< stringf(" \"ports\": {");
171 for (auto n
: module
->ports
) {
172 Wire
*w
= module
->wire(n
);
173 if (use_selection
&& !module
->selected(w
))
175 f
<< stringf("%s\n", first
? "" : ",");
176 f
<< stringf(" %s: {\n", get_name(n
).c_str());
177 f
<< stringf(" \"direction\": \"%s\",\n", w
->port_input
? w
->port_output
? "inout" : "input" : "output");
179 f
<< stringf(" \"offset\": %d,\n", w
->start_offset
);
181 f
<< stringf(" \"upto\": 1,\n");
183 f
<< stringf(" \"signed\": %d,\n", w
->is_signed
);
184 f
<< stringf(" \"bits\": %s\n", get_bits(w
).c_str());
188 f
<< stringf("\n },\n");
190 f
<< stringf(" \"cells\": {");
192 for (auto c
: module
->cells()) {
193 if (use_selection
&& !module
->selected(c
))
195 f
<< stringf("%s\n", first
? "" : ",");
196 f
<< stringf(" %s: {\n", get_name(c
->name
).c_str());
197 f
<< stringf(" \"hide_name\": %s,\n", c
->name
[0] == '$' ? "1" : "0");
198 f
<< stringf(" \"type\": %s,\n", get_name(c
->type
).c_str());
201 if (!aig
.name
.empty()) {
202 f
<< stringf(" \"model\": \"%s\",\n", aig
.name
.c_str());
203 aig_models
.insert(aig
);
206 f
<< stringf(" \"parameters\": {");
207 write_parameters(c
->parameters
);
208 f
<< stringf("\n },\n");
209 f
<< stringf(" \"attributes\": {");
210 write_parameters(c
->attributes
);
211 f
<< stringf("\n },\n");
213 f
<< stringf(" \"port_directions\": {");
215 for (auto &conn
: c
->connections()) {
216 string direction
= "output";
217 if (c
->input(conn
.first
))
218 direction
= c
->output(conn
.first
) ? "inout" : "input";
219 f
<< stringf("%s\n", first2
? "" : ",");
220 f
<< stringf(" %s: \"%s\"", get_name(conn
.first
).c_str(), direction
.c_str());
223 f
<< stringf("\n },\n");
225 f
<< stringf(" \"connections\": {");
227 for (auto &conn
: c
->connections()) {
228 f
<< stringf("%s\n", first2
? "" : ",");
229 f
<< stringf(" %s: %s", get_name(conn
.first
).c_str(), get_bits(conn
.second
).c_str());
232 f
<< stringf("\n }\n");
236 f
<< stringf("\n },\n");
238 if (!module
->memories
.empty()) {
239 f
<< stringf(" \"memories\": {");
241 for (auto &it
: module
->memories
) {
242 if (use_selection
&& !module
->selected(it
.second
))
244 f
<< stringf("%s\n", first
? "" : ",");
245 f
<< stringf(" %s: {\n", get_name(it
.second
->name
).c_str());
246 f
<< stringf(" \"hide_name\": %s,\n", it
.second
->name
[0] == '$' ? "1" : "0");
247 f
<< stringf(" \"attributes\": {");
248 write_parameters(it
.second
->attributes
);
249 f
<< stringf("\n },\n");
250 f
<< stringf(" \"width\": %d,\n", it
.second
->width
);
251 f
<< stringf(" \"start_offset\": %d,\n", it
.second
->start_offset
);
252 f
<< stringf(" \"size\": %d\n", it
.second
->size
);
256 f
<< stringf("\n },\n");
259 f
<< stringf(" \"netnames\": {");
261 for (auto w
: module
->wires()) {
262 if (use_selection
&& !module
->selected(w
))
264 f
<< stringf("%s\n", first
? "" : ",");
265 f
<< stringf(" %s: {\n", get_name(w
->name
).c_str());
266 f
<< stringf(" \"hide_name\": %s,\n", w
->name
[0] == '$' ? "1" : "0");
267 f
<< stringf(" \"bits\": %s,\n", get_bits(w
).c_str());
269 f
<< stringf(" \"offset\": %d,\n", w
->start_offset
);
271 f
<< stringf(" \"upto\": 1,\n");
273 f
<< stringf(" \"signed\": %d,\n", w
->is_signed
);
274 f
<< stringf(" \"attributes\": {");
275 write_parameters(w
->attributes
);
276 f
<< stringf("\n }\n");
280 f
<< stringf("\n }\n");
285 void write_design(Design
*design_
)
291 f
<< stringf(" \"creator\": %s,\n", get_string(yosys_version_str
).c_str());
292 f
<< stringf(" \"modules\": {\n");
293 vector
<Module
*> modules
= use_selection
? design
->selected_modules() : design
->modules();
294 bool first_module
= true;
295 for (auto mod
: modules
) {
299 first_module
= false;
301 f
<< stringf("\n }");
302 if (!aig_models
.empty()) {
303 f
<< stringf(",\n \"models\": {\n");
304 bool first_model
= true;
305 for (auto &aig
: aig_models
) {
308 f
<< stringf(" \"%s\": [\n", aig
.name
.c_str());
310 for (auto &node
: aig
.nodes
) {
313 f
<< stringf(" /* %3d */ [ ", node_idx
);
314 if (node
.portbit
>= 0)
315 f
<< stringf("\"%sport\", \"%s\", %d", node
.inverter
? "n" : "",
316 log_id(node
.portname
), node
.portbit
);
317 else if (node
.left_parent
< 0 && node
.right_parent
< 0)
318 f
<< stringf("\"%s\"", node
.inverter
? "true" : "false");
320 f
<< stringf("\"%s\", %d, %d", node
.inverter
? "nand" : "and", node
.left_parent
, node
.right_parent
);
321 for (auto &op
: node
.outports
)
322 f
<< stringf(", \"%s\", %d", log_id(op
.first
), op
.second
);
326 f
<< stringf("\n ]");
329 f
<< stringf("\n }");
331 f
<< stringf("\n}\n");
335 struct JsonBackend
: public Backend
{
336 JsonBackend() : Backend("json", "write design to a JSON file") { }
339 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
341 log(" write_json [options] [filename]\n");
343 log("Write a JSON netlist of the current design.\n");
346 log(" include AIG models for the different gate types\n");
348 log(" -compat-int\n");
349 log(" emit 32-bit or smaller fully-defined parameter values directly\n");
350 log(" as JSON numbers (for compatibility with old parsers)\n");
353 log("The general syntax of the JSON output created by this command is as follows:\n");
356 log(" \"creator\": \"Yosys <version info>\",\n");
357 log(" \"modules\": {\n");
358 log(" <module_name>: {\n");
359 log(" \"attributes\": {\n");
360 log(" <attribute_name>: <attribute_value>,\n");
363 log(" \"parameter_default_values\": {\n");
364 log(" <parameter_name>: <parameter_value>,\n");
367 log(" \"ports\": {\n");
368 log(" <port_name>: <port_details>,\n");
371 log(" \"cells\": {\n");
372 log(" <cell_name>: <cell_details>,\n");
375 log(" \"memories\": {\n");
376 log(" <memory_name>: <memory_details>,\n");
379 log(" \"netnames\": {\n");
380 log(" <net_name>: <net_details>,\n");
385 log(" \"models\": {\n");
390 log("Where <port_details> is:\n");
393 log(" \"direction\": <\"input\" | \"output\" | \"inout\">,\n");
394 log(" \"bits\": <bit_vector>\n");
395 log(" \"offset\": <the lowest bit index in use, if non-0>\n");
396 log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
399 log("The \"offset\" and \"upto\" fields are skipped if their value would be 0.");
400 log("They don't affect connection semantics, and are only used to preserve original");
401 log("HDL bit indexing.");
402 log("And <cell_details> is:\n");
405 log(" \"hide_name\": <1 | 0>,\n");
406 log(" \"type\": <cell_type>,\n");
407 log(" \"model\": <AIG model name, if -aig option used>,\n");
408 log(" \"parameters\": {\n");
409 log(" <parameter_name>: <parameter_value>,\n");
412 log(" \"attributes\": {\n");
413 log(" <attribute_name>: <attribute_value>,\n");
416 log(" \"port_directions\": {\n");
417 log(" <port_name>: <\"input\" | \"output\" | \"inout\">,\n");
420 log(" \"connections\": {\n");
421 log(" <port_name>: <bit_vector>,\n");
426 log("And <memory_details> is:\n");
429 log(" \"hide_name\": <1 | 0>,\n");
430 log(" \"attributes\": {\n");
431 log(" <attribute_name>: <attribute_value>,\n");
434 log(" \"width\": <memory width>\n");
435 log(" \"start_offset\": <the lowest valid memory address>\n");
436 log(" \"size\": <memory size>\n");
439 log("And <net_details> is:\n");
442 log(" \"hide_name\": <1 | 0>,\n");
443 log(" \"bits\": <bit_vector>\n");
444 log(" \"offset\": <the lowest bit index in use, if non-0>\n");
445 log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
448 log("The \"hide_name\" fields are set to 1 when the name of this cell or net is\n");
449 log("automatically created and is likely not of interest for a regular user.\n");
451 log("The \"port_directions\" section is only included for cells for which the\n");
452 log("interface is known.\n");
454 log("Module and cell ports and nets can be single bit wide or vectors of multiple\n");
455 log("bits. Each individual signal bit is assigned a unique integer. The <bit_vector>\n");
456 log("values referenced above are vectors of this integers. Signal bits that are\n");
457 log("connected to a constant driver are denoted as string \"0\", \"1\", \"x\", or\n");
458 log("\"z\" instead of a number.\n");
460 log("Bit vectors (including integers) are written as string holding the binary");
461 log("representation of the value. Strings are written as strings, with an appended");
462 log("blank in cases of strings of the form /[01xz]* */.\n");
464 log("For example the following Verilog code:\n");
466 log(" module test(input x, y);\n");
467 log(" (* keep *) foo #(.P(42), .Q(1337))\n");
468 log(" foo_inst (.A({x, y}), .B({y, x}), .C({4'd10, {4{x}}}));\n");
471 log("Translates to the following JSON output:\n");
475 log(" \"creator\": \"Yosys 0.9+2406 (git sha1 fb1168d8, clang 9.0.1 -fPIC -Os)\",\n");
476 log(" \"modules\": {\n");
477 log(" \"test\": {\n");
478 log(" \"attributes\": {\n");
479 log(" \"cells_not_processed\": \"00000000000000000000000000000001\",\n");
480 log(" \"src\": \"test.v:1.1-4.10\"\n");
482 log(" \"ports\": {\n");
484 log(" \"direction\": \"input\",\n");
485 log(" \"bits\": [ 2 ]\n");
488 log(" \"direction\": \"input\",\n");
489 log(" \"bits\": [ 3 ]\n");
492 log(" \"cells\": {\n");
493 log(" \"foo_inst\": {\n");
494 log(" \"hide_name\": 0,\n");
495 log(" \"type\": \"foo\",\n");
496 log(" \"parameters\": {\n");
497 log(" \"P\": \"00000000000000000000000000101010\",\n");
498 log(" \"Q\": \"00000000000000000000010100111001\"\n");
500 log(" \"attributes\": {\n");
501 log(" \"keep\": \"00000000000000000000000000000001\",\n");
502 log(" \"module_not_derived\": \"00000000000000000000000000000001\",\n");
503 log(" \"src\": \"test.v:3.1-3.55\"\n");
505 log(" \"connections\": {\n");
506 log(" \"A\": [ 3, 2 ],\n");
507 log(" \"B\": [ 2, 3 ],\n");
508 log(" \"C\": [ 2, 2, 2, 2, \"0\", \"1\", \"0\", \"1\" ]\n");
512 log(" \"netnames\": {\n");
514 log(" \"hide_name\": 0,\n");
515 log(" \"bits\": [ 2 ],\n");
516 log(" \"attributes\": {\n");
517 log(" \"src\": \"test.v:1.19-1.20\"\n");
521 log(" \"hide_name\": 0,\n");
522 log(" \"bits\": [ 3 ],\n");
523 log(" \"attributes\": {\n");
524 log(" \"src\": \"test.v:1.22-1.23\"\n");
532 log("The models are given as And-Inverter-Graphs (AIGs) in the following form:\n");
534 log(" \"models\": {\n");
535 log(" <model_name>: [\n");
536 log(" /* 0 */ [ <node-spec> ],\n");
537 log(" /* 1 */ [ <node-spec> ],\n");
538 log(" /* 2 */ [ <node-spec> ],\n");
544 log("The following node-types may be used:\n");
546 log(" [ \"port\", <portname>, <bitindex>, <out-list> ]\n");
547 log(" - the value of the specified input port bit\n");
549 log(" [ \"nport\", <portname>, <bitindex>, <out-list> ]\n");
550 log(" - the inverted value of the specified input port bit\n");
552 log(" [ \"and\", <node-index>, <node-index>, <out-list> ]\n");
553 log(" - the ANDed value of the specified nodes\n");
555 log(" [ \"nand\", <node-index>, <node-index>, <out-list> ]\n");
556 log(" - the inverted ANDed value of the specified nodes\n");
558 log(" [ \"true\", <out-list> ]\n");
559 log(" - the constant value 1\n");
561 log(" [ \"false\", <out-list> ]\n");
562 log(" - the constant value 0\n");
564 log("All nodes appear in topological order. I.e. only nodes with smaller indices\n");
565 log("are referenced by \"and\" and \"nand\" nodes.\n");
567 log("The optional <out-list> at the end of a node specification is a list of\n");
568 log("output portname and bitindex pairs, specifying the outputs driven by this node.\n");
570 log("For example, the following is the model for a 3-input 3-output $reduce_and cell\n");
571 log("inferred by the following code:\n");
573 log(" module test(input [2:0] in, output [2:0] out);\n");
574 log(" assign in = &out;\n");
577 log(" \"$reduce_and:3U:3\": [\n");
578 log(" /* 0 */ [ \"port\", \"A\", 0 ],\n");
579 log(" /* 1 */ [ \"port\", \"A\", 1 ],\n");
580 log(" /* 2 */ [ \"and\", 0, 1 ],\n");
581 log(" /* 3 */ [ \"port\", \"A\", 2 ],\n");
582 log(" /* 4 */ [ \"and\", 2, 3, \"Y\", 0 ],\n");
583 log(" /* 5 */ [ \"false\", \"Y\", 1, \"Y\", 2 ]\n");
586 log("Future version of Yosys might add support for additional fields in the JSON\n");
587 log("format. A program processing this format must ignore all unknown fields.\n");
590 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) override
592 bool aig_mode
= false;
593 bool compat_int_mode
= false;
596 for (argidx
= 1; argidx
< args
.size(); argidx
++)
598 if (args
[argidx
] == "-aig") {
602 if (args
[argidx
] == "-compat-int") {
603 compat_int_mode
= true;
608 extra_args(f
, filename
, args
, argidx
);
610 log_header(design
, "Executing JSON backend.\n");
612 JsonWriter
json_writer(*f
, false, aig_mode
, compat_int_mode
);
613 json_writer
.write_design(design
);
617 struct JsonPass
: public Pass
{
618 JsonPass() : Pass("json", "write design in JSON format") { }
621 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
623 log(" json [options] [selection]\n");
625 log("Write a JSON netlist of all selected objects.\n");
627 log(" -o <filename>\n");
628 log(" write to the specified file.\n");
631 log(" also include AIG models for the different gate types\n");
633 log(" -compat-int\n");
634 log(" emit 32-bit or smaller fully-defined parameter values directly\n");
635 log(" as JSON numbers (for compatibility with old parsers)\n");
637 log("See 'help write_json' for a description of the JSON format used.\n");
640 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) override
642 std::string filename
;
643 bool aig_mode
= false;
644 bool compat_int_mode
= false;
647 for (argidx
= 1; argidx
< args
.size(); argidx
++)
649 if (args
[argidx
] == "-o" && argidx
+1 < args
.size()) {
650 filename
= args
[++argidx
];
653 if (args
[argidx
] == "-aig") {
657 if (args
[argidx
] == "-compat-int") {
658 compat_int_mode
= true;
663 extra_args(args
, argidx
, design
);
666 std::stringstream buf
;
668 if (!filename
.empty()) {
669 rewrite_filename(filename
);
670 std::ofstream
*ff
= new std::ofstream
;
671 ff
->open(filename
.c_str(), std::ofstream::trunc
);
674 log_error("Can't open file `%s' for writing: %s\n", filename
.c_str(), strerror(errno
));
681 JsonWriter
json_writer(*f
, true, aig_mode
, compat_int_mode
);
682 json_writer
.write_design(design
);
684 if (!filename
.empty()) {
687 log("%s", buf
.str().c_str());
692 PRIVATE_NAMESPACE_END