Tutorials

Protocols

Learn More

The local variables in a sequence or properties are similar to local variables in the function or task.

  1. Local variables are dynamically created inside sequence instance and removed at the end of the sequence
  2. Each sequence instance has its copy. Hence, the sequence can not access the local variable declared in another instance.

Why Use a Local Variable?

Without a local variable, a later check reads the signal’s current value. A local variable preserves the value that was sampled earlier, even if the signal changes afterward.

The variable is created separately for each active sequence attempt.

One attempt cannot overwrite the saved value of another attempt.

The variable exists only while its sequence attempt is active.

It can be used only inside the sequence or property scope where it is declared.

Local variable in a sequence example-1

The sequence captures ff_rdata, waits five clocks, and checks whether sram_wdata is one greater than the saved value.

Consider a computing unit that reads data from a flip flop in 1 clock cycle, increments read data by 10, and writes it to the SRAM memory after 5 clock cycles.

sequence seq;
  int tmp_data;
  (##1 ff_rdata, tmp_data = ff_rdata) ##5 (sram_wdata == (tmp_data+1));
endsequence

Suppose the sequence starts at cycle 0. At cycle 1, the first sequence item matches and tmp_data = ff_rdata saves the sampled read data. The ##5 delay then moves the final comparison to cycle 6.

For example, if ff_rdata is 25 when it is captured, tmp_data keeps the value 25. At cycle 6, the sequence expects sram_wdata to be 26 because the code checks tmp_data + 1.

Changing ff_rdata after cycle 1 does not change tmp_data. The comparison uses the saved value, not the later value on the input signal.

Local variable in a sequence example-2

The local variable declared in the sequence is not accessible from another sequence where it is instantiated.

sequence seqA;
  int tmp_data;
  (##1 ff_rdata, tmp_data = ff_rdata) ##5 (sram_wdata == (tmp_data+1));
endsequence

sequence seqB;
  seqA ##2 (d_data == tmp_data); // tmp_data is not accessible even if seqA is instantiated in the seqB.
endsequence

tmp_data belongs to seqA. Writing seqA inside seqB runs the sequence, but it does not make seqA’s local declarations visible in seqB.

Therefore, (d_data == tmp_data) is illegal because tmp_data is outside its scope. Data that must be shared with another sequence should be passed through a supported sequence or property argument instead of directly accessing a private local variable.

Points to Remember

  1. Capture the value in a sequence match item using a comma and an assignment.
  2. Use the saved value in a later expression of the same sequence attempt.
  3. Every active attempt has an independent copy of the local variable.
  4. A local variable is not visible from another sequence that instantiates the declaring sequence.