Search in sources :

Example 1 with Expression

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);
            }
        }
    }
}
Also used : Expression(com.cburch.logisim.analyze.model.Expression) Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location)

Example 2 with Expression

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()));
    }
}
Also used : Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) Expression(com.cburch.logisim.analyze.model.Expression) Component(com.cburch.logisim.comp.Component) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Location(com.cburch.logisim.data.Location)

Example 3 with Expression

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);
}
Also used : ExpressionComputer(com.cburch.logisim.circuit.ExpressionComputer) WireRepair(com.cburch.logisim.tools.WireRepair) WireRepairData(com.cburch.logisim.tools.WireRepairData) Expression(com.cburch.logisim.analyze.model.Expression) Map(java.util.Map)

Example 4 with Expression

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;
}
Also used : Expression(com.cburch.logisim.analyze.model.Expression) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Location(com.cburch.logisim.data.Location)

Aggregations

Expression (com.cburch.logisim.analyze.model.Expression)4 Location (com.cburch.logisim.data.Location)3 Component (com.cburch.logisim.comp.Component)2 Map (java.util.Map)2 CircuitMutation (com.cburch.logisim.circuit.CircuitMutation)1 ExpressionComputer (com.cburch.logisim.circuit.ExpressionComputer)1 Instance (com.cburch.logisim.instance.Instance)1 WireRepair (com.cburch.logisim.tools.WireRepair)1 WireRepairData (com.cburch.logisim.tools.WireRepairData)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1