aboutsummaryrefslogtreecommitdiffstats
path: root/passes/techmap/counters.cc
blob: e6e1111c19e69c82c9ba30cb4f0dcc3d2bdf7260 (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
/*
 *  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"
#include "kernel/modtools.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

//get the list of cells hooked up to at least one bit of a given net
pool<Cell*> get_other_cells(const RTLIL::SigSpec& port, ModIndex& index, Cell* src)
{
	pool<Cell*> rval;
	for(auto b : port)
	{
		pool<ModIndex::PortInfo> ports = index.query_ports(b);
		for(auto x : ports)
		{
			if(x.cell == src)
				continue;
			rval.insert(x.cell);
		}
	}
	return rval;
}

//return true if there is a full-width bus connection from cell a port ap to cell b port bp
//if other_conns_allowed is false, then we require a strict point to point connection (no other links)
bool is_full_bus(
	const RTLIL::SigSpec& sig,
	ModIndex& index,
	Cell* a,
	RTLIL::IdString ap,
	Cell* b,
	RTLIL::IdString bp,
	bool other_conns_allowed = false)
{
	for(auto s : sig)
	{
		pool<ModIndex::PortInfo> ports = index.query_ports(s);
		bool found_a = false;
		bool found_b = false;
		for(auto x : ports)
		{
			if( (x.cell == a) && (x.port == ap) )
				found_a = true;
			else if( (x.cell == b) && (x.port == bp) )
				found_b = true;
			else if(!other_conns_allowed)
				return false;
		}
		
		if( (!found_a) || (!found_b) )
			return false;
	}
	
	return true;
}

//return true if the signal connects to one port only (nothing on the other end)
bool is_unconnected(const RTLIL::SigSpec& port, ModIndex& index)
{
	for(auto b : port)
	{
		pool<ModIndex::PortInfo> ports = index.query_ports(b);
		if(ports.size() > 1)
			return false;
	}
	
	return true;
}

void counters_worker(ModIndex& index, Module */*module*/, Cell *cell, unsigned int& total_counters)
{
	SigMap& sigmap = index.sigmap;
	
	//Core of the counter must be an ALU
	if (cell->type != "$alu")
		return;
	
	//GreenPak does not support counters larger than 14 bits so immediately skip anything bigger
	int a_width = cell->getParam("\\A_WIDTH").as_int();
	if(a_width > 14)
		return;
		
	//Second input must be a single bit
	int b_width = cell->getParam("\\B_WIDTH").as_int();
	if(b_width != 1)
		return;
		
	//Both inputs must be unsigned, so don't extract anything with a signed input
	bool a_sign = cell->getParam("\\A_SIGNED").as_bool();
	bool b_sign = cell->getParam("\\B_SIGNED").as_bool();
	if(a_sign || b_sign)
		return;

	//To be a counter, one input of the ALU must be a constant 1
	//TODO: can A or B be swapped in synthesized RTL or is B always the 1?
	const RTLIL::SigSpec b_port = sigmap(cell->getPort("\\B"));
	if(!b_port.is_fully_const() || (b_port.as_int() != 1) )
		return;
		
	//BI and CI must be constant 1 as well
	const RTLIL::SigSpec bi_port = sigmap(cell->getPort("\\BI"));
	if(!bi_port.is_fully_const() || (bi_port.as_int() != 1) )
		return;
	const RTLIL::SigSpec ci_port = sigmap(cell->getPort("\\CI"));
	if(!ci_port.is_fully_const() || (ci_port.as_int() != 1) )
		return;
				
	//CO and X must be unconnected (exactly one connection to each port)
	if(!is_unconnected(sigmap(cell->getPort("\\CO")), index))
		return;
	if(!is_unconnected(sigmap(cell->getPort("\\X")), index))
		return;
		
	//Y must have exactly one connection, and it has to be a $mux cell.
	//We must have a direct bus connection from our Y to their A.
	const RTLIL::SigSpec aluy = sigmap(cell->getPort("\\Y"));
	pool<Cell*> y_loads = get_other_cells(aluy, index, cell);
	if(y_loads.size() != 1)
		return;
	Cell* count_mux = *y_loads.begin();
	if(count_mux->type != "$mux")
		return;
	if(!is_full_bus(aluy, index, cell, "\\Y", count_mux, "\\A"))
		return;

	//B connection of the mux is our overflow value
	const RTLIL::SigSpec overflow = sigmap(count_mux->getPort("\\B"));
	if(!overflow.is_fully_const())
		return;
	int count_value = overflow.as_int();
	
	//S connection of the mux must come from an inverter (need not be the only load)
	const RTLIL::SigSpec muxsel = sigmap(count_mux->getPort("\\S"));
	pool<Cell*> muxsel_conns = get_other_cells(muxsel, index, count_mux);
	Cell* underflow_inv = NULL;
	for(auto c : muxsel_conns)
	{		
		if(c->type != "$logic_not")
			continue;
		if(!is_full_bus(muxsel, index, c, "\\Y", count_mux, "\\S", true))
			continue;
	
		underflow_inv = c;
		break;
	}
	if(underflow_inv == NULL)
		return;
	
	//Y connection of the mux must have exactly one load, the counter's internal register
	const RTLIL::SigSpec muxy = sigmap(count_mux->getPort("\\Y"));
	pool<Cell*> muxy_loads = get_other_cells(muxy, index, count_mux);
	if(muxy_loads.size() != 1)
		return;
	Cell* count_reg = *muxy_loads.begin();
	if(count_reg->type != "$dff")			//TODO: support dffr/dffs?
		return;
	if(!is_full_bus(muxy, index, count_mux, "\\Y", count_reg, "\\D"))
		return;
		
	//Register output must have exactly two loads, the inverter and ALU
	const RTLIL::SigSpec cnout = sigmap(count_reg->getPort("\\Q"));
	pool<Cell*> cnout_loads = get_other_cells(cnout, index, count_reg);
	if(cnout_loads.size() != 2)
		return;
	if(!is_full_bus(cnout, index, count_reg, "\\Q", underflow_inv, "\\A", true))
		return;
	if(!is_full_bus(cnout, index, count_reg, "\\Q", cell, "\\A", true))
		return;
	
	//Register output net must have an INIT attribute equal to the count value
	auto rwire = cnout.as_wire();
	if(rwire->attributes.find("\\init") == rwire->attributes.end())
		return;
	int rinit = rwire->attributes["\\init"].as_int();
	if(rinit != count_value)
		return;
	
	//Figure out the final cell type based on the counter size
	string celltype = "\\GP_COUNT8";
	if(a_width > 8)
		celltype = "\\GP_COUNT14";
	
	//Log it
	total_counters ++;
	log("  Extracting %d-bit counter to %s hard macro\n", a_width, celltype.c_str());
	log("    Decrementer: %s\n", cell->name.c_str());
	log("    Output mux:  %s\n", count_mux->name.c_str());
	log("    Register:    %s\n", count_reg->name.c_str());
	log("    Comparator:  %s\n", underflow_inv->name.c_str());
	log("    Count value: %d\n", count_value);
	
	
	/*
	log("Converting %s cell %s.%s to $adff.\n", log_id(cell->type), log_id(module), log_id(cell));

	if (GetSize(setctrl) == 1) {
		cell->setPort("\\ARST", setctrl);
		cell->setParam("\\ARST_POLARITY", setpol);
	} else {
		cell->setPort("\\ARST", clrctrl);
		cell->setParam("\\ARST_POLARITY", clrpol);
	}

	cell->type = "$adff";
	cell->unsetPort("\\SET");
	cell->unsetPort("\\CLR");
	cell->setParam("\\ARST_VALUE", reset_val);
	cell->unsetParam("\\SET_POLARITY");
	cell->unsetParam("\\CLR_POLARITY");

	return;
	*/
}

struct CountersPass : public Pass {
	CountersPass() : Pass("counters", "Extract counter cells") { }
	virtual void help()
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    counters [options] [selection]\n");
		log("\n");
		log("This pass converts resettable down counters to GreenPak counter cells\n");
		log("\n");
	}
	virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
	{
		log_header("Executing COUNTERS pass (mapping counters to GP_COUNTx cells).\n");
		
		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			// if (args[argidx] == "-v") {
			// 	continue;
			// }
			break;
		}
		extra_args(args, argidx, design);
		
		unsigned int total_counters = 0;
		for (auto module : design->selected_modules())
		{
			ModIndex index(module);
			for (auto cell : module->selected_cells())
				counters_worker(index, module, cell, total_counters);
		}
		
		if(total_counters)
			log("Extracted %u counters\n", total_counters);
		
	}
} CountersPass;

PRIVATE_NAMESPACE_END