/// <summary> /// Given an XML circuit representation, create an IC. /// The local IC List will be used to deference any nested ICs. /// </summary> /// <param name="circuit"></param> /// <returns></returns> public UIGates.IC LoadCircuit(XElement circuit) { Gates.Circuit c = new Gates.Circuit(); Dictionary <int, Gates.AbstractGate> gid = new Dictionary <int, Gates.AbstractGate>(); Dictionary <Gates.AbstractGate, GateLocation> gpt = new Dictionary <Gates.AbstractGate, GateLocation>(); foreach (XElement gate in circuit.Element("Gates").Elements()) { Gates.AbstractGate abgate = CreateGate(gate); c.Add(abgate); gid.Add(int.Parse(gate.Attribute("ID").Value), abgate); double x = double.Parse(gate.Element("Point").Attribute("X").Value); double y = double.Parse(gate.Element("Point").Attribute("Y").Value); double angle = double.Parse(gate.Element("Point").Attribute("Angle").Value); gpt.Add(abgate, new GateLocation() { X = x, Y = y, Angle = angle }); } foreach (XElement wire in circuit.Element("Wires").Elements()) { c[CreateTerminal(gid, wire.Element("To"))] = CreateTerminal(gid, wire.Element("From")); } ICBuilder icb = new ICBuilder(c, (Gates.AbstractGate abgate) => { return(gpt[abgate]); }); // for top level circuit, must not create terminals // otherwise input/output overridden if (circuit.Attribute("Name") != null) { string cname = circuit.Attribute("Name").Value; // check first if we need to rename if (UpdateICNames.ContainsKey(cname)) { cname = UpdateICNames[cname]; } return(icb.CreateIC(cname)); } else { return(icb.CreateNonTerminaledIC("")); } }
private Gates.AbstractGate CreateGate(XElement gate) { int numInputs = 2; // default for variable input gates if (gate.Attribute("NumInputs") != null) { numInputs = int.Parse(gate.Attribute("NumInputs").Value); } switch (gate.Attribute("Type").Value) { case "And": return(new Gates.BasicGates.And(numInputs)); case "Not": return(new Gates.BasicGates.Not()); case "Or": return(new Gates.BasicGates.Or(numInputs)); case "Nand": return(new Gates.BasicGates.Nand(numInputs)); case "Nor": return(new Gates.BasicGates.Nor(numInputs)); case "Xor": return(new Gates.BasicGates.Xor()); case "Xnor": return(new Gates.BasicGates.Xnor()); case "Buffer": return(new Gates.BasicGates.Buffer()); case "UserInput": Gates.IOGates.UserInput ui = new Gates.IOGates.UserInput(); ui.SetName(gate.Attribute("Name").Value); return(ui); case "UserOutput": Gates.IOGates.UserOutput uo = new Gates.IOGates.UserOutput(); uo.SetName(gate.Attribute("Name").Value); return(uo); case "NumericInput": Gates.IOGates.NumericInput ni = new Gates.IOGates.NumericInput(int.Parse(gate.Attribute("Bits").Value)); ni.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation) int.Parse(gate.Attribute("SelRep").Value); ni.Value = gate.Attribute("Value").Value; return(ni); case "NumericOutput": Gates.IOGates.NumericOutput no = new Gates.IOGates.NumericOutput(int.Parse(gate.Attribute("Bits").Value)); no.SelectedRepresentation = (Gates.IOGates.AbstractNumeric.Representation) int.Parse(gate.Attribute("SelRep").Value); return(no); case "Clock": Gates.IOGates.Clock clk = new Gates.IOGates.Clock(int.Parse(gate.Attribute("Milliseconds").Value)); return(clk); case "IC": // check if this ic has been renamed string cname = gate.Attribute("Name").Value; // check first if we need to rename if (UpdateICNames.ContainsKey(cname)) { cname = UpdateICNames[cname]; } return(icl.GetIC(cname).AbGate.Clone()); case "Comment": Gates.IOGates.Comment cmt = new Gates.IOGates.Comment(); cmt.Value = gate.Element("Comment").Value; return(cmt); } throw new ArgumentException("unknown gate"); }