2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2012 Martin Schmölzer <martin@schmoelzer.at>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include "kernel/log.h"
22 #include "kernel/register.h"
23 #include "kernel/sigtools.h"
24 #include "kernel/consteval.h"
25 #include "kernel/celltypes.h"
32 PRIVATE_NAMESPACE_BEGIN
35 * Convert a signal into a KISS-compatible textual representation.
37 std::string
kiss_convert_signal(const RTLIL::SigSpec
&sig
) {
38 log_assert(sig
.is_fully_const());
39 return sig
.as_const().as_string();
43 * Create a KISS2 file from a cell.
45 * The destination file name is taken from the fsm_export attribute if present,
46 * e.g. (* fsm_export="filename.kiss2" *). If this attribute is not present,
47 * the file name will be assembled from the module and cell names.
49 * @param module pointer to module which contains the FSM cell.
50 * @param cell pointer to the FSM cell which should be exported.
52 void write_kiss2(struct RTLIL::Module
*module
, struct RTLIL::Cell
*cell
, std::string filename
, bool origenc
) {
53 dict
<RTLIL::IdString
, RTLIL::Const
>::iterator attr_it
;
55 FsmData::transition_t tr
;
56 std::ofstream kiss_file
;
57 std::string kiss_name
;
60 attr_it
= cell
->attributes
.find("\\fsm_export");
61 if (!filename
.empty()) {
62 kiss_name
.assign(filename
);
63 } else if (attr_it
!= cell
->attributes
.end() && attr_it
->second
.decode_string() != "") {
64 kiss_name
.assign(attr_it
->second
.decode_string());
67 kiss_name
.assign(log_id(module
) + std::string("-") + log_id(cell
) + ".kiss2");
71 log("Exporting FSM `%s' from module `%s' to file `%s'.\n",
76 kiss_file
.open(kiss_name
, std::ios::out
| std::ios::trunc
);
78 if (!kiss_file
.is_open()) {
79 log_error("Could not open file \"%s\" with write access.\n", kiss_name
.c_str());
82 fsm_data
.copy_from_cell(cell
);
84 kiss_file
<< ".i " << std::dec
<< fsm_data
.num_inputs
<< std::endl
;
85 kiss_file
<< ".o " << std::dec
<< fsm_data
.num_outputs
<< std::endl
;
86 kiss_file
<< ".p " << std::dec
<< fsm_data
.transition_table
.size() << std::endl
;
87 kiss_file
<< ".s " << std::dec
<< fsm_data
.state_table
.size() << std::endl
;
89 kiss_file
<< ".r " << kiss_convert_signal(fsm_data
.state_table
[fsm_data
.reset_state
]) << std::endl
;
91 kiss_file
<< ".r s" << std::dec
<< fsm_data
.reset_state
<< std::endl
;
94 for (i
= 0; i
< fsm_data
.transition_table
.size(); i
++) {
95 tr
= fsm_data
.transition_table
[i
];
98 kiss_file
<< kiss_convert_signal(tr
.ctrl_in
) << ' ';
100 kiss_file
<< kiss_convert_signal(fsm_data
.state_table
[tr
.state_in
]) << ' ';
101 kiss_file
<< kiss_convert_signal(fsm_data
.state_table
[tr
.state_out
]) << ' ';
103 kiss_file
<< 's' << tr
.state_in
<< ' ';
104 kiss_file
<< 's' << tr
.state_out
<< ' ';
106 kiss_file
<< kiss_convert_signal(tr
.ctrl_out
) << std::endl
;
110 log_error("exporting an FSM input or output signal failed.\n");
118 * Exports Finite State Machines in the design to one file per FSM. Currently,
119 * only the KISS2 file format is supported.
121 struct FsmExportPass
: public Pass
{
122 FsmExportPass() : Pass("fsm_export", "exporting FSMs to KISS2 files") { }
125 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
127 log(" fsm_export [-noauto] [-o filename] [-origenc] [selection]\n");
129 log("This pass creates a KISS2 file for every selected FSM. For FSMs with the\n");
130 log("'fsm_export' attribute set, the attribute value is used as filename, otherwise\n");
131 log("the module and cell name is used as filename. If the parameter '-o' is given,\n");
132 log("the first exported FSM is written to the specified filename. This overwrites\n");
133 log("the setting as specified with the 'fsm_export' attribute. All other FSMs are\n");
134 log("exported to the default name as mentioned above.\n");
137 log(" only export FSMs that have the 'fsm_export' attribute set\n");
139 log(" -o filename\n");
140 log(" filename of the first exported FSM\n");
143 log(" use binary state encoding as state names instead of s0, s1, ...\n");
146 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
148 dict
<RTLIL::IdString
, RTLIL::Const
>::iterator attr_it
;
150 bool flag_noauto
= false;
151 std::string filename
;
152 bool flag_origenc
= false;
155 log_header("Executing FSM_EXPORT pass (exporting FSMs in KISS2 file format).\n");
157 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
159 if (arg
== "-noauto") {
165 filename
= args
[argidx
];
168 if (arg
== "-origenc") {
174 extra_args(args
, argidx
, design
);
176 for (auto &mod_it
: design
->modules_
)
177 if (design
->selected(mod_it
.second
))
178 for (auto &cell_it
: mod_it
.second
->cells_
)
179 if (cell_it
.second
->type
== "$fsm" && design
->selected(mod_it
.second
, cell_it
.second
)) {
180 attr_it
= cell_it
.second
->attributes
.find("\\fsm_export");
181 if (!flag_noauto
|| (attr_it
!= cell_it
.second
->attributes
.end())) {
182 write_kiss2(mod_it
.second
, cell_it
.second
, filename
, flag_origenc
);
189 PRIVATE_NAMESPACE_END