use of com.cburch.logisim.analyze.model.Expression in project logisim-evolution by reds-heig.
the class Analyze method propagateWires.
// propagates expressions down wires
private static void propagateWires(ExpressionMap expressionMap, HashSet<Location> pointsToProcess) throws AnalyzeException {
expressionMap.currentCause = null;
for (Location p : pointsToProcess) {
Expression e = expressionMap.get(p);
expressionMap.currentCause = expressionMap.causes.get(p);
WireBundle bundle = expressionMap.circuit.wires.getWireBundle(p);
if (e != null && bundle != null && bundle.points != null) {
for (Location p2 : bundle.points) {
if (p2.equals(p))
continue;
Expression old = expressionMap.get(p2);
if (old != null) {
Component eCause = expressionMap.currentCause;
Component oldCause = expressionMap.causes.get(p2);
if (eCause != oldCause && !old.equals(e)) {
throw new AnalyzeException.Conflict();
}
}
expressionMap.put(p2, e);
}
}
}
}
use of com.cburch.logisim.analyze.model.Expression in project logisim-evolution by reds-heig.
the class Analyze method computeExpression.
//
// computeExpression
//
/**
* Computes the expression corresponding to the given circuit, or raises
* ComputeException if difficulties arise.
*/
public static void computeExpression(AnalyzerModel model, Circuit circuit, Map<Instance, String> pinNames) throws AnalyzeException {
ExpressionMap expressionMap = new ExpressionMap(circuit);
ArrayList<String> inputNames = new ArrayList<String>();
ArrayList<String> outputNames = new ArrayList<String>();
ArrayList<Instance> outputPins = new ArrayList<Instance>();
for (Map.Entry<Instance, String> entry : pinNames.entrySet()) {
Instance pin = entry.getKey();
String label = entry.getValue();
if (Pin.FACTORY.isInputPin(pin)) {
expressionMap.currentCause = Instance.getComponentFor(pin);
Expression e = Expressions.variable(label);
expressionMap.put(pin.getLocation(), e);
inputNames.add(label);
} else {
outputPins.add(pin);
outputNames.add(label);
}
}
propagateComponents(expressionMap, circuit.getNonWires());
for (int iterations = 0; !expressionMap.dirtyPoints.isEmpty(); iterations++) {
if (iterations > MAX_ITERATIONS) {
throw new AnalyzeException.Circular();
}
propagateWires(expressionMap, new HashSet<Location>(expressionMap.dirtyPoints));
HashSet<Component> dirtyComponents = getDirtyComponents(circuit, expressionMap.dirtyPoints);
expressionMap.dirtyPoints.clear();
propagateComponents(expressionMap, dirtyComponents);
Expression expr = checkForCircularExpressions(expressionMap);
if (expr != null)
throw new AnalyzeException.Circular();
}
model.setVariables(inputNames, outputNames);
for (int i = 0; i < outputPins.size(); i++) {
Instance pin = outputPins.get(i);
model.getOutputExpressions().setExpression(outputNames.get(i), expressionMap.get(pin.getLocation()));
}
}
use of com.cburch.logisim.analyze.model.Expression in project logisim-evolution by reds-heig.
the class AbstractGate method getInstanceFeature.
@Override
protected Object getInstanceFeature(final Instance instance, Object key) {
if (key == WireRepair.class) {
return new WireRepair() {
public boolean shouldRepairWire(WireRepairData data) {
return AbstractGate.this.shouldRepairWire(instance, data);
}
};
}
if (key == ExpressionComputer.class) {
return new ExpressionComputer() {
public void computeExpression(Map<Location, Expression> expressionMap) {
GateAttributes attrs = (GateAttributes) instance.getAttributeSet();
int inputCount = attrs.inputs;
int negated = attrs.negated;
Expression[] inputs = new Expression[inputCount];
int numInputs = 0;
for (int i = 1; i <= inputCount; i++) {
Expression e = expressionMap.get(instance.getPortLocation(i));
if (e != null) {
int negatedBit = (negated >> (i - 1)) & 1;
if (negatedBit == 1) {
e = Expressions.not(e);
}
inputs[numInputs] = e;
++numInputs;
}
}
if (numInputs > 0) {
Expression out = AbstractGate.this.computeExpression(inputs, numInputs);
expressionMap.put(instance.getPortLocation(0), out);
}
}
};
}
return super.getInstanceFeature(instance, key);
}
use of com.cburch.logisim.analyze.model.Expression in project logisim-evolution by reds-heig.
the class CircuitBuilder method build.
public static CircuitMutation build(Circuit destCirc, AnalyzerModel model, boolean twoInputs, boolean useNands) {
CircuitMutation result = new CircuitMutation(destCirc);
result.clear();
Layout[] layouts = new Layout[model.getOutputs().size()];
int maxWidth = 0;
for (int i = 0; i < layouts.length; i++) {
String output = model.getOutputs().get(i);
Expression expr = model.getOutputExpressions().getExpression(output);
CircuitDetermination det = CircuitDetermination.create(expr);
if (det != null) {
if (twoInputs)
det.convertToTwoInputs();
if (useNands)
det.convertToNands();
det.repair();
layouts[i] = layoutGates(det);
maxWidth = Math.max(maxWidth, layouts[i].width);
} else {
layouts[i] = null;
}
}
InputData inputData = computeInputData(model);
int x = inputData.getStartX();
int y = 10;
int outputX = x + maxWidth + 20;
for (int i = 0; i < layouts.length; i++) {
String outputName = model.getOutputs().get(i);
Layout layout = layouts[i];
Location output;
int height;
if (layout == null) {
output = Location.create(outputX, y + 20);
height = 40;
} else {
int dy = 0;
if (layout.outputY < 20)
dy = 20 - layout.outputY;
height = Math.max(dy + layout.height, 40);
output = Location.create(outputX, y + dy + layout.outputY);
placeComponents(result, layouts[i], x, y + dy, inputData, output);
}
placeOutput(result, output, outputName);
y += height + 10;
}
placeInputs(result, inputData);
return result;
}
Aggregations