-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicSystem.js
117 lines (100 loc) · 2.84 KB
/
LogicSystem.js
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
initial_values = {
"AND": false,
"OR": false,
"NOT": true,
"NAND": true,
"NOR": true,
"XOR": false,
"XNOR": true,
"SWITCH": false,
"BULB": false
};
operations = {
"AND": function(a, b) {return a && b;},
"OR": function(a, b) {return a || b;},
"NOT": function(a, b) {return !a;},
"NAND": function(a, b) {return !(a && b);},
"NOR": function(a, b) {return !(a || b);},
"XOR": function(a, b) {return (a || b) && ! (a && b);},
"XNOR": function(a, b) {return (a && b) || (!a && !b);},
"BULB": function(a, b, c) {return a;},
"SWITCH": function(a, b, c) {return c;}
};
function runStep(gates, input_values) {
var outputs = [];
for (var i = 0; i < gates.length; i++) {
var gate = gates[i];
if (gate) {
outputs[i] = operations[gate[0]](input_values[gate[1]], input_values[gate[2]], input_values[i]);
}
}
return outputs;
}
function applyToArrays(func, a, b) {
output = [];
for (var i = 0; i < a.length; i++) {
output[i] = func(a[i], b[i]);
}
return output;
}
function any(a) {
for (var i = 0; i < a.length; i++) {
if (a[i]) {
return true;
}
}
}
/********************************************************************************************/
function LogicSystem()
/********************************************************************************************/
{
this.gates = [];
this.output_values = [false];
this.transients = [false];
this.callbacks = [];
}
LogicSystem.prototype.addGate = function(id, type) {
this.gates[id] = [type, 0, 0];
this.output_values[id] = initial_values[type];
this.transients[id] = initial_values[type];
};
LogicSystem.prototype.removeGate = function(id) {
this.gates[id] = null;
};
LogicSystem.prototype.makeConnection = function(output_id, input_id, input_number) {
this.gates[input_id][input_number + 1] = output_id;
};
LogicSystem.prototype.dropConnection = function(output_id, input_id, input_number) {
this.gate[input_id][input_number + 1] = 0;
};
LogicSystem.prototype.registerCallback = function(id, callback) {
this.callbacks[id] = callback;
};
LogicSystem.prototype.dropCallback = function(id) {
this.callbacks[i] = null;
};
LogicSystem.prototype.setOutput = function(n, value) {
this.transients[n] = true;
this.output_values[n] = value;
};
LogicSystem.prototype.injectTransient = function(n) {
this.transients[n] = true;
};
LogicSystem.prototype.runStep = function() {
var old_output_values = this.output_values;
this.output_values = runStep(this.gates, this.output_values);
this.transients = applyToArrays(operations["XOR"], this.output_values, old_output_values);
};
LogicSystem.prototype.runCallbacks = function() {
for (var i = 0; i < this.transients.length; i++) {
if (this.transients[i] && this.callbacks[i]) {
this.callbacks[i](this.output_values[i]);
}
}
};
LogicSystem.prototype.run = function() {
while (any(this.transients)) {
this.runStep();
this.runCallbacks();
}
};