Bump version
[yosys.git] / libs / subcircuit / demo.cc
1 #include "subcircuit.h"
2 #include <stdio.h>
3
4 #define VERBOSE
5
6 int main()
7 {
8 SubCircuit::Graph needle, haystack;
9
10 // create needle graph
11
12 needle.createNode("mul_1", "product");
13 needle.createPort("mul_1", "A", 4);
14 needle.createPort("mul_1", "B", 4);
15 needle.createPort("mul_1", "Y", 4);
16 needle.markExtern("mul_1", "A");
17 needle.markExtern("mul_1", "B");
18
19 needle.createNode("mul_2", "product");
20 needle.createPort("mul_2", "A", 4);
21 needle.createPort("mul_2", "B", 4);
22 needle.createPort("mul_2", "Y", 4);
23 needle.markExtern("mul_2", "A");
24 needle.markExtern("mul_2", "B");
25
26 needle.createNode("add_1", "sum");
27 needle.createPort("add_1", "A", 4);
28 needle.createPort("add_1", "B", 4);
29 needle.createPort("add_1", "Y", 4);
30 needle.markExtern("add_1", "Y");
31
32 needle.createConnection("mul_1", "Y", "add_1", "A");
33 needle.createConnection("mul_2", "Y", "add_1", "B");
34
35 #ifdef VERBOSE
36 printf("\n");
37 needle.print();
38 #endif
39
40 // create haystack graph
41
42 #if 0
43 for (int i = 0; i < 4; i++) {
44 char id[100];
45 snprintf(id, 100, "mul_%d", i);
46 haystack.createNode(id, "mul");
47 haystack.createPort(id, "A", 4);
48 haystack.createPort(id, "B", 4);
49 haystack.createPort(id, "Y", 4);
50 haystack.markExtern(id, "A");
51 haystack.markExtern(id, "B");
52 }
53
54 for (int i = 0; i < 3; i++) {
55 char id[100];
56 snprintf(id, 100, "add_%d", i);
57 haystack.createNode(id, "add");
58 haystack.createPort(id, "A", 4);
59 haystack.createPort(id, "B", 4);
60 haystack.createPort(id, "Y", 4);
61 }
62
63 haystack.createConnection("mul_0", "Y", "add_0", "A");
64 haystack.createConnection("mul_1", "Y", "add_0", "B");
65
66 haystack.createConnection("mul_2", "Y", "add_1", "A");
67 haystack.createConnection("mul_3", "Y", "add_1", "B");
68
69 haystack.createConnection("add_0", "Y", "add_2", "A");
70 haystack.createConnection("add_1", "Y", "add_2", "B");
71 haystack.markExtern("add_2", "Y");
72 #else
73 std::vector<std::string> cellIds;
74 srand48(12345);
75
76 for (int i = 0; i < 45; i++) {
77 char id[100];
78 snprintf(id, 100, "cell_%02d", i);
79 haystack.createNode(id, i < 30 ? "mul" : "add");
80 haystack.createPort(id, "A", 4);
81 haystack.createPort(id, "B", 4);
82 haystack.createPort(id, "Y", 4);
83 cellIds.push_back(id);
84 }
85
86 for (int i = 0; i < int(cellIds.size()); i++) {
87 if (lrand48() % (i < 20 ? 3 : 2) != 0)
88 continue;
89 const std::string &id = cellIds[i];
90 const std::string &id_left = cellIds[lrand48() % cellIds.size()];
91 const std::string &id_right = cellIds[lrand48() % cellIds.size()];
92 haystack.createConnection(id_left, "Y", id, "A");
93 haystack.createConnection(id_right, "Y", id, "B");
94 }
95 #endif
96
97 #ifdef VERBOSE
98 printf("\n");
99 haystack.print();
100 #endif
101
102 // search needle in haystack
103
104 SubCircuit::Solver solver;
105 std::vector<SubCircuit::Solver::Result> results;
106
107 #ifdef VERBOSE
108 solver.setVerbose();
109 #endif
110
111 solver.addCompatibleTypes("product", "mul");
112 solver.addCompatibleTypes("sum", "add");
113
114 solver.addSwappablePorts("product", "A", "B");
115 solver.addSwappablePorts("sum", "A", "B");
116
117 solver.addGraph("needle", needle);
118 solver.addGraph("haystack", haystack);
119 solver.solve(results, "needle", "haystack");
120
121 for (int i = 0; i < int(results.size()); i++) {
122 printf("\nMatch #%d: (%s in %s)\n", i, results[i].needleGraphId.c_str(), results[i].haystackGraphId.c_str());
123 for (const auto &it : results[i].mappings) {
124 printf(" %s -> %s", it.first.c_str(), it.second.haystackNodeId.c_str());
125 for (const auto &it2 : it.second.portMapping)
126 printf(" %s:%s", it2.first.c_str(), it2.second.c_str());
127 printf("\n");
128 }
129 }
130
131 printf("\n");
132 return 0;
133 }
134