use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.
the class Circuit method getBounds.
public Bounds getBounds(Graphics g) {
Bounds ret = wires.getWireBounds();
int xMin = ret.getX();
int yMin = ret.getY();
int xMax = xMin + ret.getWidth();
int yMax = yMin + ret.getHeight();
if (ret == Bounds.EMPTY_BOUNDS) {
xMin = Integer.MAX_VALUE;
yMin = Integer.MAX_VALUE;
xMax = Integer.MIN_VALUE;
yMax = Integer.MIN_VALUE;
}
for (Component c : comps) {
Bounds bds = c.getBounds(g);
if (bds != null && bds != Bounds.EMPTY_BOUNDS) {
int x0 = bds.getX();
int x1 = x0 + bds.getWidth();
int y0 = bds.getY();
int y1 = y0 + bds.getHeight();
if (x0 < xMin)
xMin = x0;
if (x1 > xMax)
xMax = x1;
if (y0 < yMin)
yMin = y0;
if (y1 > yMax)
yMax = y1;
}
}
if (xMin > xMax || yMin > yMax)
return Bounds.EMPTY_BOUNDS;
return Bounds.create(xMin, yMin, xMax - xMin, yMax - yMin);
}
use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.
the class Analyze method propagateComponents.
private static void propagateComponents(ExpressionMap expressionMap, Collection<Component> components) throws AnalyzeException {
for (Component comp : components) {
ExpressionComputer computer = (ExpressionComputer) comp.getFeature(ExpressionComputer.class);
if (computer != null) {
try {
expressionMap.currentCause = comp;
computer.computeExpression(expressionMap);
} catch (UnsupportedOperationException e) {
throw new AnalyzeException.CannotHandle(comp.getFactory().getDisplayName());
}
} else if (comp.getFactory() instanceof Pin) {
// pins are handled elsewhere
;
} else {
// pins are handled elsewhere
throw new AnalyzeException.CannotHandle(comp.getFactory().getDisplayName());
}
}
}
use of com.cburch.logisim.comp.Component 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.comp.Component 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.comp.Component in project logisim-evolution by reds-heig.
the class CircuitBuilder method placeComponents.
//
// placeComponents
//
/**
* @param circuit
* the circuit where to place the components.
* @param layout
* the layout specifying the gates to place there.
* @param x
* the left edge of where the layout should be placed.
* @param y
* the top edge of where the layout should be placed.
* @param inputData
* information about how to reach inputs.
* @param output
* a point to which the output should be connected.
*/
private static void placeComponents(CircuitMutation result, Layout layout, int x, int y, InputData inputData, Location output) {
if (layout.inputName != null) {
int inputX = inputData.getSpineX(layout.inputName);
Location input = Location.create(inputX, output.getY());
inputData.registerConnection(layout.inputName, input);
result.add(Wire.create(input, output));
return;
}
Location compOutput = Location.create(x + layout.width, output.getY());
Component parent = layout.factory.createComponent(compOutput, layout.attrs);
result.add(parent);
if (!compOutput.equals(output)) {
result.add(Wire.create(compOutput, output));
}
// handle a NOT gate pattern implemented with NAND as a special case
if (layout.factory == NandGate.FACTORY && layout.subLayouts.length == 1 && layout.subLayouts[0].inputName == null) {
Layout sub = layout.subLayouts[0];
Location input0 = parent.getEnd(1).getLocation();
Location input1 = parent.getEnd(2).getLocation();
int midX = input0.getX() - 20;
Location subOutput = Location.create(midX, output.getY());
Location midInput0 = Location.create(midX, input0.getY());
Location midInput1 = Location.create(midX, input1.getY());
result.add(Wire.create(subOutput, midInput0));
result.add(Wire.create(midInput0, input0));
result.add(Wire.create(subOutput, midInput1));
result.add(Wire.create(midInput1, input1));
int subX = x + layout.subX - sub.width;
placeComponents(result, sub, subX, y + sub.y, inputData, subOutput);
return;
}
if (layout.subLayouts.length == parent.getEnds().size() - 2) {
int index = layout.subLayouts.length / 2 + 1;
Object factory = parent.getFactory();
if (factory instanceof AbstractGate) {
Value val = ((AbstractGate) factory).getIdentity();
Integer valInt = Integer.valueOf(val.toIntValue());
Location loc = parent.getEnd(index).getLocation();
AttributeSet attrs = Constant.FACTORY.createAttributeSet();
attrs.setValue(Constant.ATTR_VALUE, valInt);
result.add(Constant.FACTORY.createComponent(loc, attrs));
}
}
for (int i = 0; i < layout.subLayouts.length; i++) {
Layout sub = layout.subLayouts[i];
int inputIndex = i + 1;
Location subDest = parent.getEnd(inputIndex).getLocation();
int subOutputY = y + sub.y + sub.outputY;
if (sub.inputName != null) {
int destY = subDest.getY();
if (i == 0 && destY < subOutputY || i == layout.subLayouts.length - 1 && destY > subOutputY) {
subOutputY = destY;
}
}
Location subOutput;
int numSubs = layout.subLayouts.length;
if (subOutputY == subDest.getY()) {
subOutput = subDest;
} else {
int back;
if (i < numSubs / 2) {
if (subOutputY < subDest.getY()) {
// bending upward
back = i;
} else {
back = ((numSubs - 1) / 2) - i;
}
} else {
if (subOutputY > subDest.getY()) {
// bending downward
back = numSubs - 1 - i;
} else {
back = i - (numSubs / 2);
}
}
int subOutputX = subDest.getX() - 20 - 10 * back;
subOutput = Location.create(subOutputX, subOutputY);
Location mid = Location.create(subOutputX, subDest.getY());
result.add(Wire.create(subOutput, mid));
result.add(Wire.create(mid, subDest));
}
int subX = x + layout.subX - sub.width;
int subY = y + sub.y;
placeComponents(result, sub, subX, subY, inputData, subOutput);
}
}
Aggregations