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
;
34 HierDirtyFlags
*parent
;
35 pool
<SigBit
> dirty_bits
;
36 pool
<Cell
*> dirty_cells
;
37 dict
<IdString
, HierDirtyFlags
*> children
;
39 HierDirtyFlags(Module
*module
, IdString hiername
, HierDirtyFlags
*parent
) : dirty(0), module(module
), hiername(hiername
), parent(parent
)
41 for (Cell
*cell
: module
->cells()) {
42 Module
*mod
= module
->design
->module(cell
->type
);
43 if (mod
) children
[cell
->name
] = new HierDirtyFlags(mod
, cell
->name
, this);
49 for (auto &child
: children
)
53 void set_dirty(SigBit bit
)
55 if (dirty_bits
.count(bit
))
58 dirty_bits
.insert(bit
);
60 HierDirtyFlags
*p
= this;
61 while (p
!= nullptr) {
67 void unset_dirty(SigBit bit
)
69 if (dirty_bits
.count(bit
) == 0)
72 dirty_bits
.erase(bit
);
74 HierDirtyFlags
*p
= this;
75 while (p
!= nullptr) {
77 log_assert(p
->dirty
>= 0);
82 void set_dirty(Cell
*cell
)
84 if (dirty_cells
.count(cell
))
87 dirty_cells
.insert(cell
);
89 HierDirtyFlags
*p
= this;
90 while (p
!= nullptr) {
96 void unset_dirty(Cell
*cell
)
98 if (dirty_cells
.count(cell
) == 0)
101 dirty_cells
.erase(cell
);
103 HierDirtyFlags
*p
= this;
104 while (p
!= nullptr) {
106 log_assert(p
->dirty
>= 0);
114 bool verbose
= false;
115 int max_uintsize
= 32;
118 dict
<Module
*, SigMap
> sigmaps
;
119 HierDirtyFlags
*dirty_flags
= nullptr;
121 vector
<string
> signal_declarations
;
122 pool
<int> generated_sigtypes
;
124 vector
<string
> util_declarations
;
125 pool
<string
> generated_utils
;
127 vector
<string
> struct_declarations
;
128 pool
<IdString
> generated_structs
;
130 vector
<string
> funct_declarations
;
132 pool
<string
> reserved_cids
;
133 dict
<IdString
, string
> id2cid
;
135 dict
<Module
*, dict
<SigBit
, pool
<tuple
<Cell
*, IdString
, int>>>> bit2cell
;
136 dict
<Module
*, dict
<SigBit
, pool
<SigBit
>>> bit2output
;
138 dict
<Cell
*, int> topoidx
;
140 SimplecWorker(Design
*design
) : design(design
)
144 string
sigtype(int n
)
146 string struct_name
= stringf("signal%d_t", n
);
148 if (generated_sigtypes
.count(n
) == 0)
150 signal_declarations
.push_back("");
151 signal_declarations
.push_back(stringf("#ifndef YOSYS_SIMPLEC_SIGNAL%d_T", n
));
152 signal_declarations
.push_back(stringf("#define YOSYS_SIMPLEC_SIGNAL%d_T", n
));
153 signal_declarations
.push_back(stringf("typedef struct {"));
155 for (int k
= 8; k
<= max_uintsize
; k
= 2*k
)
156 if (n
<= k
&& k
<= max_uintsize
) {
157 signal_declarations
.push_back(stringf(" uint%d_t value_%d_0 : %d;", k
, n
-1, n
));
161 for (int k
= 0; k
< n
; k
+= max_uintsize
) {
162 int bits
= std::min(max_uintsize
, n
-k
);
163 signal_declarations
.push_back(stringf(" uint%d_t value_%d_%d : %d;", max_uintsize
, k
+bits
-1, k
, bits
));
167 signal_declarations
.push_back(stringf("} signal%d_t;", n
));
168 signal_declarations
.push_back(stringf("#endif"));
169 generated_sigtypes
.insert(n
);
175 void util_ifdef_guard(string s
)
177 for (int i
= 0; i
< GetSize(s
); i
++)
178 if ('a' <= s
[i
] && s
[i
] <= 'z')
181 util_declarations
.push_back(stringf("#ifndef %s", s
.c_str()));
182 util_declarations
.push_back(stringf("#define %s", s
.c_str()));
185 string
util_get_bit(int n
, int idx
)
187 string util_name
= stringf("yosys_simplec_get_bit_%d_of_%d", idx
, n
);
189 if (generated_utils
.count(util_name
) == 0)
191 util_ifdef_guard(util_name
);
192 util_declarations
.push_back(stringf("bool %s(const %s *sig)", util_name
.c_str(), sigtype(n
).c_str()));
193 util_declarations
.push_back(stringf("{"));
195 int word_idx
= idx
/ max_uintsize
, word_offset
= idx
% max_uintsize
;
196 string value_name
= stringf("value_%d_%d", std::min(n
-1, (word_idx
+1)*max_uintsize
-1), word_idx
*max_uintsize
);
198 util_declarations
.push_back(stringf(" return (sig->%s >> %d) & 1;", value_name
.c_str(), word_offset
));
200 util_declarations
.push_back(stringf("}"));
201 util_declarations
.push_back(stringf("#endif"));
202 generated_utils
.insert(util_name
);
208 string
util_set_bit(int n
, int idx
)
210 string util_name
= stringf("yosys_simplec_set_bit_%d_of_%d", idx
, n
);
212 if (generated_utils
.count(util_name
) == 0)
214 util_ifdef_guard(util_name
);
215 util_declarations
.push_back(stringf("void %s(%s *sig, bool value)", util_name
.c_str(), sigtype(n
).c_str()));
216 util_declarations
.push_back(stringf("{"));
218 int word_idx
= idx
/ max_uintsize
, word_offset
= idx
% max_uintsize
;
219 string value_name
= stringf("value_%d_%d", std::min(n
-1, (word_idx
+1)*max_uintsize
-1), word_idx
*max_uintsize
);
221 util_declarations
.push_back(stringf(" if (value)"));
222 util_declarations
.push_back(stringf(" sig->%s |= 1UL << %d;", value_name
.c_str(), word_offset
));
223 util_declarations
.push_back(stringf(" else"));
224 util_declarations
.push_back(stringf(" sig->%s &= ~(1UL << %d);", value_name
.c_str(), word_offset
));
226 util_declarations
.push_back(stringf("}"));
227 util_declarations
.push_back(stringf("#endif"));
228 generated_utils
.insert(util_name
);
234 string
cid(IdString id
)
236 if (id2cid
.count(id
) == 0)
239 if (GetSize(s
) < 2) log_abort();
244 if ('0' <= s
[0] && s
[0] <= '9') {
248 for (int i
= 0; i
< GetSize(s
); i
++) {
249 if ('0' <= s
[i
] && s
[i
] <= '9') continue;
250 if ('A' <= s
[i
] && s
[i
] <= 'Z') continue;
251 if ('a' <= s
[i
] && s
[i
] <= 'z') continue;
255 while (reserved_cids
.count(s
))
258 reserved_cids
.insert(s
);
262 return id2cid
.at(id
);
265 void create_module_struct(Module
*mod
)
267 if (generated_structs
.count(mod
->name
))
270 generated_structs
.insert(mod
->name
);
271 sigmaps
[mod
].set(mod
);
273 for (Wire
*w
: mod
->wires())
276 for (auto bit
: SigSpec(w
))
277 bit2output
[mod
][sigmaps
.at(mod
)(bit
)].insert(bit
);
280 for (Cell
*c
: mod
->cells())
282 for (auto &conn
: c
->connections())
284 if (!c
->input(conn
.first
))
288 for (auto bit
: sigmaps
.at(mod
)(conn
.second
))
289 bit2cell
[mod
][bit
].insert(tuple
<Cell
*, IdString
, int>(c
, conn
.first
, idx
++));
292 if (design
->module(c
->type
))
293 create_module_struct(design
->module(c
->type
));
296 TopoSort
<IdString
> topo
;
298 for (Cell
*c
: mod
->cells())
302 for (auto &conn
: c
->connections())
304 if (!c
->input(conn
.first
))
307 for (auto bit
: sigmaps
.at(mod
)(conn
.second
))
308 for (auto &it
: bit2cell
[mod
][bit
])
309 topo
.edge(c
->name
, std::get
<0>(it
)->name
);
313 topo
.analyze_loops
= false;
316 for (int i
= 0; i
< GetSize(topo
.sorted
); i
++)
317 topoidx
[mod
->cell(topo
.sorted
[i
])] = i
;
319 struct_declarations
.push_back("");
320 struct_declarations
.push_back(stringf("struct %s_state_t", cid(mod
->name
).c_str()));
321 struct_declarations
.push_back("{");
323 struct_declarations
.push_back(" // Input Ports");
324 for (Wire
*w
: mod
->wires())
326 struct_declarations
.push_back(stringf(" %s %s; // %s", sigtype(w
->width
).c_str(), cid(w
->name
).c_str(), log_id(w
)));
328 struct_declarations
.push_back("");
329 struct_declarations
.push_back(" // Output Ports");
330 for (Wire
*w
: mod
->wires())
331 if (!w
->port_input
&& w
->port_output
)
332 struct_declarations
.push_back(stringf(" %s %s; // %s", sigtype(w
->width
).c_str(), cid(w
->name
).c_str(), log_id(w
)));
334 struct_declarations
.push_back("");
335 struct_declarations
.push_back(" // Internal Wires");
336 for (Wire
*w
: mod
->wires())
337 if (!w
->port_input
&& !w
->port_output
)
338 struct_declarations
.push_back(stringf(" %s %s; // %s", sigtype(w
->width
).c_str(), cid(w
->name
).c_str(), log_id(w
)));
340 for (Cell
*c
: mod
->cells())
341 if (design
->module(c
->type
))
342 struct_declarations
.push_back(stringf(" struct %s_state_t %s; // %s", cid(c
->type
).c_str(), cid(c
->name
).c_str(), log_id(c
)));
344 struct_declarations
.push_back(stringf("};"));
347 void eval_cell(HierDirtyFlags
*work
, const string
&prefix
, const string
&/* log_prefix */, Cell
*cell
)
349 if (cell
->type
.in("$_BUF_", "$_NOT_"))
351 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
352 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
354 string a_expr
= a
.wire
? stringf("%s(&%s)", util_get_bit(a
.wire
->width
, a
.offset
).c_str(), (prefix
+ cid(a
.wire
->name
)).c_str()) : a
.data
? "1" : "0";
357 if (cell
->type
== "$_BUF_") expr
= a_expr
;
358 if (cell
->type
== "$_NOT_") expr
= "!" + a_expr
;
361 funct_declarations
.push_back(stringf(" %s(&%s, %s); // %s (%s)", util_set_bit(y
.wire
->width
, y
.offset
).c_str(),
362 (prefix
+ cid(y
.wire
->name
)).c_str(), expr
.c_str(), log_id(cell
), log_id(cell
->type
)));
368 if (cell
->type
.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
370 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
371 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
372 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
374 string a_expr
= a
.wire
? stringf("%s(&%s)", util_get_bit(a
.wire
->width
, a
.offset
).c_str(), (prefix
+ cid(a
.wire
->name
)).c_str()) : a
.data
? "1" : "0";
375 string b_expr
= b
.wire
? stringf("%s(&%s)", util_get_bit(b
.wire
->width
, b
.offset
).c_str(), (prefix
+ cid(b
.wire
->name
)).c_str()) : b
.data
? "1" : "0";
378 if (cell
->type
== "$_AND_") expr
= stringf("%s & %s", a_expr
.c_str(), b_expr
.c_str());
379 if (cell
->type
== "$_NAND_") expr
= stringf("!(%s & %s)", a_expr
.c_str(), b_expr
.c_str());
380 if (cell
->type
== "$_OR_") expr
= stringf("%s | %s", a_expr
.c_str(), b_expr
.c_str());
381 if (cell
->type
== "$_NOR_") expr
= stringf("!(%s | %s)", a_expr
.c_str(), b_expr
.c_str());
382 if (cell
->type
== "$_XOR_") expr
= stringf("%s ^ %s", a_expr
.c_str(), b_expr
.c_str());
383 if (cell
->type
== "$_XNOR_") expr
= stringf("!(%s ^ %s)", a_expr
.c_str(), b_expr
.c_str());
386 funct_declarations
.push_back(stringf(" %s(&%s, %s); // %s (%s)", util_set_bit(y
.wire
->width
, y
.offset
).c_str(),
387 (prefix
+ cid(y
.wire
->name
)).c_str(), expr
.c_str(), log_id(cell
), log_id(cell
->type
)));
393 if (cell
->type
.in("$_AOI3_", "$_OAI3_"))
395 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
396 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
397 SigBit c
= sigmaps
.at(work
->module
)(cell
->getPort("\\C"));
398 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
400 string a_expr
= a
.wire
? stringf("%s(&%s)", util_get_bit(a
.wire
->width
, a
.offset
).c_str(), (prefix
+ cid(a
.wire
->name
)).c_str()) : a
.data
? "1" : "0";
401 string b_expr
= b
.wire
? stringf("%s(&%s)", util_get_bit(b
.wire
->width
, b
.offset
).c_str(), (prefix
+ cid(b
.wire
->name
)).c_str()) : b
.data
? "1" : "0";
402 string c_expr
= c
.wire
? stringf("%s(&%s)", util_get_bit(c
.wire
->width
, c
.offset
).c_str(), (prefix
+ cid(c
.wire
->name
)).c_str()) : c
.data
? "1" : "0";
405 if (cell
->type
== "$_AOI3_") expr
= stringf("!((%s & %s) | %s)", a_expr
.c_str(), b_expr
.c_str(), c_expr
.c_str());
406 if (cell
->type
== "$_OAI3_") expr
= stringf("!((%s | %s) & %s)", a_expr
.c_str(), b_expr
.c_str(), c_expr
.c_str());
409 funct_declarations
.push_back(stringf(" %s(&%s, %s); // %s (%s)", util_set_bit(y
.wire
->width
, y
.offset
).c_str(),
410 (prefix
+ cid(y
.wire
->name
)).c_str(), expr
.c_str(), log_id(cell
), log_id(cell
->type
)));
416 if (cell
->type
.in("$_AOI4_", "$_OAI4_"))
418 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
419 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
420 SigBit c
= sigmaps
.at(work
->module
)(cell
->getPort("\\C"));
421 SigBit d
= sigmaps
.at(work
->module
)(cell
->getPort("\\D"));
422 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
424 string a_expr
= a
.wire
? stringf("%s(&%s)", util_get_bit(a
.wire
->width
, a
.offset
).c_str(), (prefix
+ cid(a
.wire
->name
)).c_str()) : a
.data
? "1" : "0";
425 string b_expr
= b
.wire
? stringf("%s(&%s)", util_get_bit(b
.wire
->width
, b
.offset
).c_str(), (prefix
+ cid(b
.wire
->name
)).c_str()) : b
.data
? "1" : "0";
426 string c_expr
= c
.wire
? stringf("%s(&%s)", util_get_bit(c
.wire
->width
, c
.offset
).c_str(), (prefix
+ cid(c
.wire
->name
)).c_str()) : c
.data
? "1" : "0";
427 string d_expr
= d
.wire
? stringf("%s(&%s)", util_get_bit(d
.wire
->width
, d
.offset
).c_str(), (prefix
+ cid(d
.wire
->name
)).c_str()) : d
.data
? "1" : "0";
430 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());
431 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());
434 funct_declarations
.push_back(stringf(" %s(&%s, %s); // %s (%s)", util_set_bit(y
.wire
->width
, y
.offset
).c_str(),
435 (prefix
+ cid(y
.wire
->name
)).c_str(), expr
.c_str(), log_id(cell
), log_id(cell
->type
)));
441 if (cell
->type
== "$_MUX_")
443 SigBit a
= sigmaps
.at(work
->module
)(cell
->getPort("\\A"));
444 SigBit b
= sigmaps
.at(work
->module
)(cell
->getPort("\\B"));
445 SigBit s
= sigmaps
.at(work
->module
)(cell
->getPort("\\S"));
446 SigBit y
= sigmaps
.at(work
->module
)(cell
->getPort("\\Y"));
448 string a_expr
= a
.wire
? stringf("%s(&%s)", util_get_bit(a
.wire
->width
, a
.offset
).c_str(), (prefix
+ cid(a
.wire
->name
)).c_str()) : a
.data
? "1" : "0";
449 string b_expr
= b
.wire
? stringf("%s(&%s)", util_get_bit(b
.wire
->width
, b
.offset
).c_str(), (prefix
+ cid(b
.wire
->name
)).c_str()) : b
.data
? "1" : "0";
450 string s_expr
= s
.wire
? stringf("%s(&%s)", util_get_bit(s
.wire
->width
, s
.offset
).c_str(), (prefix
+ cid(s
.wire
->name
)).c_str()) : s
.data
? "1" : "0";
451 string expr
= stringf("%s ? %s : %s", s_expr
.c_str(), b_expr
.c_str(), a_expr
.c_str());
454 funct_declarations
.push_back(stringf(" %s(&%s, %s); // %s (%s)", util_set_bit(y
.wire
->width
, y
.offset
).c_str(),
455 (prefix
+ cid(y
.wire
->name
)).c_str(), expr
.c_str(), log_id(cell
), log_id(cell
->type
)));
461 log_error("No C model for %s available at the moment (FIXME).\n", log_id(cell
->type
));
464 void eval_dirty(HierDirtyFlags
*work
, const string
&prefix
, const string
&log_prefix
, const string
&parent_prefix
, const string
&parent_log_prefix
)
468 if (verbose
&& (!work
->dirty_bits
.empty() || !work
->dirty_cells
.empty()))
469 log("In %s:\n", log_prefix
.c_str());
471 while (!work
->dirty_bits
.empty() || !work
->dirty_cells
.empty())
473 if (!work
->dirty_bits
.empty())
475 SigSpec
dirtysig(work
->dirty_bits
);
476 dirtysig
.sort_and_unify();
478 for (SigChunk chunk
: dirtysig
.chunks()) {
480 log(" Propagating %s.%s[%d:%d].\n", log_prefix
.c_str(), log_id(chunk
.wire
), chunk
.offset
+chunk
.width
-1, chunk
.offset
);
481 funct_declarations
.push_back(stringf(" // Updated signal in %s: %s", log_prefix
.c_str(), log_signal(chunk
)));
484 for (SigBit bit
: dirtysig
)
486 if (bit2output
[work
->module
].count(bit
) && work
->parent
)
487 for (auto outbit
: bit2output
[work
->module
][bit
])
489 Module
*parent_mod
= work
->parent
->module
;
490 Cell
*parent_cell
= parent_mod
->cell(work
->hiername
);
492 IdString port_name
= outbit
.wire
->name
;
493 int port_offset
= outbit
.offset
;
494 SigBit parent_bit
= sigmaps
.at(parent_mod
)(parent_cell
->getPort(port_name
)[port_offset
]);
496 log_assert(bit
.wire
&& parent_bit
.wire
);
497 funct_declarations
.push_back(stringf(" %s(&%s, %s(&%s));",
498 util_set_bit(parent_bit
.wire
->width
, parent_bit
.offset
).c_str(),
499 (parent_prefix
+ cid(parent_bit
.wire
->name
)).c_str(),
500 util_get_bit(bit
.wire
->width
, bit
.offset
).c_str(),
501 (prefix
+ cid(bit
.wire
->name
)).c_str()));
502 work
->parent
->set_dirty(parent_bit
);
505 log(" Propagating %s.%s[%d] -> %s.%s[%d].\n", log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
,
506 parent_log_prefix
.c_str(), log_id(parent_bit
.wire
), parent_bit
.offset
);
509 for (auto &port
: bit2cell
[work
->module
][bit
])
511 if (work
->children
.count(std::get
<0>(port
)->name
))
513 HierDirtyFlags
*child
= work
->children
.at(std::get
<0>(port
)->name
);
514 SigBit child_bit
= sigmaps
.at(child
->module
)(SigBit(child
->module
->wire(std::get
<1>(port
)), std::get
<2>(port
)));
515 log_assert(bit
.wire
&& child_bit
.wire
);
517 funct_declarations
.push_back(stringf(" %s(&%s, %s(&%s));",
518 util_set_bit(child_bit
.wire
->width
, child_bit
.offset
).c_str(),
519 (prefix
+ cid(child
->hiername
) + "." + cid(child_bit
.wire
->name
)).c_str(),
520 util_get_bit(bit
.wire
->width
, bit
.offset
).c_str(),
521 (prefix
+ cid(bit
.wire
->name
)).c_str()));
522 child
->set_dirty(child_bit
);
525 log(" Propagating %s.%s[%d] -> %s.%s.%s[%d].\n", log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
,
526 log_prefix
.c_str(), log_id(std::get
<0>(port
)), log_id(child_bit
.wire
), child_bit
.offset
);
529 log(" Marking cell %s.%s (via %s.%s[%d]).\n", log_prefix
.c_str(), log_id(std::get
<0>(port
)),
530 log_prefix
.c_str(), log_id(bit
.wire
), bit
.offset
);
531 work
->set_dirty(std::get
<0>(port
));
534 work
->unset_dirty(bit
);
538 if (!work
->dirty_cells
.empty())
540 Cell
*cell
= nullptr;
541 for (auto c
: work
->dirty_cells
)
542 if (cell
== nullptr || topoidx
.at(cell
) < topoidx
.at(c
))
546 log(" Evaluating %s.%s (%s, best of %d).\n", log_prefix
.c_str(), log_id(cell
), log_id(cell
->type
), GetSize(work
->dirty_cells
));
547 eval_cell(work
, prefix
, log_prefix
, cell
);
548 work
->unset_dirty(cell
);
552 for (auto &child
: work
->children
)
553 eval_dirty(child
.second
, prefix
+ cid(child
.first
) + ".", log_prefix
+ "." + cid(child
.first
), prefix
, log_prefix
);
557 void run(Module
*mod
)
559 create_module_struct(mod
);
561 dirty_flags
= new HierDirtyFlags(mod
, IdString(), nullptr);
563 funct_declarations
.push_back("");
564 funct_declarations
.push_back(stringf("void %s_init(struct %s_state_t *state)", cid(mod
->name
).c_str(), cid(mod
->name
).c_str()));
565 funct_declarations
.push_back("{");
566 funct_declarations
.push_back("}");
568 funct_declarations
.push_back("");
569 funct_declarations
.push_back(stringf("void %s_eval(struct %s_state_t *state)", cid(mod
->name
).c_str(), cid(mod
->name
).c_str()));
570 funct_declarations
.push_back("{");
572 for (Wire
*w
: mod
->wires()) {
574 for (SigBit bit
: sigmaps
.at(mod
)(w
))
575 dirty_flags
->set_dirty(bit
);
578 eval_dirty(dirty_flags
, "state->", log_id(mod
), "", "");
580 funct_declarations
.push_back("}");
582 funct_declarations
.push_back("");
583 funct_declarations
.push_back(stringf("void %s_tick(struct %s_state_t *state)", cid(mod
->name
).c_str(), cid(mod
->name
).c_str()));
584 funct_declarations
.push_back("{");
585 funct_declarations
.push_back("}");
588 dirty_flags
= nullptr;
591 void write(std::ostream
&f
)
593 f
<< "#include <stdint.h>" << std::endl
;
594 f
<< "#include <stdbool.h>" << std::endl
;
596 for (auto &line
: signal_declarations
)
597 f
<< line
<< std::endl
;
599 for (auto &line
: util_declarations
)
600 f
<< line
<< std::endl
;
602 for (auto &line
: struct_declarations
)
603 f
<< line
<< std::endl
;
605 for (auto &line
: funct_declarations
)
606 f
<< line
<< std::endl
;
610 struct SimplecBackend
: public Backend
{
611 SimplecBackend() : Backend("simplec", "convert design to simple C code") { }
614 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
616 log(" write_simplec [options] [filename]\n");
618 log("Write simple C code for simulating the design. The C code writen can be used to\n");
619 log("simulate the design in a C environment, but the purpose of this command is to\n");
620 log("generate code that works well with C-based formal verification.\n");
623 log(" this will print the recursive walk used to export the modules.\n");
625 log(" -i8, -i16, -i32, -i64\n");
626 log(" set the maximum integer bit width to use in the generated code.\n");
628 log("THIS COMMAND IS UNDER CONSTRUCTION\n");
631 virtual void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
)
633 SimplecWorker
worker(design
);
635 log_header(design
, "Executing SIMPLEC backend.\n");
638 for (argidx
= 1; argidx
< args
.size(); argidx
++)
640 if (args
[argidx
] == "-verbose") {
641 worker
.verbose
= true;
644 if (args
[argidx
] == "-i8") {
645 worker
.max_uintsize
= 8;
648 if (args
[argidx
] == "-i16") {
649 worker
.max_uintsize
= 16;
652 if (args
[argidx
] == "-i32") {
653 worker
.max_uintsize
= 32;
656 if (args
[argidx
] == "-i64") {
657 worker
.max_uintsize
= 64;
662 extra_args(f
, filename
, args
, argidx
);
664 Module
*topmod
= design
->top_module();
666 if (topmod
== nullptr)
667 log_error("Current design has no top module.\n");
674 PRIVATE_NAMESPACE_END