Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            //string file = args.Length == 0 ? "test" : args[0];
            string file = args.Length == 0 ? "lut_00FE" : args[0];

            _mainNet = new Mnet();
            _mainNet.ReadMnetFile(file + @".MNET");

            var portsRep = new List<Cpoint>();
            List<Node> luts = _mainNet.GetLuts();
            var lutsMnet = new List<Mnet>();
            var bl = new Blib();
            bl.Load("MNETLib.BinLib");
            //"MNETLib.BinLib"

            foreach (Node node in luts)
            {
                var lnet = new Mnet();
                node.HaveCout = СheckCout(node, _mainNet.Wires);

                lnet.ReadMnetFileBl(@"lut_" + node.GetLutKey() + ".MNET", bl);
                if (!node.HaveCout)
                {
                    lutsMnet.Add(lnet);
                }
                else
                {
                    var lnetC = new Mnet();
                    lnetC.ReadMnetFileBl(@"lutc_" + node.GetLutKey().Substring(2, 2) + ".MNET", bl);
                    Mnet combined = MnetComb(lnet, lnetC);
                    lutsMnet.Add(combined);
                }
            }
            RenameLutNodes(lutsMnet);
            RemoveLutFromMainNet(_mainNet, luts);
            ReplacePortsByCpoints(portsRep, lutsMnet, luts);

            RpeplaceWireToCpoints(portsRep);
            CombineAllWiresAndNodes(lutsMnet);
            //remove unused Wire
            _mainNet.Wires.Remove(_mainNet.Wires.FirstOrDefault(t => t.DistPort == "dataa"));
            _mainNet.Wires.Remove(_mainNet.Wires.FirstOrDefault(t => t.DistPort == "datab"));
            _mainNet.Wires.Remove(_mainNet.Wires.FirstOrDefault(t => t.DistPort == "datac"));
            _mainNet.Wires.Remove(_mainNet.Wires.FirstOrDefault(t => t.DistPort == "datad"));
            string exportStr = _mainNet.GetSting();
            File.WriteAllText(file + @"_D.MNET", exportStr);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //string file = args.Length == 0 ? "lut_0100_D" : args[0];
            string file = args.Length == 0 ? "DIV_3_D" : args[0];
            //string file = args.Length == 0 ? "ADD_21_D" : args[0];
            _mainNet = new Mnet();
            _mainNet.ReadMnetFile(file + @".MNET");

            var dupList = _mainNet.Nodes.Where(t => t.NodeType.Contains("DUP")).ToList();

            //Обьеденение DUP
            List<Node> list = dupList;
            var dupdupWires = _mainNet.Wires.Where(t => list.FirstOrDefault(a => a.NodeName == t.SrcName) != null &&
                                                           list.FirstOrDefault(a => a.NodeName == t.DistName) != null);
            while (dupdupWires.Any())
            {
                var wire = dupdupWires.First();
                var firstDup = dupList.First(a => a.NodeName == wire.SrcName);
                var secondDup = dupList.First(a => a.NodeName == wire.DistName);
                var fromSecondWires = _mainNet.Wires.Where(t => t.SrcName == secondDup.NodeName).ToList();
                foreach (var w in fromSecondWires)
                {
                    w.SrcName = firstDup.NodeName;
                }
                _mainNet.Wires.Remove(wire);
                _mainNet.Nodes.Remove(secondDup);
            }
            dupList = _mainNet.Nodes.Where(t => t.NodeType.Contains("DUP")).ToList();
            //Обновление Типов и имен выходных портов
            foreach (var node in dupList)
            {
                var wlist = _mainNet.Wires.Where(t => t.SrcName == node.NodeName).ToList();
                for (int i = 0; i < wlist.Count; i++)
                {
                    wlist[i].SrcPort = "O" + i;
                }
                node.NodeType = "DUP" + wlist.Count;
            }

            Console.WriteLine("Всего Соеденений " + _mainNet.Wires.Count);
            Console.WriteLine();
            Optimise("AND");
            Console.WriteLine();
            Optimise("OR");
            Console.WriteLine();
            //NAND оптимизация

            TransNot();

            //string ElementName = "AND";

            Console.WriteLine("Оптимизация NOT");
            Console.WriteLine("До Оптимизации " + _mainNet.Nodes.Count(t => t.NodeType.Contains("NOT")));
            List<Node> allAnd = _mainNet.Nodes.Where(t => t.NodeType.Contains("AND")).ToList();
            var allNot = _mainNet.Nodes.Where(t => t.NodeType == "NOT");
            var allNotWire = new List<Wire>();
            foreach (Node node in allNot)
            {
                allNotWire.AddRange(_mainNet.Wires.Where(t => t.SrcName == node.NodeName));
            }
            foreach (Node node in allAnd)
            {
                var wireNotToAnd = allNotWire.Where(t => t.DistName == node.NodeName).ToList();
                var allWiresToAnd = _mainNet.Wires.Where(t => t.DistName == node.NodeName).ToList();
                var allWiresToNot = new List<Wire>();
                var allNotTodel = new List<Node>();
                var bits = new int[allWiresToAnd.Count];
                //Заполнение битов
                /*
                for (int i = 0; i < allWiresToAnd.Count; i++)
                {
                    if (wireNotToAnd.Contains(allWiresToAnd[i]))
                    {
                        bits[i] = 1;
                    }
                }
                 */
                foreach(Wire wire in wireNotToAnd)
                {
                    allNotTodel.Add(_mainNet.Nodes.FirstOrDefault(t => t.NodeName == wire.SrcName));
                    allWiresToNot.Add(_mainNet.Wires.FirstOrDefault(t => t.DistName == wire.SrcName));
                }

                //Перенос соеденений
                for (int i = 0; i < allWiresToAnd.Count; i++)
                {
                    //if (bits[i] == 1)
                    //{
                    Wire twire = _mainNet.Wires.FirstOrDefault(t => t.DistName == allWiresToAnd[i].SrcName);
                    if (twire != null)
                    {
                        if (_mainNet.Nodes.First(t => t.NodeName == twire.DistName).NodeType == "NOT")
                        {
                            allWiresToAnd[i].SrcName = twire.SrcName;
                            allWiresToAnd[i].SrcPort = twire.SrcPort;
                            bits[i] = 1;
                        }
                    }
                    //}
                    allWiresToAnd[i].DistPort = "I" + i;
                }

                //Получения числа
                int val = 0;
                for (int i = 0; i < allWiresToAnd.Count; i++)
                {
                    val = val | bits[i];
                    val = val << 1;
                }
                val = val >> 1;

                //val = RevertVal(val, allWiresToAnd.Count);

                //Преобразование and
                node.NodeType = "NANDT" + bits.Length + "_" + val.ToString("X2");

                //Удаление ненужных соеденений и нодов
                foreach (var t in allWiresToNot) _mainNet.Wires.Remove(t);
                foreach (var t in allNotTodel) _mainNet.Nodes.Remove(t);

            }

            Console.WriteLine("После Оптимизации " + _mainNet.Nodes.Count(t => t.NodeType.Contains("NOT")));

            //Удаление нодов без исходящих соеденений
            var optneed = true;
            while (optneed)
            {
                optneed = false;
                var nodetodell = new List<Node>();
                var wiretodell = new List<Wire>();
                foreach (var node in _mainNet.Nodes)
                {
                    //Удаление нодов без исходящих соеденений
                    if (!node.NodeType.Contains("Port"))
                    {
                        var outWires = _mainNet.Wires.Where(t => t.SrcName == node.NodeName).ToList();
                        if (outWires.Count < 1)
                        {
                            optneed = true;
                            nodetodell.Add(node);
                            wiretodell.AddRange(_mainNet.Wires.Where(t => t.DistName == node.NodeName).ToList());
                        }
                    }
                    //Удаление нодов без вхожящих соеденений
                    /*
                    if (!node.NodeType.Contains("Port") && !node.NodeType.Contains("GND") && !node.NodeType.Contains("VCC"))
                    {
                        var outWires = _mainNet.Wires.Where(t => t.DistName == node.NodeName).ToList();
                        if (outWires.Count < 1)
                        {
                            optneed = true;
                            nodetodell.Add(node);
                            wiretodell.AddRange(_mainNet.Wires.Where(t => t.SrcName == node.NodeName).ToList());
                        }
                    }
                     */
                }
                foreach(var node in nodetodell) _mainNet.Nodes.Remove(node);
                foreach(var wire in wiretodell) _mainNet.Wires.Remove(wire);
            }

            Console.WriteLine("Соеденений после оптимизации " + _mainNet.Wires.Count);
            System.IO.File.WriteAllText(file + @"_O.MNET" ,_mainNet.GetSting());
        }