public void SetPins()
        {
            foreach (TCLRoutingTreeNode node in RoutingTree.GetAllRoutingNodes().Where(n => !n.VirtualNode))
            {
                Tile t = node.Tile;
                foreach (Slice s in t.Slices)
                {
                    bool inport  = s.PortMapping.IsSliceInPort(node.Port);
                    bool outport = s.PortMapping.IsSliceOutPort(node.Port);
                    if ((inport || outport) && node.Port.Name.Contains('_'))
                    {
                        NetPin pin = null;
                        if (inport)
                        {
                            pin = new NetInpin();
                        }
                        else
                        {
                            pin = new NetOutpin();
                        }

                        pin.SlicePort    = node.Port.Name.Substring(node.Port.Name.LastIndexOf('_'));
                        pin.InstanceName = s.SliceName;

                        bool pinExistsAlready = NetPins.FirstOrDefault(np => np.InstanceName.Equals(pin.InstanceName) && np.SlicePort.Equals(pin.SlicePort)) != null;
                        if (!pinExistsAlready)
                        {
                            Add(pin);
                        }
                    }
                }
            }
        }
        public void WriteToFile(StreamWriter sw)
        {
            string headerExtension = string.IsNullOrEmpty(HeaderExtension) ? "" : " " + HeaderExtension;
            string config          = string.IsNullOrEmpty(Config) ? "" : " " + Config;
            string netDeclaration  = "net \"" + Name + "\"" + headerExtension + "," + config;

            sw.WriteLine(netDeclaration);

            foreach (NetPin np in NetPins.Where(x => x is NetOutpin))
            {
                sw.WriteLine("\t" + np.ToString());
            }

            foreach (NetPin np in NetPins.Where(x => x is NetInpin))//.OrderBy(x => x.InstanceName))
            {
                sw.WriteLine("\t" + np.ToString());
            }

            foreach (XDLPip pip in Pips)//.OrderBy(x => x.Location))
            {
                sw.WriteLine("\t" + pip.ToString());
            }

            sw.WriteLine(";");
        }
        public bool IsAntenna(out Dictionary <string, List <XDLPip> > pipsToRemove)
        {
            pipsToRemove = new Dictionary <string, List <XDLPip> >();

            string outpinName          = NetPins.Where(np => np is NetOutpin).First().SlicePort;
            XDLPip firstPipAfterOutpin = Pips.Where(p => p.From.EndsWith(outpinName) && IdentifierManager.Instance.IsMatch(p.Location, IdentifierManager.RegexTypes.CLB)).FirstOrDefault();

            if (firstPipAfterOutpin == null)
            {
                //Console.WriteLine("Could not find the pip right after the outpin " + outpinName + " in net " + this.Name);
                return(false);
            }

            Queue <XDLPip> reachablePips = new Queue <XDLPip>();

            reachablePips.Enqueue(firstPipAfterOutpin);

            foreach (XDLPip pip in Pips.Where(p => !p.Equals(firstPipAfterOutpin)))
            {
                if (!pipsToRemove.ContainsKey(pip.Location))
                {
                    pipsToRemove.Add(pip.Location, new List <XDLPip>());
                }
                pipsToRemove[pip.Location].Add(pip);
            }

            while (reachablePips.Count > 0)
            {
                // from the current pip
                XDLPip current = reachablePips.Dequeue();
                // go to all reachable locations
                foreach (Location loc in Navigator.GetDestinations(current.Location, current.To))
                {
                    if (!pipsToRemove.ContainsKey(loc.Tile.Location))
                    {
                        continue;
                    }
                    // for readability we reference the list of pips
                    List <XDLPip> pipsToRemoveOnTile = pipsToRemove[loc.Tile.Location];
                    while (true)
                    {
                        int index = pipsToRemoveOnTile.FindIndex(pip => pip.From.Equals(loc.Pip.Name));
                        if (index == -1)
                        {
                            break;
                        }
                        XDLPip reachable = pipsToRemoveOnTile[index];
                        pipsToRemoveOnTile.RemoveAt(index);
                        reachablePips.Enqueue(reachable);
                        // remove empty lists for readability
                        if (pipsToRemoveOnTile.Count == 0)
                        {
                            pipsToRemove.Remove(loc.Tile.Location);
                        }
                    }
                }
            }
            return(pipsToRemove.Values.Any(l => l.Count > 0));
        }
        public void UnflattenNet()
        {
            List <TCLRoutingTreeNode> nodeQueue = new List <TCLRoutingTreeNode>(RoutingTree.Root.Children);
            NetOutpin          outPin           = (NetOutpin)NetPins.First(np => np is NetOutpin);
            Tile               rootTile         = FPGA.FPGA.Instance.GetTile(outPin.TileName);
            TCLRoutingTreeNode newRoot          = nodeQueue.FirstOrDefault(n =>
                                                                           n.Tile.Location.Equals(rootTile.Location) &&
                                                                           rootTile.GetSliceByName(outPin.SliceName).PortMapping.IsSliceOutPort(outPin.SlicePort));

            nodeQueue.Remove(newRoot);

            NodeNet          = false;
            RoutingTree.Root = newRoot;
            // try to assign each node to parent
            while (nodeQueue.Count > 0)
            {
                foreach (TCLRoutingTreeNode node in RoutingTree.GetAllRoutingNodes())
                {
                    bool addedNodes = false;
                    // stay on tile
                    TCLRoutingTreeNode nodeOnThisTile = nodeQueue.Find(n =>
                                                                       n.Tile.Location.Equals(node.Tile.Location) &&
                                                                       n.Tile.SwitchMatrix.Contains(node.Port, n.Port));
                    if (nodeOnThisTile != null)
                    {
                        node.Children.Add(nodeOnThisTile);
                        nodeQueue.Remove(nodeOnThisTile);
                        addedNodes = true;
                        break;
                    }

                    // leave tile
                    foreach (Location loc in Navigator.GetDestinations(node.Tile, node.Port))
                    {
                        // look for next hop on loc.Tile
                        TCLRoutingTreeNode nodeOnOtherTile = nodeQueue.Find(n =>
                                                                            n.Tile.Location.Equals(loc.Tile.Location) &&
                                                                            n.Tile.SwitchMatrix.Contains(loc.Pip, n.Port));
                        if (nodeOnOtherTile != null)
                        {
                            node.Children.Add(nodeOnOtherTile);
                            nodeQueue.Remove(nodeOnOtherTile);
                            addedNodes = true;
                            break;
                        }
                    }
                    if (addedNodes)
                    {
                        break;
                    }
                }
            }
        }