349bc5a6d314a000d1c66465fb6f2971fa426461
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.
20 #include "kernel/yosys.h"
21 #include "kernel/sigtools.h"
22 #include "kernel/utils.h"
25 PRIVATE_NAMESPACE_BEGIN
27 struct HierDirtyFlags
;
29 static pool
<string
> reserved_cids
;
30 static dict
<IdString
, string
> id2cid
;
32 static string
cid(IdString id
)
34 if (id2cid
.count(id
) == 0)
37 if (GetSize(s
) < 2) log_abort();
42 if ('0' <= s
[0] && s
[0] <= '9') {
46 for (int i
= 0; i
< GetSize(s
); i
++) {
47 if ('0' <= s
[i
] && s
[i
] <= '9') continue;
48 if ('A' <= s
[i
] && s
[i
] <= 'Z') continue;
49 if ('a' <= s
[i
] && s
[i
] <= 'z') continue;
53 while (reserved_cids
.count(s
))
56 reserved_cids
.insert(s
);
68 HierDirtyFlags
*parent
;
69 pool
<SigBit
> dirty_bits
;
70 pool
<Cell
*> dirty_cells
;
71 pool
<SigBit
> sticky_dirty_bits
;
72 dict
<IdString
, HierDirtyFlags
*> children
;
73 string prefix
, log_prefix
;
75 HierDirtyFlags(Module
*module
, IdString hiername
, HierDirtyFlags
*parent
, const string
&prefix
, const string
&log_prefix
) :
76 dirty(0), module(module
), hiername(hiername
), parent(parent
), prefix(prefix
), log_prefix(log_prefix
)
78 for (Cell
*cell
: module
->cells()) {
79 Module
*mod
= module
->design
->module(cell
->type
);
80 if (mod
) children
[cell
->name
] = new HierDirtyFlags(mod
, cell
->name
, this,
81 prefix
+ cid(cell
->name
) + ".", log_prefix
+ "." + prefix
+ log_id(cell
->name
));
87 for (auto &child
: children
)
91 void set_dirty(SigBit bit
)
93 if (dirty_bits
.count(bit
))
96 dirty_bits
.insert(bit
);
97 sticky_dirty_bits
.insert(bit
);
99 HierDirtyFlags
*p
= this;
100 while (p
!= nullptr) {
106 void unset_dirty(SigBit bit
)
108 if (dirty_bits
.count(bit
) == 0)
111 dirty_bits
.erase(bit
);
113 HierDirtyFlags
*p
= this;
114 while (p
!= nullptr) {
116 log_assert(p
->dirty
>= 0);
121 void set_dirty(Cell
*cell
)
123 if (dirty_cells
.count(cell
))
126 dirty_cells
.insert(cell
);
128 HierDirtyFlags
*p
= this;
129 while (p
!= nullptr) {
135 void unset_dirty(Cell
*cell
)
137 if (dirty_cells
.count(cell
) == 0)
140 dirty_cells
.erase(cell
);
142 HierDirtyFlags
*p
= this;
143 while (p
!= nullptr) {
145 log_assert(p
->dirty
>= 0);
153 bool verbose
= false;
154 int max_uintsize
= 32;
157 dict
<Module
*, SigMap
> sigmaps
;
159 vector
<string
> signal_declarations
;
160 pool
<int> generated_sigtypes
;
162 vector
<string
> util_declarations
;
163 pool
<string
> generated_utils
;
165 vector
<string
> struct_declarations
;
166 pool
<IdString
> generated_structs
;
168 vector
<string
> funct_declarations
;
170 dict
<Module
*, dict
<SigBit
, pool
<tuple
<Cell
*, IdString
, int>>>> bit2cell
;
171 dict
<Module
*, dict
<SigBit
, pool
<SigBit
>>> bit2output
;
172 dict
<Module
*, pool
<SigBit
>> driven_bits
;
174 dict
<Cell
*, int> topoidx
;
176 pool
<string
> activated_cells
;
177 pool
<string
> reactivated_cells
;
179 SimplecWorker(Design
*design
) : design(design
)
183 string
sigtype(int n
)
185 string struct_name
= stringf("signal%d_t", n
);
187 if (generated_sigtypes
.count(n
) == 0)
189 signal_declarations
.push_back("");
190 signal_declarations
.push_back(stringf("#ifndef YOSYS_SIMPLEC_SIGNAL%d_T", n
));
191 signal_declarations
.push_back(stringf("#define YOSYS_SIMPLEC_SIGNAL%d_T", n
));
192 signal_declarations
.push_back(stringf("typedef struct {"));
194 for (int k
= 8; k
<= max_uintsize
; k
= 2*k
)
195 if (n
<= k
&& k
<= max_uintsize
) {
196 signal_declarations
.push_back(stringf(" uint%d_t value_%d_0 : %d;", k
, n
-1, n
));
200 for (int k
= 0; k
< n
; k
+= max_uintsize
) {
201 int bits
= std::min(max_uintsize
, n
-k
);
202 signal_declarations
.push_back(stringf(" uint%d_t value_%d_%d : %d;", max_uintsize
, k
+bits
-1, k
, bits
));
206 signal_declarations
.push_back(stringf("} signal%d_t;", n
));
207 signal_declarations
.push_back(stringf("#endif"));
208 generated_sigtypes
.insert(n
);
214 void util_ifdef_guard(string s
)
216 for (int i
= 0; i
< GetSize(s
); i
++)
217 if ('a' <= s
[i
] && s
[i
] <= 'z')
220 util_declarations
.push_back("");
221 util_declarations
.push_back(stringf("#ifndef %s", s
.c_str()));
222 util_declarations
.push_back(stringf("#define %s", s
.c_str()));
225 string
util_get_bit(const string
&signame
, int n
, int idx
)
227 if (n
== 1 && idx
== 0)
228 return signame
+ ".value_0_0";
230 string util_name
= stringf("yosys_simplec_get_bit_%d_of_%d", idx
, n
);
232 if (generated_utils
.count(util_name
) == 0)
234 util_ifdef_guard(util_name
);
235 util_declarations
.push_back(stringf("static inline bool %s(const %s *sig)", util_name
.c_str(), sigtype(n
).c_str()));
236 util_declarations
.push_back(stringf("{"));
238 int word_idx
= idx
/ max_uintsize
, word_offset
= idx
% max_uintsize
;
239 string value_name
= stringf("value_%d_%d", std::min(n
-1, (word_idx
+1)*max_uintsize
-1), word_idx
*max_uintsize
);
241 util_declarations
.push_back(stringf(" return (sig->%s >> %d) & 1;", value_name
.c_str(), word_offset
));
243 util_declarations
.push_back(stringf("}"));
244 util_declarations
.push_back(stringf("#endif"));
245 generated_utils
.insert(util_name
);
248 return stringf("%s(&%s)", util_name
.c_str(), signame
.c_str());
251 string
util_set_bit(const string
&signame
, int n
, int idx
, const string
&expr
)
253 if (n
== 1 && idx
== 0)
254 return stringf(" %s.value_0_0 = %s;", signame
.c_str(), expr
.c_str());
256 string util_name
= stringf("yosys_simplec_set_bit_%d_of_%d", idx
, n
);
258 if (generated_utils
.count(util_name
) == 0)
260 util_ifdef_guard(util_name
);
261 util_declarations
.push_back(stringf("static inline void %s(%s *sig, bool value)", util_name
.c_str(), sigtype(n
).c_str()));
262 util_declarations
.push_back(stringf("{"));
264 int word_idx
= idx
/ max_uintsize
, word_offset
= idx
% max_uintsize
;
265 string value_name
= stringf("value_%d_%d", std::min(n
-1, (word_idx
+1)*max_uintsize
-1), word_idx
*max_uintsize
);
268 util_declarations
.push_back(stringf(" if (value)"));
269 util_declarations
.push_back(stringf(" sig->%s |= 1UL << %d;", value_name
.c_str(), word_offset
));
270 util_declarations
.push_back(stringf(" else"));
271 util_declarations
.push_back(stringf(" sig->%s &= ~(1UL << %d);", value_name
.c_str(), word_offset
));
273 util_declarations
.push_back(stringf(" sig->%s = (sig->%s & ~((uint%d_t)1 << %d)) | ((uint%d_t)value << %d);",
274 value_name
.c_str(), value_name
.c_str(), max_uintsize
, word_offset
, max_uintsize
, word_offset
));
277 util_declarations
.push_back(stringf("}"));
278 util_declarations
.push_back(stringf("#endif"));
279 generated_utils
.insert(util_name
);
282 return stringf(" %s(&%s, %s);", util_name
.c_str(), signame
.c_str(), expr
.c_str());
285 void create_module_struct(Module
*mod
)
287 if (generated_structs
.count(mod
->name
))
290 generated_structs
.insert(mod
->name
);
291 sigmaps
[mod
].set(mod
);
293 for (Wire
*w
: mod
->wires())
296 for (auto bit
: SigSpec(w
))
297 bit2output
[mod
][sigmaps
.at(mod
)(bit
)].insert(bit
);
300 for (Cell
*c
: mod
->cells())
302 for (auto &conn
: c
->connections())
304 if (!c
->input(conn
.first
)) {
305 for (auto bit
: sigmaps
.at(mod
)(conn
.second
))
306 driven_bits
[mod
].insert(bit
);
311 for (auto bit
: sigmaps
.at(mod
)(conn
.second
))
312 bit2cell
[mod
][bit
].insert(tuple
<Cell
*, IdString
, int>(c
, conn
.first
, idx
++));
315 if (design
->module(c
->type
))
316 create_module_struct(design
->module(c
->type
));
319 TopoSort
<IdString
> topo
;
321 for (Cell
*c
: mod
->cells())
325 for (auto &conn
: c
->connections())
327 if (!c
->input(conn
.first
))
330 for (auto bit
: sigmaps
.at(mod
)(conn
.second
))
331 for (auto &it
: bit2cell
[mod
][bit
])
332 topo
.edge(c
->name
, std::get
<0>(it
)->name
);
336 topo
.analyze_loops
= false;
339 for (int i
= 0; i
< GetSize(topo
.sorted
); i
++)
340 topoidx
[mod
->cell(topo
.sorted
[i
])] = i
;
342 string ifdef_name
= stringf("yosys_simplec_%s_state_t", cid(mod
->name
).c_str());
344 for (int i
= 0; i
< GetSize(ifdef_name
); i
++)
345 if ('a' <= ifdef_name
[i
] && ifdef_name
[i
] <= 'z')
346 ifdef_name
[i
] -= 'a' - 'A';
348 struct_declarations
.push_back("");
349 struct_declarations
.push_back(stringf("#ifndef %s", ifdef_name
.c_str()));
350 struct_declarations
.push_back(stringf("#define %s", ifdef_name
.c_str()));
351 struct_declarations
.push_back(stringf("struct %s_state_t", cid(mod
->name
).c_str()));
352 struct_declarations
.push_back("{");
354 struct_declarations
.push_back(" // Input Ports");
355 for (Wire
*w
: mod
->wires())
357 struct_declarations
.push_back(stringf(" %s %s; // %s", sigtype(w
->width
).c_str(), cid(w
->name
).c_str(), log_id(w
)));
359 struct_declarations
.push_back("");
360 struct_declarations
.push_back(" // Output Ports");
361 for (Wire
*w
: mod
->wires())
362 if (!w
->port_input
&& w
->port_output
)
363 struct_declarations
.push_back(stringf(" %s %s; // %s", sigtype(w
->width
).c_str(), cid(w
->name
).c_str(), log_id(w
)));
365 struct_declarations
.push_back("");
366 struct_declarations
.push_back(" // Internal Wires");
367 for (Wire
*w
: mod
->wires())
368 if (!w
->port_input
&& !w
->port_output
)
369 struct_declarations
.push_back(stringf(" %s %s; // %s", sigtype(w
->width
).c_str(), cid(w
->name
).c_str(), log_id(w
)));
371 for (Cell
*c
: mod
->cells())
372 if (design
->module(c
->type
))
373 struct_declarations
.push_back(stringf(" struct %s_state_t %s; // %s", cid(c
->type
).c_str(), cid(c
->name
).c_str(), log_id(c
)));
375 struct_declarations
.push_back(stringf("};"));
376 struct_declarations
.push_back("#endif");
379 void eval_cell(HierDirtyFlags
*work
, Cell
*cell
)
381 if (cell
->type
.in("$_BUF_", "$_NOT_"))
383 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
384 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
386 string a_expr
= a
.wire
? util_get_bit(work
->prefix
+ cid(a
.wire
->name
), a
.wire
->width
, a
.offset
) : a
.data
? "1" : "0";
389 if (cell
->type
== "$_BUF_") expr
= a_expr
;
390 if (cell
->type
== "$_NOT_") expr
= "!" + a_expr
;
393 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(y
.wire
->name
), y
.wire
->width
, y
.offset
, expr
) +
394 stringf(" // %s (%s)", log_id(cell
), log_id(cell
->type
)));
400 if (cell
->type
.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_", "$_ANDNOT_", "$_ORNOT_"))
402 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
403 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
404 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
406 string a_expr
= a
.wire
? util_get_bit(work
->prefix
+ cid(a
.wire
->name
), a
.wire
->width
, a
.offset
) : a
.data
? "1" : "0";
407 string b_expr
= b
.wire
? util_get_bit(work
->prefix
+ cid(b
.wire
->name
), b
.wire
->width
, b
.offset
) : b
.data
? "1" : "0";
410 if (cell
->type
== "$_AND_") expr
= stringf("%s & %s", a_expr
.c_str(), b_expr
.c_str());
411 if (cell
->type
== "$_NAND_") expr
= stringf("!(%s & %s)", a_expr
.c_str(), b_expr
.c_str());
412 if (cell
->type
== "$_OR_") expr
= stringf("%s | %s", a_expr
.c_str(), b_expr
.c_str());
413 if (cell
->type
== "$_NOR_") expr
= stringf("!(%s | %s)", a_expr
.c_str(), b_expr
.c_str());
414 if (cell
->type
== "$_XOR_") expr
= stringf("%s ^ %s", a_expr
.c_str(), b_expr
.c_str());
415 if (cell
->type
== "$_XNOR_") expr
= stringf("!(%s ^ %s)", a_expr
.c_str(), b_expr
.c_str());
416 if (cell
->type
== "$_ANDNOT_") expr
= stringf("%s & (!%s)", a_expr
.c_str(), b_expr
.c_str());
417 if (cell
->type
== "$_ORNOT_") expr
= stringf("%s | (!%s)", a_expr
.c_str(), b_expr
.c_str());
420 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(y
.wire
->name
), y
.wire
->width
, y
.offset
, expr
) +
421 stringf(" // %s (%s)", log_id(cell
), log_id(cell
->type
)));
427 if (cell
->type
.in("$_AOI3_", "$_OAI3_"))
429 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
430 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
431 SigBit c
= sigmaps
.at(work
->module
)(cell
->getPort("\\C"));
432 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
434 string a_expr
= a
.wire
? util_get_bit(work
->prefix
+ cid(a
.wire
->name
), a
.wire
->width
, a
.offset
) : a
.data
? "1" : "0";
435 string b_expr
= b
.wire
? util_get_bit(work
->prefix
+ cid(b
.wire
->name
), b
.wire
->width
, b
.offset
) : b
.data
? "1" : "0";
436 string c_expr
= c
.wire
? util_get_bit(work
->prefix
+ cid(c
.wire
->name
), c
.wire
->width
, c
.offset
) : c
.data
? "1" : "0";
439 if (cell
->type
== "$_AOI3_") expr
= stringf("!((%s & %s) | %s)", a_expr
.c_str(), b_expr
.c_str(), c_expr
.c_str());
440 if (cell
->type
== "$_OAI3_") expr
= stringf("!((%s | %s) & %s)", a_expr
.c_str(), b_expr
.c_str(), c_expr
.c_str());
443 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(y
.wire
->name
), y
.wire
->width
, y
.offset
, expr
) +
444 stringf(" // %s (%s)", log_id(cell
), log_id(cell
->type
)));
450 if (cell
->type
.in("$_AOI4_", "$_OAI4_"))
452 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
453 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
454 SigBit c
= sigmaps
.at(work
->module
)(cell
->getPort("\\C"));
455 SigBit d
= sigmaps
.at(work
->module
)(cell
->getPort("\\D"));
456 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
458 string a_expr
= a
.wire
? util_get_bit(work
->prefix
+ cid(a
.wire
->name
), a
.wire
->width
, a
.offset
) : a
.data
? "1" : "0";
459 string b_expr
= b
.wire
? util_get_bit(work
->prefix
+ cid(b
.wire
->name
), b
.wire
->width
, b
.offset
) : b
.data
? "1" : "0";
460 string c_expr
= c
.wire
? util_get_bit(work
->prefix
+ cid(c
.wire
->name
), c
.wire
->width
, c
.offset
) : c
.data
? "1" : "0";
461 string d_expr
= d
.wire
? util_get_bit(work
->prefix
+ cid(d
.wire
->name
), d
.wire
->width
, d
.offset
) : d
.data
? "1" : "0";
464 if (cell
->type
== "$_AOI4_") expr
= stringf("!((%s & %s) | (%s & %s))", a_expr
.c_str(), b_expr
.c_str(), c_expr
.c_str(), d_expr
.c_str());
465 if (cell
->type
== "$_OAI4_") expr
= stringf("!((%s | %s) & (%s | %s))", a_expr
.c_str(), b_expr
.c_str(), c_expr
.c_str(), d_expr
.c_str());
468 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(y
.wire
->name
), y
.wire
->width
, y
.offset
, expr
) +
469 stringf(" // %s (%s)", log_id(cell
), log_id(cell
->type
)));
475 if (cell
->type
== "$_MUX_")
477 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
478 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
479 SigBit s
= sigmaps
.at(work
->module
)(cell
->getPort("\\S"));
480 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
482 string a_expr
= a
.wire
? util_get_bit(work
->prefix
+ cid(a
.wire
->name
), a
.wire
->width
, a
.offset
) : a
.data
? "1" : "0";
483 string b_expr
= b
.wire
? util_get_bit(work
->prefix
+ cid(b
.wire
->name
), b
.wire
->width
, b
.offset
) : b
.data
? "1" : "0";
484 string s_expr
= s
.wire
? util_get_bit(work
->prefix
+ cid(s
.wire
->name
), s
.wire
->width
, s
.offset
) : s
.data
? "1" : "0";
486 // casts to bool are a workaround for CBMC bug (https://github.com/diffblue/cbmc/issues/933)
487 string expr
= stringf("%s ? (bool)%s : (bool)%s", s_expr
.c_str(), b_expr
.c_str(), a_expr
.c_str());
490 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(y
.wire
->name
), y
.wire
->width
, y
.offset
, expr
) +
491 stringf(" // %s (%s)", log_id(cell
), log_id(cell
->type
)));
497 log_error("No C model for %s available at the moment (FIXME).\n", log_id(cell
->type
));
500 void eval_dirty(HierDirtyFlags
*work
)
504 if (verbose
&& (!work
->dirty_bits
.empty() || !work
->dirty_cells
.empty()))
505 log(" In %s:\n", work
->log_prefix
.c_str());
507 while (!work
->dirty_bits
.empty() || !work
->dirty_cells
.empty())
509 if (!work
->dirty_bits
.empty())
511 SigSpec
dirtysig(work
->dirty_bits
);
512 dirtysig
.sort_and_unify();
514 for (SigChunk chunk
: dirtysig
.chunks()) {
515 if (chunk
.wire
== nullptr)
518 log(" Propagating %s.%s[%d:%d].\n", work
->log_prefix
.c_str(), log_id(chunk
.wire
), chunk
.offset
+chunk
.width
-1, chunk
.offset
);
519 funct_declarations
.push_back(stringf(" // Updated signal in %s: %s", work
->log_prefix
.c_str(), log_signal(chunk
)));
522 for (SigBit bit
: dirtysig
)
524 if (bit2output
[work
->module
].count(bit
) && work
->parent
)
525 for (auto outbit
: bit2output
[work
->module
][bit
])
527 Module
*parent_mod
= work
->parent
->module
;
528 Cell
*parent_cell
= parent_mod
->cell(work
->hiername
);
530 IdString port_name
= outbit
.wire
->name
;
531 int port_offset
= outbit
.offset
;
532 SigBit parent_bit
= sigmaps
.at(parent_mod
)(parent_cell
->getPort(port_name
)[port_offset
]);
534 log_assert(bit
.wire
&& parent_bit
.wire
);
535 funct_declarations
.push_back(util_set_bit(work
->parent
->prefix
+ cid(parent_bit
.wire
->name
), parent_bit
.wire
->width
, parent_bit
.offset
,
536 util_get_bit(work
->prefix
+ cid(bit
.wire
->name
), bit
.wire
->width
, bit
.offset
)));
537 work
->parent
->set_dirty(parent_bit
);
540 log(" Propagating %s.%s[%d] -> %s.%s[%d].\n", work
->log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
,
541 work
->parent
->log_prefix
.c_str(), log_id(parent_bit
.wire
), parent_bit
.offset
);
544 for (auto &port
: bit2cell
[work
->module
][bit
])
546 if (work
->children
.count(std::get
<0>(port
)->name
))
548 HierDirtyFlags
*child
= work
->children
.at(std::get
<0>(port
)->name
);
549 SigBit child_bit
= sigmaps
.at(child
->module
)(SigBit(child
->module
->wire(std::get
<1>(port
)), std::get
<2>(port
)));
550 log_assert(bit
.wire
&& child_bit
.wire
);
552 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(child
->hiername
) + "." + cid(child_bit
.wire
->name
),
553 child_bit
.wire
->width
, child_bit
.offset
, util_get_bit(work
->prefix
+ cid(bit
.wire
->name
), bit
.wire
->width
, bit
.offset
)));
554 child
->set_dirty(child_bit
);
557 log(" Propagating %s.%s[%d] -> %s.%s.%s[%d].\n", work
->log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
,
558 work
->log_prefix
.c_str(), log_id(std::get
<0>(port
)), log_id(child_bit
.wire
), child_bit
.offset
);
561 log(" Marking cell %s.%s (via %s.%s[%d]).\n", work
->log_prefix
.c_str(), log_id(std::get
<0>(port
)),
562 work
->log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
);
563 work
->set_dirty(std::get
<0>(port
));
566 work
->unset_dirty(bit
);
570 if (!work
->dirty_cells
.empty())
572 Cell
*cell
= nullptr;
573 for (auto c
: work
->dirty_cells
)
574 if (cell
== nullptr || topoidx
.at(cell
) < topoidx
.at(c
))
577 string hiername
= work
->log_prefix
+ "." + log_id(cell
);
580 log(" Evaluating %s (%s, best of %d).\n", hiername
.c_str(), log_id(cell
->type
), GetSize(work
->dirty_cells
));
582 if (activated_cells
.count(hiername
))
583 reactivated_cells
.insert(hiername
);
584 activated_cells
.insert(hiername
);
586 eval_cell(work
, cell
);
587 work
->unset_dirty(cell
);
591 for (auto &child
: work
->children
)
592 eval_dirty(child
.second
);
596 void eval_sticky_dirty(HierDirtyFlags
*work
)
598 Module
*mod
= work
->module
;
600 for (Wire
*w
: mod
->wires())
601 for (SigBit bit
: SigSpec(w
))
603 SigBit canonical_bit
= sigmaps
.at(mod
)(bit
);
605 if (canonical_bit
== bit
)
608 if (work
->sticky_dirty_bits
.count(canonical_bit
) == 0)
611 if (bit
.wire
== nullptr || canonical_bit
.wire
== nullptr)
614 funct_declarations
.push_back(util_set_bit(work
->prefix
+ cid(bit
.wire
->name
), bit
.wire
->width
, bit
.offset
,
615 util_get_bit(work
->prefix
+ cid(canonical_bit
.wire
->name
), canonical_bit
.wire
->width
, canonical_bit
.offset
).c_str()));
618 log(" Propagating alias %s.%s[%d] -> %s.%s[%d].\n",
619 work
->log_prefix
.c_str(), log_id(canonical_bit
.wire
), canonical_bit
.offset
,
620 work
->log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
);
623 work
->sticky_dirty_bits
.clear();
625 for (auto &child
: work
->children
)
626 eval_sticky_dirty(child
.second
);
629 void make_func(HierDirtyFlags
*work
, const string
&func_name
, const vector
<string
> &preamble
)
631 log("Generating function %s():\n", func_name
.c_str());
633 activated_cells
.clear();
634 reactivated_cells
.clear();
636 funct_declarations
.push_back("");
637 funct_declarations
.push_back(stringf("static void %s(struct %s_state_t *state)", func_name
.c_str(), cid(work
->module
->name
).c_str()));
638 funct_declarations
.push_back("{");
639 for (auto &line
: preamble
)
640 funct_declarations
.push_back(line
);
642 eval_sticky_dirty(work
);
643 funct_declarations
.push_back("}");
645 log(" Activated %d cells (%d activated more than once).\n", GetSize(activated_cells
), GetSize(reactivated_cells
));
648 void eval_init(HierDirtyFlags
*work
, vector
<string
> &preamble
)
650 Module
*module
= work
->module
;
652 for (Wire
*w
: module
->wires())
654 if (w
->attributes
.count("\\init"))
656 SigSpec sig
= sigmaps
.at(module
)(w
);
657 Const val
= w
->attributes
.at("\\init");
658 val
.bits
.resize(GetSize(sig
), State::Sx
);
660 for (int i
= 0; i
< GetSize(sig
); i
++)
661 if (val
[i
] == State::S0
|| val
[i
] == State::S1
) {
663 preamble
.push_back(util_set_bit(work
->prefix
+ cid(bit
.wire
->name
), bit
.wire
->width
, bit
.offset
, val
== State::S1
? "true" : "false"));
664 work
->set_dirty(bit
);
668 for (SigBit bit
: SigSpec(w
))
670 SigBit val
= sigmaps
.at(module
)(bit
);
672 if (val
== State::S0
|| val
== State::S1
)
673 preamble
.push_back(util_set_bit(work
->prefix
+ cid(bit
.wire
->name
), bit
.wire
->width
, bit
.offset
, val
== State::S1
? "true" : "false"));
675 if (driven_bits
.at(module
).count(val
) == 0)
676 work
->set_dirty(val
);
680 work
->set_dirty(State::S0
);
681 work
->set_dirty(State::S1
);
683 for (auto &child
: work
->children
)
684 eval_init(child
.second
, preamble
);
687 void make_init_func(HierDirtyFlags
*work
)
689 vector
<string
> preamble
;
690 eval_init(work
, preamble
);
691 make_func(work
, cid(work
->module
->name
) + "_init", preamble
);
694 void make_eval_func(HierDirtyFlags
*work
)
696 Module
*mod
= work
->module
;
697 vector
<string
> preamble
;
699 for (Wire
*w
: mod
->wires()) {
701 for (SigBit bit
: sigmaps
.at(mod
)(w
))
702 work
->set_dirty(bit
);
705 make_func(work
, cid(work
->module
->name
) + "_eval", preamble
);
708 void make_tick_func(HierDirtyFlags
* /* work */)
713 void run(Module
*mod
)
715 create_module_struct(mod
);
717 HierDirtyFlags
work(mod
, IdString(), nullptr, "state->", log_id(mod
->name
));
719 make_init_func(&work
);
720 make_eval_func(&work
);
721 make_tick_func(&work
);
724 void write(std::ostream
&f
)
726 f
<< "#include <stdint.h>" << std::endl
;
727 f
<< "#include <stdbool.h>" << std::endl
;
729 for (auto &line
: signal_declarations
)
730 f
<< line
<< std::endl
;
732 for (auto &line
: util_declarations
)
733 f
<< line
<< std::endl
;
735 for (auto &line
: struct_declarations
)
736 f
<< line
<< std::endl
;
738 for (auto &line
: funct_declarations
)
739 f
<< line
<< std::endl
;
743 struct SimplecBackend
: public Backend
{
744 SimplecBackend() : Backend("simplec", "convert design to simple C code") { }
745 void help() YS_OVERRIDE
747 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
749 log(" write_simplec [options] [filename]\n");
751 log("Write simple C code for simulating the design. The C code writen can be used to\n");
752 log("simulate the design in a C environment, but the purpose of this command is to\n");
753 log("generate code that works well with C-based formal verification.\n");
756 log(" this will print the recursive walk used to export the modules.\n");
758 log(" -i8, -i16, -i32, -i64\n");
759 log(" set the maximum integer bit width to use in the generated code.\n");
761 log("THIS COMMAND IS UNDER CONSTRUCTION\n");
764 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
766 reserved_cids
.clear();
769 SimplecWorker
worker(design
);
771 log_header(design
, "Executing SIMPLEC backend.\n");
774 for (argidx
= 1; argidx
< args
.size(); argidx
++)
776 if (args
[argidx
] == "-verbose") {
777 worker
.verbose
= true;
780 if (args
[argidx
] == "-i8") {
781 worker
.max_uintsize
= 8;
784 if (args
[argidx
] == "-i16") {
785 worker
.max_uintsize
= 16;
788 if (args
[argidx
] == "-i32") {
789 worker
.max_uintsize
= 32;
792 if (args
[argidx
] == "-i64") {
793 worker
.max_uintsize
= 64;
798 extra_args(f
, filename
, args
, argidx
);
800 Module
*topmod
= design
->top_module();
802 if (topmod
== nullptr)
803 log_error("Current design has no top module.\n");
810 PRIVATE_NAMESPACE_END