aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/algo.h
blob: f029ad6abfaf65a389ff347b75042d2a9ac6e47e (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- c++ -*-
 *  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.
 *
 */

#ifndef SATGEN_ALGO_H
#define SATGEN_ALGO_H

#include "kernel/celltypes.h"
#include "kernel/rtlil.h"
#include "kernel/sigtools.h"
#include <stack>

YOSYS_NAMESPACE_BEGIN

CellTypes comb_cells_filt()
{
	CellTypes ct;

	ct.setup_internals();
	ct.setup_stdcells();

	return ct;
}

struct Netlist {
	RTLIL::Module *module;
	SigMap sigmap;
	CellTypes ct;
	dict<RTLIL::SigBit, Cell *> sigbit_driver_map;
	dict<RTLIL::Cell *, std::set<RTLIL::SigBit>> cell_inputs_map;

	Netlist(RTLIL::Module *module) : module(module), sigmap(module), ct(module->design) { setup_netlist(module, ct); }

	Netlist(RTLIL::Module *module, const CellTypes &ct) : module(module), sigmap(module), ct(ct) { setup_netlist(module, ct); }

	RTLIL::Cell *driver_cell(RTLIL::SigBit sig) const
	{
		sig = sigmap(sig);
		if (!sigbit_driver_map.count(sig)) {
			return NULL;
		}

		return sigbit_driver_map.at(sig);
	}

	RTLIL::SigSpec driver_port(RTLIL::SigBit sig)
	{
		RTLIL::Cell *cell = driver_cell(sig);

		if (!cell) {
			return RTLIL::SigSpec();
		}

		for (auto &port : cell->connections_) {
			if (ct.cell_output(cell->type, port.first)) {
				RTLIL::SigSpec port_sig = sigmap(port.second);
				for (int i = 0; i < GetSize(port_sig); i++) {
					if (port_sig[i] == sig) {
						return port.second[i];
					}
				}
			}
		}

		return RTLIL::SigSpec();
	}

	void setup_netlist(RTLIL::Module *module, const CellTypes &ct)
	{
		for (auto cell : module->cells()) {
			if (ct.cell_known(cell->type)) {
				std::set<RTLIL::SigBit> inputs, outputs;
				for (auto &port : cell->connections()) {
					std::vector<RTLIL::SigBit> bits = sigmap(port.second).to_sigbit_vector();
					if (ct.cell_output(cell->type, port.first))
						outputs.insert(bits.begin(), bits.end());
					else
						inputs.insert(bits.begin(), bits.end());
				}
				cell_inputs_map[cell] = inputs;
				for (auto &bit : outputs) {
					sigbit_driver_map[bit] = cell;
				};
			}
		}
	}
};

namespace detail
{
struct NetlistConeWireIter : public std::iterator<std::input_iterator_tag, RTLIL::SigBit> {
	using set_iter_t = std::set<RTLIL::SigBit>::iterator;

	const Netlist &net;
	RTLIL::SigBit sig;
	bool sentinel;
	CellTypes *cell_filter;

	std::stack<std::pair<set_iter_t, set_iter_t>> dfs_path_stack;
	std::set<RTLIL::Cell *> cells_visited;

	NetlistConeWireIter(const Netlist &net) : net(net), sentinel(true), cell_filter(NULL) {}

	NetlistConeWireIter(const Netlist &net, RTLIL::SigBit sig, CellTypes *cell_filter = NULL)
	    : net(net), sig(sig), sentinel(false), cell_filter(cell_filter)
	{
	}

	const RTLIL::SigBit &operator*() const { return sig; };
	bool operator!=(const NetlistConeWireIter &other) const
	{
		if (sentinel || other.sentinel) {
			return sentinel != other.sentinel;
		} else {
			return sig != other.sig;
		}
	}

	bool operator==(const NetlistConeWireIter &other) const
	{
		if (sentinel || other.sentinel) {
			return sentinel == other.sentinel;
		} else {
			return sig == other.sig;
		}
	}

	void next_sig_in_dag()
	{
		while (1) {
			if (dfs_path_stack.empty()) {
				sentinel = true;
				return;
			}

			auto &cell_inputs_iter = dfs_path_stack.top().first;
			auto &cell_inputs_iter_guard = dfs_path_stack.top().second;

			cell_inputs_iter++;
			if (cell_inputs_iter != cell_inputs_iter_guard) {
				sig = *cell_inputs_iter;
				return;
			} else {
				dfs_path_stack.pop();
			}
		}
	}

	NetlistConeWireIter &operator++()
	{
		RTLIL::Cell *cell = net.driver_cell(sig);

		if (!cell) {
			next_sig_in_dag();
			return *this;
		}

		if (cells_visited.count(cell)) {
			next_sig_in_dag();
			return *this;
		}

		if ((cell_filter) && (!cell_filter->cell_known(cell->type))) {
			next_sig_in_dag();
			return *this;
		}

		auto &inputs = net.cell_inputs_map.at(cell);
		dfs_path_stack.push(std::make_pair(inputs.begin(), inputs.end()));
		cells_visited.insert(cell);
		sig = (*dfs_path_stack.top().first);
		return *this;
	}
};

struct NetlistConeWireIterable {
	const Netlist &net;
	RTLIL::SigBit sig;
	CellTypes *cell_filter;

	NetlistConeWireIterable(const Netlist &net, RTLIL::SigBit sig, CellTypes *cell_filter = NULL) : net(net), sig(sig), cell_filter(cell_filter)
	{
	}

	NetlistConeWireIter begin() { return NetlistConeWireIter(net, sig, cell_filter); }
	NetlistConeWireIter end() { return NetlistConeWireIter(net); }
};

struct NetlistConeCellIter : public std::iterator<std::input_iterator_tag, RTLIL::Cell *> {
	const Netlist &net;

	NetlistConeWireIter sig_iter;

	NetlistConeCellIter(const Netlist &net) : net(net), sig_iter(net) {}

	NetlistConeCellIter(const Netlist &net, RTLIL::SigBit sig, CellTypes *cell_filter = NULL) : net(net), sig_iter(net, sig, cell_filter)
	{
		if ((!sig_iter.sentinel) && (!has_driver_cell(*sig_iter))) {
			++(*this);
		}
	}

	bool has_driver_cell(const RTLIL::SigBit &s) { return net.sigbit_driver_map.count(s); }

	RTLIL::Cell *operator*() const { return net.sigbit_driver_map.at(*sig_iter); };

	bool operator!=(const NetlistConeCellIter &other) const { return sig_iter != other.sig_iter; }
	bool operator==(const NetlistConeCellIter &other) const { return sig_iter == other.sig_iter; }
	NetlistConeCellIter &operator++()
	{
		while (true) {
			++sig_iter;
			if (sig_iter.sentinel) {
				return *this;
			}

			RTLIL::Cell* cell = net.driver_cell(*sig_iter);

			if (!cell) {
				continue;
			}

			if ((sig_iter.cell_filter) && (!sig_iter.cell_filter->cell_known(cell->type))) {
				continue;
			}

			if (!sig_iter.cells_visited.count(cell)) {
				return *this;
			}
		}
	}
};

struct NetlistConeCellIterable {
	const Netlist &net;
	RTLIL::SigBit sig;
	CellTypes *cell_filter;

	NetlistConeCellIterable(const Netlist &net, RTLIL::SigBit sig, CellTypes *cell_filter = NULL) : net(net), sig(sig), cell_filter(cell_filter)
	{
	}

	NetlistConeCellIter begin() { return NetlistConeCellIter(net, sig, cell_filter); }
	NetlistConeCellIter end() { return NetlistConeCellIter(net); }
};

// struct NetlistConeInputsIter : public std::iterator<std::input_iterator_tag, const RTLIL::Cell *> {
// 	const Netlist &net;
// 	RTLIL::SigBit sig;

// 	NetlistConeWireIter sig_iter;

// 	bool has_driver_cell(const RTLIL::SigBit &s) { return net.sigbit_driver_map.count(s); }

// 	NetlistConeInputsIter(const Netlist &net, RTLIL::SigBit sig = NULL) : net(net), sig(sig), sig_iter(net, sig)
// 	{
// 		if ((sig != NULL) && (has_driver_cell(sig_iter))) {
// 			++(*this);
// 		}
// 	}

// 	const RTLIL::SigBit &operator*() const { return sig_iter; };
// 	bool operator!=(const NetlistConeInputsIter &other) const { return sig_iter != other.sig_iter; }
// 	bool operator==(const NetlistConeInputsIter &other) const { return sig_iter == other.sig_iter; }
// 	NetlistConeInputsIter &operator++()
// 	{
// 		do {
// 			++sig_iter;
// 			if (sig_iter->empty()) {
// 				return *this;
// 			}
// 		} while (has_driver_cell(sig_iter));

// 		return *this;
// 	}
// };

// struct NetlistConeInputsIterable {
// 	const Netlist &net;
// 	RTLIL::SigBit sig;

// 	NetlistConeInputsIterable(const Netlist &net, RTLIL::SigBit sig) : net(net), sig(sig) {}

// 	NetlistConeInputsIter begin() { return NetlistConeInputsIter(net, sig); }
// 	NetlistConeInputsIter end() { return NetlistConeInputsIter(net); }
// };
} // namespace detail

detail::NetlistConeWireIterable cone(const Netlist &net, RTLIL::SigBit sig, CellTypes *cell_filter = NULL)
{
	return detail::NetlistConeWireIterable(net, net.sigmap(sig), cell_filter = cell_filter);
}

// detail::NetlistConeInputsIterable cone_inputs(RTLIL::SigBit sig) { return NetlistConeInputsIterable(this, &sig); }
detail::NetlistConeCellIterable cell_cone(const Netlist &net, RTLIL::SigBit sig, CellTypes *cell_filter = NULL)
{
	return detail::NetlistConeCellIterable(net, net.sigmap(sig), cell_filter);
}

YOSYS_NAMESPACE_END

#endif