63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
#include "circuit.h"
|
|
|
|
#include "assert.h"
|
|
|
|
bool Gate::is_propagated() {
|
|
return sa[0] || sa[1];
|
|
}
|
|
|
|
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;
|
|
default:
|
|
assert(false);
|
|
break;
|
|
}
|
|
return res;
|
|
} |