-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutputopt.cpp
More file actions
121 lines (102 loc) · 3.17 KB
/
outputopt.cpp
File metadata and controls
121 lines (102 loc) · 3.17 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
#include <fstream>
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
const char *inputfile = argv[1];
const char *outputfile = argv[2];
ifstream iVerilog;
ofstream oVerilog;
map<int, int> exchangemap;
map<int, int>::iterator it;
iVerilog.open(inputfile);
oVerilog.open(outputfile);
string readline;
string writeline;
uint x_num = 0;
uint y_num = 0;
uint n_shift = 0;
uint n_num = 0;
int bottom, top;
string::size_type pos;
while (getline(iVerilog, readline)) {
if (readline.find("output") != std::string::npos) {
y_num = count(readline.begin(), readline.end(), 'y');
continue;
} else if (readline.find("input") != std::string::npos) {
x_num = count(readline.begin(), readline.end(), 'x');
continue;
} else if (readline.find("wire") != std::string::npos) {
n_num = count(readline.begin(), readline.end(), 'n');
pos = readline.find_first_of('n');
pos++;
n_shift = 0;
while (readline[pos] <= '9' && readline[pos] >= '0') {
n_shift = n_shift * 10 + readline[pos++] - '0';
}
bottom = n_shift;
top = n_shift + y_num;
continue;
} else if (readline.find("assign y") == std::string::npos) continue; // invalid line
pos = readline.find("y") + 1;
int p1, p2;
p1 = 0;
p2 = 0;
while (readline[pos] <= '9' && readline[pos] >= '0') {
p2 = p2 * 10 + readline[pos++] - '0';
}
while (readline[pos++] != 'n');
// now readline[pos-1] = 'n'
while (readline[pos] <= '9' && readline[pos] >= '0') {
p1 = p1 * 10 + readline[pos++] - '0';
}
p2 += bottom;
exchangemap[p1] = p2;
}
it = exchangemap.lower_bound(top);
for (int i = bottom; i < top; ++i) {
if (exchangemap.find(i) == exchangemap.end()) {
exchangemap[i] = it->first;
++it;
}
}
iVerilog.clear();
iVerilog.seekg(0,ios::beg);
int num;
while (getline(iVerilog, readline)) {
if (readline.find("assign") == std::string::npos) {
oVerilog << readline << endl;
continue;
}
writeline = "";
pos = 0;
while (readline[pos]) {
writeline += readline[pos];
if (readline[pos++] == 'n') {
num = 0;
while (readline[pos] <= '9' && readline[pos] >= '0') {
num = num * 10 + readline[pos++] - '0';
}
if (num) {
it = exchangemap.find(num);
if (it != exchangemap.end()) {
writeline += to_string(it->second);
}
else {
writeline += to_string(num);
}
}
}
}
oVerilog << writeline << endl;
}
iVerilog.close();
oVerilog.close();
return 0;
}