Where the Right Answer Comes From
A check is only as good as its expected value. Meet the ways a bench can know the right answer: constants, a reference model and rules. Add a scoreboard to hold answers that arrive later. Learn what each costs and what each cannot see.
You Will Be Able To
- Name four ways a bench can know the right answer.
- Give the cost and the blind spot of each.
- Choose a source of truth for a given check.
- Lesson
- B2.3
- Time
- 20 min
- Runs on
- Your browser
- Check
- 5 questions
Four hand-written checks pass, a rule reports twelve failures, and a second model of the adder reports twenty-four. All three looked at the same broken adder. Which one is telling the truth?
- File
- Model
- Test
- Scoreboard
- DUT
- Result
- Analysis
- Data
- A constant
Someone works out the answer by hand. Cheap and clear, but it covers only the cases written down.
- A reference model
A second description of the same behaviour computes the answer for any input.
- A scoreboard
When answers come out later, or in bursts, the expected ones wait in a scoreboard until they can be matched.
- A rule
Some facts must always hold, whatever the inputs. A rule checks one such fact and nothing more.
Read the diagram as text
- Constant: "3 + 4 must be 7", written by hand
- Reference model: a second, simpler description
- Rule: a fact that must always hold
- Scoreboard: keeps expected answers until the real ones arrive
- Design under test: adder4
- Compare: right or wrong
- Constant → Compare: a few cases
- Reference model → Scoreboard: every case
- Scoreboard → Compare: in order
- Rule → Compare: one property
- Design under test → Compare: actual
Where an expected value can come from. Real benches use several at once.
Before You Start
Read The Simplest Test That Can Fail first. It showed that the expected value must be worked out apart from the design. The Parts of a Testbench named the checker. This lesson is about what it compares with.
| Question | |
|---|---|
| 1 | What are the ways a bench can know the right answer? |
| 2 | What does each one cost to build? |
| 3 | What can each one never see? |
| 4 | When do you need a scoreboard? |
| 5 | Why do real benches use several at once? |
The Idea: Every Check Borrows Its Truth
A checker compares the design with an expected value, so the expected value is the truth of the check. If it is wrong, or missing for some inputs, the check is wrong or missing too. Where that truth comes from is a design decision with costs.
There are three basic sources. You can write the answer down by hand, as a constant. You can build a second, simpler description that computes the answer: a reference model. Or you can state a fact that must always hold: a rule. One example is "the low bit of a sum is the XOR of the low bits".
A fourth piece is not a source but a store. When a design answers later, or several answers are in flight, the expected values must wait somewhere until the real ones arrive. That store is a scoreboard.
New Words
| Word | Meaning |
|---|---|
| Reference model | A second, simpler description of the design's behaviour, used to compute expected values. |
| Rule | A fact about the outputs that must always hold, checked on its own. An assertion is a rule written in the language. |
| Assertion | A rule written into the code, checked by the simulator every time it applies. |
| Scoreboard | A store that keeps expected results until the design's real results arrive, then matches them. |
| Blind spot | The kinds of bug a check can never see, whatever the design does. |
Cost and Blind Spot of Each
| Way | Costs | Cannot see |
|---|---|---|
| Constant | Almost nothing per case | Every case nobody wrote down; a wrong hand calculation |
| Reference model | A second design to write and maintain | A misreading of the specification it shares with the design |
| Rule | One line per fact | Any wrong value that still obeys the rule |
| Scoreboard | Storage and matching logic | Nothing by itself: it is only as good as the model feeding it |
No row is free of blind spots. That is why real benches combine them. A model with a scoreboard handles the bulk. Rules guard local facts that must never break. A few constants give cases a reader can check by hand.
A model that shares the designer's misreading is the most dangerous blind spot. If one person writes both from the same notes, both can be wrong in the same way and agree perfectly. That is the second reading of the specification at work again.
The Rules
Rule 1: The expected value is the truth of a check.
A check cannot be better than where its answer comes from.
Rule 2: Constants cover only what was written down.
They are clear and cheap, and blind to every other case.
Rule 3: A reference model covers every input it is given.
It costs a second design, and must be written from the specification, not the RTL.
Rule 4: A rule checks one fact, everywhere.
It points at the place the fact broke, and misses wrong values that obey it.
Rule 5: A scoreboard holds answers across time.
Use one when results come out later, or several are in flight.
Sources for These Rules
Spear and Tumbush (2012), section 1.5: automatic stimulus needs an automatic prediction, generally a scoreboard or a reference model. Section 1.6: the stimulus also goes to a high-level model that predicts the result.
Spear and Tumbush (2012), section 1.10.2: assertions look at signals across a command. Section 1.10.3: the scoreboard predicts results and the checker compares them with the monitor.
Mehta (2018), section 2.1.3: an assertion points at the source of a failure instead of its effect at the outputs.
The Lab: One Adder, Three Truths
The lab checks the buggy adder of the first lesson three ways at once. The reference model is one line: in the bench, i + j is computed by the simulator, not by the adder. Two rules check facts that must hold for every pair. Four constants are written by hand.
if ({cout, sum} !== i + j) m_wrong++; // reference model
if (sum[0] !== (a[0] ^ b[0])) r1++; // rule: the low bit
if (!cout && sum < a) r2++; // rule: never smallerThe ^ is XOR. !cout means "no carry out". Each line adds 1 to its own count when its truth disagrees with the adder.
reference model: 24 of 256 sums wrong
rule "low bit": 0 violations
rule "never smaller": 12 violations
constants: 0 of 4 hand-written cases wrongThe reference model finds all 24 wrong sums: rule 3 at work, since it was given every input. The low-bit rule finds none, because the bug is in bit 3 and bit 0 is always right. The "never smaller" rule finds 12: the wrong sums that came out smaller than a. The other 12 wrong sums are still not smaller than a, so they obey it. That is rule 4's blind spot.
The constants find nothing. The pairs 1 + 1, 3 + 4, 9 + 6 and 15 + 1 never set up the bug. So rule 2 applies: four right answers, and silence about the other 252 pairs.
Why Rules Are Still Worth Writing
If a model finds everything, why write rules? Because a model tells you that an output is wrong, often many cycles after the cause. A rule placed inside the design fires where and when the fact breaks. Debug starts at the cause instead of tracing back from the output.
Rules are also cheap to add and hard to get wrong. A model is a second design and can have its own bugs. Most benches use a model for the answers and rules for the facts that must never break.
Traps
The first trap is a model copied from the RTL. It is quicker to write the model by reading the design's code. Then every misreading in the design is in the model too, and they agree. Write the model from the specification.
The second trap is a scoreboard that never empties. Expected answers wait for real ones; if the design drops one, its expected value waits for ever. At the end of a test, anything still in the scoreboard is an error. How a Test Ends comes back to it.
The third trap is believing a rule's silence. A rule that never fails may never have applied. A rule about a full FIFO says nothing in a run where the FIFO never filled.
Where This Goes Next
This lesson chose a source of truth. These lessons build each one.
| The idea from this page | Where it is taught |
|---|---|
| A rule checked at every clock edge, as an assertion | Assertions: Checks That Travel With the Design |
| Writing rules as assertions | Why Assertions |
| Building a scoreboard for a FIFO | Build the Monitor and Scoreboard |
| Queues, the usual store inside a scoreboard | Queues and Ordering |
| Scoreboard accounting at the end of a test, in UVM | Scoreboard Accounting and Drain Checks |
| How far inside the design a check may look | Black Box, Grey Box, White Box |
Try It
Run the lab and compare the four counts.
Replace the hand-written case 3 + 4 with 3 + 5. Predict the constants line.
Write a third rule of your own, such as "a + 0 is a". Does it catch the bug?
reference model: 24 of 256 sums wrong rule "low bit": 0 violations rule "never smaller": 12 violations constants: 0 of 4 hand-written cases wrongCheck Yourself
Common Mistakes
Wrong Write the reference model by reading the RTL.
The model inherits the design's misreadings, and the two agree when both are wrong.
Fix Write the model from the specification, ideally by someone other than the designer.
Wrong Rely on a few hand-written expected values.
They cover only the cases written down; the bug may need another one.
Fix Use a model for the bulk of cases and keep constants for hand-checkable examples.
Wrong Treat a rule that never fails as proof.
A rule checks one fact, and only when its situation happens.
Fix Combine rules with a model, and check that each rule's situation was reached.
Remember
- The expected value is the truth of a check; a check is never better than its source.
- Constants are cheap and cover only what was written; a model covers every input but costs a second design.
- Rules check one fact everywhere and miss wrong values that obey it; a scoreboard stores answers across time.
- Real benches combine them, and write models from the specification, not the RTL.