2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
23 #include "kernel/yosys.h"
30 pool
<RTLIL::IdString
> inputs
, outputs
;
36 dict
<RTLIL::IdString
, CellType
> cell_types
;
42 CellTypes(RTLIL::Design
*design
)
47 void setup(RTLIL::Design
*design
= NULL
)
53 setup_internals_mem();
58 void setup_type(RTLIL::IdString type
, const pool
<RTLIL::IdString
> &inputs
, const pool
<RTLIL::IdString
> &outputs
, bool is_evaluable
= false)
60 CellType ct
= {type
, inputs
, outputs
, is_evaluable
};
61 cell_types
[ct
.type
] = ct
;
64 void setup_module(RTLIL::Module
*module
)
66 pool
<RTLIL::IdString
> inputs
, outputs
;
67 for (RTLIL::IdString wire_name
: module
->ports
) {
68 RTLIL::Wire
*wire
= module
->wire(wire_name
);
70 inputs
.insert(wire
->name
);
71 if (wire
->port_output
)
72 outputs
.insert(wire
->name
);
74 setup_type(module
->name
, inputs
, outputs
);
77 void setup_design(RTLIL::Design
*design
)
79 for (auto module
: design
->modules())
83 void setup_internals()
85 setup_internals_eval();
87 setup_type(ID($tribuf
), {ID::A
, ID::EN
}, {ID::Y
}, true);
89 setup_type(ID($
assert), {ID::A
, ID::EN
}, pool
<RTLIL::IdString
>(), true);
90 setup_type(ID($assume
), {ID::A
, ID::EN
}, pool
<RTLIL::IdString
>(), true);
91 setup_type(ID($live
), {ID::A
, ID::EN
}, pool
<RTLIL::IdString
>(), true);
92 setup_type(ID($fair
), {ID::A
, ID::EN
}, pool
<RTLIL::IdString
>(), true);
93 setup_type(ID($cover
), {ID::A
, ID::EN
}, pool
<RTLIL::IdString
>(), true);
94 setup_type(ID($initstate
), pool
<RTLIL::IdString
>(), {ID::Y
}, true);
95 setup_type(ID($anyconst
), pool
<RTLIL::IdString
>(), {ID::Y
}, true);
96 setup_type(ID($anyseq
), pool
<RTLIL::IdString
>(), {ID::Y
}, true);
97 setup_type(ID($allconst
), pool
<RTLIL::IdString
>(), {ID::Y
}, true);
98 setup_type(ID($allseq
), pool
<RTLIL::IdString
>(), {ID::Y
}, true);
99 setup_type(ID($equiv
), {ID::A
, ID::B
}, {ID::Y
}, true);
100 setup_type(ID($specify2
), {ID::EN
, ID::SRC
, ID::DST
}, pool
<RTLIL::IdString
>(), true);
101 setup_type(ID($specify3
), {ID::EN
, ID::SRC
, ID::DST
, ID::DAT
}, pool
<RTLIL::IdString
>(), true);
102 setup_type(ID($specrule
), {ID::EN_SRC
, ID::EN_DST
, ID::SRC
, ID::DST
}, pool
<RTLIL::IdString
>(), true);
105 void setup_internals_eval()
107 std::vector
<RTLIL::IdString
> unary_ops
= {
108 ID($
not), ID($pos
), ID($neg
),
109 ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
),
110 ID($logic_not
), ID($slice
), ID($lut
), ID($sop
)
113 std::vector
<RTLIL::IdString
> binary_ops
= {
114 ID($
and), ID($
or), ID($
xor), ID($xnor
),
115 ID($shl
), ID($shr
), ID($sshl
), ID($sshr
), ID($shift
), ID($shiftx
),
116 ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
),
117 ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($pow
),
118 ID($logic_and
), ID($logic_or
), ID($concat
), ID($macc
)
121 for (auto type
: unary_ops
)
122 setup_type(type
, {ID::A
}, {ID::Y
}, true);
124 for (auto type
: binary_ops
)
125 setup_type(type
, {ID::A
, ID::B
}, {ID::Y
}, true);
127 for (auto type
: std::vector
<RTLIL::IdString
>({ID($mux
), ID($pmux
)}))
128 setup_type(type
, {ID::A
, ID::B
, ID::S
}, {ID::Y
}, true);
130 setup_type(ID($lcu
), {ID::P
, ID::G
, ID::CI
}, {ID::CO
}, true);
131 setup_type(ID($alu
), {ID::A
, ID::B
, ID::CI
, ID::BI
}, {ID::X
, ID::Y
, ID::CO
}, true);
132 setup_type(ID($fa
), {ID::A
, ID::B
, ID::C
}, {ID::X
, ID::Y
}, true);
135 void setup_internals_ff()
137 setup_type(ID($sr
), {ID::SET
, ID::CLR
}, {ID::Q
});
138 setup_type(ID($ff
), {ID::D
}, {ID::Q
});
139 setup_type(ID($dff
), {ID::CLK
, ID::D
}, {ID::Q
});
140 setup_type(ID($dffe
), {ID::CLK
, ID::EN
, ID::D
}, {ID::Q
});
141 setup_type(ID($dffsr
), {ID::CLK
, ID::SET
, ID::CLR
, ID::D
}, {ID::Q
});
142 setup_type(ID($adff
), {ID::CLK
, ID::ARST
, ID::D
}, {ID::Q
});
143 setup_type(ID($dlatch
), {ID::EN
, ID::D
}, {ID::Q
});
144 setup_type(ID($dlatchsr
), {ID::EN
, ID::SET
, ID::CLR
, ID::D
}, {ID::Q
});
147 void setup_internals_mem()
149 setup_internals_ff();
151 setup_type(ID($memrd
), {ID::CLK
, ID::EN
, ID::ADDR
}, {ID::DATA
});
152 setup_type(ID($memwr
), {ID::CLK
, ID::EN
, ID::ADDR
, ID::DATA
}, pool
<RTLIL::IdString
>());
153 setup_type(ID($meminit
), {ID::ADDR
, ID::DATA
}, pool
<RTLIL::IdString
>());
154 setup_type(ID($mem
), {ID::RD_CLK
, ID::RD_EN
, ID::RD_ADDR
, ID::WR_CLK
, ID::WR_EN
, ID::WR_ADDR
, ID::WR_DATA
}, {ID::RD_DATA
});
156 setup_type(ID($fsm
), {ID::CLK
, ID::ARST
, ID::CTRL_IN
}, {ID::CTRL_OUT
});
159 void setup_stdcells()
161 setup_stdcells_eval();
163 setup_type(ID($_TBUF_
), {ID::A
, ID::E
}, {ID::Y
}, true);
166 void setup_stdcells_eval()
168 setup_type(ID($_BUF_
), {ID::A
}, {ID::Y
}, true);
169 setup_type(ID($_NOT_
), {ID::A
}, {ID::Y
}, true);
170 setup_type(ID($_AND_
), {ID::A
, ID::B
}, {ID::Y
}, true);
171 setup_type(ID($_NAND_
), {ID::A
, ID::B
}, {ID::Y
}, true);
172 setup_type(ID($_OR_
), {ID::A
, ID::B
}, {ID::Y
}, true);
173 setup_type(ID($_NOR_
), {ID::A
, ID::B
}, {ID::Y
}, true);
174 setup_type(ID($_XOR_
), {ID::A
, ID::B
}, {ID::Y
}, true);
175 setup_type(ID($_XNOR_
), {ID::A
, ID::B
}, {ID::Y
}, true);
176 setup_type(ID($_ANDNOT_
), {ID::A
, ID::B
}, {ID::Y
}, true);
177 setup_type(ID($_ORNOT_
), {ID::A
, ID::B
}, {ID::Y
}, true);
178 setup_type(ID($_MUX_
), {ID::A
, ID::B
, ID::S
}, {ID::Y
}, true);
179 setup_type(ID($_NMUX_
), {ID::A
, ID::B
, ID::S
}, {ID::Y
}, true);
180 setup_type(ID($_MUX4_
), {ID::A
, ID::B
, ID::C
, ID::D
, ID::S
, ID::T
}, {ID::Y
}, true);
181 setup_type(ID($_MUX8_
), {ID::A
, ID::B
, ID::C
, ID::D
, ID::E
, ID::F
, ID::G
, ID::H
, ID::S
, ID::T
, ID::U
}, {ID::Y
}, true);
182 setup_type(ID($_MUX16_
), {ID::A
, ID::B
, ID::C
, ID::D
, ID::E
, ID::F
, ID::G
, ID::H
, ID::I
, ID::J
, ID::K
, ID::L
, ID::M
, ID::N
, ID::O
, ID::P
, ID::S
, ID::T
, ID::U
, ID::V
}, {ID::Y
}, true);
183 setup_type(ID($_AOI3_
), {ID::A
, ID::B
, ID::C
}, {ID::Y
}, true);
184 setup_type(ID($_OAI3_
), {ID::A
, ID::B
, ID::C
}, {ID::Y
}, true);
185 setup_type(ID($_AOI4_
), {ID::A
, ID::B
, ID::C
, ID::D
}, {ID::Y
}, true);
186 setup_type(ID($_OAI4_
), {ID::A
, ID::B
, ID::C
, ID::D
}, {ID::Y
}, true);
189 void setup_stdcells_mem()
191 std::vector
<char> list_np
= {'N', 'P'}, list_01
= {'0', '1'};
193 for (auto c1
: list_np
)
194 for (auto c2
: list_np
)
195 setup_type(stringf("$_SR_%c%c_", c1
, c2
), {ID::S
, ID::R
}, {ID::Q
});
197 setup_type(ID($_FF_
), {ID::D
}, {ID::Q
});
199 for (auto c1
: list_np
)
200 setup_type(stringf("$_DFF_%c_", c1
), {ID::C
, ID::D
}, {ID::Q
});
202 for (auto c1
: list_np
)
203 for (auto c2
: list_np
)
204 setup_type(stringf("$_DFFE_%c%c_", c1
, c2
), {ID::C
, ID::D
, ID::E
}, {ID::Q
});
206 for (auto c1
: list_np
)
207 for (auto c2
: list_np
)
208 for (auto c3
: list_01
)
209 setup_type(stringf("$_DFF_%c%c%c_", c1
, c2
, c3
), {ID::C
, ID::R
, ID::D
}, {ID::Q
});
211 for (auto c1
: list_np
)
212 for (auto c2
: list_np
)
213 for (auto c3
: list_np
)
214 setup_type(stringf("$_DFFSR_%c%c%c_", c1
, c2
, c3
), {ID::C
, ID::S
, ID::R
, ID::D
}, {ID::Q
});
216 for (auto c1
: list_np
)
217 setup_type(stringf("$_DLATCH_%c_", c1
), {ID::E
, ID::D
}, {ID::Q
});
219 for (auto c1
: list_np
)
220 for (auto c2
: list_np
)
221 for (auto c3
: list_np
)
222 setup_type(stringf("$_DLATCHSR_%c%c%c_", c1
, c2
, c3
), {ID::E
, ID::S
, ID::R
, ID::D
}, {ID::Q
});
230 bool cell_known(RTLIL::IdString type
) const
232 return cell_types
.count(type
) != 0;
235 bool cell_output(RTLIL::IdString type
, RTLIL::IdString port
) const
237 auto it
= cell_types
.find(type
);
238 return it
!= cell_types
.end() && it
->second
.outputs
.count(port
) != 0;
241 bool cell_input(RTLIL::IdString type
, RTLIL::IdString port
) const
243 auto it
= cell_types
.find(type
);
244 return it
!= cell_types
.end() && it
->second
.inputs
.count(port
) != 0;
247 bool cell_evaluable(RTLIL::IdString type
) const
249 auto it
= cell_types
.find(type
);
250 return it
!= cell_types
.end() && it
->second
.is_evaluable
;
253 static RTLIL::Const
eval_not(RTLIL::Const v
)
255 for (auto &bit
: v
.bits
)
256 if (bit
== State::S0
) bit
= State::S1
;
257 else if (bit
== State::S1
) bit
= State::S0
;
261 static RTLIL::Const
eval(RTLIL::IdString type
, const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
, bool *errp
= nullptr)
263 if (type
== ID($sshr
) && !signed1
)
265 if (type
== ID($sshl
) && !signed1
)
268 if (type
!= ID($sshr
) && type
!= ID($sshl
) && type
!= ID($shr
) && type
!= ID($shl
) && type
!= ID($shift
) && type
!= ID($shiftx
) &&
269 type
!= ID($pos
) && type
!= ID($neg
) && type
!= ID($
not)) {
270 if (!signed1
|| !signed2
)
271 signed1
= false, signed2
= false;
274 #define HANDLE_CELL_TYPE(_t) if (type == ID($##_t)) return const_ ## _t(arg1, arg2, signed1, signed2, result_len);
275 HANDLE_CELL_TYPE(not)
276 HANDLE_CELL_TYPE(and)
278 HANDLE_CELL_TYPE(xor)
279 HANDLE_CELL_TYPE(xnor
)
280 HANDLE_CELL_TYPE(reduce_and
)
281 HANDLE_CELL_TYPE(reduce_or
)
282 HANDLE_CELL_TYPE(reduce_xor
)
283 HANDLE_CELL_TYPE(reduce_xnor
)
284 HANDLE_CELL_TYPE(reduce_bool
)
285 HANDLE_CELL_TYPE(logic_not
)
286 HANDLE_CELL_TYPE(logic_and
)
287 HANDLE_CELL_TYPE(logic_or
)
288 HANDLE_CELL_TYPE(shl
)
289 HANDLE_CELL_TYPE(shr
)
290 HANDLE_CELL_TYPE(sshl
)
291 HANDLE_CELL_TYPE(sshr
)
292 HANDLE_CELL_TYPE(shift
)
293 HANDLE_CELL_TYPE(shiftx
)
298 HANDLE_CELL_TYPE(eqx
)
299 HANDLE_CELL_TYPE(nex
)
302 HANDLE_CELL_TYPE(add
)
303 HANDLE_CELL_TYPE(sub
)
304 HANDLE_CELL_TYPE(mul
)
305 HANDLE_CELL_TYPE(div
)
306 HANDLE_CELL_TYPE(mod
)
307 HANDLE_CELL_TYPE(pow
)
308 HANDLE_CELL_TYPE(pos
)
309 HANDLE_CELL_TYPE(neg
)
310 #undef HANDLE_CELL_TYPE
312 if (type
== ID($_BUF_
))
314 if (type
== ID($_NOT_
))
315 return eval_not(arg1
);
316 if (type
== ID($_AND_
))
317 return const_and(arg1
, arg2
, false, false, 1);
318 if (type
== ID($_NAND_
))
319 return eval_not(const_and(arg1
, arg2
, false, false, 1));
320 if (type
== ID($_OR_
))
321 return const_or(arg1
, arg2
, false, false, 1);
322 if (type
== ID($_NOR_
))
323 return eval_not(const_or(arg1
, arg2
, false, false, 1));
324 if (type
== ID($_XOR_
))
325 return const_xor(arg1
, arg2
, false, false, 1);
326 if (type
== ID($_XNOR_
))
327 return const_xnor(arg1
, arg2
, false, false, 1);
328 if (type
== ID($_ANDNOT_
))
329 return const_and(arg1
, eval_not(arg2
), false, false, 1);
330 if (type
== ID($_ORNOT_
))
331 return const_or(arg1
, eval_not(arg2
), false, false, 1);
333 if (errp
!= nullptr) {
341 static RTLIL::Const
eval(RTLIL::Cell
*cell
, const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool *errp
= nullptr)
343 if (cell
->type
== ID($slice
)) {
345 int width
= cell
->parameters
.at(ID::Y_WIDTH
).as_int();
346 int offset
= cell
->parameters
.at(ID::OFFSET
).as_int();
347 ret
.bits
.insert(ret
.bits
.end(), arg1
.bits
.begin()+offset
, arg1
.bits
.begin()+offset
+width
);
351 if (cell
->type
== ID($concat
)) {
352 RTLIL::Const ret
= arg1
;
353 ret
.bits
.insert(ret
.bits
.end(), arg2
.bits
.begin(), arg2
.bits
.end());
357 if (cell
->type
== ID($lut
))
359 int width
= cell
->parameters
.at(ID::WIDTH
).as_int();
361 std::vector
<RTLIL::State
> t
= cell
->parameters
.at(ID::LUT
).bits
;
362 while (GetSize(t
) < (1 << width
))
363 t
.push_back(State::S0
);
364 t
.resize(1 << width
);
366 for (int i
= width
-1; i
>= 0; i
--) {
367 RTLIL::State sel
= arg1
.bits
.at(i
);
368 std::vector
<RTLIL::State
> new_t
;
369 if (sel
== State::S0
)
370 new_t
= std::vector
<RTLIL::State
>(t
.begin(), t
.begin() + GetSize(t
)/2);
371 else if (sel
== State::S1
)
372 new_t
= std::vector
<RTLIL::State
>(t
.begin() + GetSize(t
)/2, t
.end());
374 for (int j
= 0; j
< GetSize(t
)/2; j
++)
375 new_t
.push_back(t
[j
] == t
[j
+ GetSize(t
)/2] ? t
[j
] : RTLIL::Sx
);
379 log_assert(GetSize(t
) == 1);
383 if (cell
->type
== ID($sop
))
385 int width
= cell
->parameters
.at(ID::WIDTH
).as_int();
386 int depth
= cell
->parameters
.at(ID::DEPTH
).as_int();
387 std::vector
<RTLIL::State
> t
= cell
->parameters
.at(ID::TABLE
).bits
;
389 while (GetSize(t
) < width
*depth
*2)
390 t
.push_back(State::S0
);
392 RTLIL::State default_ret
= State::S0
;
394 for (int i
= 0; i
< depth
; i
++)
399 for (int j
= 0; j
< width
; j
++) {
400 RTLIL::State a
= arg1
.bits
.at(j
);
401 if (t
.at(2*width
*i
+ 2*j
+ 0) == State::S1
) {
402 if (a
== State::S1
) match_x
= false;
403 if (a
!= State::S0
) match
= false;
405 if (t
.at(2*width
*i
+ 2*j
+ 1) == State::S1
) {
406 if (a
== State::S0
) match_x
= false;
407 if (a
!= State::S1
) match
= false;
415 default_ret
= State::Sx
;
421 bool signed_a
= cell
->parameters
.count(ID::A_SIGNED
) > 0 && cell
->parameters
[ID::A_SIGNED
].as_bool();
422 bool signed_b
= cell
->parameters
.count(ID::B_SIGNED
) > 0 && cell
->parameters
[ID::B_SIGNED
].as_bool();
423 int result_len
= cell
->parameters
.count(ID::Y_WIDTH
) > 0 ? cell
->parameters
[ID::Y_WIDTH
].as_int() : -1;
424 return eval(cell
->type
, arg1
, arg2
, signed_a
, signed_b
, result_len
, errp
);
427 static RTLIL::Const
eval(RTLIL::Cell
*cell
, const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, const RTLIL::Const
&arg3
, bool *errp
= nullptr)
429 if (cell
->type
.in(ID($mux
), ID($pmux
), ID($_MUX_
))) {
430 RTLIL::Const ret
= arg1
;
431 for (size_t i
= 0; i
< arg3
.bits
.size(); i
++)
432 if (arg3
.bits
[i
] == RTLIL::State::S1
) {
433 std::vector
<RTLIL::State
> bits(arg2
.bits
.begin() + i
*arg1
.bits
.size(), arg2
.bits
.begin() + (i
+1)*arg1
.bits
.size());
434 ret
= RTLIL::Const(bits
);
439 if (cell
->type
== ID($_AOI3_
))
440 return eval_not(const_or(const_and(arg1
, arg2
, false, false, 1), arg3
, false, false, 1));
441 if (cell
->type
== ID($_OAI3_
))
442 return eval_not(const_and(const_or(arg1
, arg2
, false, false, 1), arg3
, false, false, 1));
444 log_assert(arg3
.bits
.size() == 0);
445 return eval(cell
, arg1
, arg2
, errp
);
448 static RTLIL::Const
eval(RTLIL::Cell
*cell
, const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, const RTLIL::Const
&arg3
, const RTLIL::Const
&arg4
, bool *errp
= nullptr)
450 if (cell
->type
== ID($_AOI4_
))
451 return eval_not(const_or(const_and(arg1
, arg2
, false, false, 1), const_and(arg3
, arg4
, false, false, 1), false, false, 1));
452 if (cell
->type
== ID($_OAI4_
))
453 return eval_not(const_and(const_or(arg1
, arg2
, false, false, 1), const_or(arg3
, arg4
, false, false, 1), false, false, 1));
455 log_assert(arg4
.bits
.size() == 0);
456 return eval(cell
, arg1
, arg2
, arg3
, errp
);
460 // initialized by yosys_setup()
461 extern CellTypes yosys_celltypes
;