B2.1.1 Write a Check That Can Fail

EasyImplement12 minIcarus · browser samples / server checksReview the topic

The starter is a check that can never fail: it says ok to anything. Write the check of a 4-bit adder that works out the right answer itself. It must fail every wrong result, including an unknown bit.

Hardware context: A 4-bit adder with carry in and a decade counter under test. A self-checking test for a regression.

A self-checking test needs a check that can fail. This one watches a 4-bit adder with a carry in: a, b and cin went in, and sum and cout came out. It must say ok only when the adder was right.

Write add_check. Work out the expected result from a, b and cin alone, never from the adder's outputs. Compare all five result bits.

Signals in the Timeline

SignalWidthRole
scenario4input of each check
Full specification
What came outok
{cout, sum} equals a + b + cin1
A wrong sum, or a wrong carry, or both0
Any bit of sum or cout is x or z0: an unknown answer is not a right answer

The bench applies eight results per case and reads ok after each one. The inputs a, b and cin are always known. A case lists its results as a+b+cin->cout:sum.

Examples

Example 1

Eight Right Answers
Input
14+9+1->1:8, 1+1+0->0:2, 4+5+1->0:10, 7+1+0->0:8, 15+13+1->1:13, 1+2+0->0:3, 2+2+1->0:5, 6+13+1->1:4
Output
ok_0 = 1, ok_1 = 1, ok_2 = 1, ok_3 = 1, ok_4 = 1, ok_5 = 1, ok_6 = 1, ok_7 = 1

Every result is right, so ok is 1 eight times. The starter also passes this case, which is why one case is never enough.

Example 2

Wrong Sums Among Right Ones
Input
12+5+0->1:1, 6+4+1->0:0, 11+7+1->1:3, 13+9+1->1:5, 7+13+0->1:4, 4+1+0->0:5, 9+9+1->1:7, 15+2+1->1:2
Output
ok_0 = 1, ok_1 = 0, ok_2 = 1, ok_3 = 0, ok_4 = 1, ok_5 = 1, ok_6 = 0, ok_7 = 1

Three sums are wrong. A real check says 0 for exactly those three.

Example 3

Right Sums with a Wrong Carry
Input
5+7+0->1:12, 9+7+1->1:1, 2+14+0->0:0, 1+6+1->0:8, 9+12+0->1:5, 12+1+1->1:14, 9+13+0->1:6, 12+0+1->1:13
Output
ok_0 = 0, ok_1 = 1, ok_2 = 0, ok_3 = 1, ok_4 = 1, ok_5 = 0, ok_6 = 1, ok_7 = 0

The sums are right but four carries are wrong. A check that compares only the 4-bit sum misses all four.

Constraints and Contract

  • Keep the module name and the ports.
  • ok must settle within 1 ns of a change.

Hints

Hint 1

How wide must the expected value be to hold 15 + 15 + 1?

Hint 2

Join cout and sum with braces to compare them as one number.

Hint 3

Which comparison says "different" when a bit is x?

What the grader checks: Eight cases of eight results each, made by a fixed seed. They hold right answers, wrong sums, wrong carries, an adder that ignores the carry in, and x or z bits. Inputs are always known.

8 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Eight Right Answers

Input
14+9+1->1:8, 1+1+0->0:2, 4+5+1->0:10, 7+1+0->0:8, 15+13+1->1:13, 1+2+0->0:3, 2+2+1->0:5, 6+13+1->1:4
Expected output
ok_0 = 1, ok_1 = 1, ok_2 = 1, ok_3 = 1, ok_4 = 1, ok_5 = 1, ok_6 = 1, ok_7 = 1

Every result is right, so ok is 1 eight times. The starter also passes this case, which is why one case is never enough.

Run samples executes all 3 public checks. Check all tests runs 8 checks on the server.

Icarus · browser samples / server checks