Skip to content
You Start with one site
Amounts in Start with one site
Electronics · Concept

Testbenches

One idea Intermediate 5 min read
Found this useful?

A teacher marks an examination with an answer sheet. Every answer is compared with the sheet, and the result is a count of mistakes. A testbench does this to a digital design before any hardware exists: it applies input cases, compares each output with the right answer, and reports pass or fail.

Sure: it passedThe test printed PASS.Ask what was applied
Not sure: tests prove nothingThen why does anyone test?Too far
As sure as the casesPASS covers the cases that were applied, and no others.Count them

See it

Predict first

A circuit that adds two numbers has a fault: it gives a wrong answer in 120 of its 256 input cases. A testbench applies the first 16 cases and compares each answer. All 16 are correct, and it prints PASS. What do you know?

See it: 256 input cases and a fault

Cases applied
Faulty cases met
The testbench prints

The design adds a and b, each from 0 to 15. Its fault: the carry, which is the fifth bit of the answer, is not connected and always reads 0. So the answer is wrong whenever a + b is above 15. The testbench applies the cases row by row and compares each answer with a + b.

The idea

A digital design is written as text and later becomes a chip, where a fault is slow and costly to correct. So it is first run in simulation: a computer behaves as the design would, and every signal can be seen.

A testbench is a second piece of code around the design. It applies the inputs (the stimulus), holds the right answer for each case (the expected value), compares the two, and prints one line.

To know how many cases exist, count the input bits:

cases = 2n

n is the number of input bits: two 4-bit numbers are 8 bits, so 28 = 256 cases

Apply all 256: a simulator needs only a moment. For a large design this is impossible: two 32-bit numbers and a carry input have 265 cases, which would take more than 1,000 years at a thousand million cases a second. Engineers then choose cases: the smallest and largest values, those where a carry happens, and many random ones. The lesson on simulation and testbenches writes a testbench for this adder.

One worked number

The question

The adder in the figure is wrong whenever a + b is above 15. The testbench applies the cases row by row: a = 0 with every b, then a = 1, and so on. How many of the 256 cases are wrong, and which case gives the first FAIL?

Show the worked solution, 2 steps
  1. Count the wrong cases row by row

    The row a = 0 has none. The row a = 1 has 1 (b = 15), the row a = 2 has 2, and so on to 15. The sum 1 + 2 + … + 15 is 120, and 120 ÷ 256 is 47% of the cases.

  2. Find the first wrong case in the order

    The first wrong case is a = 1, b = 15, the last of the second row: case 16 + 16 = 32. A testbench with 31 cases prints PASS. Check it in the figure: set 31 cases, then add one.

The mistake to avoid

PASS does not mean that the design is correct. It means that the cases which were applied are correct. The adder in the figure is wrong in 47% of its cases and passes a test of 31. Before you trust a PASS, ask which cases were applied.

A second trap: the expected values must be independent of the design. Computed by a copy of its logic, they hold the same fault and agree with it.

Check yourself

Question 1

The protection logic of a motor drive has 5 inputs of one bit each: over-current, over-temperature, door open, emergency stop and reset. How many input cases must a testbench apply to cover every case?

Question 2

A design has 12 input bits, which is 4,096 cases. Its testbench applies 40 cases and prints PASS. Which statement is true?

Question 3

To save time, an engineer computes the expected values of a testbench with a copy of the code of the design. The design has a fault. The testbench applies every case. What does it print?

Where you meet it

  • The protection logic of a motor driveThe trip on over-current is proven for every input case before the logic is near a motor.
  • A chip sent to a factoryA fault found after manufacture costs months, so the testbench is written together with the design.
  • A controller program for a plantThe same habit under another name: apply the input cases in a simulator before the pump is connected.
Key points
  • A testbench applies input cases, compares each output with an expected value, and prints pass or fail.
  • A design with n input bits has 2n cases. Apply all of them when the number is small.
  • PASS covers only the cases that were applied, and the expected values must be independent of the design.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.