aboutsummaryrefslogtreecommitdiffstats
path: root/passes/sat/fminit.cc
blob: f3f00b382ee8b99dab26bede390f471467d2d278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "kernel/yosys.h"
#include "kernel/sigtools.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct FminitPass : public Pass {
	FminitPass() : Pass("fminit", "set init values/sequences for formal") { }
	void help() YS_OVERRIDE
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    fminit [options] <selection>\n");
		log("\n");
		log("This pass creates init constraints (for example for reset sequences) in a formal\n");
		log("model.\n");
		log("\n");
		log("    -seq <signal> <sequence>\n");
		log("        Set sequence using comma-separated list of values, use 'z for\n");
		log("        unconstrained bits. The last value is used for the remainder of the\n");
		log("        trace.\n");
		log("\n");
		log("    -set <signal> <value>\n");
		log("        Add constant value constraint\n");
		log("\n");
		log("    -posedge <signal>\n");
		log("    -negedge <signal>\n");
		log("        Set clock for init sequences\n");
		log("\n");
	}
	void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
	{
		vector<pair<string, vector<string>>> initdata;
		vector<pair<string, string>> setdata;
		string clocksignal;
		bool clockedge;

		log_header(design, "Executing FMINIT pass.\n");

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			// if (args[argidx] == "-o" && argidx+1 < args.size()) {
			// 	filename = args[++argidx];
			// 	continue;
			// }
			if (args[argidx] == "-seq" && argidx+2 < args.size()) {
				string lhs = args[++argidx];
				string rhs = args[++argidx];
				initdata.push_back(make_pair(lhs, split_tokens(rhs, ",")));
				continue;
			}
			if (args[argidx] == "-set" && argidx+2 < args.size()) {
				string lhs = args[++argidx];
				string rhs = args[++argidx];
				setdata.push_back(make_pair(lhs, rhs));
				continue;
			}
			if (args[argidx] == "-posedge" && argidx+1 < args.size()) {
				clocksignal = args[++argidx];
				clockedge = true;
				continue;
			}
			if (args[argidx] == "-negedge" && argidx+1 < args.size()) {
				clocksignal = args[++argidx];
				clockedge = true;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		Module *module = nullptr;

		for (auto mod : design->selected_modules()) {
			if (module != nullptr)
				log_error("'fminit' requires exactly one module to be selected.\n");
			module = mod;
		}

		if (module == nullptr)
			log_error("'fminit' requires exactly one module to be selected.\n");

		SigSpec clksig;
		if (!clocksignal.empty()) {
			if (!SigSpec::parse(clksig, module, clocksignal))
				log_error("Error parsing expression '%s'.\n", clocksignal.c_str());
		}

		for (auto &it : setdata)
		{
			SigSpec lhs, rhs;

			if (!SigSpec::parse(lhs, module, it.first))
				log_error("Error parsing expression '%s'.\n", it.first.c_str());

			if (!SigSpec::parse_rhs(lhs, rhs, module, it.second))
				log_error("Error parsing expression '%s'.\n", it.second.c_str());

			SigSpec final_lhs, final_rhs;

			for (int i = 0; i < GetSize(rhs); i++)
				if (rhs[i] != State::Sz) {
					final_lhs.append(lhs[i]);
					final_rhs.append(rhs[i]);
				}

			if (!final_lhs.empty()) {
				SigSpec eq = module->Eq(NEW_ID, final_lhs, final_rhs);
				module->addAssume(NEW_ID, eq, State::S1);
			}
		}

		vector<SigSpec> ctrlsig;
		vector<SigSpec> ctrlsig_latched;

		for (auto &it : initdata)
		{
			SigSpec lhs, rhs;

			if (!SigSpec::parse(lhs, module, it.first))
				log_error("Error parsing expression '%s'.\n", it.first.c_str());

			for (int i = 0; i < GetSize(it.second); i++)
			{
				if (i >= GetSize(ctrlsig))
				{
					SigSpec insig = i > 0 ? ctrlsig.at(i-1) : State::S0;

					Wire *outwire = module->addWire(NEW_ID);
					outwire->attributes[ID(init)] = i > 0 ? State::S0 : State::S1;

					if (clksig.empty())
						module->addFf(NEW_ID, insig, outwire);
					else
						module->addDff(NEW_ID, clksig, insig, outwire, clockedge);

					ctrlsig.push_back(outwire);
					ctrlsig_latched.push_back(SigSpec());
				}

				if (i+1 == GetSize(it.second) && ctrlsig_latched[i].empty())
				{
					Wire *ffwire = module->addWire(NEW_ID);
					ffwire->attributes[ID(init)] = State::S0;
					SigSpec outsig = module->Or(NEW_ID, ffwire, ctrlsig[i]);

					if (clksig.empty())
						module->addFf(NEW_ID, outsig, ffwire);
					else
						module->addDff(NEW_ID, clksig, outsig, ffwire, clockedge);

					ctrlsig_latched[i] = outsig;
				}

				SigSpec ctrl = i+1 == GetSize(it.second) ? ctrlsig_latched[i] : ctrlsig[i];

				SigSpec final_lhs, final_rhs;

				if (!SigSpec::parse_rhs(lhs, rhs, module, it.second[i]))
					log_error("Error parsing expression '%s'.\n", it.second[i].c_str());

				for (int i = 0; i < GetSize(rhs); i++)
					if (rhs[i] != State::Sz) {
						final_lhs.append(lhs[i]);
						final_rhs.append(rhs[i]);
					}

				if (!final_lhs.empty()) {
					SigSpec eq = module->Eq(NEW_ID, final_lhs, final_rhs);
					module->addAssume(NEW_ID, eq, ctrl);
				}
			}
		}
	}
} FminitPass;

PRIVATE_NAMESPACE_END