Merge https://github.com/YosysHQ/yosys into xaig
[yosys.git] / passes / fsm / fsm.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/register.h"
21 #include "kernel/log.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 USING_YOSYS_NAMESPACE
26 PRIVATE_NAMESPACE_BEGIN
27
28 struct FsmPass : public Pass {
29 FsmPass() : Pass("fsm", "extract and optimize finite state machines") { }
30 void help() YS_OVERRIDE
31 {
32 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
33 log("\n");
34 log(" fsm [options] [selection]\n");
35 log("\n");
36 log("This pass calls all the other fsm_* passes in a useful order. This performs\n");
37 log("FSM extraction and optimization. It also calls opt_clean as needed:\n");
38 log("\n");
39 log(" fsm_detect unless got option -nodetect\n");
40 log(" fsm_extract\n");
41 log("\n");
42 log(" fsm_opt\n");
43 log(" opt_clean\n");
44 log(" fsm_opt\n");
45 log("\n");
46 log(" fsm_expand if got option -expand\n");
47 log(" opt_clean if got option -expand\n");
48 log(" fsm_opt if got option -expand\n");
49 log("\n");
50 log(" fsm_recode unless got option -norecode\n");
51 log("\n");
52 log(" fsm_info\n");
53 log("\n");
54 log(" fsm_export if got option -export\n");
55 log(" fsm_map unless got option -nomap\n");
56 log("\n");
57 log("Options:\n");
58 log("\n");
59 log(" -expand, -norecode, -export, -nomap\n");
60 log(" enable or disable passes as indicated above\n");
61 log("\n");
62 log(" -fullexpand\n");
63 log(" call expand with -full option\n");
64 log("\n");
65 log(" -encoding type\n");
66 log(" -fm_set_fsm_file file\n");
67 log(" -encfile file\n");
68 log(" passed through to fsm_recode pass\n");
69 log("\n");
70 }
71 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
72 {
73 bool flag_nomap = false;
74 bool flag_norecode = false;
75 bool flag_nodetect = false;
76 bool flag_expand = false;
77 bool flag_fullexpand = false;
78 bool flag_export = false;
79 std::string fm_set_fsm_file_opt;
80 std::string encfile_opt;
81 std::string encoding_opt;
82
83 log_header(design, "Executing FSM pass (extract and optimize FSM).\n");
84 log_push();
85
86 size_t argidx;
87 for (argidx = 1; argidx < args.size(); argidx++) {
88 std::string arg = args[argidx];
89 if (arg == "-fm_set_fsm_file" && argidx+1 < args.size() && fm_set_fsm_file_opt.empty()) {
90 fm_set_fsm_file_opt = " -fm_set_fsm_file " + args[++argidx];
91 continue;
92 }
93 if (arg == "-encfile" && argidx+1 < args.size() && encfile_opt.empty()) {
94 encfile_opt = " -encfile " + args[++argidx];
95 continue;
96 }
97 if (arg == "-encoding" && argidx+1 < args.size() && encoding_opt.empty()) {
98 encoding_opt = " -encoding " + args[++argidx];
99 continue;
100 }
101 if (arg == "-nodetect") {
102 flag_nodetect = true;
103 continue;
104 }
105 if (arg == "-norecode") {
106 flag_norecode = true;
107 continue;
108 }
109 if (arg == "-nomap") {
110 flag_nomap = true;
111 continue;
112 }
113 if (arg == "-expand") {
114 flag_expand = true;
115 continue;
116 }
117 if (arg == "-fullexpand") {
118 flag_fullexpand = true;
119 continue;
120 }
121 if (arg == "-export") {
122 flag_export = true;
123 continue;
124 }
125 break;
126 }
127 extra_args(args, argidx, design);
128
129 if (!flag_nodetect)
130 Pass::call(design, "fsm_detect");
131 Pass::call(design, "fsm_extract");
132
133 Pass::call(design, "fsm_opt");
134 Pass::call(design, "opt_clean");
135 Pass::call(design, "fsm_opt");
136
137 if (flag_expand || flag_fullexpand) {
138 Pass::call(design, flag_fullexpand ? "fsm_expand -full" : "fsm_expand");
139 Pass::call(design, "opt_clean");
140 Pass::call(design, "fsm_opt");
141 }
142
143 if (!flag_norecode)
144 Pass::call(design, "fsm_recode" + fm_set_fsm_file_opt + encfile_opt + encoding_opt);
145 Pass::call(design, "fsm_info");
146
147 if (flag_export)
148 Pass::call(design, "fsm_export");
149
150 if (!flag_nomap)
151 Pass::call(design, "fsm_map");
152
153 log_pop();
154 }
155 } FsmPass;
156
157 PRIVATE_NAMESPACE_END