-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpgen.cpp
More file actions
170 lines (152 loc) · 5.35 KB
/
dpgen.cpp
File metadata and controls
170 lines (152 loc) · 5.35 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
struct variable {
int bitwidth;
string name;
};
struct operation {
variable result;
vector<variable> operands;
char operation;
};
string validOperations[11] = { "+", "-", "*", ">", "<", "==", "?", ">>", "<<", "/", "%"};
void PrintOperands(vector<variable> &inputs, vector<variable> &outputs, vector<variable> &wires){
cout << "Inputs:\n";
for(vector<variable>::iterator i = inputs.begin(); i != inputs.end(); i++){
cout << distance(inputs.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bitwidth << "\n";
}
cout << "\nOutputs:\n";
for(vector<variable>::iterator i = outputs.begin(); i != outputs.end(); i++){
cout << distance(outputs.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bitwidth << "\n";
}
cout << "\nWires:\n";
for(vector<variable>::iterator i = wires.begin(); i != wires.end(); i++){
cout << distance(wires.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bitwidth << "\n";
}
}
vector<string> ReadFile(string inputFile){
cout << "Reading File...\n";
vector<string> parsedFile;
ifstream netlistFile;
netlistFile.open(inputFile);
if(netlistFile){
string str;
while(getline(netlistFile, str)){
parsedFile.push_back(str);
}
}
else{
cout << "unable to read file";
}
cout << "Done reading file.";
return parsedFile;
}
bool CheckValidOutput(vector<variable>& outputs, string newOutput){
for(vector<variable>::iterator it = outputs.begin(); it != outputs.end(); it++){
if((*it).name.compare(newOutput)==0){
return (*it).bitwidth;
}
}
return -1;
}
void ParseLine(vector<variable>& inputs, vector<variable>& outputs,vector<variable>& wires, string line){
//parse into inputs, outputs, wires and operations
if(line.find("input")==0){
//input Int8 a, b, c
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
for(int i = line.find(' ',line.find("Int")); line[i] != '\0'; i++){
if(line[i] != ' ' && line[i]!=','){
variable input;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' '){
if((j-i)>0)
i = j;
input.name += line[j];
j++;
}
input.bitwidth = BW;
cout << "adding " << input.name << " to inputs\n";
inputs.push_back(input);
}
}
}
else if(line.find("output")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
for(int i = line.find(' ',line.find("Int")); line[i] != '\0'; i++){
if(line[i] != ' ' && line[i]!=','){
variable output;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' '){
if((j-i)>0)
i = j;
output.name += line[j];
j++;
}
output.bitwidth = BW;
outputs.push_back(output);
}
}
}
else if(line.find("wire")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
for(int i = line.find(' ',line.find("Int")); line[i] != '\0'; i++){
if(line[i] != ' ' && line[i]!=','){
variable wire;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' '){
if((j-i)>0)// check to skip rest of whole words
i = j;
wire.name += line[j];
j++;
}
wire.bitwidth = BW;
wires.push_back(wire);
}
}
}
//else if(line.find("register")==0)
else{
//operand time
//read one char at a time
operation newOp;
for(int i = 0; line[i] != '\0'; i++){
if(line[i] != ' '){
switch(i){
case 0:
int j = 0;
variable newOutput;
while(line[j]!=' '){
newOutput.name += line[j];
j++;
}
newOutput.bitwidth = CheckValidOutput(outputs, newOutput.name);
if(newOutput.bitwidth != -1){
newOp.result = newOutput;
}
}
}
}
}
}
int main(int argc, char** argv)
{
string filesDebug[16] = {
"./assignment_2_circuits/474a_circuit2.txt"
};
vector<variable> inputs;
vector<variable> outputs;
vector<variable> wires;
vector<variable> registers;
vector<operation> flow;
vector<string> parsedFile = ReadFile(filesDebug[0]);
for(vector<string>::iterator i = parsedFile.begin(); i != parsedFile.end(); ++i){
ParseLine(inputs, outputs, wires, *i);
}
PrintOperands(inputs, outputs, wires);
}