1 /* A simple Yosys plugin. (Copy&paste from http://stackoverflow.com/questions/32093541/how-does-the-yosys-consteval-api-work)
5 $ cat > evaldemo.v <<EOT
6 module main(input [1:0] A, input [7:0] B, C, D, output [7:0] Y);
7 assign Y = A == 0 ? B : A == 1 ? C : A == 2 ? D : 42;
11 $ yosys-config --build evaldemo.so evaldemo.cc
12 $ yosys -m evaldemo.so -p evaldemo evaldemo.v
15 #include "kernel/yosys.h"
16 #include "kernel/consteval.h"
19 PRIVATE_NAMESPACE_BEGIN
21 struct EvalDemoPass
: public Pass
23 EvalDemoPass() : Pass("evaldemo") { }
25 void execute(vector
<string
>, Design
*design
) YS_OVERRIDE
27 Module
*module
= design
->top_module();
29 if (module
== nullptr)
30 log_error("No top module found!\n");
32 Wire
*wire_a
= module
->wire(ID::A
);
33 Wire
*wire_y
= module
->wire(ID::Y
);
35 if (wire_a
== nullptr)
36 log_error("No wire A found!\n");
38 if (wire_y
== nullptr)
39 log_error("No wire Y found!\n");
42 for (int v
= 0; v
< 4; v
++) {
44 ce
.set(wire_a
, Const(v
, GetSize(wire_a
)));
45 SigSpec sig_y
= wire_y
, sig_undef
;
46 if (ce
.eval(sig_y
, sig_undef
))
47 log("Eval results for A=%d: Y=%s\n", v
, log_signal(sig_y
));
49 log("Eval failed for A=%d: Missing value for %s\n", v
, log_signal(sig_undef
));