aboutsummaryrefslogtreecommitdiffstats
path: root/passes/cmds/connwrappers.cc
blob: a65a63644d38492f397e25c63c1823b1aed9b680 (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
/*
 *  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/register.h"
#include "kernel/sigtools.h"
#include "kernel/rtlil.h"
#include "kernel/log.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct ConnwrappersWorker
{
	struct portdecl_t {
		// key: celltype, portname;
		std::string widthparam, signparam;
		bool is_signed;
	};

	std::set<RTLIL::IdString> decl_celltypes;
	std::map<std::pair<RTLIL::IdString, RTLIL::IdString>, portdecl_t> decls;

	void add_port(std::string celltype, std::string portname, std::string widthparam, std::string signparam)
	{
		std::pair<std::string, std::string> key(RTLIL::escape_id(celltype), RTLIL::escape_id(portname));
		decl_celltypes.insert(key.first);

		if (decls.count(key))
			log_cmd_error("Duplicate port decl: %s %s\n", celltype.c_str(), portname.c_str());

		portdecl_t decl;
		decl.widthparam = RTLIL::escape_id(widthparam);
		decl.signparam = RTLIL::escape_id(signparam);
		decl.is_signed = false;
		decls[key] = decl;
	}

	void add_port(std::string celltype, std::string portname, std::string widthparam, bool is_signed)
	{
		std::pair<std::string, std::string> key(RTLIL::escape_id(celltype), RTLIL::escape_id(portname));
		decl_celltypes.insert(key.first);

		if (decls.count(key))
			log_cmd_error("Duplicate port decl: %s %s\n", celltype.c_str(), portname.c_str());

		portdecl_t decl;
		decl.widthparam = RTLIL::escape_id(widthparam);
		decl.is_signed = is_signed;
		decls[key] = decl;
	}

	void work(RTLIL::Design *design, RTLIL::Module *module)
	{
		std::map<RTLIL::SigBit, std::pair<bool, RTLIL::SigSpec>> extend_map;
		SigMap sigmap(module);

		for (auto &it : module->cells_)
		{
			RTLIL::Cell *cell = it.second;

			if (!decl_celltypes.count(cell->type))
				continue;

			for (auto &conn : cell->connections())
			{
				std::pair<RTLIL::IdString, RTLIL::IdString> key(cell->type, conn.first);

				if (!decls.count(key))
					continue;

				portdecl_t &decl = decls.at(key);

				if (!cell->parameters.count(decl.widthparam))
					continue;

				if (!decl.signparam.empty() && !cell->parameters.count(decl.signparam))
					continue;

				int inner_width = cell->parameters.at(decl.widthparam).as_int();
				int outer_width = conn.second.size();
				bool is_signed = decl.signparam.empty() ? decl.is_signed : cell->parameters.at(decl.signparam).as_bool();

				if (inner_width >= outer_width)
					continue;

				RTLIL::SigSpec sig = sigmap(conn.second);
				extend_map[sig.extract(inner_width - 1, 1)] = std::pair<bool, RTLIL::SigSpec>(is_signed,
						sig.extract(inner_width, outer_width - inner_width));
			}
		}

		for (auto &it : module->cells_)
		{
			RTLIL::Cell *cell = it.second;

			if (!design->selected(module, cell))
				continue;

			for (auto &conn : cell->connections_)
			{
				std::vector<RTLIL::SigBit> sigbits = sigmap(conn.second).to_sigbit_vector();
				RTLIL::SigSpec old_sig;

				for (size_t i = 0; i < sigbits.size(); i++)
				{
					if (!extend_map.count(sigbits[i]))
						continue;

					bool is_signed = extend_map.at(sigbits[i]).first;
					RTLIL::SigSpec extend_sig = extend_map.at(sigbits[i]).second;

					int extend_width = 0;
					RTLIL::SigBit extend_bit = is_signed ? sigbits[i] : RTLIL::SigBit(RTLIL::State::S0);
					while (extend_width < extend_sig.size() && i + extend_width + 1 < sigbits.size() &&
							sigbits[i + extend_width + 1] == extend_bit) extend_width++;

					if (extend_width == 0)
						continue;

					if (old_sig.size() == 0)
						old_sig = conn.second;

					conn.second.replace(i+1, extend_sig.extract(0, extend_width));
					i += extend_width;
				}

				if (old_sig.size())
					log("Connected extended bits of %s.%s:%s: %s -> %s\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name),
							RTLIL::id2cstr(conn.first), log_signal(old_sig), log_signal(conn.second));
			}
		}
	}
};

struct ConnwrappersPass : public Pass {
	ConnwrappersPass() : Pass("connwrappers", "replace undef values with defined constants") { }
	virtual void help()
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    connwrappers [options] [selection]\n");
		log("\n");
		log("Wrappers are used in coarse-grain synthesis to wrap cells with smaller ports\n");
		log("in wrapper cells with a (larger) constant port size. I.e. the upper bits\n");
		log("of the wrapper outut are signed/unsigned bit extended. This command uses this\n");
		log("knowlege to rewire the inputs of the driven cells to match the output of\n");
		log("the driving cell.\n");
		log("\n");
		log("    -signed <cell_type> <port_name> <width_param>\n");
		log("    -unsigned <cell_type> <port_name> <width_param>\n");
		log("        consider the specified signed/unsigned wrapper output\n");
		log("\n");
		log("    -port <cell_type> <port_name> <width_param> <sign_param>\n");
		log("        use the specified parameter to decide if signed or unsigned\n");
		log("\n");
		log("The options -signed, -unsigned, and -port can be specified multiple times.\n");
		log("\n");
	}
	virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
	{
		ConnwrappersWorker worker;

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			if (args[argidx] == "-signed" && argidx+3 < args.size()) {
				worker.add_port(args[argidx+1], args[argidx+2], args[argidx+3], true);
				argidx += 3;
				continue;
			}
			if (args[argidx] == "-unsigned" && argidx+3 < args.size()) {
				worker.add_port(args[argidx+1], args[argidx+2], args[argidx+3], false);
				argidx += 3;
				continue;
			}
			if (args[argidx] == "-port" && argidx+4 < args.size()) {
				worker.add_port(args[argidx+1], args[argidx+2], args[argidx+3], args[argidx+4]);
				argidx += 4;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		log_header("Executing CONNWRAPPERS pass (connect extended ports of wrapper cells).\n");

		for (auto &mod_it : design->modules_)
			if (design->selected(mod_it.second))
				worker.work(design, mod_it.second);
	}
} ConnwrappersPass;
 
PRIVATE_NAMESPACE_END
class="o">*cell, const RTLIL::IdString &port, const RTLIL::SigSpec &old_sig, RTLIL::SigSpec &sig) YS_OVERRIDE { log_assert(module == cell->module); if (auto_reload_module) return; port_del(cell, port, old_sig); port_add(cell, port, sig); } void notify_connect(RTLIL::Module *mod YS_ATTRIBUTE(unused), const RTLIL::SigSig &sigsig) YS_OVERRIDE { log_assert(module == mod); if (auto_reload_module) return; for (int i = 0; i < GetSize(sigsig.first); i++) { RTLIL::SigBit lhs = sigmap(sigsig.first[i]); RTLIL::SigBit rhs = sigmap(sigsig.second[i]); bool has_lhs = database.count(lhs) != 0; bool has_rhs = database.count(rhs) != 0; if (!has_lhs && !has_rhs) { sigmap.add(lhs, rhs); } else if (!has_rhs) { SigBitInfo new_info = database.at(lhs); database.erase(lhs); sigmap.add(lhs, rhs); lhs = sigmap(lhs); if (lhs.wire) database[lhs] = new_info; } else if (!has_lhs) { SigBitInfo new_info = database.at(rhs); database.erase(rhs); sigmap.add(lhs, rhs); rhs = sigmap(rhs); if (rhs.wire) database[rhs] = new_info; } else { SigBitInfo new_info = database.at(lhs); new_info.merge(database.at(rhs)); database.erase(lhs); database.erase(rhs); sigmap.add(lhs, rhs); rhs = sigmap(rhs); if (rhs.wire) database[rhs] = new_info; } } } void notify_connect(RTLIL::Module *mod YS_ATTRIBUTE(unused), const std::vector<RTLIL::SigSig>&) YS_OVERRIDE { log_assert(module == mod); auto_reload_module = true; } void notify_blackout(RTLIL::Module *mod YS_ATTRIBUTE(unused)) YS_OVERRIDE { log_assert(module == mod); auto_reload_module = true; } ModIndex(RTLIL::Module *_m) : sigmap(_m), module(_m) { auto_reload_counter = 0; auto_reload_module = true; module->monitors.insert(this); } ~ModIndex() { module->monitors.erase(this); } SigBitInfo *query(RTLIL::SigBit bit) { if (auto_reload_module) reload_module(); auto it = database.find(sigmap(bit)); if (it == database.end()) return nullptr; else return &it->second; } bool query_is_input(RTLIL::SigBit bit) { const SigBitInfo *info = query(bit); if (info == nullptr) return false; return info->is_input; } bool query_is_output(RTLIL::SigBit bit) { const SigBitInfo *info = query(bit); if (info == nullptr) return false; return info->is_output; } pool<PortInfo> &query_ports(RTLIL::SigBit bit) { static pool<PortInfo> empty_result_set; SigBitInfo *info = query(bit); if (info == nullptr) return empty_result_set; return info->ports; } void dump_db() { log("--- ModIndex Dump ---\n"); if (auto_reload_module) { log("AUTO-RELOAD\n"); reload_module(); } for (auto &it : database) { log("BIT %s:\n", log_signal(it.first)); if (it.second.is_input) log(" PRIMARY INPUT\n"); if (it.second.is_output) log(" PRIMARY OUTPUT\n"); for (auto &port : it.second.ports) log(" PORT: %s.%s[%d] (%s)\n", log_id(port.cell), log_id(port.port), port.offset, log_id(port.cell->type)); } } }; struct ModWalker { struct PortBit { RTLIL::Cell *cell; RTLIL::IdString port; int offset; bool operator<(const PortBit &other) const { if (cell != other.cell) return cell < other.cell; if (port != other.port) return port < other.port; return offset < other.offset; } bool operator==(const PortBit &other) const { return cell == other.cell && port == other.port && offset == other.offset; } unsigned int hash() const { return mkhash_add(mkhash(cell->name.hash(), port.hash()), offset); } }; RTLIL::Design *design; RTLIL::Module *module; CellTypes ct; SigMap sigmap; dict<RTLIL::SigBit, pool<PortBit>> signal_drivers; dict<RTLIL::SigBit, pool<PortBit>> signal_consumers; pool<RTLIL::SigBit> signal_inputs, signal_outputs; dict<RTLIL::Cell*, pool<RTLIL::SigBit>> cell_outputs, cell_inputs; void add_wire(RTLIL::Wire *wire) { if (wire->port_input) { std::vector<RTLIL::SigBit> bits = sigmap(wire); for (auto bit : bits) if (bit.wire != NULL) signal_inputs.insert(bit); } if (wire->port_output) { std::vector<RTLIL::SigBit> bits = sigmap(wire); for (auto bit : bits) if (bit.wire != NULL) signal_outputs.insert(bit); } } void add_cell_port(RTLIL::Cell *cell, RTLIL::IdString port, std::vector<RTLIL::SigBit> bits, bool is_output, bool is_input) { for (int i = 0; i < int(bits.size()); i++) if (bits[i].wire != NULL) { PortBit pbit = { cell, port, i }; if (is_output) { signal_drivers[bits[i]].insert(pbit); cell_outputs[cell].insert(bits[i]); } if (is_input) { signal_consumers[bits[i]].insert(pbit); cell_inputs[cell].insert(bits[i]); } } } void add_cell(RTLIL::Cell *cell) { if (ct.cell_known(cell->type)) { for (auto &conn : cell->connections()) add_cell_port(cell, conn.first, sigmap(conn.second), ct.cell_output(cell->type, conn.first), ct.cell_input(cell->type, conn.first)); } else { for (auto &conn : cell->connections()) add_cell_port(cell, conn.first, sigmap(conn.second), true, true); } } ModWalker() : design(NULL), module(NULL) { } ModWalker(RTLIL::Design *design, RTLIL::Module *module, CellTypes *filter_ct = NULL) { setup(design, module, filter_ct); } void setup(RTLIL::Design *design, RTLIL::Module *module, CellTypes *filter_ct = NULL) { this->design = design; this->module = module; ct.clear(); ct.setup(design); sigmap.set(module); signal_drivers.clear(); signal_consumers.clear(); signal_inputs.clear(); signal_outputs.clear(); for (auto &it : module->wires_) add_wire(it.second); for (auto &it : module->cells_) if (filter_ct == NULL || filter_ct->cell_known(it.second->type)) add_cell(it.second); } // get_* methods -- single RTLIL::SigBit template<typename T> inline bool get_drivers(pool<PortBit> &result, RTLIL::SigBit bit) const { bool found = false; if (signal_drivers.count(bit)) { const pool<PortBit> &r = signal_drivers.at(bit); result.insert(r.begin(), r.end()); found = true; } return found; } template<typename T> inline bool get_consumers(pool<PortBit> &result, RTLIL::SigBit bit) const { bool found = false; if (signal_consumers.count(bit)) { const pool<PortBit> &r = signal_consumers.at(bit); result.insert(r.begin(), r.end()); found = true; } return found; } template<typename T> inline bool get_inputs(pool<RTLIL::SigBit> &result, RTLIL::SigBit bit) const { bool found = false; if (signal_inputs.count(bit)) result.insert(bit), found = true; return found; } template<typename T> inline bool get_outputs(pool<RTLIL::SigBit> &result, RTLIL::SigBit bit) const { bool found = false; if (signal_outputs.count(bit)) result.insert(bit), found = true; return found; } // get_* methods -- container of RTLIL::SigBit's (always by reference) template<typename T> inline bool get_drivers(pool<PortBit> &result, const T &bits) const { bool found = false; for (RTLIL::SigBit bit : bits) if (signal_drivers.count(bit)) { const pool<PortBit> &r = signal_drivers.at(bit); result.insert(r.begin(), r.end()); found = true; } return found; } template<typename T> inline bool get_consumers(pool<PortBit> &result, const T &bits) const { bool found = false; for (RTLIL::SigBit bit : bits) if (signal_consumers.count(bit)) { const pool<PortBit> &r = signal_consumers.at(bit); result.insert(r.begin(), r.end()); found = true; } return found; } template<typename T> inline bool get_inputs(pool<RTLIL::SigBit> &result, const T &bits) const { bool found = false; for (RTLIL::SigBit bit : bits) if (signal_inputs.count(bit)) result.insert(bit), found = true; return found; } template<typename T> inline bool get_outputs(pool<RTLIL::SigBit> &result, const T &bits) const { bool found = false; for (RTLIL::SigBit bit : bits) if (signal_outputs.count(bit)) result.insert(bit), found = true; return found; } // get_* methods -- call by RTLIL::SigSpec (always by value) bool get_drivers(pool<PortBit> &result, RTLIL::SigSpec signal) const { std::vector<RTLIL::SigBit> bits = sigmap(signal); return get_drivers(result, bits); } bool get_consumers(pool<PortBit> &result, RTLIL::SigSpec signal) const { std::vector<RTLIL::SigBit> bits = sigmap(signal); return get_consumers(result, bits); } bool get_inputs(pool<RTLIL::SigBit> &result, RTLIL::SigSpec signal) const { std::vector<RTLIL::SigBit> bits = sigmap(signal); return get_inputs(result, bits); } bool get_outputs(pool<RTLIL::SigBit> &result, RTLIL::SigSpec signal) const { std::vector<RTLIL::SigBit> bits = sigmap(signal); return get_outputs(result, bits); } // has_* methods -- call by reference template<typename T> inline bool has_drivers(const T &sig) const { pool<PortBit> result; return get_drivers(result, sig); } template<typename T> inline bool has_consumers(const T &sig) const { pool<PortBit> result; return get_consumers(result, sig); } template<typename T> inline bool has_inputs(const T &sig) const { pool<RTLIL::SigBit> result; return get_inputs(result, sig); } template<typename T> inline bool has_outputs(const T &sig) const { pool<RTLIL::SigBit> result; return get_outputs(result, sig); } // has_* methods -- call by value inline bool has_drivers(RTLIL::SigSpec sig) const { pool<PortBit> result; return get_drivers(result, sig); } inline bool has_consumers(RTLIL::SigSpec sig) const { pool<PortBit> result; return get_consumers(result, sig); } inline bool has_inputs(RTLIL::SigSpec sig) const { pool<RTLIL::SigBit> result; return get_inputs(result, sig); } inline bool has_outputs(RTLIL::SigSpec sig) const { pool<RTLIL::SigBit> result; return get_outputs(result, sig); } }; YOSYS_NAMESPACE_END #endif