-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.cpp
138 lines (132 loc) · 4.33 KB
/
backend.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
138
#include "backend.h"
#include <fstream>
#include "BDD.h"
using namespace std;
using namespace BDD;
float evaluation(Tree& tree)
{
//Evaluate the area of BDD
float area = 0;
for(int i = 0; i<tree.groups.size(); i++)
{
for(int j = 0; j<tree.groups[i].size(); j++)
{
auto& tmp = tree.groups[i][j];
if(tmp.Node_type == "MUX Node")
{
area += 2;
}
else if(tmp.Node_type == "NOT Node")
{
area += 0.1;
}else if(tmp.Node_type == "AND Node")
{
area += 1;
}
}
}
for(int k = 0; k<tree.sel_groups.size(); k++)
{
auto& sel_tmp = tree.sel_groups[k];
if(sel_tmp.Node_type == "NOT Node")
{
area += 0.1;
}else if(sel_tmp.Node_type == "AND Node")
{
area += 1;
}
}
// cout << "net area = " << area << endl;
// cout << "net delay= " << tree.delay[0] << endl;
return area;
}
void outputveri(int *inout_info, Tree &tree, int bit_num)
{
// open a file for outputting VERILOG
ofstream outputfile;
outputfile.open ("out.v", ios::app);
//output verilog
if (outputfile.is_open()) {
//output module inout_info
if(bit_num == 0)
{
outputfile << "module mux_out(" << endl;
outputfile << "\t" << "input [" << inout_info[0] << ":0] sel," << endl;
outputfile << "\t" << "input [" << inout_info[1] << ":0] din," << endl;
outputfile << "\t" << "output [" << inout_info[2] << ":0] dout" << endl;
outputfile << ");" << endl << endl;
}
//output wire
outputfile <<"wire ";
int flag = 0;
for(int i = 0; i<tree.groups.size(); i++)
{
for(int j = 0; j<tree.groups[i].size(); j++)
{
auto& tmp = tree.groups[i][j];
if(flag == 0){
outputfile <<tmp.out;
flag = 1;
} else {
outputfile <<", "<<tmp.out;
}
}
}
outputfile << ";" << endl << endl;
//output assign
for(int i = 0; i < tree.groups.size(); i++)
{
for(int j = 0; j < tree.groups[i].size(); j++)
{
auto& tmp = tree.groups[i][j];
if(tmp.Node_type == "MUX Node")
{
outputfile << "assign " << tmp.out << " = "
<< "(" << tmp.low << " & " << "~" << tmp.sel << ")"
<< " | "
<< "(" << tmp.high << " & " << tmp.sel << ")"
<< ";" << endl;
}
else if(tmp.Node_type == "NOT Node")
{
outputfile << "assign " << tmp.out << " = "
<< "~" << tmp.low
<< ";" << endl;
}else if(tmp.Node_type == "AND Node")
{
outputfile << "assign " << tmp.out << " = "
<< tmp.low << " & " << tmp.high
<< ";" << endl;
}
if(i == (tree.groups.size() - 1) && j == (tree.groups[i].size() - 1))
{
outputfile <<"assign " << "dout[" << bit_num << "] = " << tmp.out << ";" << endl << endl ;
}
}
}
//output sel_assign
for(int k = 0; k<tree.sel_groups.size(); k++)
{
auto& sel_tmp = tree.sel_groups[k];
if(sel_tmp.Node_type == "NOT Node")
{
outputfile << "assign " << sel_tmp.out << " = "
<< "~" << sel_tmp.low
<< ";" << endl;
}else if(sel_tmp.Node_type == "AND Node")
{
outputfile << "assign " << sel_tmp.out << " = "
<< sel_tmp.low << " & " << sel_tmp.high
<< ";" << endl;
}
}
//output end module
if(bit_num == inout_info[2])
{
outputfile << endl << "endmodule";
}
}else{
cout<<"open outputfile failed!"<<endl;
}
outputfile.close();
}