Let Declarations
Name an expression once with let and use it in immediate assertions, concurrent assertions and covergroups, with arguments, defaults and its own scope.
You Will Be Able To
- Declare a
letin a package, a module or a block, and say which variables it binds to. - Give a
letarguments with defaults, and call it by position or by name. - Use one
letin an immediate assertion, a concurrent assertion and a covergroup. - Choose between
let, a text macro and a function.
- Lesson
- A7.2
- Time
- 25 min
- Runs on
- Verilator
- Check
- 5 questions
Three engineers wrote "the burst is too long" three times. One used a macro, one a function, and one pasted the expression into a covergroup. When the limit changed from 8 to 12, one copy was missed. A let names the expression once, in a scope, and every user reads the same definition.
- Neutral
- Property
- Model
- Structure
- One definition
The
letnames the expression and its default limit. - Three expansions
Each use is replaced by the body in parentheses, with the arguments put in.
Read the diagram as text
- let short_burst(l, limit = 8): = l <= limit
- immediate assert: assert (short_burst(len))
- concurrent assert: valid |-> short_burst(len)
- coverpoint: short_burst(len, 12)
- let short_burst(l, limit = 8) → immediate assert: expanded
- let short_burst(l, limit = 8) → concurrent assert: expanded
- let short_burst(l, limit = 8) → coverpoint: expanded
IEEE 1800-2023 11.12: the body is expanded at each use, in parentheses.
Where a Let Lives, and What It Binds To
A let may be declared in a package, module, interface, program or checker. It may also sit in a clocking block, a generate block, a begin-end block or a subroutine. It is visible from there down, like a variable.
Names in the body that are not arguments bind where the let is declared, not where it is used. A block variable with the same name does not capture it.
Arguments, Defaults and Types
Arguments work like a function call: by position or by name. An argument may have a default, used when the call leaves it out.
An argument may also have a type. The actual value is then cast to that type before it is put in. Verilator 5.052 rejects typed arguments, so here the cast is written in the body.
Standard (IEEE 1800-2023 11.12)
let low4(logic [3:0] v) = v;
// low4(8'hA5) is 4'h5: the cast keeps 4 bits
Legal. The tool reports "Unsupported: let typed ports".
Runs on Verilator 5.052
let low4(v) = v[3:0];
// low4(8'hA5) is 4'h5
An untyped argument and a slice in the body give the same value here.
Let in Immediate Assertions
A let is an expression, so an immediate assertion can use it like any other condition. It reads the current values, as the assertion does.
Let in Concurrent Assertions
In a concurrent assertion the body reads sampled values. A sampled value function in the body, such as $rose, takes its clock from the assertion that uses the let.
Let in Covergroups
A coverpoint expression and an iff guard may use a let too. The covergroup then counts the same condition the assertions check.
Let, Macro or Function
| let | Text macro | function | |
|---|---|---|---|
| Scope | where declared | the rest of the compilation unit | where declared |
| Expanded as | one expression, in parentheses | text, as written | a call |
$rose or $past inside | clock from the place of use | clock from the place of use | an error, unless the call names its clock or a default clocking applies |
| Output or ref arguments | none | any text | not allowed in assertions |
IEEE 1800-2023 11.12, 16.6 and 16.9.3.
A macro is pasted as text. With ! in front, only the first operand is negated.
In a function body there is no clock to infer, so a plain $rose(s) there is an error. It is legal when the call names its clock, $rose(s, @(posedge clk)), or a default clocking applies. Verilator 5.052 reports "Concurrent assertion has no clock" for the plain form. In a let the plain form works.
Where the Tool and the Standard Part
| Code | Standard | Verilator 5.052 (measured) |
|---|---|---|
let low4(logic [3:0] v) = v; | legal: the argument is cast to 4 bits | compile error: unsupported |
a let used before its declaration in the same scope | illegal | accepted |
IEEE 1800-2023 11.12. Measured by tools/probes/sva-w1b/departures.mjs.
On this site, declare each let before its first use and write casts in the body.
Try It
One let in a package serves two assertions. The immediate one counts the long bursts at 25 and 35. The concurrent one checks that each long burst is followed by err: the one at 35 is not.
A coverpoint could use short_burst(len, 4'd12) in the same way. Change the default limit to 4'd10 and predict both outputs again.
t=45 long burst of 12 not flagged long_seen=2Check Yourself
Common Mistakes
Wrong Wrap a condition in a text macro and negate the macro.
The macro is pasted as text, so operator precedence splits the condition.
Fix Use a let, which expands as one parenthesised expression.
`define LONG(l) l > 8
... !`LONG(len) ...let long_burst(l) = l > 8;
... !long_burst(len) ...Wrong Put $rose or $past inside a function used by an assertion.
A function body has no clock to infer, so the call is an error.
Fix Put the sampled value function in a let; it takes the clock of the assertion.
Wrong Expect a let to read a local variable of the block that calls it.
Free names in the body bind where the let is declared.
Fix Pass the value you want as an argument.
Remember
- A
letnames an expression in a scope; it expands at each use, in parentheses. - Arguments may have defaults and may be passed by name; names in the body bind at the declaration.
- The same
letworks in immediate assertions, concurrent assertions and covergroups. - Sampled value functions in a
lettake the clock of the place of use, which a function cannot do. - On Verilator 5.052, use untyped arguments and declare each
letbefore its first use.