Search in sources :

Example 26 with ComponentFactory

use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.

the class CircuitBuilder method placeOutput.

// 
// placeOutput
// 
private static void placeOutput(CircuitMutation result, Location loc, String name) {
    ComponentFactory factory = Pin.FACTORY;
    AttributeSet attrs = factory.createAttributeSet();
    attrs.setValue(StdAttr.FACING, Direction.WEST);
    attrs.setValue(Pin.ATTR_TYPE, Boolean.TRUE);
    attrs.setValue(StdAttr.LABEL, name);
    attrs.setValue(Pin.ATTR_LABEL_LOC, Direction.NORTH);
    result.add(factory.createComponent(loc, attrs));
}
Also used : AttributeSet(com.cburch.logisim.data.AttributeSet) ComponentFactory(com.cburch.logisim.comp.ComponentFactory)

Example 27 with ComponentFactory

use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.

the class CircuitBuilder method layoutGatesSub.

private static Layout layoutGatesSub(CircuitDetermination det) {
    if (det instanceof CircuitDetermination.Input) {
        CircuitDetermination.Input input = (CircuitDetermination.Input) det;
        return new Layout(input.getName());
    } else if (det instanceof CircuitDetermination.Value) {
        CircuitDetermination.Value value = (CircuitDetermination.Value) det;
        ComponentFactory factory = Constant.FACTORY;
        AttributeSet attrs = factory.createAttributeSet();
        attrs.setValue(Constant.ATTR_VALUE, Integer.valueOf(value.getValue()));
        Bounds bds = factory.getOffsetBounds(attrs);
        return new Layout(bds.getWidth(), bds.getHeight(), -bds.getY(), factory, attrs, new Layout[0], 0);
    }
    // We know det is a Gate. Determine sublayouts.
    CircuitDetermination.Gate gate = (CircuitDetermination.Gate) det;
    ComponentFactory factory = gate.getFactory();
    ArrayList<CircuitDetermination> inputs = gate.getInputs();
    // Handle a NOT implemented with a NAND as a special case
    if (gate.isNandNot()) {
        CircuitDetermination subDet = inputs.get(0);
        if (!(subDet instanceof CircuitDetermination.Input)) {
            Layout[] sub = new Layout[1];
            sub[0] = layoutGatesSub(subDet);
            sub[0].y = 0;
            AttributeSet attrs = factory.createAttributeSet();
            attrs.setValue(GateAttributes.ATTR_SIZE, GateAttributes.SIZE_NARROW);
            attrs.setValue(GateAttributes.ATTR_INPUTS, Integer.valueOf(2));
            // determine layout's width
            Bounds bds = factory.getOffsetBounds(attrs);
            int betweenWidth = 40;
            if (sub[0].width == 0)
                betweenWidth = 0;
            int width = sub[0].width + betweenWidth + bds.getWidth();
            // determine outputY and layout's height.
            int outputY = sub[0].y + sub[0].outputY;
            int height = sub[0].height;
            int minOutputY = roundUp(-bds.getY());
            if (minOutputY > outputY) {
                // we have to shift everything down because otherwise
                // the component will peek over the rectangle's top.
                int dy = minOutputY - outputY;
                sub[0].y += dy;
                height += dy;
                outputY += dy;
            }
            int minHeight = outputY + bds.getY() + bds.getHeight();
            if (minHeight > height)
                height = minHeight;
            // ok; create and return the layout.
            return new Layout(width, height, outputY, factory, attrs, sub, sub[0].width);
        }
    }
    Layout[] sub = new Layout[inputs.size()];
    // maximum width of sublayouts
    int subWidth = 0;
    // total height of sublayouts
    int subHeight = 0;
    for (int i = 0; i < sub.length; i++) {
        sub[i] = layoutGatesSub(inputs.get(i));
        if (sub.length % 2 == 0 && i == (sub.length + 1) / 2 && sub[i - 1].height + sub[i].height == 0) {
            // if there are an even number of inputs, then there is a
            // 20-tall gap between the middle two inputs. Ensure the two
            // middle inputs are at least 20 pixels apart.
            subHeight += 10;
        }
        sub[i].y = subHeight;
        subWidth = Math.max(subWidth, sub[i].width);
        subHeight += sub[i].height + 10;
    }
    subHeight -= 10;
    AttributeSet attrs = factory.createAttributeSet();
    if (factory == NotGate.FACTORY) {
        attrs.setValue(NotGate.ATTR_SIZE, NotGate.SIZE_NARROW);
    } else {
        attrs.setValue(GateAttributes.ATTR_SIZE, GateAttributes.SIZE_NARROW);
        int ins = sub.length;
        attrs.setValue(GateAttributes.ATTR_INPUTS, Integer.valueOf(ins));
    }
    // determine layout's width
    Bounds bds = factory.getOffsetBounds(attrs);
    int betweenWidth = 40 + 10 * (sub.length / 2 - 1);
    if (sub.length == 1)
        betweenWidth = 20;
    if (subWidth == 0)
        betweenWidth = 0;
    int width = subWidth + betweenWidth + bds.getWidth();
    // determine outputY and layout's height.
    int outputY;
    if (sub.length % 2 == 1) {
        // odd number - match the middle input
        int i = (sub.length - 1) / 2;
        outputY = sub[i].y + sub[i].outputY;
    } else {
        // even number - halfway between middle two inputs
        int i0 = (sub.length / 2) - 1;
        int i1 = (sub.length / 2);
        int o0 = sub[i0].y + sub[i0].outputY;
        int o1 = sub[i1].y + sub[i1].outputY;
        outputY = roundDown((o0 + o1) / 2);
    }
    int height = subHeight;
    int minOutputY = roundUp(-bds.getY());
    if (minOutputY > outputY) {
        // we have to shift everything down because otherwise
        // the component will peek over the rectangle's top.
        int dy = minOutputY - outputY;
        for (int i = 0; i < sub.length; i++) sub[i].y += dy;
        height += dy;
        outputY += dy;
    }
    int minHeight = outputY + bds.getY() + bds.getHeight();
    if (minHeight > height)
        height = minHeight;
    // ok; create and return the layout.
    return new Layout(width, height, outputY, factory, attrs, sub, subWidth);
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Bounds(com.cburch.logisim.data.Bounds) AttributeSet(com.cburch.logisim.data.AttributeSet) Value(com.cburch.logisim.data.Value)

Example 28 with ComponentFactory

use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.

the class Netlist method WarningTraceForGatedClock.

private void WarningTraceForGatedClock(ConnectionPoint Source, int index, ArrayList<Netlist> HierarchyNetlists, ArrayList<String> HierarchyNames, FPGAReport Reporter) {
    Component comp = Source.GetComp();
    if (comp.getFactory() instanceof Pin) {
        if (HierarchyNames.isEmpty())
            /* we cannot go up at toplevel, so leave */
            return;
        int idx = -1;
        for (int i = 0; i < MyInputPorts.size(); i++) {
            if (MyInputPorts.get(i).GetComponent().equals(comp))
                idx = i;
        }
        if (idx < 0) {
            Reporter.AddFatalError("BUG: Could not find port!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
            return;
        }
        ConnectionPoint SubNet = HierarchyNetlists.get(HierarchyNetlists.size() - 2).GetNetlistConnectionForSubCircuitInput(HierarchyNames.get(HierarchyNames.size() - 1), idx, (byte) index);
        if (SubNet == null) {
            Reporter.AddFatalError("BUG: Could not find a sub-circuit connection in overlying hierarchy level!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
            return;
        }
        if (SubNet.GetParrentNet() != null) {
            ArrayList<String> NewHierarchyNames = new ArrayList<String>();
            ArrayList<Netlist> NewHierarchyNetlists = new ArrayList<Netlist>();
            NewHierarchyNames.addAll(HierarchyNames);
            NewHierarchyNames.remove(NewHierarchyNames.size() - 1);
            NewHierarchyNetlists.addAll(HierarchyNetlists);
            NewHierarchyNetlists.remove(NewHierarchyNetlists.size() - 1);
            Netlist SubNetList = HierarchyNetlists.get(HierarchyNetlists.size() - 2);
            Net NewNet = SubNet.GetParrentNet();
            Byte NewNetIndex = SubNet.GetParrentNetBitIndex();
            Set<Wire> Segments = new HashSet<Wire>();
            SourceInfo source = SubNetList.GetHiddenSource(NewNet, NewNetIndex, SubNetList.MyComplexSplitters, null, new HashSet<String>(), Segments, Reporter);
            if (source == null) {
                Reporter.AddFatalError("BUG: Unable to find source in sub-circuit!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
                return;
            }
            ComponentFactory sfac = source.getSource().GetComp().getFactory();
            if (sfac instanceof Pin || sfac instanceof SubcircuitFactory) {
                SimpleDRCContainer warn = new SimpleDRCContainer(SubNetList.getCircuit(), Strings.get("NetList_GatedClockInt"), SimpleDRCContainer.LEVEL_NORMAL, SimpleDRCContainer.MARK_WIRE, true);
                warn.AddMarkComponents(Segments);
                Reporter.AddWarning(warn);
                SubNetList.WarningTraceForGatedClock(source.getSource(), source.getIndex(), NewHierarchyNetlists, NewHierarchyNames, Reporter);
            } else {
                SimpleDRCContainer warn = new SimpleDRCContainer(SubNetList.getCircuit(), Strings.get("NetList_GatedClockSource"), SimpleDRCContainer.LEVEL_NORMAL, SimpleDRCContainer.MARK_WIRE, true);
                warn.AddMarkComponents(Segments);
                Reporter.AddWarning(warn);
            }
        }
    }
    if (comp.getFactory() instanceof SubcircuitFactory) {
        /* TODO */
        SubcircuitFactory sub = (SubcircuitFactory) comp.getFactory();
        if (Source.getChildsPortIndex() < 0) {
            Reporter.AddFatalError("BUG: Subcircuit port is not annotated!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
            return;
        }
        NetlistComponent OutputPort = sub.getSubcircuit().getNetList().GetOutputPin(Source.getChildsPortIndex());
        if (OutputPort == null) {
            Reporter.AddFatalError("BUG: Unable to find Subcircuit output port!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
            return;
        }
        Net ConnectedNet = Source.GetParrentNet();
        /* Find the correct subcircuit */
        NetlistComponent SubCirc = null;
        for (NetlistComponent subc : MySubCircuits) {
            if (subc.GetComponent().equals(Source.GetComp()))
                SubCirc = subc;
        }
        if (SubCirc == null) {
            Reporter.AddFatalError("BUG: Unable to find Subcircuit!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
            return;
        }
        byte BitIndex = SubCirc.GetConnectionBitIndex(ConnectedNet, (byte) index);
        if (BitIndex < 0) {
            Reporter.AddFatalError("BUG: Unable to find the bit index of a Subcircuit output port!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
            return;
        }
        ConnectionPoint SubNet = OutputPort.getEnd(0).GetConnection(BitIndex);
        if (SubNet.GetParrentNet() != null) {
            /* we have a connected pin */
            Netlist SubNetList = sub.getSubcircuit().getNetList();
            ArrayList<String> NewHierarchyNames = new ArrayList<String>();
            ArrayList<Netlist> NewHierarchyNetlists = new ArrayList<Netlist>();
            NewHierarchyNames.addAll(HierarchyNames);
            NewHierarchyNames.add(CorrectLabel.getCorrectLabel(SubCirc.GetComponent().getAttributeSet().getValue(StdAttr.LABEL)));
            NewHierarchyNetlists.addAll(HierarchyNetlists);
            NewHierarchyNetlists.add(SubNetList);
            Net NewNet = SubNet.GetParrentNet();
            Byte NewNetIndex = SubNet.GetParrentNetBitIndex();
            Set<Wire> Segments = new HashSet<Wire>();
            SourceInfo source = SubNetList.GetHiddenSource(NewNet, NewNetIndex, SubNetList.MyComplexSplitters, null, new HashSet<String>(), Segments, Reporter);
            if (source == null) {
                Reporter.AddFatalError("BUG: Unable to find source in sub-circuit!\n ==> " + this.getClass().getName().replaceAll("\\.", "/") + ":" + Thread.currentThread().getStackTrace()[2].getLineNumber() + "\n");
                return;
            }
            ComponentFactory sfac = source.getSource().GetComp().getFactory();
            if (sfac instanceof Pin || sfac instanceof SubcircuitFactory) {
                SimpleDRCContainer warn = new SimpleDRCContainer(SubNetList.getCircuit(), Strings.get("NetList_GatedClockInt"), SimpleDRCContainer.LEVEL_NORMAL, SimpleDRCContainer.MARK_WIRE, true);
                warn.AddMarkComponents(Segments);
                Reporter.AddWarning(warn);
                SubNetList.WarningTraceForGatedClock(source.getSource(), source.getIndex(), NewHierarchyNetlists, NewHierarchyNames, Reporter);
            } else {
                SimpleDRCContainer warn = new SimpleDRCContainer(SubNetList.getCircuit(), Strings.get("NetList_GatedClockSource"), SimpleDRCContainer.LEVEL_NORMAL, SimpleDRCContainer.MARK_WIRE, true);
                warn.AddMarkComponents(Segments);
                Reporter.AddWarning(warn);
            }
        }
    }
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory) ArrayList(java.util.ArrayList) Wire(com.cburch.logisim.circuit.Wire) Pin(com.cburch.logisim.std.wiring.Pin) SubcircuitFactory(com.cburch.logisim.circuit.SubcircuitFactory) InstanceComponent(com.cburch.logisim.instance.InstanceComponent) Component(com.cburch.logisim.comp.Component) HashSet(java.util.HashSet)

Example 29 with ComponentFactory

use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.

the class XmlWriter method fromComponent.

Element fromComponent(Component comp) {
    ComponentFactory source = comp.getFactory();
    Library lib = findLibrary(source);
    String lib_name;
    if (lib == null) {
        loader.showError(source.getName() + " component not found");
        return null;
    } else if (lib == file) {
        lib_name = null;
    } else {
        lib_name = libs.get(lib);
        if (lib_name == null) {
            loader.showError("unknown library within file");
            return null;
        }
    }
    Element ret = doc.createElement("comp");
    if (lib_name != null)
        ret.setAttribute("lib", lib_name);
    ret.setAttribute("name", source.getName());
    ret.setAttribute("loc", comp.getLocation().toString());
    addAttributeSetContent(ret, comp.getAttributeSet(), comp.getFactory());
    return ret;
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Element(org.w3c.dom.Element) Library(com.cburch.logisim.tools.Library)

Example 30 with ComponentFactory

use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.

the class MouseMappings method replaceAll.

// 
// package-protected methods
// 
void replaceAll(Map<Tool, Tool> toolMap) {
    boolean changed = false;
    for (Map.Entry<Integer, Tool> entry : map.entrySet()) {
        Integer key = entry.getKey();
        Tool tool = entry.getValue();
        if (tool instanceof AddTool) {
            ComponentFactory factory = ((AddTool) tool).getFactory();
            if (toolMap.containsKey(factory)) {
                changed = true;
                Tool newTool = toolMap.get(factory);
                if (newTool == null) {
                    map.remove(key);
                } else {
                    Tool clone = newTool.cloneTool();
                    LoadedLibrary.copyAttributes(clone.getAttributeSet(), tool.getAttributeSet());
                    map.put(key, clone);
                }
            }
        } else {
            if (toolMap.containsKey(tool)) {
                changed = true;
                Tool newTool = toolMap.get(tool);
                if (newTool == null) {
                    map.remove(key);
                } else {
                    Tool clone = newTool.cloneTool();
                    LoadedLibrary.copyAttributes(clone.getAttributeSet(), tool.getAttributeSet());
                    map.put(key, clone);
                }
            }
        }
    }
    if (changed)
        fireMouseMappingsChanged();
}
Also used : ComponentFactory(com.cburch.logisim.comp.ComponentFactory) AddTool(com.cburch.logisim.tools.AddTool) Map(java.util.Map) HashMap(java.util.HashMap) SelectTool(com.cburch.logisim.tools.SelectTool) Tool(com.cburch.logisim.tools.Tool) AddTool(com.cburch.logisim.tools.AddTool)

Aggregations

ComponentFactory (com.cburch.logisim.comp.ComponentFactory)37 AttributeSet (com.cburch.logisim.data.AttributeSet)16 AddTool (com.cburch.logisim.tools.AddTool)9 Tool (com.cburch.logisim.tools.Tool)9 Circuit (com.cburch.logisim.circuit.Circuit)8 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)8 Component (com.cburch.logisim.comp.Component)8 Library (com.cburch.logisim.tools.Library)8 ArrayList (java.util.ArrayList)6 Wire (com.cburch.logisim.circuit.Wire)4 Location (com.cburch.logisim.data.Location)4 ProjectExplorerToolNode (com.cburch.logisim.gui.generic.ProjectExplorerToolNode)4 HashSet (java.util.HashSet)4 Attribute (com.cburch.logisim.data.Attribute)3 Bounds (com.cburch.logisim.data.Bounds)3 ToolAttributeAction (com.cburch.logisim.gui.main.ToolAttributeAction)3 Action (com.cburch.logisim.proj.Action)3 HashMap (java.util.HashMap)3 CircuitMutation (com.cburch.logisim.circuit.CircuitMutation)2 Direction (com.cburch.logisim.data.Direction)2