K-map

From Turing Complete
Revision as of 07:16, 30 October 2023 by UnsignedRobin (talk | contribs)

What are Karnaugh-Veitch maps?

A Karnaugh-Veitch map is a modified truth table for 4 bits, which allows to derive simplified circuits for this table just by looking at it. So everytime you come across a logic, that has 3 or 4 bits of input, you can plug its truth table into a KV map and build the circuit.

Okay, so how does it look like?

Usually you start with a truth table with 4 inputs and an output column with values that are either {{Off}} or {{On}}. In this example the output values doesn't matter (yet), but notice that i gave them numbers from 0 to 15 that matches their corresponding input bit pattern.

[previewimg=28056693;sizeOriginal;truthtable.png][/previewimg]


A Karnaugh map is a truth table, where each bit combination is represented in 2D form. So the outputs are not a straight line like above, but like a square:

[previewimg=28056815;sizeOriginal;KV.png][/previewimg]⠀

As a careful observer, you may have noticed something strange: The input combinations are not numbered 0-1-2-3 (in binary), but [b]0-1-3-2[/b]!

[quote=Whyman]Why though?![/quote]

The answer: We make sure, that [b]only 1 bit changes in each step[/b]. So if we would go 0-1-2-3, then the step from 1 to 2 in binary would be 01 to 10. [b]Both[/b] bits would change, but we only want 1 bit to change at max. This is why we swap the order of the inputs, but make sure, that we still have all possible combinations available.

[quote=Whyman]BUT WHYYY?! [/quote] Yes, yes... later...

Changing the order of the input also changes the order in which we fill in the outputs. Going from left to right, skipping the third column and going from top to bottom, skipping the third row. The white numbers are the order in which you plug in the output values.

[previewimg=28056874;sizeOriginal,floatLeft;KV-1.png][/previewimg] [previewimg=28056875;sizeOriginal,floatLeft;KV-2.png][/previewimg]

How does this help me in any way?

[b]BLOCKS![/b]

The anwser is blocks.

Let me explain...

Humans are very good at visual pattern recognition (if the layout favors it). By using the [i]only-1-bit-change[/i] trick, we can now combine bits in blocks of size 1,2,4 and 8. When forming blocks, we only focus on the bits! Leaving the bits alone.

Lets say there are the following 2 green output bits that we are interested in:

[previewimg=28057199;sizeOriginal;KV-3.png][/previewimg]


Question is: How can we describe these two bits using the bits from the columns and rows?

[previewimg=28057214;sizeOriginal,floatLeft;KV-4.png][/previewimg] [previewimg=28057267;sizeOriginal;KV-5.png][/previewimg] ⠀

[b]Row:[/b] The bits that we are interested in, are in the row where: [table]

   [tr]
       [td][previewicon=28057314;sizeOriginal,inline;D.png][/previewicon][/td]
       [td][previewicon=28057312;sizeOriginal,inline;C.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=29434865;sizeOriginal,inline;off2_u2.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
   [/tr]

[/table]

[b]Column:[/b] The bits that we are interested in, are in the columns where: [table]

   [tr]
       [td][previewicon=28057310;sizeOriginal,inline;A.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
   [/tr]

[/table] We don't need [previewicon=28057311;sizeThumb,inline;B.png][/previewicon] to describe the two columns, because [previewicon=28057310;sizeThumb,inline;A.png][/previewicon] = already covers both columns.

By putting those together, we can describe the bits as [b]2-bit block[/b]. The bigger the block, the less inputs we need to describe it! So we want to make the blocks as big as possible. To describe a 2-bit block, we need 3 inputs - the 4th does not matter (also a feature of our [i]1-bit-change-only trick[/i]).

[previewimg=28057553;sizeOriginal;KV-6.png][/previewimg]


The block is described by this combined input conditions: [table]

   [tr]
       [td][previewicon=28057314;sizeOriginal,inline;D.png][/previewicon][/td]
       [td][previewicon=28057312;sizeOriginal,inline;C.png][/previewicon][/td]
       [td][previewicon=28057311;sizeOriginal,inline;B.png][/previewicon][/td]
       [td][previewicon=28057310;sizeOriginal,inline;A.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=29434865;sizeOriginal,inline;off2_u2.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
   [/tr]

[/table]

So now we can [b]AND[/b] everything together and the block is done. [previewimg=28057706;sizeOriginal,floatLeft;out1.PNG][/previewimg]

Any more block limitations?

Only the 1,2,4,8 block size rule.

Additional note: The blocks can also [i][b]go around[/b][/i] from left to right, and from top to bottom. So for example, you can do an 8-bit block from left to right (using only one input variable to describe it).

[previewicon=28057828;sizeOriginal,inline;KV-7.png][/previewicon]

This block is described only by one input variable

[table]

   [tr]
       [td][previewicon=28057314;sizeOriginal,inline;D.png][/previewicon][/td]
       [td][previewicon=28057312;sizeOriginal,inline;C.png][/previewicon][/td]
       [td][previewicon=28057311;sizeOriginal,inline;B.png][/previewicon][/td]
       [td][previewicon=28057310;sizeOriginal,inline;A.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=29434865;sizeOriginal,inline;off2_u2.png][/previewicon][/td]
   [/tr]

[/table]

[b]Question for you:[/b] [i]Can you do a block, that covers only the 4 corners?[/i]

So this is it?

Not quite.

Since we can only do 1,2,4 and 8 bit blocks, we have to combine multiple blocks together, to cover all outputs. Each one of them has to be part of a block. Overlapping blocks are no problem, they are actually quite helpful to create big blocks for missing bits.

[h1]Example[/h1] This is our truth table, that we already plugged into our Karnaugh map:

[previewimg=28057944;sizeOriginal;KV-8.png][/previewimg]


Now we have to do blocks of 1,2,4 or 8 (as big as possible) to cover all output bits.

We can do it like this: [previewicon=28057959;sizeOriginal;KV-9.png][/previewicon]

We need 3 blocks to cover all bits: [olist]

   [*]A yellow 2-bit block
   [*]A blue 4-bit block
   [*]A white 4-bit block

[/olist]

Combined they look like this: [table]

   [tr]
       [td][/td]
       [td][previewicon=28057314;sizeOriginal,inline;D.png][/previewicon][/td]
       [td][previewicon=28057312;sizeOriginal,inline;C.png][/previewicon][/td]
       [td][previewicon=28057311;sizeOriginal,inline;B.png][/previewicon][/td]
       [td][previewicon=28057310;sizeOriginal,inline;A.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=28058069;sizeOriginal,inline;yellow.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=28058070;sizeOriginal,inline;blue.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=29434865;sizeOriginal,inline;off2_u2.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
   [/tr]
   [tr]
       [td][previewicon=28058072;sizeOriginal,inline;white.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=28535754;sizeOriginal,inline;gone.png][/previewicon][/td]
       [td][previewicon=28057370;sizeOriginal,inline;on2.png][/previewicon][/td]
   [/tr]

[/table]

To build the blocks, we [b]AND[/b] the inputs and after that we [b]OR[/b] the blocks together

[previewicon=28058186;sizeOriginal,inline;out2.PNG][/previewicon]

And thats it! We created a circuit that describes the truth table completely!

Final notes

The whole idea is based on the fact that a circuit like this:

[code] (SuperComplexExpression AND a) OR (SuperComplexExpression AND NOT a) [/code]

is the same as just

[code] (SuperComplexExpression) [/code]

Because it doesn't matter if A is or - either the first part of the bracket is passed through, or the second part. So if only one bit changes, then we can remove that bit from the equation. (I was asked about that fact in my exam, so maybe it is good to know).

[hr][/hr]

You can do a Karnaugh map for 3 bits, by throwing away the two rows on the bottom and the D input.

[hr][/hr]

You can also do a Karnaugh map for 5 bits, by creating two 4-bit maps. One where the 5th bit is and one where the 5th bit is . Then imagine you lay both maps on top of each other, so you can do additional blocks by piercing through both maps.

[hr][/hr]

The way we collected the bits and put together the blocks is called [i]Disjunctive Normal Form (DNF)[/i] which focuses on the output bits, which are then [b]ORd[/b] together. There is also a [i]Conjunctive Normal Form (CNF)[/i] where you focus on the bits and then [b]AND[/b] the blocks together (a bit more complicated - you have to look it up).

[hr][/hr]

If you have two potential blocks of bits, but the intersection of those two blocks are , then you can [b]XOR[/b] them together instead of [b]OR[/b].

[previewicon=28058474;sizeOriginal,inline;XOR.png][/previewicon]

[hr][/hr]

If you have a [i]chess board pattern[/i] in your KV map, you can just XOR all inputs together.

[hr][/hr]

Now its your turn!

Here is a task for you:

You get a 4 bit number as input, and you have to display the number in decimal. For this purpose there is the 7-Segment display in the sandbox, which has 7 segments that you can turn on and off.

Fill a Karnaugh map for each segment (Seg1 to Seg7) and build a circuit, so that the correct decimal number is displayed for each binary input number. If you want, you can also display 10 as A, 11 as b etc. up to 15 (hex converter). Otherwise the numbers 10-15 do not matter and can be used to build bigger blocks.

In this picture the input number is a binary 6. Build the circuit in the purple box (the box is way to small, i know), so that for the binary 6 the segments 1,6,7,5,4, and 3 are [i]"glowing"[/i]. (And for the rest of the binary numbers as well from 0 to 9). You need a KV map for each segment. Create the "normal" truth table first, and then fill the map, create blocks and OR them together.

[previewimg=28058654;sizeOriginal;task.PNG][/previewimg]

[hr][/hr] Happy wiring! Hope you learned something :)