Frank Heckenbach wrote:
CBFalconer wrote:
I think you will find that:
IF a AND b THEN c ELSE IF (NOT a) AND (NOT b) AND d THEN e; or IF a AND b THEN c ELSE IF (NOT (a OR b)) AND d THEN e;
with the parentheses for clarity, covers it exactly, including all do-nothing cases.
But it duplicates a. That is a problem if it has side-effects as Dave pointed out, and if it's a longish expression, it's at least more error-prone.
Of course, you can use a temp variable. But then again, the assignment necessary is an additional statement, and might even require another `begin'/`end' (if otherwise the `if' statement was the only one within a loop or conditional).
I have no problems answering the question 'when is c executed' or 'when is e executed' with the above. With the original I do (have a problem).
Seems so, since you apparently misread the condition for e. It should be `if not a and d then e' (no mention of b), just like Dave said.
But I can't really understand why you find reading the original code harder. To me it clearly reads:
if a [...] else if d then e
(where the indentation makes it clear which `else' belongs to what) which is clearly the same as
if not a and d then e
A Karnaugh map is a very useful thing. It may show that the NOT b term is unnecessary in some applications. Or other economies. In some cases I could conceive of:
IF b THEN c ELSE IF d THEN e;
being satisfactory, even though it doesn't match the original logic, simply by mapping explicit do-nothings and don't cares into a Karnaugh map. This sort of thing can show the uselessness of a, and thus lead to major snippage in the original source. Which is an important form of optimization.
Well, I don't know much about Karnaugh maps, but in this example I assume that a is not useless (otherwise the example would be off). So, that doesn't really address the question at hand.
c and e are outcomes, blank is no action, X is don't care
This is the map that I (mis)filled for the original action:
.... a .... ------------------------- | | | c | | ------------------------- d | e | | c | | ------------------------- .... b ....
which should have been:
.... a .... ------------------------- | | | c | | ------------------------- d | e | e | c | | ------------------------- .... b .... giving: IF a AND b THEN c ELSE IF NOT a AND d THEN e;
They allow you to see the minimum conditions for any logical action quickly, and can easily be extended to 4 and 5 variables. Any more is not convenient. Don't care X marks can simplify the logic, because you have the option to include or not include such a location in an expression. X's usually result from input conditions that cannot arise, such as 1010 in a binary coded decimal counter. Then the map would look like:
.... 1 .... ------------------------- | 0 | 2 | 3 | 1 | ------------------------- . | 4 | 6 | 7 | 5 | 4 ------------------------- . | | | | | . ------------------------- 8 | 8 | | | 9 | . ------------------------- .... 2 ....
where the 1, 2, 4, 8 are the weights of the counter bits. The interior values are the state of the counter, in 0..9. In this case all the blank entries are don't cares. By combining desired states with don't cares it is obvious that the 8 and 9 states can be detected with 2 bits only, the 0 and 1 states require 4, and that all others require 3 bits. For example, combining the 4 state with an adjacent blank spans the 8 bit transition, which is thus NOT required in detecting that state.
Note the Gray coding style of the map. The state in each square is 1 bit different from each adjacent square. The map rolls around, both vertically and horizontally.
End of quick course in Karnaugh maps and logical reductions thereby :-)