C751 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 ∑m(0,1,2,5,6,7,8,9,10,14) written out in full. It has six prime implicants; G uses three of them, B¯C¯+CD¯+A¯BD, 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?

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

  1. Write every minterm and don't-care as an n-character string of 0s and 1s.
  2. 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.
  3. Repeat with the merged strings until no more merges happen. Every string that was never used in a merge is a prime implicant.
  4. 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 ∑m(0,1,5,7) (three inputs).

Show the solution
  1. List the minterms. 000, 001, 101, 111.
  2. Merge pairs. 000 + 001 → 00−; 001 + 101 → −01; 101 + 111 → 1−1.
  3. Try to merge again. None of those merge further (their dashes differ).
  4. Read off the primes. The primes are 00−, −01 and 1−1: A¯B¯, B¯C and AC.

Answer A¯B¯, B¯C 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
  1. Run the machine. Starting in S0, each input moves it to the next state with the output shown:
Input10101101
Next stateS1S2S1S2S1S1S2S1
Output00101001
  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 2n−1 (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?

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.