-
Notifications
You must be signed in to change notification settings - Fork 0
/
s.js
184 lines (160 loc) · 4.59 KB
/
s.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
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
var running = false;
var singleStep = false;
var nextBatch = new SetArray;
var currentBatch;
var oscillation = false;
var resets = [];
var clocks = [];
var diodes = [];
var tick = 0;
var phase = 0;
function run(stepThrough=false) {
save('current');
nextBatch.clear();
updateQueue = [];
currentBatch = new Set(nextBatch);
resets = circuit.components.filter(gate => gate.type === 'Reset');
clocks = circuit.components.filter(c => c.type === 'Clock');
diodes = circuit.components.filter(c => c.type === 'Diode');
if (circuit.settings.script !== null) {
var context = {addComponent, addWire};
var fn = new Function([...Object.keys(context),...circuit.settings.parameters], circuit.settings.script);
fn.apply(circuit.settings, [...Object.values(context),...circuit.settings.parameters.map(p => circuit.settings[p])]);
}
var ics = circuit.components.filter(c => ['IC','Script'].includes(c.type));
ics.forEach(ic => ic.init());
createConnectionGroups();
ics.forEach(ic => ic.createGroups());
ics.forEach(ic => ic.link());
ConnectionGroup.init();
circuit.components.filter(gate => ['Input','Clock','Button'].includes(gate.type)).forEach(inp => inp.state = 0);
circuit.components.filter(gate => ['Joystick','RotaryEncoder'].includes(gate.type)).forEach(inp => inp.resetThumb());
circuit.components.filter(gate => ['Supply','Ground','Constant','Reset','PullUp','PullDown','Input','Clock','Button','Joystick','RotaryEncoder'].includes(gate.type)).forEach(g => nextBatch.add(g));
diodes.forEach(diode => {
var {anode,cathode} = diode;
var bgroup = anode.connectionGroup.pins.some(pin =>
pin !== anode && pin.component.type === 'Diode' && pin.component.anode === pin
);
var fgroup = cathode.connectionGroup.pins.some(pin =>
pin !== cathode && pin.component.type === 'Diode' && pin.component.cathode === pin
);
if (bgroup && !fgroup) return diode.update = diode.backwardUpdate;
if (!bgroup && fgroup) return diode.update = diode.forwardUpdate;
if (bgroup && anode.connectionGroup.pins.some(pin =>
pin.component.type === 'PullUp'
)) return diode.update = diode.backwardUpdate;
if (fgroup && cathode.connectionGroup.pins.some(pin =>
pin.component.type === 'PullDown'
)) return diode.update = diode.forwardUpdate;
anode.type = 'input';
cathode.type = 'output';
});
if (!singleStep) {
stabilize();
nextBatch.clear();
resets.forEach(g => nextBatch.add(g));
stabilize();
} else {
phase = 0;
}
running = true;
oscillation = false;
singleStep = stepThrough;
if (stepThrough) return;
tick = 0;
var frequency = circuit.settings.frequency;
var state = 1;
var halfCycle = 500/frequency;
var lt = 0;
var dt;
var acc = 0;
function exloop(t) {
if (!lt) lt = t;
dt = t - lt;
acc += dt;
while (acc >= halfCycle) {
state = 1-state;
clocks.forEach(clock => {
clock.state = state;
nextBatch.push(clock);
});
stabilize();
if (oscillation) break;
acc -= halfCycle;
}
rerender = true;
lt = t;
if (running) {
if (oscillation) {
frequency = min(frequency, 5);
requestAnimationFrame(unstableLoop);
} else {
requestAnimationFrame(exloop);
}
}
}
function unstableLoop(t) {
if (!lt) lt = t;
dt = t - lt;
acc += dt;
while (acc >= halfCycle) {
state = 1-state;
clocks.forEach(clock => {
clock.state = state;
clock.update();
});
step();
acc -= halfCycle;
}
rerender = true;
lt = t;
if (running) requestAnimationFrame(unstableLoop);
}
requestAnimationFrame(exloop);
}
//old
/*
function exloop() {
stabilize();
if (running) requestAnimationFrame(exloop);
}
*/
function stop() {
load('current');
}
var updateQueue;
function step() {
ConnectionGroup.initStep();
currentBatch = new Set(nextBatch);
nextBatch.clear();
updateQueue = [];
currentBatch.forEach(c => c.update());
updateQueue.forEach(q => q[0].update(q[1],q[2]));
tick++;
if (singleStep && phase === 0 && !nextBatch.length) {
resets.forEach(g => nextBatch.add(g));
phase = 1;
}
}
function stabilize() {
var o = 0;
while (nextBatch.length > 0) {
o++;
if (o > 1000) {
break;
}
step();
}
rerender = true;
if (o > 1000) {
// stop();
console.log('oscillation detected, running step-by-step...');
oscillation = true;
}
return;
var ss = ConnectionGroup.all.find(group => group.checkShortCircuit());
if (ss) {
stop();
throw Error('short circuit detected');
}
}