Skip to content

Commit 21e7781

Browse files
committed
Merge remote-tracking branch 'refs/remotes/uobteachingtechnologist/master'
2 parents 32f370f + 31ee5de commit 21e7781

File tree

10 files changed

+81
-26
lines changed

10 files changed

+81
-26
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ $RECYCLE.BIN/
6767
# Windows shortcuts
6868
*.lnk
6969
/bin/
70+
/bin1/

src/com/modsim/Main.java

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void run() {
3636
ui.showUI(true);
3737

3838
// Start sim ticking - sim is initialized below *before* this is called
39+
sim.newSim();
3940
sim.start();
4041
}
4142
});

src/com/modsim/gui/view/View.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,6 @@ public void paintStatic() {
130130
drawGrid(staticG);
131131
staticG.setTransform(oldStatic);
132132

133-
// Draw links
134-
staticG.transform(wToV);
135-
for (Link l : Main.sim.getLinks()) {
136-
if (l == null) {
137-
System.err.println("Warning: Null link encountered while drawing");
138-
continue;
139-
}
140-
l.draw(staticG);
141-
}
142-
143133
// Draw modules - static
144134
staticG.setTransform(oldStatic);
145135
for (BaseModule m : Main.sim.getModules()) {
@@ -149,6 +139,16 @@ public void paintStatic() {
149139
staticG.setTransform(oldStatic);
150140
}
151141

142+
// Draw links
143+
staticG.transform(wToV);
144+
for (Link l : Main.sim.getLinks()) {
145+
if (l == null) {
146+
System.err.println("Warning: Null link encountered while drawing");
147+
continue;
148+
}
149+
l.draw(staticG);
150+
}
151+
152152
staticG.setTransform(oldStatic);
153153

154154
// Static canvas is now up-to-date

src/com/modsim/modules/Link.java

-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ else if (source.canInput() && target.canOutput()) {
167167
// Changes are done
168168
Main.opStack.endCompoundOp();
169169

170-
// Propagate
171170
newLink.targ.setVal(newLink.src.getVal());
172-
Main.sim.propagate(newLink.targ.owner);
173171

174172
return newLink;
175173
}

src/com/modsim/modules/SplitMerge.java

+29-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
import java.util.Collections;
99
import java.util.List;
1010

11+
import javax.swing.JOptionPane;
12+
1113
import com.modsim.modules.ports.BidirPort;
14+
import com.modsim.Main;
1215
import com.modsim.modules.parts.Port;
16+
import com.modsim.modules.parts.SSText;
1317
import com.modsim.res.Colors;
1418
import com.modsim.simulator.PickableEntity;
1519
import com.modsim.util.BezierCurve;
@@ -74,6 +78,14 @@ public SplitMerge() {
7478
cs[8] = new BezierCurve(new Vec2(B + b1, 23), new Vec2(d - b0, -23), // B1-d0
7579
new Vec2(B + b1, -15), new Vec2(d - b0, 15));
7680
curves = Collections.unmodifiableList(Arrays.asList(cs));
81+
82+
parts.add(new SSText(A-10, 24, "DCBA", 7, Colors.labelText));
83+
parts.add(new SSText(B-10, 24, "XXDC", 7, Colors.labelText));
84+
parts.add(new SSText(a+10, -24, "ABXX", -7, Colors.labelText));
85+
parts.add(new SSText(b+10, -24, "BXXX", -7, Colors.labelText));
86+
parts.add(new SSText(c+10, -24, "CDXX", -7, Colors.labelText));
87+
parts.add(new SSText(d+10, -24, "DXXX", -7, Colors.labelText));
88+
7789
}
7890

7991
@Override
@@ -133,12 +145,22 @@ public void propagate() {
133145

134146
// Switch based on propagation direction
135147
if (portA0.wasUpdated() || portA1.wasUpdated()) {
148+
if(portA0.isConnected() && portA1.isConnected())
149+
{
150+
JOptionPane.showMessageDialog(Main.ui.pane, "Error: There must only be one connection to that size of split/merge.");
151+
Port port = portA0.wasUpdated()?portA0:portA1;
152+
synchronized (Main.sim)
153+
{
154+
Main.sim.removeLink(port.link);
155+
}
156+
return;
157+
}
136158
b0_val.setBit(0, a0_val.getBit(0)); // A0-a0
137159
b1_val.setBit(0, a0_val.getBit(1)); // A1-b1
138160
b0_val.setBit(1, a0_val.getBit(1)); // A1-a1
139161

140162
// Resolution of 3-state logic for merges
141-
b3_val.setBit(0, a0_val.getBit(3)); // A3-d0
163+
b3_val.setBit(0, a0_val.getBit(3)); // A3-d0
142164
b3_val.resolveBit(0, a1_val.getBit(1)); // B1-d0
143165

144166
b2_val.setBit(0, a0_val.getBit(2)); // A2-c0
@@ -151,15 +173,15 @@ else if ( portB0.wasUpdated() || portB1.wasUpdated() ||
151173
portB2.wasUpdated() || portB3.wasUpdated()) {
152174
a0_val.setBit(0, b0_val.getBit(0)); // a0-A0
153175
a0_val.setBit(2, b2_val.getBit(0)); // c0-A2
154-
a0_val.setBit(3, b2_val.getBit(1)); // c1-A3
155176
a1_val.setBit(0, b2_val.getBit(0)); // c0-B0
156177

157178
// Resolution of 3-state logic for merges
158-
a1_val.setBit(1, b2_val.getBit(1)); // c1-B1
159-
a1_val.resolveBit(1, b3_val.getBit(0)); // d0-B1
160-
161-
a0_val.setBit(1, b0_val.getBit(1)); // a1-A1
162-
a0_val.resolveBit(1, b1_val.getBit(0)); // b0-A1
179+
int val = b2_val.getBit(1) | b3_val.getBit(0);
180+
a1_val.setBit(1, val);
181+
a0_val.setBit(3, val);
182+
183+
val = b0_val.getBit(1) | b1_val.getBit(0);
184+
a0_val.setBit(1, val);
163185
}
164186

165187
// Set the values

src/com/modsim/modules/ports/BidirPort.java

+9
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,13 @@ public boolean hasDirection() {
107107
return !(mode == Mode.MODE_BIDIR);
108108
}
109109

110+
public boolean isConnected() {
111+
BinData data = this.getVal();
112+
for(int i = 0; i<4; i++)
113+
{
114+
if(data.getBit(i)!=BinData.NOCON) return true;
115+
}
116+
return false;
117+
}
118+
110119
}

src/com/modsim/simulator/Sim.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.LinkedList;
56
import java.util.List;
7+
import java.util.Queue;
68

79
import javax.swing.JOptionPane;
810

@@ -11,6 +13,8 @@
1113
import static com.modsim.modules.BaseModule.AvailableModules;
1214
import com.modsim.modules.parts.Port;
1315
import sun.awt.Mutex;
16+
17+
import com.modsim.util.BinData;
1418
import com.modsim.util.CtrlPt;
1519

1620
public class Sim implements Runnable {
@@ -38,6 +42,8 @@ public class Sim implements Runnable {
3842
private List<BaseModule> deferredPropagators = new ArrayList<>();
3943
private int deferring = 0;
4044

45+
private Queue<QueueItem> propagationQueue;
46+
4147
/**
4248
* Begin deferring propagation operations (preventing errors during large-scale operations)
4349
*/
@@ -101,7 +107,7 @@ public void newSim() {
101107
filePath = "";
102108
Main.ui.updateTitle();
103109
}
104-
110+
propagationQueue = new LinkedList<QueueItem>();
105111
Main.ui.view.flagStaticRedraw();
106112
}
107113

@@ -197,6 +203,8 @@ public void removeLink(Link l) {
197203
synchronized (this) {
198204
links.remove(l);
199205
}
206+
l.src.link = null;
207+
l.targ.setVal(new BinData());
200208
}
201209

202210
/**
@@ -324,9 +332,9 @@ private void doPropagate(BaseModule m, boolean[] visited) {
324332
p.link.targ.setVal(p.getVal());
325333

326334
// Add link to visited - remove after propagation
327-
visited[id] = true;
328-
doPropagate(p.link.targ.owner, visited);
329-
visited[id] = false;
335+
boolean[] clone = visited.clone();
336+
clone[id] = true;
337+
propagationQueue.add(new QueueItem(p.link.targ.owner, clone));
330338
}
331339
p.updated = false;
332340
}
@@ -339,7 +347,21 @@ private void doPropagate(BaseModule m, boolean[] visited) {
339347
*/
340348
public void propagate(BaseModule m) {
341349
synchronized (lock) {
342-
doPropagate(m, new boolean[1024]);
350+
propagationQueue.add(new QueueItem(m, new boolean[1024]));
351+
while(!propagationQueue.isEmpty()){
352+
QueueItem it = propagationQueue.remove();
353+
doPropagate(it.baseModule, it.visited);
354+
}
343355
}
344356
}
357+
358+
class QueueItem {
359+
private BaseModule baseModule;
360+
private boolean[] visited;
361+
public QueueItem(BaseModule m, boolean[] v){
362+
baseModule = m;
363+
visited = v;
364+
}
365+
}
366+
345367
}

src/com/modsim/tools/MakeLinkTool.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public boolean endLink(int x, int y) {
120120
Main.opStack.beginCompoundOp();
121121
Link l = Link.createLink(source, targ, curve);
122122
if (l != null) {
123-
Main.sim.addLink(l);
123+
Main.sim.addLink(l);
124+
Main.sim.propagate(l.targ.owner);
124125
Main.opStack.pushOp(new CreateOperation(l));
125126
}
126127
Main.opStack.endCompoundOp();

src/com/modsim/util/ModuleClipboard.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected void doCopy(List<BaseModule> src, List<BaseModule> destModules, List<L
152152
// Store the new link
153153
if (newLink != null) {
154154
assert newLink.path != oldPort.link.path;
155-
155+
Main.sim.propagate(newLink.targ.owner);
156156
newPort.link = newLink;
157157
destLinks.add(newLink);
158158
}

src/com/modsim/util/XMLReader.java

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ else if (p.ID == targID) {
188188
// Add to the simulation
189189
if (l != null) {
190190
Main.sim.addLink(l);
191+
Main.sim.propagate(l.targ.owner);
191192
}
192193
else {
193194
badLinks++;

0 commit comments

Comments
 (0)