-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatpg_circuit.cpp
209 lines (185 loc) · 5.78 KB
/
atpg_circuit.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "atpg_circuit.h"
#include "gate_tables.h"
ATPGCircuitElement& ATPGCircuit::get_element(std::string name) {
for(auto& element : elements) {
if(element.name == name) {
return element;
}
}
printf("shouldnt come here..\n");
}
std::vector<ATPGCircuitElement*> ATPGCircuit::get_inputs(std::string name) {
ATPGCircuitElement& element = get_element(name);
std::vector<ATPGCircuitElement*> inputs;
for(std::string& input : element.inputs) {
ATPGCircuitElement& input_element = get_element(input);
inputs.push_back(&input_element);
}
return inputs;
}
// input has 1 or more D/D', output has X
std::vector<ATPGCircuitElement*> ATPGCircuit::get_d_frontiers() {
std::vector<ATPGCircuitElement*> d_frontiers;
for(auto& element : elements) {
if(element.type == ATPGCircuitElementType::WIRE) {
continue;
}
auto gate_inputs = get_inputs(element.name);
auto& output = get_element(element.outputs[0]);
if(output.cvalue != 'x') {
continue;
}
for(auto input : gate_inputs) {
if(input->cvalue == 'D' || input->cvalue == 'E') {
d_frontiers.push_back(&get_element(element.name));
break;
}
}
}
return d_frontiers;
}
// output value assigned, inputs not decided
std::vector<ATPGCircuitElement*> ATPGCircuit::get_j_frontiers() {
std::vector<ATPGCircuitElement*> j_frontiers;
for(auto& element : elements) {
if(element.type == ATPGCircuitElementType::WIRE) {
continue;
}
auto gate_inputs = get_inputs(element.name);
auto& output = get_element(element.outputs[0]);
if(output.cvalue == 'x') {
continue;
}
bool all_x = true;
for(auto input : gate_inputs) {
if(input->cvalue != 'x') {
all_x = false;
break;
}
}
if(all_x) {
j_frontiers.push_back(&get_element(element.name));
}
}
return j_frontiers;
}
// input and output has C
std::vector<ATPGCircuitElement*> ATPGCircuit::get_c_frontiers() {
std::vector<ATPGCircuitElement*> c_frontiers;
for(auto& element : elements) {
if(element.type == ATPGCircuitElementType::WIRE) {
continue;
}
auto gate_inputs = get_inputs(element.name);
auto& output = get_element(element.outputs[0]);
if(output.cvalue != 'C') {
continue;
}
for(auto input : gate_inputs) {
if(input->cvalue == 'C') {
c_frontiers.push_back(&get_element(element.name));
break;
}
}
}
return c_frontiers;
}
// output known, 1 or more inputs unknown
std::vector<ATPGCircuitElement*> ATPGCircuit::get_cj_frontiers() {
std::vector<ATPGCircuitElement*> cj_frontiers;
for(auto& element : elements) {
if(element.type == ATPGCircuitElementType::WIRE) {
continue;
}
auto gate_inputs = get_inputs(element.name);
auto& output = get_element(element.outputs[0]);
if(output.cvalue == 'x') {
continue;
}
bool one_x = false;
for(auto input : gate_inputs) {
if(input->cvalue == 'x') {
one_x = true;
break;
}
}
if(one_x) {
cj_frontiers.push_back(&get_element(element.name));
}
}
return cj_frontiers;
}
bool ATPGCircuit::has_conflict(std::string exclude_gate) {
for(auto& element : elements) {
if(element.type == ATPGCircuitElementType::WIRE) {
continue;
}
if(element.name == exclude_gate) {
continue;
}
auto inputs = get_inputs(element.name);
auto& output = get_element(element.outputs[0]);
std::string i0{inputs[0]->cvalue};
std::string i1{inputs[1]->cvalue};
std::string map_output{output.cvalue};
std::string map_input = i0 + i1;
std::string map_value;
switch (element.type)
{
case ATPGCircuitElementType::AND:
map_value = GateTables::gate_table_and[map_input];
break;
case ATPGCircuitElementType::NAND:
map_value = GateTables::gate_table_nand[map_input];
break;
case ATPGCircuitElementType::OR:
map_value = GateTables::gate_table_or[map_input];
break;
case ATPGCircuitElementType::NOR:
map_value = GateTables::gate_table_nor[map_input];
break;
default:
printf("Gate type %d is not supported! has_conflict()\n", element.type);
return true;
}
if(map_input.find('x') == std::string::npos) {
if(map_output != "x" && map_value != map_output) {
return true;
}
}
}
return false;
}
std::vector<ATPGCircuitElement*> ATPGCircuit::get_pos() {
std::vector<ATPGCircuitElement*> pos;
for(auto& e : elements) {
if(e.outputs.size() == 0) {
pos.push_back(&e);
}
}
return pos;
}
std::vector<ATPGCircuitElement*> ATPGCircuit::get_pis() {
std::vector<ATPGCircuitElement*> pis;
for(auto& e : elements) {
if(e.inputs.size() == 0) {
pis.push_back(&e);
}
}
return pis;
}
void ATPGCircuit::print_pattern() {
std::vector<ATPGCircuitElement*> pis = get_pis();
std::vector<ATPGCircuitElement*> pos = get_pos();
printf("- Generated Pattern -\n");
printf("PIs: ");
for(auto pi : pis) {
printf("%s[%c] ", pi->name.c_str(), pi->cvalue);
}
printf("\n");
printf("POs: ");
for(auto po : pos) {
printf("%s[%c] ", po->name.c_str(), po->cvalue);
}
printf("\n");
}