aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorClifford Wolf <clifford@clifford.at>2016-03-08 16:54:15 +0100
committerClifford Wolf <clifford@clifford.at>2016-03-08 16:54:15 +0100
commitdcd4fb998435b13193a9816e54d4c6b9284d119b (patch)
tree584977ca50ec77473301f327e0eaa54f068430a2 /examples
parente7ed653771ab3483f988f4adfca5c635c10c3480 (diff)
downloadyosys-dcd4fb998435b13193a9816e54d4c6b9284d119b.tar.gz
yosys-dcd4fb998435b13193a9816e54d4c6b9284d119b.tar.bz2
yosys-dcd4fb998435b13193a9816e54d4c6b9284d119b.zip
Added examples/cxx-api/evaldemo.cc
Diffstat (limited to 'examples')
-rw-r--r--examples/cxx-api/evaldemo.cc55
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/cxx-api/evaldemo.cc b/examples/cxx-api/evaldemo.cc
new file mode 100644
index 000000000..e5cc8d8e7
--- /dev/null
+++ b/examples/cxx-api/evaldemo.cc
@@ -0,0 +1,55 @@
+/* A simple Yosys plugin. (Copy&paste from http://stackoverflow.com/questions/32093541/how-does-the-yosys-consteval-api-work)
+
+Usage example:
+
+$ cat > evaldemo.v <<EOT
+module main(input [1:0] A, input [7:0] B, C, D, output [7:0] Y);
+ assign Y = A == 0 ? B : A == 1 ? C : A == 2 ? D : 42;
+endmodule
+EOT
+
+$ yosys-config --build evaldemo.so evaldemo.cc
+$ yosys -m evaldemo.so -p evaldemo evaldemo.v
+*/
+
+#include "kernel/yosys.h"
+#include "kernel/consteval.h"
+
+USING_YOSYS_NAMESPACE
+PRIVATE_NAMESPACE_BEGIN
+
+struct EvalDemoPass : public Pass
+{
+ EvalDemoPass() : Pass("evaldemo") { }
+
+ virtual void execute(vector<string>, Design *design)
+ {
+ Module *module = design->top_module();
+
+ if (module == nullptr)
+ log_error("No top module found!\n");
+
+ Wire *wire_a = module->wire("\\A");
+ Wire *wire_y = module->wire("\\Y");
+
+ if (wire_a == nullptr)
+ log_error("No wire A found!\n");
+
+ if (wire_y == nullptr)
+ log_error("No wire Y found!\n");
+
+ ConstEval ce(module);
+ for (int v = 0; v < 4; v++) {
+ ce.push();
+ ce.set(wire_a, Const(v, GetSize(wire_a)));
+ SigSpec sig_y = wire_y, sig_undef;
+ if (ce.eval(sig_y, sig_undef))
+ log("Eval results for A=%d: Y=%s\n", v, log_signal(sig_y));
+ else
+ log("Eval failed for A=%d: Missing value for %s\n", v, log_signal(sig_undef));
+ ce.pop();
+ }
+ }
+} EvalDemoPass;
+
+PRIVATE_NAMESPACE_END