Exemplo n.º 1
0
        /*
         * Groups binary numbers based on 1's.
         * Stores group in a hashtable associated with a list (bucket) for each group.
         */
        private static Dictionary <int, ImplicantCollection> Group(ImplicantCollection implicants)
        {
            var group = new Dictionary <int, ImplicantCollection>();

            foreach (Implicant m in implicants)
            {
                int count = GetOneCount(m.Mask);

                if (!group.ContainsKey(count))
                {
                    group.Add(count, new ImplicantCollection());
                }

                group[count].Add(m);
            }

            return(group);
        }
Exemplo n.º 2
0
        /*
         * Populates a matrix based on a given set of implicants and minterms.
         */
        private static void PopulateMatrix(ref bool[,] matrix, ImplicantCollection implicants, int[] inputs)
        {
            for (int m = 0; m < implicants.Count; m++)
            {
                int y = implicants.IndexOf(implicants[m]);

                foreach (int i in implicants[m].Minterms)
                {
                    for (int index = 0; index < inputs.Length; index++)
                    {
                        if (i == inputs[index])
                        {
                            matrix[y, index] = true;
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        /*
         * Retreives the final simplified expression in readable format.
         */
        private static string GetFinalExpression(ImplicantCollection implicants)
        {
            int    longest = 0;
            string final   = string.Empty;

            foreach (Implicant m in implicants)
            {
                if (m.Mask.Length > longest)
                {
                    longest = m.Mask.Length;
                }
            }

            for (int i = implicants.Count - 1; i >= 0; i--)
            {
                final += implicants[i].ToChar(longest) + " + ";
            }

            return(final.Length > 3 ? final.Substring(0, final.Length - 3) : final);
        }
Exemplo n.º 4
0
        /*
         * Retreives the final simplified expression in readable format.
         */
        private static string GetFinalExpression(ImplicantCollection implicants, bool lsba = false, bool negate = false)
        {
            int    longest = 0;
            string final   = string.Empty;

            foreach (Implicant m in implicants)
            {
                if (m.Mask.Length > longest)
                {
                    longest = m.Mask.Length;
                }
            }

            for (int i = implicants.Count - 1; i >= 0; i--)
            {
                if (negate)
                {
                    final += implicants[i].ToChar(longest, lsba, negate) + " & ";
                }
                else
                {
                    final += implicants[i].ToChar(longest, lsba, negate) + " + ";
                }
            }

            string ret = (final.Length > 3 ? final.Substring(0, final.Length - 3) : final);

            switch (ret)
            {
            case " + ":
                return("1");

            case "":
                return("0");

            default:
                return(ret);
            }
        }
Exemplo n.º 5
0
        public static string GetSimplified(IEnumerable <LogicItem> List, int variables, bool hazardsafe = false, bool lsba = false, bool negate = false)
        {
            var implicants = new ImplicantCollection();

            var items        = (from i in List where i.Checked == true || i.Checked == null orderby i.Index ascending select i.Index).ToArray();
            var careminterms = (from i in List where i.Checked == true orderby i.Index ascending select i.Index).ToArray();



            foreach (var item in items)
            {
                var m = new Implicant();
                m.Mask = LogicItem.GetBinaryValue(item, variables);
                m.Minterms.Add(item);
                implicants.Add(m);
            }

            //int count = 0;
            while (Simplify(ref implicants))
            {
                //Populate a matrix.
                bool[,] matrix = new bool[implicants.Count, items.Length]; //x, y
                PopulateMatrix(ref matrix, implicants, items);
            }
            ImplicantCollection selected;

            if (hazardsafe)
            {
                selected = implicants;
            }
            else
            {
                selected = SelectImplicants(implicants, careminterms);
            }
            return(GetFinalExpression(selected, lsba, negate));
        }
Exemplo n.º 6
0
        /*
         * Entry point.
         */
        public static void Main(string[] args)
        {
            int[] input = null;

            Console.WriteLine("Michael Landi\t\tComp. Architecture.");
            Console.WriteLine("Boolean Simplification Program");
            Console.WriteLine();
            Console.Write("ENTER MINTERMS: ");
            string[] minterms = Console.ReadLine().Split(' ');
            Console.WriteLine();

            //Parse and validate input.
            try
            {
                input = new int[minterms.Length];
                for (int i = 0; i < input.Length; i++)
                {
                    input[i] = -1; //That way we can check for duplicate zeroes.
                }
                //Empty string passed.
                if (minterms.Length == 0 || minterms[0].Trim() == string.Empty)
                {
                    throw new Exception("No input.");
                }

                for (int i = 0; i < minterms.Length; i++)
                {
                    int check = Int32.Parse(minterms[i]);

                    if (check < 0)
                    {
                        throw new Exception("Input cannot be less than zero.");
                    }
                    if (input.Contains(check))
                    {
                        throw new Exception("Input cannot contain the same minterm twice.");
                    }

                    input[i] = check;
                }
                Array.Sort(input);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                Environment.Exit(-1);
            }

            //Create initial list of  minterms.
            ImplicantCollection implicants = new ImplicantCollection();

            foreach (int i in input)
            {
                Implicant m = new Implicant();
                m.Mask = Convert.ToString(i, 2);
                m.Minterms.Add(i);
                implicants.Add(m);
            }

            //Simplify expressions.
            int count = 0;

            while (Simplify(ref implicants))
            {
                //Populate a matrix.
                bool[,] matrix = new bool[implicants.Count, input.Length]; //x, y
                PopulateMatrix(ref matrix, implicants, input);
                PrintMatrix(matrix, input, ++count);
            }

            //Select implicants.
            ImplicantCollection selected = SelectImplicants(implicants, input);
            string strFinal = GetFinalExpression(selected);

            Console.WriteLine();
            Console.WriteLine("SIMPLIFIED EXPRESSION: ");
            Console.WriteLine(strFinal);
            Console.ReadKey();
        }
Exemplo n.º 7
0
        /*
         * Simplifies a givenset of implicants.
         */
        private static bool Simplify(ref ImplicantCollection implicants)
        {
            /*
             * Group by number of 1's and determine relationships by comparing.
             */
            ImplicantGroup group = Group(implicants);
            ImplicantRelationshipCollection relationships = new ImplicantRelationshipCollection();

            for (int i = 0; i < group.Keys.Count; i++)
            {
                if (i == (group.Keys.Count - 1))
                {
                    break;
                }

                ImplicantCollection thisGroup = group[group.Keys.ElementAt(i)];
                ImplicantCollection nextGroup = group[group.Keys.ElementAt(i + 1)];

                foreach (Implicant a in thisGroup)
                {
                    foreach (Implicant b in nextGroup)
                    {
                        if (GetDifferences(a.Mask, b.Mask) == 1)
                        {
                            relationships.Add(new ImplicantRelationship(a, b));
                        }
                    }
                }
            }

            /*
             * For each relationship, find the affected minterms and remove them.
             * Then add a new implicant which simplifies the affected minterms.
             */
            foreach (ImplicantRelationship r in relationships)
            {
                ImplicantCollection rmList = new ImplicantCollection();

                foreach (Implicant m in implicants)
                {
                    if (r.a.Equals(m) || r.b.Equals(m))
                    {
                        rmList.Add(m);
                    }
                }

                foreach (Implicant m in rmList)
                {
                    implicants.Remove(m);
                }

                Implicant newImplicant = new Implicant();
                newImplicant.Mask = GetMask(r.a.Mask, r.b.Mask);
                newImplicant.Minterms.AddRange(r.a.Minterms);
                newImplicant.Minterms.AddRange(r.b.Minterms);

                bool exist = false;
                foreach (Implicant m in implicants)
                {
                    if (m.Mask == newImplicant.Mask)
                    {
                        exist = true;
                    }
                }

                if (!exist) //Why am I getting dupes?
                {
                    implicants.Add(newImplicant);
                }
            }

            //Return true if simplification occurred, false otherwise.
            return(!(relationships.Count == 0));
        }
Exemplo n.º 8
0
        /*
         * Simplifies a givenset of implicants.
         */
        private static bool Simplify(ref ImplicantCollection implicants)
        {
            /*
             * Group by number of 1's and determine relationships by comparing.
             */
            var groups        = (from i in Group(implicants) orderby i.Key select i).ToDictionary(i => i.Key, i => i.Value);
            var relationships = new ImplicantRelationshipCollection();

            for (int i = 0; i < groups.Keys.Count; i++)
            {
                if (i == (groups.Keys.Count - 1))
                {
                    break;
                }

                var thisGroup = groups[groups.Keys.ElementAt(i)];
                var nextGroup = groups[groups.Keys.ElementAt(i + 1)];

                var q = from a in thisGroup from b in nextGroup where GetDifferences(a.Mask, b.Mask) == 1 select new ImplicantRelationship(a, b);
                relationships.AddRange(q);
            }

            /*
             * For each relationship, find the affected minterms and remove them.
             * Then add a new implicant which simplifies the affected minterms.
             */
            foreach (ImplicantRelationship r in relationships)
            {
                var rmList = new ImplicantCollection();

                foreach (Implicant m in implicants)
                {
                    if (r.a.Equals(m) || r.b.Equals(m))
                    {
                        rmList.Add(m);
                    }
                }

                foreach (Implicant m in rmList)
                {
                    implicants.Remove(m);
                }

                var newImplicant = new Implicant();
                newImplicant.Mask = GetMask(r.a.Mask, r.b.Mask);
                newImplicant.Minterms.AddRange(r.a.Minterms);
                newImplicant.Minterms.AddRange(r.b.Minterms);

                bool exist = false;
                foreach (Implicant m in implicants)
                {
                    if (m.Mask == newImplicant.Mask)
                    {
                        exist = true;
                    }
                }

                if (!exist) //Why am I getting dupes?
                {
                    implicants.Add(newImplicant);
                }
            }

            //Return true if simplification occurred, false otherwise.
            return(!(relationships.Count == 0));
        }