-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hu.cpp
137 lines (119 loc) · 2.88 KB
/
Hu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# include "Hu.h"
# include "Blif.h"
const int MAX_DEPTH = 20;
static vector<Node*> tree_nodes;
void Hu_Scheduling(string filepath)
{
cout << "Hu's ordering:" << endl;
Blif* blif = Blif_Reader(filepath);
MyDesign* des = Elaborate(blif);
Generate_Tree(des);
Hu_Ordering();
cout << endl << endl << endl;
}
void Generate_Tree(MyDesign* des)
{
MyScope* scope = des->get_scope();
vector<CELL> cells = scope->cells;
// generate tree root
Node* root = new Node();
if (!root) {
cerr << "No space!" << endl;
exit(0);
}
root->node_name = "root";
for (string s : scope->output_ports) { // add children for root
Node* child = new Node();
if (!child) {
cerr << "No space!" << endl;
exit(0);
}
child->node_name = s;
child->depth = 0;
for (CELL cell : cells) {
if (cell.cell_name == s) {
child->op = cell.op;
break;
}
}
// add root's children
Generate_Subtree(cells, child, 1);
tree_nodes.push_back(child);
root->children.push_back(child);
}
}
void Generate_Subtree(vector<CELL> cells, Node* root, int depth)
{
string cell_output_name = root->node_name;
vector<PORT> children;
// search the output name in cells from the module
for (CELL cell : cells) {
if (cell.cell_name == cell_output_name) {
children = cell.operands;
break;
}
}
for (PORT p : children) {
// check if the node already exists
Node* child = new Node();
bool is_found = false;
for (Node* node : tree_nodes) {
if (node->node_name == p.port_name) {
child = node;
child->depth = depth > child->depth ? depth : child->depth;
is_found = true;
break;
}
}
if (!is_found) {
child->node_name = p.port_name;
child->depth = depth;
for (CELL cell : cells) {
if (cell.cell_name == child->node_name) {
child->op = cell.op;
break;
}
}
Generate_Subtree(cells, child, depth + 1);
tree_nodes.push_back(child);
}
root->children.push_back(child);
}
}
void Hu_Ordering()
{
// check the urgentest level
int max_depth = 0;
for (Node* n : tree_nodes) {
max_depth = max(max_depth, n->depth);
}
cout << "Total " << max_depth << " Cycles:" << endl;
int cycle = 0;
while (max_depth-- > 0) {
vector<Node*> _and, _or, _not;
for (Node* node : tree_nodes) {
if (node->depth == max_depth) {
if (node->op == '&') _and.push_back(node);
else if (node->op == '|') _or.push_back(node);
else if (node->op == '!') _not.push_back(node);
else continue;
}
}
string and_output = "{ ";
string or_output = and_output, not_output = and_output;
for (Node* node : _and) {
and_output += node->node_name + " ";
}
and_output += "}";
for (Node* node : _or) {
or_output += node->node_name + " ";
}
or_output += "}";
for (Node* node : _not) {
not_output += node->node_name + " ";
}
not_output += "}";
cout << "Cycle " << cycle << ":" << and_output << " " << or_output << " " << not_output << endl;
cycle++;
}
}