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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <iostream>
class Gate
{
public:
Gate() {}
Gate(Gate *g1, Gate *g2)
{
A = g1->GetState(); B = g2->GetState();
}
virtual void DoLogic() =0;
bool GetState()
{
return C; // Return state
}
void SetPin(bool p, bool i) // Set pin A and B with Input
{
if(p)
{
B = i; // 1 is Pin B
DoLogic(); // Update Gate
}
else
{
A = i; // Pin A is 0
DoLogic();
}
}
bool GetPin(bool p) { if(p) return B; else return A; }
void LinkPin(bool dp, Gate *sg, bool sp)
{
}
protected:
bool A; // Pin A
bool B; // Pin B
bool C; // OUT
};
class Link
{
public:
Link(Gate *o, Gate *i, bool p)
{
OUT = o; IN = i; PIN = p;
}
void Update()
{
IN->SetPin(PIN, OUT->GetState());
}
private:
Gate *IN;
Gate *OUT;
bool PIN;
};
std::ostream& operator<<(std::ostream& os, Gate& g) // Not fully sure
{ // But it works
os << g.GetState(); // I imagine putting value into cout
return os; // Then printing it out
}
class NOT : public Gate
{
public:
NOT() {}
NOT(bool a)
{
A = a;
DoLogic();
}
NOT(Gate *g1)
{
A = g1->GetState();
DoLogic();
}
void DoLogic()
{
if(A) C = 0; // Gate logic
else C = 1; // Set output
}
};
class AND : public Gate
{
public:
AND() {}
AND(bool a, bool b)
{
A = a; B = b;
DoLogic();
}
AND(Gate *g1, Gate *g2)
{
A = g1->GetState(); B = g2->GetState();
DoLogic();
}
void DoLogic()
{
if(A && B) C = 1;
else C = 0;
}
private:
};
class NAND : public Gate
{
public:
NAND() {}
NAND(bool a, bool b)
{
A = a; B = b;
DoLogic();
}
NAND(Gate *g1, Gate *g2)
{
A = g1->GetState(); B = g2->GetState();
DoLogic();
}
void DoLogic()
{
if(!(A&&B)) C = 1;
else C = 0;
}
};
class OR : public Gate
{
public:
OR() {}
OR(bool a, bool b)
{
A = a; B = b;
DoLogic();
}
OR(Gate *g1, Gate *g2)
{
A = g1->GetState(); B = g2->GetState();
DoLogic();
}
void DoLogic()
{
if(A || B) C = 1;
else C = 0;
}
};
class NOR : public Gate
{
public:
NOR() {}
NOR(bool a, bool b)
{
A = a; B = b;
DoLogic();
}
NOR(Gate *g1, Gate *g2)
{
A = g1->GetState(); B = g2->GetState();
DoLogic();
}
void DoLogic()
{
if(!(A || B)) C = 1;
else C = 0;
}
};
class XOR : public Gate
{
public:
XOR() {}
XOR(bool a, bool b)
{
A = a; B = B;
DoLogic();
}
XOR(Gate *g1, Gate *g2)
{
A = g1->GetState(); B = g2->GetState();
DoLogic();
}
void DoLogic()
{
if(A^B) C = 1;
else C = 0;
}
};
void TestCircuit()
{
NOT n1(true); // Create a NOT gate
Link l1(&n1, &n1, 0); // Create a Link from carry to PIN A
int iters; iters = 0; // Count cycles
int counta, countb; // How many times 0 or 1
counta = 0; countb = 0;
while(iters < 100)
{
std::cout << "\rNOT1: " << n1
<< "\tIterations: "<< iters
<< "\t0: " << counta
<< "\t1: " << countb;
l1.Update(); // Update the PINS
if(n1.GetState()) countb++;
else counta++;
iters++;
}
}
int main()
{
// Test all expressions
AND a1(false,false); // 0,0 were ambiguous
std::cout << "AND1 = " << a1 << "\n";
AND a2(0,1);
std::cout << "AND2 = " << a2 << "\n";
AND a3(1,0);
std::cout << "AND3 = " << a3 << "\n";
AND a4(1,1);
std::cout << "AND4 = " << a4 << "\n\n";
NAND na1(false,false);
std::cout << "NAND1 = " << na1 << "\n";
NAND na2(0,1);
std::cout << "NAND2 = " << na2 << "\n";
NAND na3(1,0);
std::cout << "NAND3 = " << na3 << "\n";
NAND na4(1,1);
std::cout << "NAND4 = " << na4 << "\n\n";
OR o1(&a1,&na1);
std::cout << "OR1 = " << o1 << "\n";
OR o2(&a2,&na2);
std::cout << "OR2 = " << o2 << "\n";
OR o3(&a3,&na3);
std::cout << "OR3 = " << o3 << "\n";
OR o4(&a4,&na4);
std::cout << "OR4 = " << o4 << "\n\n";
NOR no1(&a1,&na1);
std::cout << "NOR1 = " << no1 << "\n";
NOR no2(&a2,&na2);
std::cout << "NOR2 = " << no2 << "\n";
NOR no3(&a3,&na3);
std::cout << "NOR3 = " << no3 << "\n";
NOR no4(&a1,&na4);
std::cout << "NOR4 = " << no4 << "\n\n";
XOR xo1(&a1,&na1);
std::cout << "XOR1 = " << xo1 << "\n";
XOR xo2(&a2,&na2);
std::cout << "XOR2 = " << xo2 << "\n";
XOR xo3(&a3,&na3);
std::cout << "XOR3 = " << xo3 << "\n";
XOR xo4(&a4,&na4);
std::cout << "XOR4 = " << xo4 << "\n\n";
XOR xo5;
Link l1(&a1, &xo5, 0);
Link l2(&a3, &xo5, 1);
l1.Update();
l2.Update();
std::cout << "XOR5 = " << xo5 << "\n";
XOR xo6;
Link l3(&a1, &xo6, 0);
Link l4(&a4, &xo6, 1);
l3.Update();
l4.Update();
std::cout << "XOR6 = " << xo6 << "\n\n";
Link l5(&xo6, &xo5, 0); // Link XOR6 OUT to XOR5 PIN A
Link l6(&xo6, &xo6, 1); // Link XOR6 OUT to XOR6 PIN B
TestCircuit();
std::cout << "\n\n";
AND Test(&a1, &a2);
Test.LinkPin(0, &a2, 1);
std::cout << "TEST: " << Test << "\n\n";
return 0;
}
|