Merge pull request #576 from cr1901/no-resource
[yosys.git] / kernel / celltypes.h
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 #ifndef CELLTYPES_H
21 #define CELLTYPES_H
22
23 #include "kernel/yosys.h"
24
25 YOSYS_NAMESPACE_BEGIN
26
27 struct CellType
28 {
29 RTLIL::IdString type;
30 pool<RTLIL::IdString> inputs, outputs;
31 bool is_evaluable;
32 };
33
34 struct CellTypes
35 {
36 dict<RTLIL::IdString, CellType> cell_types;
37
38 CellTypes()
39 {
40 }
41
42 CellTypes(RTLIL::Design *design)
43 {
44 setup(design);
45 }
46
47 void setup(RTLIL::Design *design = NULL)
48 {
49 if (design)
50 setup_design(design);
51
52 setup_internals();
53 setup_internals_mem();
54 setup_stdcells();
55 setup_stdcells_mem();
56 }
57
58 void setup_type(RTLIL::IdString type, const pool<RTLIL::IdString> &inputs, const pool<RTLIL::IdString> &outputs, bool is_evaluable = false)
59 {
60 CellType ct = {type, inputs, outputs, is_evaluable};
61 cell_types[ct.type] = ct;
62 }
63
64 void setup_module(RTLIL::Module *module)
65 {
66 pool<RTLIL::IdString> inputs, outputs;
67 for (RTLIL::IdString wire_name : module->ports) {
68 RTLIL::Wire *wire = module->wire(wire_name);
69 if (wire->port_input)
70 inputs.insert(wire->name);
71 if (wire->port_output)
72 outputs.insert(wire->name);
73 }
74 setup_type(module->name, inputs, outputs);
75 }
76
77 void setup_design(RTLIL::Design *design)
78 {
79 for (auto module : design->modules())
80 setup_module(module);
81 }
82
83 void setup_internals()
84 {
85 std::vector<RTLIL::IdString> unary_ops = {
86 "$not", "$pos", "$neg",
87 "$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool",
88 "$logic_not", "$slice", "$lut", "$sop"
89 };
90
91 std::vector<RTLIL::IdString> binary_ops = {
92 "$and", "$or", "$xor", "$xnor",
93 "$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx",
94 "$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt",
95 "$add", "$sub", "$mul", "$div", "$mod", "$pow",
96 "$logic_and", "$logic_or", "$concat", "$macc"
97 };
98 IdString A = "\\A", B = "\\B", S = "\\S", Y = "\\Y";
99 IdString P = "\\P", G = "\\G", C = "\\C", X = "\\X";
100 IdString BI = "\\BI", CI = "\\CI", CO = "\\CO", EN = "\\EN";
101
102 for (auto type : unary_ops)
103 setup_type(type, {A}, {Y}, true);
104
105 for (auto type : binary_ops)
106 setup_type(type, {A, B}, {Y}, true);
107
108 for (auto type : std::vector<RTLIL::IdString>({"$mux", "$pmux"}))
109 setup_type(type, {A, B, S}, {Y}, true);
110
111 setup_type("$lcu", {P, G, CI}, {CO}, true);
112 setup_type("$alu", {A, B, CI, BI}, {X, Y, CO}, true);
113 setup_type("$fa", {A, B, C}, {X, Y}, true);
114
115 setup_type("$tribuf", {A, EN}, {Y}, true);
116
117 setup_type("$assert", {A, EN}, pool<RTLIL::IdString>(), true);
118 setup_type("$assume", {A, EN}, pool<RTLIL::IdString>(), true);
119 setup_type("$live", {A, EN}, pool<RTLIL::IdString>(), true);
120 setup_type("$fair", {A, EN}, pool<RTLIL::IdString>(), true);
121 setup_type("$cover", {A, EN}, pool<RTLIL::IdString>(), true);
122 setup_type("$initstate", pool<RTLIL::IdString>(), {Y}, true);
123 setup_type("$anyconst", pool<RTLIL::IdString>(), {Y}, true);
124 setup_type("$anyseq", pool<RTLIL::IdString>(), {Y}, true);
125 setup_type("$allconst", pool<RTLIL::IdString>(), {Y}, true);
126 setup_type("$allseq", pool<RTLIL::IdString>(), {Y}, true);
127 setup_type("$equiv", {A, B}, {Y}, true);
128 }
129
130 void setup_internals_mem()
131 {
132 IdString SET = "\\SET", CLR = "\\CLR", CLK = "\\CLK", ARST = "\\ARST", EN = "\\EN";
133 IdString Q = "\\Q", D = "\\D", ADDR = "\\ADDR", DATA = "\\DATA", RD_EN = "\\RD_EN";
134 IdString RD_CLK = "\\RD_CLK", RD_ADDR = "\\RD_ADDR", WR_CLK = "\\WR_CLK", WR_EN = "\\WR_EN";
135 IdString WR_ADDR = "\\WR_ADDR", WR_DATA = "\\WR_DATA", RD_DATA = "\\RD_DATA";
136 IdString CTRL_IN = "\\CTRL_IN", CTRL_OUT = "\\CTRL_OUT";
137
138 setup_type("$sr", {SET, CLR}, {Q});
139 setup_type("$ff", {D}, {Q});
140 setup_type("$dff", {CLK, D}, {Q});
141 setup_type("$dffe", {CLK, EN, D}, {Q});
142 setup_type("$dffsr", {CLK, SET, CLR, D}, {Q});
143 setup_type("$adff", {CLK, ARST, D}, {Q});
144 setup_type("$dlatch", {EN, D}, {Q});
145 setup_type("$dlatchsr", {EN, SET, CLR, D}, {Q});
146
147 setup_type("$memrd", {CLK, EN, ADDR}, {DATA});
148 setup_type("$memwr", {CLK, EN, ADDR, DATA}, pool<RTLIL::IdString>());
149 setup_type("$meminit", {ADDR, DATA}, pool<RTLIL::IdString>());
150 setup_type("$mem", {RD_CLK, RD_EN, RD_ADDR, WR_CLK, WR_EN, WR_ADDR, WR_DATA}, {RD_DATA});
151
152 setup_type("$fsm", {CLK, ARST, CTRL_IN}, {CTRL_OUT});
153 }
154
155 void setup_stdcells()
156 {
157 IdString A = "\\A", B = "\\B", C = "\\C", D = "\\D";
158 IdString E = "\\E", F = "\\F", G = "\\G", H = "\\H";
159 IdString I = "\\I", J = "\\J", K = "\\K", L = "\\L";
160 IdString M = "\\I", N = "\\N", O = "\\O", P = "\\P";
161 IdString S = "\\S", T = "\\T", U = "\\U", V = "\\V";
162 IdString Y = "\\Y";
163
164 setup_type("$_BUF_", {A}, {Y}, true);
165 setup_type("$_NOT_", {A}, {Y}, true);
166 setup_type("$_AND_", {A, B}, {Y}, true);
167 setup_type("$_NAND_", {A, B}, {Y}, true);
168 setup_type("$_OR_", {A, B}, {Y}, true);
169 setup_type("$_NOR_", {A, B}, {Y}, true);
170 setup_type("$_XOR_", {A, B}, {Y}, true);
171 setup_type("$_XNOR_", {A, B}, {Y}, true);
172 setup_type("$_ANDNOT_", {A, B}, {Y}, true);
173 setup_type("$_ORNOT_", {A, B}, {Y}, true);
174 setup_type("$_MUX_", {A, B, S}, {Y}, true);
175 setup_type("$_MUX4_", {A, B, C, D, S, T}, {Y}, true);
176 setup_type("$_MUX8_", {A, B, C, D, E, F, G, H, S, T, U}, {Y}, true);
177 setup_type("$_MUX16_", {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, S, T, U, V}, {Y}, true);
178 setup_type("$_AOI3_", {A, B, C}, {Y}, true);
179 setup_type("$_OAI3_", {A, B, C}, {Y}, true);
180 setup_type("$_AOI4_", {A, B, C, D}, {Y}, true);
181 setup_type("$_OAI4_", {A, B, C, D}, {Y}, true);
182 setup_type("$_TBUF_", {A, E}, {Y}, true);
183 }
184
185 void setup_stdcells_mem()
186 {
187 IdString S = "\\S", R = "\\R", C = "\\C";
188 IdString D = "\\D", Q = "\\Q", E = "\\E";
189
190 std::vector<char> list_np = {'N', 'P'}, list_01 = {'0', '1'};
191
192 for (auto c1 : list_np)
193 for (auto c2 : list_np)
194 setup_type(stringf("$_SR_%c%c_", c1, c2), {S, R}, {Q});
195
196 setup_type("$_FF_", {D}, {Q});
197
198 for (auto c1 : list_np)
199 setup_type(stringf("$_DFF_%c_", c1), {C, D}, {Q});
200
201 for (auto c1 : list_np)
202 for (auto c2 : list_np)
203 setup_type(stringf("$_DFFE_%c%c_", c1, c2), {C, D, E}, {Q});
204
205 for (auto c1 : list_np)
206 for (auto c2 : list_np)
207 for (auto c3 : list_01)
208 setup_type(stringf("$_DFF_%c%c%c_", c1, c2, c3), {C, R, D}, {Q});
209
210 for (auto c1 : list_np)
211 for (auto c2 : list_np)
212 for (auto c3 : list_np)
213 setup_type(stringf("$_DFFSR_%c%c%c_", c1, c2, c3), {C, S, R, D}, {Q});
214
215 for (auto c1 : list_np)
216 setup_type(stringf("$_DLATCH_%c_", c1), {E, D}, {Q});
217
218 for (auto c1 : list_np)
219 for (auto c2 : list_np)
220 for (auto c3 : list_np)
221 setup_type(stringf("$_DLATCHSR_%c%c%c_", c1, c2, c3), {E, S, R, D}, {Q});
222 }
223
224 void clear()
225 {
226 cell_types.clear();
227 }
228
229 bool cell_known(RTLIL::IdString type)
230 {
231 return cell_types.count(type) != 0;
232 }
233
234 bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
235 {
236 auto it = cell_types.find(type);
237 return it != cell_types.end() && it->second.outputs.count(port) != 0;
238 }
239
240 bool cell_input(RTLIL::IdString type, RTLIL::IdString port)
241 {
242 auto it = cell_types.find(type);
243 return it != cell_types.end() && it->second.inputs.count(port) != 0;
244 }
245
246 bool cell_evaluable(RTLIL::IdString type)
247 {
248 auto it = cell_types.find(type);
249 return it != cell_types.end() && it->second.is_evaluable;
250 }
251
252 static RTLIL::Const eval_not(RTLIL::Const v)
253 {
254 for (auto &bit : v.bits)
255 if (bit == RTLIL::S0) bit = RTLIL::S1;
256 else if (bit == RTLIL::S1) bit = RTLIL::S0;
257 return v;
258 }
259
260 static RTLIL::Const eval(RTLIL::IdString type, const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
261 {
262 if (type == "$sshr" && !signed1)
263 type = "$shr";
264 if (type == "$sshl" && !signed1)
265 type = "$shl";
266
267 if (type != "$sshr" && type != "$sshl" && type != "$shr" && type != "$shl" && type != "$shift" && type != "$shiftx" &&
268 type != "$pos" && type != "$neg" && type != "$not") {
269 if (!signed1 || !signed2)
270 signed1 = false, signed2 = false;
271 }
272
273 #define HANDLE_CELL_TYPE(_t) if (type == "$" #_t) return const_ ## _t(arg1, arg2, signed1, signed2, result_len);
274 HANDLE_CELL_TYPE(not)
275 HANDLE_CELL_TYPE(and)
276 HANDLE_CELL_TYPE(or)
277 HANDLE_CELL_TYPE(xor)
278 HANDLE_CELL_TYPE(xnor)
279 HANDLE_CELL_TYPE(reduce_and)
280 HANDLE_CELL_TYPE(reduce_or)
281 HANDLE_CELL_TYPE(reduce_xor)
282 HANDLE_CELL_TYPE(reduce_xnor)
283 HANDLE_CELL_TYPE(reduce_bool)
284 HANDLE_CELL_TYPE(logic_not)
285 HANDLE_CELL_TYPE(logic_and)
286 HANDLE_CELL_TYPE(logic_or)
287 HANDLE_CELL_TYPE(shl)
288 HANDLE_CELL_TYPE(shr)
289 HANDLE_CELL_TYPE(sshl)
290 HANDLE_CELL_TYPE(sshr)
291 HANDLE_CELL_TYPE(shift)
292 HANDLE_CELL_TYPE(shiftx)
293 HANDLE_CELL_TYPE(lt)
294 HANDLE_CELL_TYPE(le)
295 HANDLE_CELL_TYPE(eq)
296 HANDLE_CELL_TYPE(ne)
297 HANDLE_CELL_TYPE(eqx)
298 HANDLE_CELL_TYPE(nex)
299 HANDLE_CELL_TYPE(ge)
300 HANDLE_CELL_TYPE(gt)
301 HANDLE_CELL_TYPE(add)
302 HANDLE_CELL_TYPE(sub)
303 HANDLE_CELL_TYPE(mul)
304 HANDLE_CELL_TYPE(div)
305 HANDLE_CELL_TYPE(mod)
306 HANDLE_CELL_TYPE(pow)
307 HANDLE_CELL_TYPE(pos)
308 HANDLE_CELL_TYPE(neg)
309 #undef HANDLE_CELL_TYPE
310
311 if (type == "$_BUF_")
312 return arg1;
313 if (type == "$_NOT_")
314 return eval_not(arg1);
315 if (type == "$_AND_")
316 return const_and(arg1, arg2, false, false, 1);
317 if (type == "$_NAND_")
318 return eval_not(const_and(arg1, arg2, false, false, 1));
319 if (type == "$_OR_")
320 return const_or(arg1, arg2, false, false, 1);
321 if (type == "$_NOR_")
322 return eval_not(const_or(arg1, arg2, false, false, 1));
323 if (type == "$_XOR_")
324 return const_xor(arg1, arg2, false, false, 1);
325 if (type == "$_XNOR_")
326 return const_xnor(arg1, arg2, false, false, 1);
327 if (type == "$_ANDNOT_")
328 return const_and(arg1, eval_not(arg2), false, false, 1);
329 if (type == "$_ORNOT_")
330 return const_or(arg1, eval_not(arg2), false, false, 1);
331
332 log_abort();
333 }
334
335 static RTLIL::Const eval(RTLIL::Cell *cell, const RTLIL::Const &arg1, const RTLIL::Const &arg2)
336 {
337 if (cell->type == "$slice") {
338 RTLIL::Const ret;
339 int width = cell->parameters.at("\\Y_WIDTH").as_int();
340 int offset = cell->parameters.at("\\OFFSET").as_int();
341 ret.bits.insert(ret.bits.end(), arg1.bits.begin()+offset, arg1.bits.begin()+offset+width);
342 return ret;
343 }
344
345 if (cell->type == "$concat") {
346 RTLIL::Const ret = arg1;
347 ret.bits.insert(ret.bits.end(), arg2.bits.begin(), arg2.bits.end());
348 return ret;
349 }
350
351 if (cell->type == "$lut")
352 {
353 int width = cell->parameters.at("\\WIDTH").as_int();
354
355 std::vector<RTLIL::State> t = cell->parameters.at("\\LUT").bits;
356 while (GetSize(t) < (1 << width))
357 t.push_back(RTLIL::S0);
358 t.resize(1 << width);
359
360 for (int i = width-1; i >= 0; i--) {
361 RTLIL::State sel = arg1.bits.at(i);
362 std::vector<RTLIL::State> new_t;
363 if (sel == RTLIL::S0)
364 new_t = std::vector<RTLIL::State>(t.begin(), t.begin() + GetSize(t)/2);
365 else if (sel == RTLIL::S1)
366 new_t = std::vector<RTLIL::State>(t.begin() + GetSize(t)/2, t.end());
367 else
368 for (int j = 0; j < GetSize(t)/2; j++)
369 new_t.push_back(t[j] == t[j + GetSize(t)/2] ? t[j] : RTLIL::Sx);
370 t.swap(new_t);
371 }
372
373 log_assert(GetSize(t) == 1);
374 return t;
375 }
376
377 if (cell->type == "$sop")
378 {
379 int width = cell->parameters.at("\\WIDTH").as_int();
380 int depth = cell->parameters.at("\\DEPTH").as_int();
381 std::vector<RTLIL::State> t = cell->parameters.at("\\TABLE").bits;
382
383 while (GetSize(t) < width*depth*2)
384 t.push_back(RTLIL::S0);
385
386 RTLIL::State default_ret = State::S0;
387
388 for (int i = 0; i < depth; i++)
389 {
390 bool match = true;
391 bool match_x = true;
392
393 for (int j = 0; j < width; j++) {
394 RTLIL::State a = arg1.bits.at(j);
395 if (t.at(2*width*i + 2*j + 0) == State::S1) {
396 if (a == State::S1) match_x = false;
397 if (a != State::S0) match = false;
398 }
399 if (t.at(2*width*i + 2*j + 1) == State::S1) {
400 if (a == State::S0) match_x = false;
401 if (a != State::S1) match = false;
402 }
403 }
404
405 if (match)
406 return State::S1;
407
408 if (match_x)
409 default_ret = State::Sx;
410 }
411
412 return default_ret;
413 }
414
415 bool signed_a = cell->parameters.count("\\A_SIGNED") > 0 && cell->parameters["\\A_SIGNED"].as_bool();
416 bool signed_b = cell->parameters.count("\\B_SIGNED") > 0 && cell->parameters["\\B_SIGNED"].as_bool();
417 int result_len = cell->parameters.count("\\Y_WIDTH") > 0 ? cell->parameters["\\Y_WIDTH"].as_int() : -1;
418 return eval(cell->type, arg1, arg2, signed_a, signed_b, result_len);
419 }
420
421 static RTLIL::Const eval(RTLIL::Cell *cell, const RTLIL::Const &arg1, const RTLIL::Const &arg2, const RTLIL::Const &arg3)
422 {
423 if (cell->type.in("$mux", "$pmux", "$_MUX_")) {
424 RTLIL::Const ret = arg1;
425 for (size_t i = 0; i < arg3.bits.size(); i++)
426 if (arg3.bits[i] == RTLIL::State::S1) {
427 std::vector<RTLIL::State> bits(arg2.bits.begin() + i*arg1.bits.size(), arg2.bits.begin() + (i+1)*arg1.bits.size());
428 ret = RTLIL::Const(bits);
429 }
430 return ret;
431 }
432
433 if (cell->type == "$_AOI3_")
434 return eval_not(const_or(const_and(arg1, arg2, false, false, 1), arg3, false, false, 1));
435 if (cell->type == "$_OAI3_")
436 return eval_not(const_and(const_or(arg1, arg2, false, false, 1), arg3, false, false, 1));
437
438 log_assert(arg3.bits.size() == 0);
439 return eval(cell, arg1, arg2);
440 }
441
442 static RTLIL::Const eval(RTLIL::Cell *cell, const RTLIL::Const &arg1, const RTLIL::Const &arg2, const RTLIL::Const &arg3, const RTLIL::Const &arg4)
443 {
444 if (cell->type == "$_AOI4_")
445 return eval_not(const_or(const_and(arg1, arg2, false, false, 1), const_and(arg3, arg4, false, false, 1), false, false, 1));
446 if (cell->type == "$_OAI4_")
447 return eval_not(const_and(const_or(arg1, arg2, false, false, 1), const_and(arg3, arg4, false, false, 1), false, false, 1));
448
449 log_assert(arg4.bits.size() == 0);
450 return eval(cell, arg1, arg2, arg3);
451 }
452 };
453
454 // initialized by yosys_setup()
455 extern CellTypes yosys_celltypes;
456
457 YOSYS_NAMESPACE_END
458
459 #endif
460