C7 · Lesson 51 · Logic lab
Truth tables, prime implicants and state machines in Python
Karnaugh maps work by eye up to four variables; past that, and inside every synthesis tool, logic is minimized by algorithms. The classic one, Quine–McCluskey, finds every prime implicant by repeatedly merging terms that differ in one bit, which is exactly what circling neighbors on a map does. In this lab you'll write a truth-table generator, the prime-implicant search, and a simulator that runs a state machine from its table, and check them against the circuits of this course.
- F is written out in full. It has six prime implicants; G uses three of them, , which together cover every minterm.
- The truth table confirms F and G agree in all 16 rows. Your prime-implicant function will find all six.
Predict
Quine–McCluskey writes each term as a pattern of 0, 1 and −. Which two patterns can merge?
01−1 and 00−1: they have their dashes in the same place and differ in exactly one other bit, so they merge into 0−−1. Merging two terms that differ in one variable removes that variable, just like circling two neighboring groups on a map.
Name it
Truth tables in code
Represent an n-input function as a Python function of n bits. Row r (counting from 0) has bit i of r as input i, with the first input as the top bit: bits = [(r >> (n - 1 - i)) & 1 for i in range(n)].
Quine–McCluskey
- Write every minterm and don't-care as an n-character string of 0s and 1s.
- Compare every pair. Two strings merge if their dashes are in the same places and they differ in exactly one other position; the merged string has a dash there. Mark both as used.
- Repeat with the merged strings until no more merges happen. Every string that was never used in a merge is a prime implicant.
- Drop prime implicants that cover only don't-cares.
Choosing the fewest prime implicants that cover all the minterms is a separate step (lesson 14's essential-first method, or a search).
Running a state machine
A Mealy machine's table maps (state, input) to (next state, output). Simulating it is a loop: look up the current state and input, record the output, and move to the next state.
Worked example
Find the prime implicants of (three inputs).
Show the solution
- List the minterms. 000, 001, 101, 111.
- Merge pairs. 000 + 001 → 00−; 001 + 101 → −01; 101 + 111 → 1−1.
- Try to merge again. None of those merge further (their dashes differ).
- Read off the primes. The primes are 00−, −01 and 1−1: , and AC.
Answer , and AC.
Worked example
Run the Mealy 101 detector (states S0, S1, S2; output 1 on S2 with input 1) on 1, 0, 1, 0, 1, 1, 0, 1.
Show the solution
- Run the machine. Starting in S0, each input moves it to the next state with the output shown:
| Input | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|
| Next state | S1 | S2 | S1 | S2 | S1 | S1 | S2 | S1 |
| Output | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 1 |
- Compare with Moore. The outputs, 0, 0, 1, 0, 1, 0, 0, 1, come one cycle earlier than the Moore version would flag them.
Answer the outputs are 0, 0, 1, 0, 1, 0, 0, 1.
Check
A pattern in a 5-variable Quine–McCluskey table has 1 dashes. How many literals does its product term have?
Check
How many minterms does a pattern with 1 dashes cover?
Check
Write truth_table(f, n): call f with a tuple of n bits for each row 0 to (first bit as the top bit) and return the list of outputs as 0s and 1s.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write prime_implicants(minterms, n, dontcares=()): the Quine–McCluskey prime implicants as a sorted list of strings of 0, 1 and -, leaving out any that cover only don't-cares.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write run_mealy(table, start, xs): table maps (state, x) to (next_state, output). Starting in start, return the list of outputs for the inputs xs.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why do the prime implicants found by merging pairs match the groups you'd circle on a Karnaugh map?
Two minterms are neighbors on a Karnaugh map exactly when their binary codes differ in one bit, and circling them removes that variable. Quine–McCluskey's merge rule is the same test: two patterns combine when they differ in one position, and the merged pattern puts a dash, a removed variable, there. Merging merged patterns builds groups of 4, 8 and so on, the same rectangles the map shows. A pattern that never merges with anything is a group that can't be doubled, which is the definition of a prime implicant. The algorithm just does the circling systematically, for any number of variables.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.