Merge automatic and manual code changes for new cell connections API
[yosys.git] / backends / intersynth / intersynth.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/log.h"
25 #include <string>
26 #include <assert.h>
27
28
29 static std::string netname(std::set<std::string> &conntypes_code, std::set<std::string> &celltypes_code, std::set<std::string> &constcells_code, RTLIL::SigSpec sig)
30 {
31 if (!sig.is_fully_const() && !sig.is_wire())
32 log_error("Can't export composite or non-word-wide signal %s.\n", log_signal(sig));
33
34 conntypes_code.insert(stringf("conntype b%d %d 2 %d\n", sig.size(), sig.size(), sig.size()));
35
36 if (sig.is_fully_const()) {
37 celltypes_code.insert(stringf("celltype CONST_%d b%d *CONST cfg:%d VALUE\n", sig.size(), sig.size(), sig.size()));
38 constcells_code.insert(stringf("node CONST_%d_0x%x CONST_%d CONST CONST_%d_0x%x VALUE 0x%x\n",
39 sig.size(), sig.as_int(), sig.size(), sig.size(), sig.as_int(), sig.as_int()));
40 return stringf("CONST_%d_0x%x", sig.size(), sig.as_int());
41 }
42
43 return RTLIL::unescape_id(sig.as_wire()->name);
44 }
45
46 struct IntersynthBackend : public Backend {
47 IntersynthBackend() : Backend("intersynth", "write design to InterSynth netlist file") { }
48 virtual void help()
49 {
50 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
51 log("\n");
52 log(" write_intersynth [options] [filename]\n");
53 log("\n");
54 log("Write the current design to an 'intersynth' netlist file. InterSynth is\n");
55 log("a tool for Coarse-Grain Example-Driven Interconnect Synthesis.\n");
56 log("\n");
57 log(" -notypes\n");
58 log(" do not generate celltypes and conntypes commands. i.e. just output\n");
59 log(" the netlists. this is used for postsilicon synthesis.\n");
60 log("\n");
61 log(" -lib <verilog_or_ilang_file>\n");
62 log(" Use the specified library file for determining whether cell ports are\n");
63 log(" inputs or outputs. This option can be used multiple times to specify\n");
64 log(" more than one library.\n");
65 log("\n");
66 log(" -selected\n");
67 log(" only write selected modules. modules must be selected entirely or\n");
68 log(" not at all.\n");
69 log("\n");
70 log("http://www.clifford.at/intersynth/\n");
71 log("\n");
72 }
73 virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
74 {
75 log_header("Executing INTERSYNTH backend.\n");
76 log_push();
77
78 std::vector<std::string> libfiles;
79 std::vector<RTLIL::Design*> libs;
80 bool flag_notypes = false;
81 bool selected = false;
82
83 size_t argidx;
84 for (argidx = 1; argidx < args.size(); argidx++)
85 {
86 if (args[argidx] == "-notypes") {
87 flag_notypes = true;
88 continue;
89 }
90 if (args[argidx] == "-lib" && argidx+1 < args.size()) {
91 libfiles.push_back(args[++argidx]);
92 continue;
93 }
94 if (args[argidx] == "-selected") {
95 selected = true;
96 continue;
97 }
98 break;
99 }
100 extra_args(f, filename, args, argidx);
101
102 log("Output filename: %s\n", filename.c_str());
103
104 for (auto filename : libfiles) {
105 FILE *f = fopen(filename.c_str(), "rt");
106 if (f == NULL)
107 log_error("Can't open lib file `%s'.\n", filename.c_str());
108 RTLIL::Design *lib = new RTLIL::Design;
109 Frontend::frontend_call(lib, f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
110 libs.push_back(lib);
111 fclose(f);
112 }
113
114 if (libs.size() > 0)
115 log_header("Continuing INTERSYNTH backend.\n");
116
117 std::set<std::string> conntypes_code, celltypes_code;
118 std::string netlists_code;
119 CellTypes ct(design);
120
121 for (auto lib : libs)
122 ct.setup_design(lib);
123
124 for (auto module_it : design->modules)
125 {
126 RTLIL::Module *module = module_it.second;
127 SigMap sigmap(module);
128
129 if (module->get_bool_attribute("\\blackbox"))
130 continue;
131 if (module->memories.size() == 0 && module->processes.size() == 0 && module->cells.size() == 0)
132 continue;
133
134 if (selected && !design->selected_whole_module(module->name)) {
135 if (design->selected_module(module->name))
136 log_cmd_error("Can't handle partially selected module %s!\n", RTLIL::id2cstr(module->name));
137 continue;
138 }
139
140 log("Generating netlist %s.\n", RTLIL::id2cstr(module->name));
141
142 if (module->memories.size() != 0 || module->processes.size() != 0)
143 log_error("Can't generate a netlist for a module with unprocessed memories or processes!\n");
144
145 std::set<std::string> constcells_code;
146 netlists_code += stringf("# Netlist of module %s\n", RTLIL::id2cstr(module->name));
147 netlists_code += stringf("netlist %s\n", RTLIL::id2cstr(module->name));
148
149 // Module Ports: "std::set<string> celltypes_code" prevents duplicate top level ports
150 for (auto wire_it : module->wires) {
151 RTLIL::Wire *wire = wire_it.second;
152 if (wire->port_input || wire->port_output) {
153 celltypes_code.insert(stringf("celltype !%s b%d %sPORT\n" "%s %s %d %s PORT\n",
154 RTLIL::id2cstr(wire->name), wire->width, wire->port_input ? "*" : "",
155 wire->port_input ? "input" : "output", RTLIL::id2cstr(wire->name), wire->width, RTLIL::id2cstr(wire->name)));
156 netlists_code += stringf("node %s %s PORT %s\n", RTLIL::id2cstr(wire->name), RTLIL::id2cstr(wire->name),
157 netname(conntypes_code, celltypes_code, constcells_code, sigmap(wire)).c_str());
158 }
159 }
160
161 // Submodules: "std::set<string> celltypes_code" prevents duplicate cell types
162 for (auto cell_it : module->cells)
163 {
164 RTLIL::Cell *cell = cell_it.second;
165 std::string celltype_code, node_code;
166
167 if (!ct.cell_known(cell->type))
168 log_error("Found unknown cell type %s in module!\n", RTLIL::id2cstr(cell->type));
169
170 celltype_code = stringf("celltype %s", RTLIL::id2cstr(cell->type));
171 node_code = stringf("node %s %s", RTLIL::id2cstr(cell->name), RTLIL::id2cstr(cell->type));
172 for (auto &port : cell->connections()) {
173 RTLIL::SigSpec sig = sigmap(port.second);
174 if (sig.size() != 0) {
175 conntypes_code.insert(stringf("conntype b%d %d 2 %d\n", sig.size(), sig.size(), sig.size()));
176 celltype_code += stringf(" b%d %s%s", sig.size(), ct.cell_output(cell->type, port.first) ? "*" : "", RTLIL::id2cstr(port.first));
177 node_code += stringf(" %s %s", RTLIL::id2cstr(port.first), netname(conntypes_code, celltypes_code, constcells_code, sig).c_str());
178 }
179 }
180 for (auto &param : cell->parameters) {
181 celltype_code += stringf(" cfg:%d %s", int(param.second.bits.size()), RTLIL::id2cstr(param.first));
182 if (param.second.bits.size() != 32) {
183 node_code += stringf(" %s '", RTLIL::id2cstr(param.first));
184 for (int i = param.second.bits.size()-1; i >= 0; i--)
185 node_code += param.second.bits[i] == RTLIL::S1 ? "1" : "0";
186 } else
187 node_code += stringf(" %s 0x%x", RTLIL::id2cstr(param.first), param.second.as_int());
188 }
189
190 celltypes_code.insert(celltype_code + "\n");
191 netlists_code += node_code + "\n";
192 }
193
194 if (constcells_code.size() > 0)
195 netlists_code += "# constant cells\n";
196 for (auto code : constcells_code)
197 netlists_code += code;
198 netlists_code += "\n";
199 }
200
201 if (!flag_notypes) {
202 fprintf(f, "### Connection Types\n");
203 for (auto code : conntypes_code)
204 fprintf(f, "%s", code.c_str());
205 fprintf(f, "\n### Cell Types\n");
206 for (auto code : celltypes_code)
207 fprintf(f, "%s", code.c_str());
208 }
209 fprintf(f, "\n### Netlists\n");
210 fprintf(f, "%s", netlists_code.c_str());
211
212 for (auto lib : libs)
213 delete lib;
214
215 log_pop();
216 }
217 } IntersynthBackend;
218