forked from TeachingTechnologistBeth/ModuleSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSim.java
More file actions
366 lines (307 loc) · 9.3 KB
/
Sim.java
File metadata and controls
366 lines (307 loc) · 9.3 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.modsim.simulator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.swing.JOptionPane;
import com.modsim.Main;
import com.modsim.modules.*;
import static com.modsim.modules.BaseModule.AvailableModules;
import com.modsim.modules.parts.Port;
import com.modsim.util.BinData;
import com.modsim.util.CtrlPt;
public class Sim implements Runnable {
private Thread thread;
public final Object lock = new Object();
private int lastLinkInd = 0;
public static long delay = 2500000;
public volatile boolean running = false;
public String filePath = "";
// Module list
private final List<BaseModule> modules = new ArrayList<>();
private final List<BaseModule> propModules = new ArrayList<>();
private final List<Link> links = new ArrayList<>();
private final List<PickableEntity> entities = new ArrayList<>();
public double itrPerSec = 0;
public int iterations = 0;
// Deferred propagation mechanism
private List<BaseModule> deferredPropagators = new ArrayList<>();
private int deferring = 0;
private Queue<QueueItem> propagationQueue;
/**
* Begin deferring propagation operations (preventing errors during large-scale operations)
*/
public void beginDeferPropagations() {
deferring++;
}
/**
* Finish deferring propagation operations (carries out the deferred propagations)
*/
public void endDeferPropagations() {
deferring--;
assert(deferring >= 0);
if (deferring == 0) {
for (BaseModule m : deferredPropagators) {
propagate(m);
}
deferredPropagators.clear();
}
}
/**
* Start the sim
*/
public void start() {
running = true;
clearErrors();
if (thread == null || !thread.isAlive()) {
thread = new Thread(this);
thread.start();
}
}
/**
* Stop the sim
*/
public void stop() {
clearErrors();
running = false;
itrPerSec = 0;
}
// Grid size
public int grid = 25;
// New simulation
public void newSim() {
// Reset the camera position
Main.ui.view.camX = 0;
Main.ui.view.camY = 0;
synchronized (this) {
modules.clear();
links.clear();
propModules.clear();
entities.clear();
Main.opStack.clearAll();
filePath = "";
Main.ui.updateTitle();
}
propagationQueue = new LinkedList<QueueItem>();
Main.ui.view.flagStaticRedraw();
}
/**
* Module access (MUST be contained in
* synchronized (Main.sim) block)
*/
public List<BaseModule> getModules() {
return modules;
}
/**
* Entity access (MUST be contained in
* synchronized (Main.sim) block)
*/
public List<PickableEntity> getEntities() {
return entities;
}
/**
* Link access (MUST be contained in
* synchronized (Main.sim) block)
*/
public List<Link> getLinks() {
return links;
}
/**
* Thread safe entity add
*/
public void addEntity(PickableEntity ent) {
synchronized (this) {
clearErrors();
if (ent.getType() == PickableEntity.MODULE) {
BaseModule m = (BaseModule) ent;
modules.add(m);
if (m.getModType() == AvailableModules.CLOCK) {
propModules.add(m);
}
}
entities.add(ent);
}
}
/**
* Thread safe entity removal. Removes module links.
*/
public void removeEntity(PickableEntity ent) {
synchronized (this) {
entities.remove(ent);
// Module-specific cleanup also removes leftover links
if (ent.getType() == PickableEntity.MODULE) {
BaseModule module = (BaseModule) ent;
modules.remove(ent);
propModules.remove(ent);
for (Port p : module.ports) {
if (p.link != null) {
p.link.delete();
}
}
}
}
}
/**
* Thread safe link (& control points) addition
*/
public void addLink(Link l) {
synchronized (this) {
clearErrors();
links.add(l);
for (CtrlPt c : l.path.getCtrlPts()) {
addEntity(c);
}
}
}
/**
* Yields a unique ID for a link
*/
public int assignLinkID() {
return lastLinkInd++;
}
/**
* Thread safe link remove
*/
public void removeLink(Link l) {
synchronized (this) {
links.remove(l);
}
l.src.link = null;
l.targ.setVal(new BinData());
}
/**
* Removes error flags from com.modsim.modules
*/
public void clearErrors() {
for (BaseModule m : modules) {
m.error = false;
}
}
public void run() {
int iterations = 0;
long start = System.currentTimeMillis();
// Runs the sim constantly
while(running) {
// Iterate
step();
// Calculate speed
iterations++;
long now = System.currentTimeMillis();
long delta = now - start;
if (delta > 1000) {
itrPerSec = iterations;
iterations = 0;
start = now;
}
// Speed control
if (delay != 0) {
nanoWait(delay);
}
}
}
/**
* Nanosecond(ish)-accurate wait
*/
private void nanoWait(long interval) {
int ms = (int) (interval / 100000);
try {Thread.sleep(ms);}
catch (Exception e) {
System.err.println("Warning: thread sleep exception!");
}
interval = interval % 100000;
long start = System.nanoTime();
long end;
do {
end = System.nanoTime();
} while (start + interval >= end);
}
/**
* Recursive simulation
*/
public void step() {
synchronized (lock) {
// Don't run while we're deferring operations
if (deferring != 0) return;
//System.out.print("\nIteration " + iterations + " : ");
iterations++;
for (int i = 0; i < propModules.size(); i++) {
BaseModule m = propModules.get(i);
// Tick the clock(s)
if (m.getModType().equals(AvailableModules.CLOCK)) {
((Clock) m).tick();
}
// Begin propagation at the clocks AND switches
propagate(m);
}
}
// Request view update
Main.ui.view.flagDynamicRedraw();
}
/**
* Thread-local propagation
* @param m Module to propagate on
* @param visited ID-indexed array indicating whether we've already propagated over each module
*/
private void doPropagate(BaseModule m, boolean[] visited) {
if (deferring != 0) {
deferredPropagators.add(m);
}
else {
if (m == null) return;
m.propagate();
for (Port p : m.ports) {
if (!p.canOutput()) {
p.updated = false;
continue;
}
if (p.wasUpdated() && p.link != null) {
// First make sure 'visited' array is big enough
int id = p.link.getLinkID();
if (id >= visited.length) {
visited = Arrays.copyOf(visited, id * 2 + 1);
}
// Check if we've visited this link before
if (visited[id]) {
p.owner.error = true;
running = false;
JOptionPane.showMessageDialog(null, "Runtime loop detected! Halting simulation. Did you forget a register?");
return;
}
// Recursively propagate
if (p.link.targ == null) {
System.out.println("Warning: Null propagation target");
return;
}
p.link.targ.setVal(p.getVal());
// Add link to visited - remove after propagation
boolean[] clone = visited.clone();
clone[id] = true;
propagationQueue.add(new QueueItem(p.link.targ.owner, clone));
}
p.updated = false;
}
}
}
/**
* Propagates through a module
* @param m Module to propagate
*/
public void propagate(BaseModule m) {
synchronized (lock) {
propagationQueue.add(new QueueItem(m, new boolean[1024]));
while(!propagationQueue.isEmpty()){
QueueItem it = propagationQueue.remove();
doPropagate(it.baseModule, it.visited);
}
}
}
class QueueItem {
private BaseModule baseModule;
private boolean[] visited;
public QueueItem(BaseModule m, boolean[] v){
baseModule = m;
visited = v;
}
}
}