Skip to content

Commit

Permalink
Merge branch 'master' into ready
Browse files Browse the repository at this point in the history
  • Loading branch information
sr14978 committed Dec 5, 2016
2 parents fc2a986 + ff748ad commit 2bac6ea
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/com/modsim/modules/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ else if (source.canInput() && target.canOutput()) {
// Changes are done
Main.opStack.endCompoundOp();

// Propagate
newLink.targ.setVal(newLink.src.getVal());
Main.sim.propagate(newLink.targ.owner);

return newLink;
}
Expand Down
27 changes: 20 additions & 7 deletions src/com/modsim/modules/SplitMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.util.Collections;
import java.util.List;

import javax.swing.JOptionPane;

import com.modsim.modules.ports.BidirPort;
import com.modsim.Main;
import com.modsim.modules.parts.Port;
import com.modsim.res.Colors;
import com.modsim.simulator.PickableEntity;
Expand Down Expand Up @@ -133,12 +136,22 @@ public void propagate() {

// Switch based on propagation direction
if (portA0.wasUpdated() || portA1.wasUpdated()) {
if(portA0.isConnected() && portA1.isConnected())
{
JOptionPane.showMessageDialog(Main.ui.pane, "Error: There must only be one connection to that size of split/merge.");
Port port = portA0.wasUpdated()?portA0:portA1;
synchronized (Main.sim)
{
Main.sim.removeLink(port.link);
}
return;
}
b0_val.setBit(0, a0_val.getBit(0)); // A0-a0
b1_val.setBit(0, a0_val.getBit(1)); // A1-b1
b0_val.setBit(1, a0_val.getBit(1)); // A1-a1

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

b2_val.setBit(0, a0_val.getBit(2)); // A2-c0
Expand All @@ -151,15 +164,15 @@ else if ( portB0.wasUpdated() || portB1.wasUpdated() ||
portB2.wasUpdated() || portB3.wasUpdated()) {
a0_val.setBit(0, b0_val.getBit(0)); // a0-A0
a0_val.setBit(2, b2_val.getBit(0)); // c0-A2
a0_val.setBit(3, b2_val.getBit(1)); // c1-A3
a1_val.setBit(0, b2_val.getBit(0)); // c0-B0

// Resolution of 3-state logic for merges
a1_val.setBit(1, b2_val.getBit(1)); // c1-B1
a1_val.resolveBit(1, b3_val.getBit(0)); // d0-B1

a0_val.setBit(1, b0_val.getBit(1)); // a1-A1
a0_val.resolveBit(1, b1_val.getBit(0)); // b0-A1
int val = b2_val.getBit(1) | b3_val.getBit(0);
a1_val.setBit(1, val);
a0_val.setBit(3, val);

val = b0_val.getBit(1) | b1_val.getBit(0);
a0_val.setBit(1, val);
}

// Set the values
Expand Down
9 changes: 9 additions & 0 deletions src/com/modsim/modules/ports/BidirPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,13 @@ public boolean hasDirection() {
return !(mode == Mode.MODE_BIDIR);
}

public boolean isConnected() {
BinData data = this.getVal();
for(int i = 0; i<4; i++)
{
if(data.getBit(i)!=BinData.NOCON) return true;
}
return false;
}

}
32 changes: 27 additions & 5 deletions src/com/modsim/simulator/Sim.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.swing.JOptionPane;

Expand All @@ -11,6 +13,8 @@
import static com.modsim.modules.BaseModule.AvailableModules;
import com.modsim.modules.parts.Port;
import sun.awt.Mutex;

import com.modsim.util.BinData;
import com.modsim.util.CtrlPt;

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

private Queue<QueueItem> propagationQueue;

/**
* Begin deferring propagation operations (preventing errors during large-scale operations)
*/
Expand Down Expand Up @@ -101,7 +107,7 @@ public void newSim() {
filePath = "";
Main.ui.updateTitle();
}

propagationQueue = new LinkedList<QueueItem>();
Main.ui.view.flagStaticRedraw();
}

Expand Down Expand Up @@ -197,6 +203,8 @@ public void removeLink(Link l) {
synchronized (this) {
links.remove(l);
}
l.src.link = null;
l.targ.setVal(new BinData());
}

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

// Add link to visited - remove after propagation
visited[id] = true;
doPropagate(p.link.targ.owner, visited);
visited[id] = false;
boolean[] clone = visited.clone();
clone[id] = true;
propagationQueue.add(new QueueItem(p.link.targ.owner, clone));
}
p.updated = false;
}
Expand All @@ -339,7 +347,21 @@ private void doPropagate(BaseModule m, boolean[] visited) {
*/
public void propagate(BaseModule m) {
synchronized (lock) {
doPropagate(m, new boolean[1024]);
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;
}
}

}
3 changes: 2 additions & 1 deletion src/com/modsim/tools/MakeLinkTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public boolean endLink(int x, int y) {
Main.opStack.beginCompoundOp();
Link l = Link.createLink(source, targ, curve);
if (l != null) {
Main.sim.addLink(l);
Main.sim.addLink(l);
Main.sim.propagate(l.targ.owner);
Main.opStack.pushOp(new CreateOperation(l));
}
Main.opStack.endCompoundOp();
Expand Down
2 changes: 1 addition & 1 deletion src/com/modsim/util/ModuleClipboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected void doCopy(List<BaseModule> src, List<BaseModule> destModules, List<L
// Store the new link
if (newLink != null) {
assert newLink.path != oldPort.link.path;

Main.sim.propagate(newLink.targ.owner);
newPort.link = newLink;
destLinks.add(newLink);
}
Expand Down
1 change: 1 addition & 0 deletions src/com/modsim/util/XMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ else if (p.ID == targID) {
// Add to the simulation
if (l != null) {
Main.sim.addLink(l);
Main.sim.propagate(l.targ.owner);
}
else {
badLinks++;
Expand Down

0 comments on commit 2bac6ea

Please sign in to comment.