2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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 // [[CITE]] EDIF Version 2 0 0 Grammar
21 // http://web.archive.org/web/20050730021644/http://www.edif.org/documentation/BNF_GRAMMAR/index.html
23 #include "kernel/rtlil.h"
24 #include "kernel/register.h"
25 #include "kernel/sigtools.h"
26 #include "kernel/celltypes.h"
27 #include "kernel/log.h"
31 PRIVATE_NAMESPACE_BEGIN
33 #define EDIF_DEF(_id) edif_names(RTLIL::unescape_id(_id), true).c_str()
34 #define EDIF_DEFR(_id, _ren, _bl, _br) edif_names(RTLIL::unescape_id(_id), true, _ren, _bl, _br).c_str()
35 #define EDIF_REF(_id) edif_names(RTLIL::unescape_id(_id), false).c_str()
40 char delim_left
, delim_right
;
41 std::set
<std::string
> generated_names
, used_names
;
42 std::map
<std::string
, std::string
> name_map
;
44 EdifNames() : counter(1), delim_left('['), delim_right(']') { }
46 std::string
operator()(std::string id
, bool define
, bool port_rename
= false, int range_left
= 0, int range_right
= 0)
49 std::string new_id
= operator()(id
, false);
51 return stringf("(rename %s \"%s%c%d:%d%c\")", new_id
.c_str(), id
.c_str(), delim_left
, range_left
, range_right
, delim_right
);
52 return new_id
!= id
? stringf("(rename %s \"%s\")", new_id
.c_str(), id
.c_str()) : id
;
55 if (name_map
.count(id
) > 0)
56 return name_map
.at(id
);
57 if (generated_names
.count(id
) > 0)
59 if (id
== "GND" || id
== "VCC")
62 for (size_t i
= 0; i
< id
.size(); i
++) {
63 if ('A' <= id
[i
] && id
[i
] <= 'Z')
65 if ('a' <= id
[i
] && id
[i
] <= 'z')
67 if ('0' <= id
[i
] && id
[i
] <= '9' && i
> 0)
69 if (id
[i
] == '_' && i
> 0 && i
!= id
.size()-1)
74 used_names
.insert(id
);
80 gen_name
= stringf("id%05d", counter
++);
81 if (generated_names
.count(gen_name
) == 0 &&
82 used_names
.count(gen_name
) == 0)
85 generated_names
.insert(gen_name
);
86 name_map
[id
] = gen_name
;
91 struct EdifBackend
: public Backend
{
92 EdifBackend() : Backend("edif", "write design to EDIF netlist file") { }
93 void help() YS_OVERRIDE
95 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
97 log(" write_edif [options] [filename]\n");
99 log("Write the current design to an EDIF netlist file.\n");
101 log(" -top top_module\n");
102 log(" set the specified module as design top module\n");
105 log(" do not create \"GND\" and \"VCC\" cells. (this will produce an error\n");
106 log(" if the design contains constant nets. use \"hilomap\" to map to custom\n");
107 log(" constant drivers first)\n");
110 log(" create \"GND\" and \"VCC\" cells with \"Y\" outputs. (the default is \"G\"\n");
111 log(" for \"GND\" and \"P\" for \"VCC\".)\n");
114 log(" create EDIF properties for cell attributes\n");
116 log(" -pvector {par|bra|ang}\n");
117 log(" sets the delimiting character for module port rename clauses to\n");
118 log(" parentheses, square brackets, or angle brackets.\n");
120 log("Unfortunately there are different \"flavors\" of the EDIF file format. This\n");
121 log("command generates EDIF files for the Xilinx place&route tools. It might be\n");
122 log("necessary to make small modifications to this command when a different tool\n");
123 log("is targeted.\n");
126 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
128 log_header(design
, "Executing EDIF backend.\n");
129 std::string top_module_name
;
130 bool port_rename
= false;
131 bool attr_properties
= false;
132 std::map
<RTLIL::IdString
, std::map
<RTLIL::IdString
, int>> lib_cell_ports
;
133 bool nogndvcc
= false, gndvccy
= false;
134 CellTypes
ct(design
);
135 EdifNames edif_names
;
138 for (argidx
= 1; argidx
< args
.size(); argidx
++)
140 if (args
[argidx
] == "-top" && argidx
+1 < args
.size()) {
141 top_module_name
= args
[++argidx
];
144 if (args
[argidx
] == "-nogndvcc") {
148 if (args
[argidx
] == "-gndvccy") {
152 if (args
[argidx
] == "-attrprop") {
153 attr_properties
= true;
156 if (args
[argidx
] == "-pvector" && argidx
+1 < args
.size()) {
159 parray
= args
[++argidx
];
160 if (parray
== "par") {
161 edif_names
.delim_left
= '(';edif_names
.delim_right
= ')';
162 } else if (parray
== "ang") {
163 edif_names
.delim_left
= '<';edif_names
.delim_right
= '>';
165 edif_names
.delim_left
= '[';edif_names
.delim_right
= ']';
171 extra_args(f
, filename
, args
, argidx
);
173 if (top_module_name
.empty())
174 for (auto & mod_it
:design
->modules_
)
175 if (mod_it
.second
->get_bool_attribute("\\top"))
176 top_module_name
= mod_it
.first
.str();
178 for (auto module_it
: design
->modules_
)
180 RTLIL::Module
*module
= module_it
.second
;
181 if (module
->get_blackbox_attribute())
184 if (top_module_name
.empty())
185 top_module_name
= module
->name
.str();
187 if (module
->processes
.size() != 0)
188 log_error("Found unmapped processes in module %s: unmapped processes are not supported in EDIF backend!\n", RTLIL::id2cstr(module
->name
));
189 if (module
->memories
.size() != 0)
190 log_error("Found unmapped memories in module %s: unmapped memories are not supported in EDIF backend!\n", RTLIL::id2cstr(module
->name
));
192 for (auto cell_it
: module
->cells_
)
194 RTLIL::Cell
*cell
= cell_it
.second
;
195 if (!design
->modules_
.count(cell
->type
) || design
->modules_
.at(cell
->type
)->get_blackbox_attribute()) {
196 lib_cell_ports
[cell
->type
];
197 for (auto p
: cell
->connections())
198 lib_cell_ports
[cell
->type
][p
.first
] = GetSize(p
.second
);
203 if (top_module_name
.empty())
204 log_error("No module found in design!\n");
206 *f
<< stringf("(edif %s\n", EDIF_DEF(top_module_name
));
207 *f
<< stringf(" (edifVersion 2 0 0)\n");
208 *f
<< stringf(" (edifLevel 0)\n");
209 *f
<< stringf(" (keywordMap (keywordLevel 0))\n");
210 *f
<< stringf(" (comment \"Generated by %s\")\n", yosys_version_str
);
212 *f
<< stringf(" (external LIB\n");
213 *f
<< stringf(" (edifLevel 0)\n");
214 *f
<< stringf(" (technology (numberDefinition))\n");
218 *f
<< stringf(" (cell GND\n");
219 *f
<< stringf(" (cellType GENERIC)\n");
220 *f
<< stringf(" (view VIEW_NETLIST\n");
221 *f
<< stringf(" (viewType NETLIST)\n");
222 *f
<< stringf(" (interface (port %c (direction OUTPUT)))\n", gndvccy
? 'Y' : 'G');
223 *f
<< stringf(" )\n");
224 *f
<< stringf(" )\n");
226 *f
<< stringf(" (cell VCC\n");
227 *f
<< stringf(" (cellType GENERIC)\n");
228 *f
<< stringf(" (view VIEW_NETLIST\n");
229 *f
<< stringf(" (viewType NETLIST)\n");
230 *f
<< stringf(" (interface (port %c (direction OUTPUT)))\n", gndvccy
? 'Y' : 'P');
231 *f
<< stringf(" )\n");
232 *f
<< stringf(" )\n");
235 for (auto &cell_it
: lib_cell_ports
) {
236 *f
<< stringf(" (cell %s\n", EDIF_DEF(cell_it
.first
));
237 *f
<< stringf(" (cellType GENERIC)\n");
238 *f
<< stringf(" (view VIEW_NETLIST\n");
239 *f
<< stringf(" (viewType NETLIST)\n");
240 *f
<< stringf(" (interface\n");
241 for (auto &port_it
: cell_it
.second
) {
242 const char *dir
= "INOUT";
243 if (ct
.cell_known(cell_it
.first
)) {
244 if (!ct
.cell_output(cell_it
.first
, port_it
.first
))
246 else if (!ct
.cell_input(cell_it
.first
, port_it
.first
))
249 if (port_it
.second
== 1)
250 *f
<< stringf(" (port %s (direction %s))\n", EDIF_DEF(port_it
.first
), dir
);
252 int b
[2] = {port_it
.second
-1, 0};
253 auto m
= design
->module(cell_it
.first
);
255 auto w
= m
->wire(port_it
.first
);
257 b
[w
->upto
? 0 : 1] = w
->start_offset
;
258 b
[w
->upto
? 1 : 0] = w
->start_offset
+GetSize(w
)-1;
261 *f
<< stringf(" (port (array %s %d) (direction %s))\n", EDIF_DEFR(port_it
.first
, port_rename
, b
[0], b
[1]), port_it
.second
, dir
);
264 *f
<< stringf(" )\n");
265 *f
<< stringf(" )\n");
266 *f
<< stringf(" )\n");
268 *f
<< stringf(" )\n");
270 std::vector
<RTLIL::Module
*> sorted_modules
;
272 // extract module dependencies
273 std::map
<RTLIL::Module
*, std::set
<RTLIL::Module
*>> module_deps
;
274 for (auto &mod_it
: design
->modules_
) {
275 module_deps
[mod_it
.second
] = std::set
<RTLIL::Module
*>();
276 for (auto &cell_it
: mod_it
.second
->cells_
)
277 if (design
->modules_
.count(cell_it
.second
->type
) > 0)
278 module_deps
[mod_it
.second
].insert(design
->modules_
.at(cell_it
.second
->type
));
281 // simple good-enough topological sort
282 // (O(n*m) on n elements and depth m)
283 while (module_deps
.size() > 0) {
284 size_t sorted_modules_idx
= sorted_modules
.size();
285 for (auto &it
: module_deps
) {
286 for (auto &dep
: it
.second
)
287 if (module_deps
.count(dep
) > 0)
289 // log("Next in topological sort: %s\n", RTLIL::id2cstr(it.first->name));
290 sorted_modules
.push_back(it
.first
);
293 if (sorted_modules_idx
== sorted_modules
.size())
294 log_error("Cyclic dependency between modules found! Cycle includes module %s.\n", RTLIL::id2cstr(module_deps
.begin()->first
->name
));
295 while (sorted_modules_idx
< sorted_modules
.size())
296 module_deps
.erase(sorted_modules
.at(sorted_modules_idx
++));
300 *f
<< stringf(" (library DESIGN\n");
301 *f
<< stringf(" (edifLevel 0)\n");
302 *f
<< stringf(" (technology (numberDefinition))\n");
303 for (auto module
: sorted_modules
)
305 if (module
->get_blackbox_attribute())
308 SigMap
sigmap(module
);
309 std::map
<RTLIL::SigSpec
, std::set
<std::string
>> net_join_db
;
311 *f
<< stringf(" (cell %s\n", EDIF_DEF(module
->name
));
312 *f
<< stringf(" (cellType GENERIC)\n");
313 *f
<< stringf(" (view VIEW_NETLIST\n");
314 *f
<< stringf(" (viewType NETLIST)\n");
315 *f
<< stringf(" (interface\n");
316 for (auto &wire_it
: module
->wires_
) {
317 RTLIL::Wire
*wire
= wire_it
.second
;
318 if (wire
->port_id
== 0)
320 const char *dir
= "INOUT";
321 if (!wire
->port_output
)
323 else if (!wire
->port_input
)
325 if (wire
->width
== 1) {
326 *f
<< stringf(" (port %s (direction %s))\n", EDIF_DEF(wire
->name
), dir
);
327 RTLIL::SigSpec sig
= sigmap(RTLIL::SigSpec(wire
));
328 net_join_db
[sig
].insert(stringf("(portRef %s)", EDIF_REF(wire
->name
)));
331 b
[wire
->upto
? 0 : 1] = wire
->start_offset
;
332 b
[wire
->upto
? 1 : 0] = wire
->start_offset
+ GetSize(wire
) - 1;
333 *f
<< stringf(" (port (array %s %d) (direction %s))\n", EDIF_DEFR(wire
->name
, port_rename
, b
[0], b
[1]), wire
->width
, dir
);
334 for (int i
= 0; i
< wire
->width
; i
++) {
335 RTLIL::SigSpec sig
= sigmap(RTLIL::SigSpec(wire
, i
));
336 net_join_db
[sig
].insert(stringf("(portRef (member %s %d))", EDIF_REF(wire
->name
), GetSize(wire
)-i
-1));
340 *f
<< stringf(" )\n");
341 *f
<< stringf(" (contents\n");
343 *f
<< stringf(" (instance GND (viewRef VIEW_NETLIST (cellRef GND (libraryRef LIB))))\n");
344 *f
<< stringf(" (instance VCC (viewRef VIEW_NETLIST (cellRef VCC (libraryRef LIB))))\n");
346 for (auto &cell_it
: module
->cells_
) {
347 RTLIL::Cell
*cell
= cell_it
.second
;
348 *f
<< stringf(" (instance %s\n", EDIF_DEF(cell
->name
));
349 *f
<< stringf(" (viewRef VIEW_NETLIST (cellRef %s%s))", EDIF_REF(cell
->type
),
350 lib_cell_ports
.count(cell
->type
) > 0 ? " (libraryRef LIB)" : "");
352 auto add_prop
= [&](IdString name
, Const val
) {
353 if ((val
.flags
& RTLIL::CONST_FLAG_STRING
) != 0)
354 *f
<< stringf("\n (property %s (string \"%s\"))", EDIF_DEF(name
), val
.decode_string().c_str());
355 else if (val
.bits
.size() <= 32 && RTLIL::SigSpec(val
).is_fully_def())
356 *f
<< stringf("\n (property %s (integer %u))", EDIF_DEF(name
), val
.as_int());
358 std::string hex_string
= "";
359 for (size_t i
= 0; i
< val
.bits
.size(); i
+= 4) {
361 if (i
+0 < val
.bits
.size() && val
.bits
.at(i
+0) == RTLIL::State::S1
) digit_value
|= 1;
362 if (i
+1 < val
.bits
.size() && val
.bits
.at(i
+1) == RTLIL::State::S1
) digit_value
|= 2;
363 if (i
+2 < val
.bits
.size() && val
.bits
.at(i
+2) == RTLIL::State::S1
) digit_value
|= 4;
364 if (i
+3 < val
.bits
.size() && val
.bits
.at(i
+3) == RTLIL::State::S1
) digit_value
|= 8;
365 char digit_str
[2] = { "0123456789abcdef"[digit_value
], 0 };
366 hex_string
= std::string(digit_str
) + hex_string
;
368 *f
<< stringf("\n (property %s (string \"%d'h%s\"))", EDIF_DEF(name
), GetSize(val
.bits
), hex_string
.c_str());
372 for (auto &p
: cell
->parameters
)
373 add_prop(p
.first
, p
.second
);
375 for (auto &p
: cell
->attributes
)
376 add_prop(p
.first
, p
.second
);
378 *f
<< stringf(")\n");
379 for (auto &p
: cell
->connections()) {
380 RTLIL::SigSpec sig
= sigmap(p
.second
);
381 for (int i
= 0; i
< GetSize(sig
); i
++)
382 if (sig
[i
].wire
== NULL
&& sig
[i
] != RTLIL::State::S0
&& sig
[i
] != RTLIL::State::S1
)
383 log_warning("Bit %d of cell port %s.%s.%s driven by %s will be left unconnected in EDIF output.\n",
384 i
, log_id(module
), log_id(cell
), log_id(p
.first
), log_signal(sig
[i
]));
385 else if (sig
.size() == 1)
386 net_join_db
[sig
[i
]].insert(stringf("(portRef %s (instanceRef %s))", EDIF_REF(p
.first
), EDIF_REF(cell
->name
)));
388 int member_idx
= GetSize(sig
)-i
-1;
389 auto m
= design
->module(cell
->type
);
391 auto w
= m
->wire(p
.first
);
393 member_idx
= GetSize(w
)-i
-1;
395 net_join_db
[sig
[i
]].insert(stringf("(portRef (member %s %d) (instanceRef %s))",
396 EDIF_REF(p
.first
), member_idx
, EDIF_REF(cell
->name
)));
400 for (auto &it
: net_join_db
) {
401 RTLIL::SigBit sig
= it
.first
;
402 if (sig
.wire
== NULL
&& sig
!= RTLIL::State::S0
&& sig
!= RTLIL::State::S1
) {
403 if (sig
== RTLIL::State::Sx
) {
404 for (auto &ref
: it
.second
)
405 log_warning("Exporting x-bit on %s as zero bit.\n", ref
.c_str());
406 sig
= RTLIL::State::S0
;
408 for (auto &ref
: it
.second
)
409 log_error("Don't know how to handle %s on %s.\n", log_signal(sig
), ref
.c_str());
414 if (sig
== RTLIL::State::S0
)
416 else if (sig
== RTLIL::State::S1
)
419 netname
= log_signal(sig
);
420 for (size_t i
= 0; i
< netname
.size(); i
++)
421 if (netname
[i
] == ' ' || netname
[i
] == '\\')
422 netname
.erase(netname
.begin() + i
--);
424 *f
<< stringf(" (net %s (joined\n", EDIF_DEF(netname
));
425 for (auto &ref
: it
.second
)
426 *f
<< stringf(" %s\n", ref
.c_str());
427 if (sig
.wire
== NULL
) {
429 log_error("Design contains constant nodes (map with \"hilomap\" first).\n");
430 if (sig
== RTLIL::State::S0
)
431 *f
<< stringf(" (portRef %c (instanceRef GND))\n", gndvccy
? 'Y' : 'G');
432 if (sig
== RTLIL::State::S1
)
433 *f
<< stringf(" (portRef %c (instanceRef VCC))\n", gndvccy
? 'Y' : 'P');
435 *f
<< stringf(" ))\n");
437 *f
<< stringf(" )\n");
438 *f
<< stringf(" )\n");
439 *f
<< stringf(" )\n");
441 *f
<< stringf(" )\n");
443 *f
<< stringf(" (design %s\n", EDIF_DEF(top_module_name
));
444 *f
<< stringf(" (cellRef %s (libraryRef DESIGN))\n", EDIF_REF(top_module_name
));
445 *f
<< stringf(" )\n");
447 *f
<< stringf(")\n");
451 PRIVATE_NAMESPACE_END