a primer on karnaugh maps - cabrillo college - …snerton/cs23/links/karnaugh_qm.pdf ·  ·...

18
A primer on Karnaugh maps By Jack W. Crenshaw, Courtesy of Embedded Systems Programming Nov 17 2003 (14:00 PM) URL: http://www.embedded.com/showArticle.jhtml?articleID=16100908 Simplification can actually reduce the number of logic gates you need. And Karnaugh maps are a good simplification tool. Last time, I introduced the ideas behind symbolic logic, logic circuits, and so on. This month, I’ll talk more about minimizing logic equations; but before we start, I’d like to make a couple of corrections. Electronics Workbench First, last month I mentioned the computer program, Electronics Workbench. I wondered out loud if the company that makes it was still around. A Google search reveals that they are (www.electronicsworkbench.com ). Their current product, MultiSim, seems to fill the niche formerly occupied by their Windows product. Unfortunately, a sales rep told me that they now concentrate only on industrial customers and no longer sell individual licenses. For the record, I invited their sales manager to write a paragraph explaining their current product line, but got no reply. I guess that’s because I’m not a corporation. Should you be interested in tinkering with electronics sims, better choose one of the others. Second, I should explain where I’m going with this new focus on electronics. Some of you may find electronics in general and digital electronics in particular interesting, and there’s certainly nothing wrong with that. Some of us still get that nostalgic glow when we smell rosin-core solder, so if I pique your interest, that’s fine. But it’s not my intention to turn you all into electronics wizards. Rather, my focus here is on the use of symbolic logic. It’s my view that a little knowledge of the rules and techniques of symbolic logic will be a nice tool to add to your toolbox and will help you write better software. NAND or NOR? Finally, reader Hank Schultz e-mailed to chide me for using the terms NAND and NOR too loosely. At first, I thought Hank was being too critical. I distinctly remembered using symbols for Fairchild’s RTL logic gates as both NAND and NOR gates. Here’s the deal. Last month, I gave you DeMorgan’s theorem, which I’ll reproduce here for convenience: (1) From this, it’s easy to see that a logic gate designed to do the AND function can just as easily do the OR function; it’s simply a matter of defining which voltagehigh or lowyou define as representing a 1 (true) or 0 (false). I showed you symbols for each usage, for both RTL and TTL logic families (though the former is now mostly useful only to historians). By convention, any signal that has a little circle at its connection is considered to be inverted. With TTL NAND gates, the inputs work correctly if a high voltage is 1. The circle is on the output. I showed the circles on the input for the RTL gate. Well, maybe I used the gates that way, but Fairchild never did. The URL www.wps.com/archives/solid-state-datasheets/Datasheets/Fairchild-uL900-914/1.JPG shows an original Fairchild data sheet, copyrighted 1966. Their diagram clearly shows the

Upload: dinhnga

Post on 22-Mar-2018

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

A primer on Karnaugh maps By Jack W. Crenshaw, Courtesy of Embedded Systems Programming Nov 17 2003 (14:00 PM) URL: http://www.embedded.com/showArticle.jhtml?articleID=16100908

Simplification can actually reduce the number of logic gates you need. And Karnaugh maps are a good simplification tool.

Last time, I introduced the ideas behind symbolic logic, logic circuits, and so on. This month, I'll talk more about minimizing logic equations; but before we start, I'd like to make a couple of corrections.

Electronics Workbench First, last month I mentioned the computer program, Electronics Workbench. I wondered out loud if the company that makes it was still around. A Google search reveals that they are (www.electronicsworkbench.com). Their current product, MultiSim, seems to fill the niche formerly occupied by their Windows product. Unfortunately, a sales rep told me that they now concentrate only on industrial customers and no longer sell individual licenses. For the record, I invited their sales manager to write a paragraph explaining their current product line, but got no reply. I guess that's because I'm not a corporation. Should you be interested in tinkering with electronics sims, better choose one of the others.

Second, I should explain where I'm going with this new focus on electronics. Some of you may find electronics in general and digital electronics in particular interesting, and there's certainly nothing wrong with that. Some of us still get that nostalgic glow when we smell rosin-core solder, so if I pique your interest, that's fine. But it's not my intention to turn you all into electronics wizards. Rather, my focus here is on the use of symbolic logic. It's my view that a little knowledge of the rules and techniques of symbolic logic will be a nice tool to add to your toolbox and will help you write better software.

NAND or NOR? Finally, reader Hank Schultz e-mailed to chide me for using the terms NAND and NOR too loosely. At first, I thought Hank was being too critical. I distinctly remembered using symbols for Fairchild's RTL logic gates as both NAND and NOR gates.

Here's the deal. Last month, I gave you DeMorgan's theorem, which I'll reproduce here for convenience:

(1)

From this, it's easy to see that a logic gate designed to do the AND function can just as easily do the OR function; it's simply a matter of defining which voltage�high or low�you define as representing a 1 (true) or 0 (false). I showed you symbols for each usage, for both RTL and TTL logic families (though the former is now mostly useful only to historians).

By convention, any signal that has a little circle at its connection is considered to be inverted. With TTL NAND gates, the inputs work correctly if a high voltage is 1. The circle is on the output. I showed the circles on the input for the RTL gate.

Well, maybe I used the gates that way, but Fairchild never did. The URL www.wps.com/archives/solid-state-datasheets/Datasheets/Fairchild-uL900-914/1.JPG shows an original Fairchild data sheet, copyrighted 1966. Their diagram clearly shows the

Page 2: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

µL914 as a NOR gate, not a NAND. They were consistent in that depiction. Figure 1 shows the symbol they used, which is perfectly consistent with modern practice.

Figure 1: Equivalent logic

In my defense, consider Fairchild's own words: "The µL914 device is a dual two-input gate. Each gate performs the NAND/NOR logical function using RTL circuitry."

Ok, maybe they didn't use the NAND symbol for the 914, but they certainly agreed that one could use it as a NAND gate. And I did.

Minimal is good Let's get back to logic minimization, with an example. X is a function of four variables, A through D. Remember, in this notation, "+" stands for "or," and "." stands for "and." Also remember, a bar over a symbol or expression stands for "not."

(2)

Looks pretty messy, doesn't it? Nevertheless, this is often the kind of equation one gets after considering what a certain logical behavior must be. Let's see if we can simplify the equation by factoring. We'll start by factoring out terms in A and (omitting the and-ing dots for simplicity):

(3)

Note that the term is repeated in the last set of parentheses. A term OR'd with itself is redundant; we clearly only need one of the terms. We write:

(4)

A little more factoring gives:

(5)

Page 3: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Now, C has to be either true or not true; it can't be something else:

(6)

And our equation becomes:

(7)

It's not obvious how we can simplify further, but perhaps we can. Let's try expanding the terms again, and factoring in a different way. We get:

(8)

Now, DeMorgan's theorem says:

(9)

The first factored term in Equation 8 becomes:

(10)

Sometimes it helps to multiply a term by 1, in the form of Equation 6:

(11)

Now we can combine the two terms that have D as a factor, to get:

(12)

In the first term, we have a thing� �or'd with its own complement. That's the same form as in Equation 6, so we know how to handle it:

(13)

Equation 12 now becomes:

(14)

Page 4: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

When all else fails, try expanding again:

(15)

Now look at the first and fourth terms. They factor to give, simply, D:

(16)

At this point, we can see that we have one term involving D alone, plus a lot of terms involving . Time to factor again:

(17)

Inside the parentheses, we have one term involving A alone, plus a lot of terms involving . Factoring yet again gives:

(18)

There's that familiar form of Equation 6 again. Simplifying from here gives:

(19)

Are you kidding me? After all that work, the equation reduces to nothing but a single constant?

Yep, that's what it does. You might have guessed it's because I designed the equation that way. Even so, I wasn't faking the simplification; I needed every step to get to the final form.

Admittedly, this is an extreme case. But it serves to illustrate a very important point: simplifying logic equations can be important. If we were to implement Equation 2 directly, using hardware logic gates, we'd need 27 gates. After simplifying, we need none at all; only a single wire from the high (plus, true, 1) supply to X. Perhaps, if X feeds into some other equation, we'd get even further simplifications there.

Now you can see why my friend's computer program that simplified logic automatically was so valuable. Equation 2, as complicated as it is, is only one equation with four inputs. Real systems might have hundreds of such equations, with dozens of inputs. If you're blessed with so much intuition that you could look at Equation 2 and see immediately that its simplification is trivial, you'll go far. Most of us have trouble even finding the best simplification. I had trouble doing this myself, and I'm the one who wrote down the equation in the first place. One wrong turn in the process, and we might easily have missed the key step.

Page 5: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

I mentioned last month that there's more than one way to represent a given logical relationship. The most fundamental one is the truth table, in which you write down the desired output for all possible sets of inputs. The other is the logic equation and a third is an electronic circuit mechanizing the relationship. In this particular example, the truth table would have been the one to use. The truth table would have shown 1s for every single set of inputs, so the logic's trivial nature would have been obvious.

We don't, however, usually get to choose the form of the problem as it's given to us. We can convert from one form to another, but the advantage of one form over the other is rarely so clear. What we need, in general, are techniques that lead us to minimizations that aren't so obvious.

The Karnaugh map One such technique is called the Karnaugh map, and it's one of the neatest approaches you'll ever see. I remember when I first saw it in a GE manual, I felt as though I'd found the pot of gold at the end of the rainbow.

Here's how it works. First, for four inputs (the case where the Karnaugh map works best), make a 4x4 array as in Figure 2.

Figure 2: The basic Karnaugh map

Two things are noteworthy about this map. First, we've arranged the 16 possible values of the four inputs as a 4x4 array, with two bits encoding each row or column.

The second and key feature is the way we number the rows and columns. They aren't in binary sequence, as you might think. As you can see, they have the sequence 00, 01, 11, 10. Some of you may recognize this as a Gray code.

Why this particular sequence? Because the codes associated with any two adjacent rows or columns represent a change in only one variable. In a true binary counting code, sometimes several digits can change in a single step; for example, the next step after 0x1111 is 0x10000. Five output signals must change values simultaneously. In digital circuits, this can cause glitches if one gate delay is a bit faster or slower than another. The Gray code avoids the problem. It's commonly used in optical encoders.

Suppose the output value for two adjacent cells is the same. Since only one input variable is changed between the two cells, this tells us that the output doesn't depend on that input. It's a "don't care" for that output.

Page 6: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Figure 3: Looking for patterns

Look at Figure 3. Group X is true for inputs ABCD = 0100 and 1100. That means that it doesn't depend on A, and we can write:

(20)

Similarly, Group Y doesn't depend on B or C. Its value is:

(21)

Note that the groupings don't necessarily have to be in contiguous rows or columns. In Group Z, the group wraps around the edges of the map.

If we can group cells by twos, we eliminate one input. By fours, two inputs, and so on. If the cell associated with a given output is isolated, it depends on all four inputs, and no minimization is possible.

The Karnaugh map gives us a wonderful, graphical picture that lets us group the cells in a near-optimal fashion. In doing so, we minimize the representation. Neat, eh?

Figure 4: Equation 2, mapped

Now let's see how Equation 2 plots onto the Karnaugh map, as shown in Figure 4. To make it easier to see, I'll assign a different lowercase letter to each term of the equation:

Page 7: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

(22) In this case, the individual groups don't matter much. All that counts is the largest group we can identify, which is of course the entire array of 16 cells. The output X is true for all values of the inputs, so all four are don't-care variables.

As you can see, using a Karnaugh map lets us see the results and, usually, the optimal grouping at a glance. It might seem that drawing a bunch of graphs is a tedious way to minimize logic, but after a little practice, you get where you can draw these simple arrays fast and see the best groupings quickly. You have to admit, it's a better approach than slogging through Equations 2 through 19.

The decade counter Next, I'd like to show you a classic problem that we used to think was really important�at least until we got chips that did all the work for us. It's the decade counter. In this problem, we have four inputs and four outputs, which happen to be the next state of the four inputs. I'll denote the current states by the upper case letters, A through D, and the output (next value) states, lowercase a through d. Table 1 gives the truth table.

Note that, although a 4-bit number can encode 16 unique values, there are only 10 rows in the truth table. The other six rows aren't needed because the counter should never be in those states. When we draw the Karnaugh maps for this truth table, the unused cells will show up as six "don't-care" states. Quite literally, we don't care what value is output for those cases, since the inputs should never occur.

I should note in passing that if we truly implement this design, we had danged well better make sure that the don't- care states really never, ever occur. This may require some care when things power up.

Page 8: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Figure 5: Looking for patterns in decade counters

For the decade counter, we'll need four Karnaugh maps as shown in figure 5, one for each output value. In the figures, note how I let the groupings spill over into the don't-care areas if it will let me make larger groups.

The logic equations are:

(23)

These are by no means the only possible choices of terms, but we have reasonable assurance that they are, if not optimal, at least nearly so. That's because we used the groupings of the Karnaugh map to get the largest groups we could.

An exercise for the student As another example, I'll give you a problem and its solution. This one encodes the output of a decade counter, to drive a seven-segment display.

figure 6: The seven-segment display

The standard nomenclature for this display is shown in figure 6. It assigns the letters a through g to the segments. Given any combination of 4-bit numbers, our goal is to light the combination of the seven segments in such a way as to display the proper decimal digit.

The truth table is shown in Table 2. Note that, as in Table 1, I'm using A as the most significant bit of the binary number.

Page 9: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Table 2: Seven-segment decoder

A B C D a b c d e f g

0 0 0 0 1 1 1 1 1 1 0

0 0 0 1 0 1 1 0 0 0 0

0 0 1 0 1 1 0 1 1 0 1

0 0 1 1 1 1 1 1 0 0 1

0 1 0 0 0 1 1 0 0 1 1

0 1 0 1 1 0 1 1 0 1 1

0 1 1 0 1 0 1 1 1 1 1

0 1 1 1 1 1 1 0 0 0 0

1 0 0 0 1 1 1 1 1 1 1

1 0 0 1 1 1 1 1 0 1 1

As I said, I was going to show you how to generate the equations for the decoder. In restrospect, though, I think it's much better to involve a little audience participation. You have the truth table, you have the technology. You can use the practice, so how about you solve the problem instead of me?

You're going to need seven different Karnaugh maps; one for every segment of the display. Remember, we're talking decade counter here, so don't try to generate the remaining hex digits. Because only 10 of the 16 states are going to occur, you can use the remaining six as don't-care states, as I did in Figures 5a through 5d.

One hint: look for similar patterns in each of the seven maps. If you can minimize the logic the same way on more than one map, it would lower the total gate count for a practical implementation.

Next month, I'll show you my solution and share a personal tale about how I tried to build my own displays. I'll also show you the more powerful method by Quine and McCuskey.

See you then.

Jack Crenshaw is a senior software engineer at Spectrum-Astro and the author of Math Toolkit for Real-Time Programming, from CMP Books. He holds a PhD in physics from Auburn University. E-mail him at [email protected].

Page 10: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

All about Quine-McClusky By Jack W. Crenshaw, Courtesy of Embedded Systems Programming Aug 19 2004 (15:00 PM) URL: http://www.embedded.com/showArticle.jhtml?articleID=29111968

Using K-maps to simplify logic equations in hardware and software makes a lot of sense, but Quine-McClusky is more a systematic approach.

If you've been following this column for the last few months, you know that we've been talking about symbolic logic and the hardware circuits used to implement it. It wasn't really my intention to talk much about the hardware, since the focus of this column is on software tools. But I've noticed that most programmers don't know much about symbolic logic unless they've taken courses in digital circuitry. Since I believe that software can sometimes be simplified by using symbolic logic, I set out to teach the fundamentals. One thing led to another, and the next thing I knew, I was describing how to build counters with J-K flip-flops.

One doesn't have to wire up digital chips in order to learn and use symbolic logic. But if the end product of the exercise is a circuit, there's good reason to try to simplify the logic as much as you can before implementing it. After all, chips cost money, as do circuit boards. This fact leads us very naturally into the subject of logic minimization, which was really the point of the series.

I think perhaps the best way to explain the value of logic minimization is by analogy to ordinary algebra. Consider the following function in Equation 1.

Equation 1: A function begging for minimization

Looks pretty daunting, doesn't it? Some of those coefficients, such as the 87 or the 99, don't look too promising. But if you plot the graph of the function, you'll see that it's a simple parabola. Something must be going on to make the function seem simple. The function can, in fact, be greatly simplified. Probably the most straightforward approach is to divide the numerator by the denominator, using synthetic division. Or, if you prefer, you can factor both top and bottom polynomials, to get:

Equation 2

This, of course, immediately simplifies down to:

Equation 3

Which function would you prefer to implement in software? Equation 1 or 3? When the simplification involves hardware, as it does in logic implementations, simplifying is very important. It may not seem so important in software, and perhaps it's not. One might argue that, hey, either form works in the software. Yes, Equation 1 is more complicated, but both forms give the same result, and anyway, my CPU is really fast.

Page 11: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

But look again. In the factored form of Equation 2, there are two sets of multiple poles; the denominator goes to zero at x = 1 and x = 1/2. Guess what happens when you divide by zero? Simplifying the equation gets rid of the poles, which greatly improves the behavior of the function.

Karnaugh maps revisited If I've convinced you that it's important to simplify logic expressions before implementing them�either in hardware or software�then I've made my point. The question remains, how do we simplify the logic equations? We have almost all the tools of ordinary algebra�factoring, grouping, and so on�at our disposal, but it would be nice to have some systematic method that's guaranteed to give�or at least has a good chance of yielding�optimally simple forms.

In earlier columns I've shown you the Karnaugh map (K-map), which is among my favorite tools for dealing with logic. Figure 1 shows a typical K-map from one of those columns. The context is not important.

Figure 1: A typical K-map

As you can see, the K-map is an array of cells, each of which represents one possible combination of inputs. We order the rows and columns of the matrix in a rather special way. The order is, in fact, the Gray code, which has the property that, between any two adjacent values, only one bit has changed.

This is important, because when we see that the same output is desired for two adjacent cells, it means that the value of the one bit that's different, doesn't affect the result. For example, the two cells in row 11, columns 01 and 11, differ only in the value of A. This should tell us that the value of A doesn't matter. (If you're wondering, the shaded cells in Figure 1 are "don't care" cells. These combinations of inputs should never occur, so we're free to encroach on these cells if it helps us reduce the logic.) For Figure 1, the equivalent logic equation is:

Equation 4

Page 12: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

The K-map works great when the number of input variables is four or less. In a pinch, we can stretch it to five or six. After that, forget it. The relationships between groups gets so confusing that the approach quickly loses its value. Even for only four inputs, it's not always easy to decide how best to collect the cells into groups. Especially when there are multiple maps, as in the case of the seven-segment display, it's not at all obvious which terms will work best.

For the cases where we're using logical variables in software, four inputs are almost always enough, so the K-map works fine to help us optimize our software logic. But when we want to get serious about logic minimization, a more systematic approach, and one that can handle larger sets of terms, is called for.

The way of truth You can express logical relationships in at least three ways. The K-map is one way, logic equations are another. We could even express them as circuit diagrams. Electronic Workbench, which I've mentioned before, not only lets you use multiple forms, but it will transform one to the other.

Of all these ways, though, the most fundamental one is definitely the truth table. The truth table captures, with no possibility of misinterpretation, the expected output for every combination of inputs. The truth table contains a row for each combination, and the expected output. The truth table corresponding to Figure 1, for example, is shown in Table 1.

Page 13: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

This truth table gives us exactly the same information as the K-map in Figure 1. The first three columns represent three alternative ways to express the inputs. The second column gives the values of the individual bits, the third is the equivalent logic term, and the first is simply the equivalent decimal integer.

For the record, every row in the truth table has a name�it's called a minterm. Every cell in the K-map is also a minterm. As the name implies, it's the cell of minimum size, and it therefore depends on the values of all the inputs.

The fourth column gives the desired output for each case. The only two required non-zero values are the values for rows 7 and 9 (0111, 1001, , or ). The x's in the last few cells mean that we don't care what the output is; these combinations should never occur anyway.

From the truth table, we need to build a structure of some kind, suitable for minimization. Up until now, that structure has been the K-map. It's easier to see patterns in the K-map than in the truth table itself. In the past, we would transcribe the desired outputs to a K-map, then seek simplifications.

The Quine-McClusky method Like the K-map, the Quine-McClusky method (hereafter called Q-M) seeks to collect terms by looking for entries that differ only in a single bit. The only difference from a K-map is that we do it by searching, rather than mapping. The beauty of the Q-M method is that it takes over where the K-map begins to fail. It's capable of minimizing logic relationships for any number of inputs. It also happens to be nicely suited to a computerized solution.

Table 2 shows another truth table (here, I've shown only the desired 1's for clarity).

Now let's build a second table that contains only the minterms that require the output to be true. That table, complete with cell numbers, is shown in Table 3.

Page 14: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Just as with the K-map, we try to minimize the logic by finding "adjacent" cells that can be combined. The only difference is, this time the adjective "adjacent" doesn't refer to physical location but to bit pattern.

This business of matching up entries is the heart of the Q-M method. I'll go through it slowly so nobody will miss the technique.

Consider the first entry, which is number 0, pattern 0000. We ask ourselves, what other entries can we possibly match up to this one? Remember, the two entries can differ only in a single bit. That pretty well narrows down the range. This pattern can only be matched by:

(1) = 0001 (2) = 0010 (4) = 0100 (8) = 1000

The table doesn't contain any of these entries, so pattern number 0 remains unmatched, and we move on to the next entry, 3 = 0011. Again, there are four possible matching patterns, each generated from 0011 by changing a single bit:

(2) = 0010 (1) = 0001 (7) = 0111 (11) = 1011

There is no entry for index 2 or 1, but there are for 7 and 11. Entries 3 and 7 differ only in the second bit, which corresponds to input B:

0011 <=> 0111

What is this match telling us? The same thing it would tell us for two adjacent cells in a K-map: For this particular pattern, the output doesn't depend on the second input, which is B. To reflect this fact, we create a new entry:

(3,7) = 0x11

The next obvious question is: Where do we put this new entry? Answer: We create a new column. As it turns out, we also must leave some information behind in the previous column, to indicate which terms we've used. The reason is this: If a term has been checked, it's subsumed by one of the terms in the next column, so we can be assured that its contribution is accounted for. But if it's unchecked, (like 0000, for which we found no match) it must be included explicitly in the final expression.

Similarly, entries 3 and 11 reduce to x011. At this point, the table should look like Table 4. I've included the combined index numbers for clarity. In practice, we probably wouldn't bother, depending only on the patterns to guide us.

Page 15: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Now let's proceed down the remaining entries, looking for matches. You should find a total of six, as shown in Table 5. Note that the search gets easier as we go, because we need only search below the current position; if a match exists with an earlier entry, it's already been accounted for.

What shall we do next? Why, we repeat the process, looking for matches in the last column. As we do, note that the only thing that can match an x is another x in the same place. This is the case, for example, for entries (3,7) and (11,15). At the end of this search, we have a new column and a new set of check marks. Note that two different combinations (3,7)-(11,15) and (3,11)-(7,15) yield the same result. Clearly, we don't need the same term twice, so we'll delete the duplicate.

No further reductions are possible, so we're done. For this example in Table 6, the final patterns are given by the unchecked entries.

The last two terms can be further simplified by factoring. Q-M can't help us with that. We must still do the factoring ourselves. The end result , and an optimized one, is:

Equation 5

Streamlining the process You can see that the Q-M method accomplishes the same end result as we get from a K-map. I think you can also see that it's rather tedious, and fraught with error. With Q-M, we trade off a convenient and intuitive method to get both a capability to handle more input variables, and an (almost) guaranteed optimality.

Although there's not much we can do to remove the tedium, one trick enables us to streamline the process. Consider the first minterm of our previous example: 0000. Recalling that any match can only vary in a single bit, it should be obvious that only the patterns with one non-zero bit�that is, 0001, 0010, 0100, and 1000, can possibly match it. There's no need to search the remainder of the table; only the terms with a single bit qualify.

Similarly, if a given minterm has n bits, it can only be matched by terms with n-1 or n+1 bits. This suggests that we can gain some efficiency by ordering the terms into groups with 0, 1, 2, 3, or 4 ones. When searching for matches, we need only look in the groups just above and below the current one. Here's another example, to show what I mean.

Page 16: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Equation 6

In Table 7A, I've ordered the terms according to the number of zeroes.

Now let's do the reduction as before. You're on your own on this one. See if you get the same thing I do. In Table 7A, I've streamlined the process by eliminating the cell numbers.

Note that, when I sort the entries by their 1's count, it greatly reduces the number of places I have to look for a match. The term (4) = 0100, which has one 1, can only match entries with two (or zero, but that one doesn't exist).

Now, this is important: The ordering by number of 1's works when there are no x's. In subsequent columns, we must arrange the terms by location of the x's AND number of 1's.

The final terms are the unchecked ones in Table 7B. In the form of a logic equation, we end up with:

Equation 7

Which we can then further factor to:

Equation 8

What implications? Time for a new term. Each of the terms in Equation 7 represents a grouping of adjacent cells. We could as easily have gotten them from a K-map. These terms have a name: prime implicant. That's simply a fancy name for the groups of a K-map.

Having gone from eight minterms and 31 logical operations to six prime implicants and seven logical operations, you should be feeling pretty good about things. You might think that we've arrived at the optimal representation. But you'd be wrong. The reason you'd be wrong is one that would be obvious from a K-map. In fact, let's just show the K-map so you can see what's going on (remember, Q-M doesn't replace the K-map for four inputs; its forte is solving the larger problems).

Page 17: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

Figure 2: Example 2 K-map

From this K-map in Figure 2, it's easy to see the problem: It's not that our solution doesn't give the proper cover; it clearly does. The problem is, it covers too well. Two of the 1's in the region are also covered by other terms�terms that aren't necessary. When we use K-maps, it's easy to see when we've got all the 1's covered. With Q-M, it's not so easy.

What we need is a way to know when we can omit a term. Since the Q-M method is a tabular method rather than a graphical one, it should not surprise you to learn that the solution involves another table.

Let's build a table like Table 8, with the prime implicants in the left-hand column, and the minterms in the row at the top. A check mark in any cell shows that this particular minterm is generated by the corresponding prime implicant.

Now look at the check marks in the columns for 0100 and 1100 (colored blue in the table). These check marks appear only in the bottom row. This means that these particular minterms can only be generated by the prime implicant x10x. It's an essential prime implicant. The adjective, "essential," should tell you all you need to know. We must include this term, or miss the two 1's in the 00 row of the K-map.

But if we must include the x10x prime implicant, we also get, for free, the minterms colored yellow. That, in turn, means we can delete the check marks required for those minterms in other rows. This could possibly free up prime implicants that are no longer needed. It doesn't, in this case, but it could.

I think you can see how things go from here. We can use coloring to process the other prime implicants, or we could redraw the table. If we redraw it, we should eliminate the rows and columns that we've already identified. Table 9 shows the reduced matrix.

We have no other columns with a single check mark. We do, however, have two rows with them, and those minterms are also generated by other prime implicants. Operating on the assumption that more is better, we might as well select, for the next prime implicant, one that has more checks in its row. There are three such choices, and not much to recommend one over the other. However, we note with interest that the prime inplicants 0x11 and x011

Page 18: A primer on Karnaugh maps - Cabrillo College - …snerton/cs23/Links/Karnaugh_QM.pdf ·  · 2011-01-11A primer on Karnaugh maps ... X is a function of four variables, A ... One such

have 11 in the last two bits, which hints at the possibility of factoring. Let's select one of those�0x11�as the next prime implicant. We get the new table, Table 10, by eliminating rows and columns again:

From here, it's not hard to figure out what other prime implicant we should use. It's 10x1, the one that picks up both remaining minterms. We end up with three prime implicants, which cover the desired function:

or

or

Equation 9

So we've reduced the prime implicants from six down to three, and the logical operations to six. That's pretty hard to beat.

Wrapping up Now you've seen the Quine-McClusky algorithm in all its splendor. It may seem tedious, but it's very reliable, and it's really your only option once the number of inputs gets much greater than four. I can't really guarantee you, even now, that the results will be optimal. Part of that conclusion depends on your definition of "optimal." It might mean minimizing the number of prime implicants, as we've done above, but it might not. Likewise, it may or may not mean reducing the number of operations. In the end, it could come down to something like where the traces have to run on the circuit board.

Even so, the Q-M method gets us about as close to a fully automated optimization as we are likely to find. It should not surprise you to learn that many software packages exist that do Q-M optimization. Now you know how they work. Use them, if you have the need, but don't forget that sometimes a little human judgement may still be required.

There may be cases where one set of prime implicants is preferred over another. For example, in the seven-segment decoder that we discussed a few months back, we have seven different outputs, and therefore seven different K-maps or Q-M reductions. To get the "global optimum," we should really give preference to prime implicants that also appear in other output expressions.

I'll leave that as an exercise for the student.

Jack Crenshaw is a senior software engineer at Spectrum-Astro and the author of Math Toolkit for Real-Time Programming, from CMP Books. He holds a PhD in physics from Auburn University. E-mail him at [email protected].