96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
#include "circuit.h"
|
|
|
|
#include "assert.h"
|
|
|
|
// bool Gate::sa(bool x) {
|
|
// if(x == 0) {
|
|
// return propagate && value;
|
|
// } else {
|
|
// return propagate && !value;
|
|
// }
|
|
// }
|
|
|
|
bool Gate::cal_sa(bool x) {
|
|
if(po) {
|
|
if(x == 0) return value;
|
|
else return !value;
|
|
}
|
|
|
|
bool sa0 = 0;
|
|
bool sa1 = 0;
|
|
|
|
for(Gate* out : outputs) {
|
|
if(!out->propagate) continue;
|
|
|
|
if(out->cal_value() != out->value) continue;
|
|
|
|
this->value = !this->value;
|
|
bool detect = out->cal_value() != out->value;
|
|
this->value = !this->value;
|
|
if(!detect) continue;
|
|
|
|
sa0 |= this->value;
|
|
sa1 |= !this->value;
|
|
}
|
|
if(x == 0) return sa0;
|
|
else return sa1;
|
|
}
|
|
|
|
int Gate::cal_value() {
|
|
int res;
|
|
|
|
switch(type) {
|
|
case NOT:
|
|
res = !inputs[0]->value;
|
|
break;
|
|
case BUF:
|
|
res = inputs[0]->value;
|
|
break;
|
|
case AND:
|
|
res = inputs[0]->value;
|
|
for(int i=1; i<inputs.size(); i++) {
|
|
res &= inputs[i]->value;
|
|
}
|
|
break;
|
|
case NAND:
|
|
res = inputs[0]->value;
|
|
for(int i=1; i<inputs.size(); i++) {
|
|
res &= inputs[i]->value;
|
|
}
|
|
res = !res;
|
|
break;
|
|
case OR:
|
|
res = inputs[0]->value;
|
|
for(int i=1; i<inputs.size(); i++) {
|
|
res |= inputs[i]->value;
|
|
}
|
|
break;
|
|
case NOR:
|
|
res = inputs[0]->value;
|
|
for(int i=1; i<inputs.size(); i++) {
|
|
res |= inputs[i]->value;
|
|
}
|
|
res = !res;
|
|
break;
|
|
case XOR:
|
|
res = inputs[0]->value;
|
|
for(int i=1; i<inputs.size(); i++) {
|
|
res ^= inputs[i]->value;
|
|
}
|
|
break;
|
|
case XNOR:
|
|
res = inputs[0]->value;
|
|
for(int i=1; i<inputs.size(); i++) {
|
|
res ^= inputs[i]->value;
|
|
}
|
|
res = !res;
|
|
break;
|
|
case INPUT:
|
|
res = value;
|
|
break;
|
|
default:
|
|
assert(false);
|
|
break;
|
|
}
|
|
return res;
|
|
} |