public static Circuit ReadXML(string xml_string) { Circuit circuit = null; bool in_ic = false; string ic_name = null; string ic_id = null; string ic_type = null; List <Gate> circuit_gate_list = new List <Gate>(); bool parsed_ok = true; Dictionary <string, Gate> circuit_gate_map = new Dictionary <string, Gate>(); Dictionary <string, IC> circuit_ic_map = new Dictionary <string, IC>(); List <Gate> ic_gate_list = null; List <Port> ic_port_list = null; Dictionary <string, Gate> ic_gate_map = null; Dictionary <string, Port> ic_port_map = null; using (XmlReader reader = XmlReader.Create(new StringReader(xml_string))) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name.Equals("IC", StringComparison.OrdinalIgnoreCase)) { in_ic = true; ic_gate_list = new List <Gate>(); ic_port_list = new List <Port>(); ic_gate_map = new Dictionary <string, Gate>(); ic_port_map = new Dictionary <string, Port>(); if (reader.MoveToFirstAttribute()) { string attrib_name = reader.Name; if (attrib_name.Equals("NAME", StringComparison.OrdinalIgnoreCase)) { ic_name = reader.Value; } else if (attrib_name.Equals("ID", StringComparison.OrdinalIgnoreCase)) { ic_id = reader.Value; } else if (attrib_name.Equals("TYPE", StringComparison.OrdinalIgnoreCase)) { ic_type = reader.Value; } } } else if (reader.Name.Equals("GATE", StringComparison.OrdinalIgnoreCase)) { string gate_name = null; string gate_type = null; string gate_id = null; if (reader.MoveToFirstAttribute()) { do { string attrib_name = reader.Name; if (attrib_name.Equals("NAME", StringComparison.OrdinalIgnoreCase)) { gate_name = reader.Value; } else if (attrib_name.Equals("TYPE", StringComparison.OrdinalIgnoreCase)) { gate_type = reader.Value; } else if (attrib_name.Equals("ID", StringComparison.OrdinalIgnoreCase)) { gate_id = reader.Value; } }while (reader.MoveToNextAttribute()); if (gate_id == null) { System.Console.WriteLine(string.Format("Gate Type {0} at {1} has no ID", gate_type, ((IXmlLineInfo)reader).LineNumber)); } else { Gate gate = null; if (circuit_ic_map.ContainsKey(gate_type)) { gate = circuit_ic_map[gate_type].Clone(); } else { gate = GenerateGate(gate_type); } if (gate == null) { System.Console.WriteLine(string.Format("Invalid Gate Type {0} at {1}", gate_type, ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } else { if (gate_name != null) { gate.SetName(gate_name); } if (in_ic) { ic_gate_map.Add(gate_id, gate); } else { circuit_gate_map.Add(gate_id, gate); } if (in_ic) { ic_gate_list.Add(gate); } else { circuit_gate_list.Add(gate); } } } } } else if (reader.Name.Equals("PORT", StringComparison.OrdinalIgnoreCase)) { string port_name = null; string port_direction = null; string port_id = null; if (reader.MoveToFirstAttribute()) { do { string attrib_name = reader.Name; if (attrib_name.Equals("NAME", StringComparison.OrdinalIgnoreCase)) { port_name = reader.Value; } else if (attrib_name.Equals("TYPE", StringComparison.OrdinalIgnoreCase)) { port_direction = reader.Value; } else if (attrib_name.Equals("ID", StringComparison.OrdinalIgnoreCase)) { port_id = reader.Value; } }while (reader.MoveToNextAttribute()); if (port_id == null) { System.Console.WriteLine(string.Format("Port Type {0} at {1} has no ID", port_direction, ((IXmlLineInfo)reader).LineNumber)); } else { bool is_port_input = false; if (ParsePortDirection(port_direction, out is_port_input)) { Port port = new Port(is_port_input); if (port_name != null) { port.SetName(port_name); } if (in_ic) { ic_port_map.Add(port_id, port); ic_port_list.Add(port); } else { System.Console.WriteLine(string.Format("Port Type {0} at {1} is not in IC element", port_direction, ((IXmlLineInfo)reader).LineNumber)); } } } } } else if (reader.Name.Equals("WIRE", StringComparison.OrdinalIgnoreCase)) { string from_string = null; string to_string = null; if (reader.MoveToFirstAttribute()) { do { string attrib_name = reader.Name; if (attrib_name.Equals("FROM", StringComparison.OrdinalIgnoreCase)) { from_string = reader.Value; } else if (attrib_name.Equals("TO", StringComparison.OrdinalIgnoreCase)) { to_string = reader.Value; } }while (reader.MoveToNextAttribute()); if (from_string == null || to_string == null) { if (from_string == null) { System.Console.WriteLine(string.Format("WIRE at {0} has no FROM", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } if (to_string == null) { System.Console.WriteLine(string.Format("WIRE at {0} has no TO", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } } else { bool from_is_port = false; string from_id = null; bool port_on_from_gate_is_input = false; int port_on_from_gate_index = -1; string port_on_from_gate_id = null; bool to_is_port = false; string to_id = null; bool port_on_to_gate_is_input = false; int port_on_to_gate_index = -1; string port_on_to_gate_id = null; bool from_parsed = ParseWireConnection(from_string, out from_is_port, out from_id, out port_on_from_gate_is_input, out port_on_from_gate_index, out port_on_from_gate_id); bool to_parsed = ParseWireConnection(to_string, out to_is_port, out to_id, out port_on_to_gate_is_input, out port_on_to_gate_index, out port_on_to_gate_id); if (!from_parsed || !to_parsed) { if (!from_parsed) { System.Console.WriteLine(string.Format("WIRE at {0} has invalid FROM", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } if (!to_parsed) { System.Console.WriteLine(string.Format("WIRE at {0} has invalid TO", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } } else { Port from_port = null; Port to_port = null; if (from_is_port) { if (in_ic) { from_port = ic_port_map[from_id]; } else { System.Console.WriteLine(string.Format("WIRE at {0} has invalid FROM", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } } else { Gate from_gate = null; if (in_ic) { from_gate = ic_gate_map[from_id]; } else { from_gate = circuit_gate_map[from_id]; } if (from_gate != null) { if (port_on_from_gate_index < 0) { from_port = from_gate.FindPortById(port_on_from_gate_id); } else { if (port_on_from_gate_is_input) { from_port = from_gate.GetInputPort(port_on_from_gate_index); } else { from_port = from_gate.GetOutputPort(port_on_from_gate_index); } } } } if (to_is_port) { if (in_ic) { to_port = ic_port_map[to_id]; } else { System.Console.WriteLine(string.Format("WIRE at {0} has invalid TO", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } } else { Gate to_gate = null; if (in_ic) { to_gate = ic_gate_map[to_id]; } else { to_gate = circuit_gate_map[to_id]; } if (to_gate != null) { if (port_on_to_gate_index < 0) { to_port = to_gate.FindPortById(port_on_to_gate_id); } else { if (port_on_to_gate_is_input) { to_port = to_gate.GetInputPort(port_on_to_gate_index); } else { to_port = to_gate.GetOutputPort(port_on_to_gate_index); } } } } if (from_port == null || to_port == null) { if (from_port == null) { System.Console.WriteLine(string.Format("WIRE at {0} has invalid FROM Port", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } if (to_port == null) { System.Console.WriteLine(string.Format("WIRE at {0} has invalid TO Port", ((IXmlLineInfo)reader).LineNumber)); parsed_ok = false; } } else { from_port.LinkTo(to_port); } } } } } break; case XmlNodeType.EndElement: if (reader.Name.Equals("IC", StringComparison.OrdinalIgnoreCase)) { in_ic = false; IC ic = new IC(ic_gate_list, ic_port_list, ic_port_map); if (ic_name != null) { ic.SetName(ic_name); } circuit_ic_map.Add(ic_id, ic); } break; } } } if (parsed_ok) { circuit = new Circuit(circuit_gate_list, circuit_gate_map, circuit_ic_map); } return(circuit); }
static void Main(string[] args) { #if false Gate output1 = new OutputOnlyGate(); Gate output2 = new OutputOnlyGate(); // Gate logic_gate = new AndGate(); Gate logic_gate = new OrGate(); // Gate logic_gate = new XorGate(); Gate input1 = new InputOnlyGate(); List <Gate> gate_list = new List <Gate>(); gate_list.Add(output1); gate_list.Add(output2); gate_list.Add(logic_gate); gate_list.Add(input1); output1.GetOutputPort(0).LinkTo(logic_gate.GetInputPort(0)); output2.GetOutputPort(0).LinkTo(logic_gate.GetInputPort(1)); logic_gate.GetOutputPort(0).LinkTo(input1.GetInputPort(0)); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); #endif #if false Gate output3 = new OutputOnlyGate(); Gate not_gate = new NotGate(); Gate input2 = new InputOnlyGate(); List <Gate> gate_list = new List <Gate>(); gate_list.Add(output3); gate_list.Add(not_gate); gate_list.Add(input2); output3.GetOutputPort(0).LinkTo(not_gate.GetInputPort(0)); not_gate.GetOutputPort(0).LinkTo(input2.GetInputPort(0)); output3.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input2.GetInputPort(0).GetState()); output3.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input2.GetInputPort(0).GetState()); #endif #if false Gate clock = new Clock(); Gate input = new InputOnlyGate(); List <Gate> gate_list = new List <Gate>(); gate_list.Add(clock); gate_list.Add(input); clock.GetOutputPort(0).LinkTo(input.GetInputPort(0)); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input.GetInputPort(0).GetState()); #endif #if false // Gate lg1 = new AndGate(); // Gate lg2 = new AndGate(); // Gate lg1 = new OrGate(); // Gate lg2 = new OrGate(); // Gate lg1 = new AndGate(); // Gate lg2 = new OrGate(); Gate lg1 = new OrGate(); Gate lg2 = new AndGate(); lg1.GetOutputPort(0).LinkTo(lg2.GetInputPort(0)); Port icport1 = new Port(true); Port icport2 = new Port(true); Port icport3 = new Port(true); Port icport4 = new Port(false); icport1.LinkTo(lg1.GetInputPort(0)); icport2.LinkTo(lg1.GetInputPort(1)); icport3.LinkTo(lg2.GetInputPort(1)); lg2.GetOutputPort(0).LinkTo(icport4); List <Gate> ic_gate_list = new List <Gate>(); ic_gate_list.Add(lg1); ic_gate_list.Add(lg2); List <Port> ic_port_list = new List <Port>(); ic_port_list.Add(icport1); ic_port_list.Add(icport2); ic_port_list.Add(icport3); ic_port_list.Add(icport4); IC ic1 = new IC(ic_gate_list, ic_port_list); Gate output1 = new OutputOnlyGate(); Gate output2 = new OutputOnlyGate(); Gate output3 = new OutputOnlyGate(); Gate input1 = new InputOnlyGate(); output1.GetOutputPort(0).LinkTo(ic1.GetInputPort(0)); output2.GetOutputPort(0).LinkTo(ic1.GetInputPort(1)); output3.GetOutputPort(0).LinkTo(ic1.GetInputPort(2)); ic1.GetOutputPort(0).LinkTo(input1.GetInputPort(0)); List <Gate> gate_list = new List <Gate>(); gate_list.Add(ic1); gate_list.Add(output1); gate_list.Add(output2); gate_list.Add(output3); gate_list.Add(input1); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(false); output3.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(false); output3.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(true); output3.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(true); output3.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(false); output3.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(false); output3.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(true); output3.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(true); output3.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState()); #endif #if false // string cwd = Directory.GetCurrentDirectory(); // System.Console.WriteLine(cwd); string text = System.IO.File.ReadAllText(@"..\..\..\ic\half_adder.xml"); IC ic2 = ICFactory.ReadXML(text); IC ic1 = ic2.Clone() as IC; Gate output1 = new OutputOnlyGate(); Gate output2 = new OutputOnlyGate(); Gate input1 = new InputOnlyGate(); Gate input2 = new InputOnlyGate(); output1.GetOutputPort(0).LinkTo(ic1.GetInputPort(0)); output2.GetOutputPort(0).LinkTo(ic1.GetInputPort(1)); ic1.GetOutputPort(0).LinkTo(input1.GetInputPort(0)); ic1.GetOutputPort(1).LinkTo(input2.GetInputPort(0)); List <Gate> gate_list = new List <Gate>(); gate_list.Add(ic1); gate_list.Add(output1); gate_list.Add(output2); gate_list.Add(input1); gate_list.Add(input2); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); #endif #if false // string cwd = Directory.GetCurrentDirectory(); // System.Console.WriteLine(cwd); // string text = System.IO.File.ReadAllText(@"..\..\..\ic\circuit2.xml"); string text = System.IO.File.ReadAllText(@"..\..\..\ic\circuit3.xml"); Circuit circuit1 = Circuit.ReadXML(text); List <Gate> gate_list = circuit1.GetGateList(); Gate output1 = circuit1.FindGateByID("out1"); Gate output2 = circuit1.FindGateByID("out2"); Gate input1 = circuit1.FindGateByID("in1"); Gate input2 = circuit1.FindGateByID("in2"); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(false); output2.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); output1.GetOutputPort(0).SetState(true); output2.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(input1.GetInputPort(0).GetState() + "," + input2.GetInputPort(0).GetState()); #endif #if true // string cwd = Directory.GetCurrentDirectory(); // System.Console.WriteLine(cwd); // string text = System.IO.File.ReadAllText(@"..\..\..\ic\circuit2.xml"); string text = System.IO.File.ReadAllText(@"..\..\..\ic\circuit4.xml"); Circuit circuit1 = Circuit.ReadXML(text); List <Gate> gate_list = circuit1.GetGateList(); Gate A = circuit1.FindGateByID("A"); Gate B = circuit1.FindGateByID("B"); Gate Cin = circuit1.FindGateByID("Cin"); Gate S = circuit1.FindGateByID("S"); Gate Cout = circuit1.FindGateByID("Cout"); A.GetOutputPort(0).SetState(false); B.GetOutputPort(0).SetState(false); Cin.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(false); B.GetOutputPort(0).SetState(false); Cin.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(false); B.GetOutputPort(0).SetState(true); Cin.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(false); B.GetOutputPort(0).SetState(true); Cin.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(true); B.GetOutputPort(0).SetState(false); Cin.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(true); B.GetOutputPort(0).SetState(false); Cin.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(true); B.GetOutputPort(0).SetState(true); Cin.GetOutputPort(0).SetState(false); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); A.GetOutputPort(0).SetState(true); B.GetOutputPort(0).SetState(true); Cin.GetOutputPort(0).SetState(true); LogicSimulator.RunTick(gate_list); System.Console.WriteLine(S.GetInputPort(0).GetState() + "," + Cout.GetInputPort(0).GetState()); #endif }