-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPxLinkData.cpp
133 lines (99 loc) · 3.27 KB
/
APxLinkData.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
#include "APxLinkData.hh"
#include <sstream>
using namespace std;
bool APxLinkData::LinkValue::operator ==(const LinkValue &b) const {
if (this->user != b.user || this->data != b.data) return false;
return true;
}
APxLinkData::APxLinkData(size_t links) {
this->links = links;
this->max_cycles = 0;
}
void APxLinkData::add(size_t cycle, size_t link, const LinkValue value) {
if (link > this->links) throw runtime_error("Link out-of-range");
CycleDataMap &v = this->data[cycle];
//std::map<size_t, LinkValue> &v = this->data[cycle];
v[link] = value;
if (cycle >= this->max_cycles)
this->max_cycles = cycle+1;
}
bool APxLinkData::get(size_t cycle, size_t link, LinkValue& value) const {
LinkDataMap::const_iterator it1 = this->data.find(cycle);
if (it1 == this->data.end()) return false;
CycleDataMap::const_iterator it2 = it1->second.find(link);
if (it2 == it1->second.end()) return false;
value = it2->second;
return true;
}
void APxLinkData::write(const string filename) const {
ofstream of;
of.open(filename.c_str());
if (!of) throw runtime_error("Cannot open output file: " + filename);
of << "# Automatically generated" << endl;
of << endl;
of << "# CLK ";
for (size_t k = 0; k < this->links; k++) {
of << "Link " << setw(2) << k << " ";
}
of << endl;
for (LinkDataMap::const_iterator it1 = this->data.begin(); it1 != this->data.end(); it1++) {
of << dec << setfill(' ') << setw(4) << it1->first << " ";
for (size_t k = 0; k < this->links; k++) {
CycleDataMap::const_iterator it2 = it1->second.find(k);
if (it2 == it1->second.end()) {
of << "- - ";
} else {
of << "0x" << hex << setfill('0') << setw(2) << it2->second.user << " 0x" << setw(16) << it2->second.data << " ";
}
}
of << endl;
}
of.close();
}
void APxLinkData::read(const std::string filename) {
ifstream rs(filename.c_str());
if (!rs) throw runtime_error("Cannot open input file: " + filename);
for (string line; getline(rs, line);) {
if (line.length() <= 1 || line[0] == '#') continue;
int cycle;
istringstream ss(line);
ss >> cycle;
for (size_t i = 0; i < this->links; i++) {
string user, data;
ss >> user;
ss >> data;
if (user[0] == '-' && data[0] == '-') continue;
LinkValue word;
word.user = std::stoi(user, nullptr, 0);
word.data = std::stoull(data, nullptr, 0);
this->add(cycle, i, word);
}
}
rs.close();
}
void APxLinkData::print() const {
for (LinkDataMap::const_iterator it1 = this->data.begin(); it1 != this->data.end(); it1++) {
cout << dec << setfill(' ') << setw(4) << it1->first << " ";
for (size_t k = 0; k < this->links; k++) {
CycleDataMap::const_iterator it2 = it1->second.find(k);
if (it2 == it1->second.end()) {
cout << "- - ";
} else {
cout << "0x" << hex << setfill('0') << setw(2) << it2->second.user << " 0x" << setw(16) << it2->second.data << " ";
}
}
cout << endl;
}
}
bool APxLinkData::operator ==(const APxLinkData &b) const {
for (size_t i = 0; i < this->max_cycles; i++) {
for (size_t k = 0; k < this->links; k++) {
LinkValue v1, v2;
bool r1 = this->get(i, k, v1);
bool r2 = b.get(i, k, v2); // hack: remove const
if (r1 != r2) return false;
if ((r1 && r2) && (v1 != v2)) return false;
}
}
return true;
}