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 let in a package, a module or a block, and say which variables it binds to.
  • Give a let arguments with defaults, and call it by position or by name.
  • Use one let in 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.

One Let, Three UsersThe let is declared once. An immediate assertion, a concurrent assertion and a coverpoint all expand it in place.
  • Neutral
  • Property
  • Model
  • Structure
call: who calls whom
One let, three usersThe let is declared once. An immediate assertion, a concurrent assertion and a coverpoint all expand it in place. neutral letshort_burst(l,limit = 8) = l <= limit property immediate assert assert(short_burst(len)) property concurrentassert valid |->short_burst(len) model coverpoint short_burst(len, 12) expandedexpandedexpanded
  1. neutral let short_burst(l, limit = 8)= l <= limit
    • expanded → immediate assert
    • expanded → concurrent assert
    • expanded → coverpoint
  2. property immediate assertassert (short_burst(len))
  3. property concurrent assertvalid |-> short_burst(len)
  4. model coverpointshort_burst(len, 12)
  1. One definition

    The let names the expression and its default limit.

  2. 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

letText macrofunction
Scopewhere declaredthe rest of the compilation unitwhere declared
Expanded asone expression, in parenthesestext, as writtena call
$rose or $past insideclock from the place of useclock from the place of usean error, unless the call names its clock or a default clocking applies
Output or ref argumentsnoneany textnot 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

CodeStandardVerilator 5.052 (measured)
let low4(logic [3:0] v) = v;legal: the argument is cast to 4 bitscompile error: unsupported
a let used before its declaration in the same scopeillegalaccepted

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.

RUNVerilator · simulator service
Edit the code, then run it
Expected: t=45 long burst of 12 not flagged long_seen=2

Check Yourself

Predict the outputPredict the output.
module tb;
  logic [3:0] a = 4'd6, b = 4'd3;
  let diff(x, y = 4'd1) = x - y;
  initial $display("%0d %0d %0d", diff(a), diff(a, b), diff(.y(a), .x(4'd9)));
endmodule
ConceptA let in a module reads len. A task in the same module declares its own len and calls the let. Which len is read?
Conceptlet rose_ok(s) = $rose(s); is used in assert property (@(posedge clk) rose_ok(req) |-> gnt);. Which clock does $rose use?
ConceptA text macro LONG(l) is defined as l > 8. Why is negating it with ! in front a bug?
ConceptWhich statement about let is true in IEEE 1800-2023?

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.

Before
`define LONG(l) l > 8
... !`LONG(len) ...
After
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 let names 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 let works in immediate assertions, concurrent assertions and covergroups.
  • Sampled value functions in a let take the clock of the place of use, which a function cannot do.
  • On Verilator 5.052, use untyped arguments and declare each let before its first use.

Practice

Report a problem with this page