Skip to content

Procedural Constructs

return Statement

Returns a value to the caller from a function or exec block. Syntax:

return expression ;

  • expression : An optional value to be returned to the caller.

repeat Statement

v1.0.0

Executes procedural_stmt repeatedly for the specified number of iteration. Syntax:

repeat (index_identifier : expression) procedural_stmt

  • index_identifier : An optional index-variable identifier can be specified that ranges from 0 to *iteration* - 1. It is read-only: the loop body must not change it (stated explicitly since PSS 3.1).
  • expression : Specifies iteration by a non-negative integer (e.g., int or bit).
  • procedural_stmt : Iterated by the number of iteration. Not executed if iteration is 0.
1
2
3
4
5
int intVal = 0;

repeat (i : 3) {
    intVal = i;     //  intVal: 0 -> 0 -> 1 -> 2
}

repeat-while Statement

v2.2.0

Executes procedural_stmt repeatedly as long as the expression is true. Syntax:

repeat procedural_stmt while (expression);

  • procedural_stmt : Iterated so long as the expression is true.
  • expression : Evaluates true or false every time after procedural_stmt.
1
2
3
4
5
int intVal = 1;

repeat {
    intVal += 1;    //  intVal: 1 -> 2
} while (intVal < 1);

while Statement

v2.2.0

Executes procedural_stmt repeatedly as long as the expression is true. Syntax:

while (expression) procedural_stmt

  • procedural_stmt : Iterated so long as the expression is true.
  • expression : Evaluates true or false every time before procedural_stmt.
1
2
3
4
5
int intVal = 0;

while (intVal < 3) {
    intVal += 1;    //  intVal: 0 -> 1 -> 2 -> 3
}

foreach Statement

v2.2.0

Executes procedural_stmt for each element of the expression. Syntax:

foreach (iterator_identifier : expression [index_identifier]) procedural_stmt

  • iterator_identifier : An optional iterator-variable identifier with the collection element type, and is an alias of the current element.
  • index_identifier : An optional index-variable identifier can be specified that ranges from 0 to size() - 1 when expression is array-type or list-type, or over all keys when expression is map-type.
  • expression : A collection type (e.g., array, list, map, or set).
  • procedural_stmt : Iterated for the number of size() of the expression.

Warning

  • For set-type, index_identifier should NOT be specified.
  • At least one of iterator_identifier or index_identifier should be specified.
  • Both iterator_identifier and index_identifier should NOT be changed inside procedural_stmt.
Note
  • A colon (:) must be appended after iterator_identifier if it is specified.
  • A pair of square brackets ([]) must be used to contain index_identifier if it is specified.
  • The order of keys is undetermined when expression is map-type.
1
2
3
4
5
6
7
8
array<bit [4], 3> nibbleArray = {4'd4, 4'd5, 4'd6};
bit [4] nibbleVal = 0;
int intVal = 0;

foreach (i : nibbleArray[j]) {
    nibbleVal = i;  //  nibbleVal: 0 -> 4'd4 -> 4'd5 -> 4'd6
    intVal    = j;  //  intVal   : 0 ->   0  ->   1  ->   2
}

if-else Statement

v1.0.0

Executes procedural_stmt_for_true when expression is true. Syntax:

if (expression) procedural_stmt_for_true else procedural_stmt_for_false

  • expression : Evaluates true or false.
  • procedural_stmt_for_true : Executed when the expression is true.
  • procedural_stmt_for_false : An optional procedural statement that will be executed when the expression is false.
1
2
3
4
5
6
7
8
9
int intVal = 3;
bit bitVal = 0;

if (intVal > 0) {
    bitVal = 1;     //  bitVal: 0 -> 1
}
else {
    bitVal = 0;
}

match Statement

v2.2.0

Warning

There is no match form in a constraint block (LRM B.14). For a conditional constraint, use if-else or an implication (->).

Executes procedural_stmt when the corresponding range matches the expression. Syntax:

match (expression) {
[range] : procedural_stmt
[range] : procedural_stmt
...
default : procedural_stmt
}

  • expression : The item to be evaluated.
  • range : An open range list is compared to the expression.
  • procedural_stmt : Executed when expression matches any one in the corresponding range.
  • default : An optional procedural statement that will be executed when no range was matched.

Warning

  • Only one of ranges should be matched.
  • The default is executed if no range was matched. If no range was matched and default is not provided, it is an error.
int intVal = 3;
bit [4] nibbleVal = 0;

match (intVal) {
    [-100..-1]   : nibbleVal = 4'b0001;
    [0..9]       : nibbleVal = 4'b0010; //  nibbleVal: 0 -> 4'b0010
    [10, 12, 14] : nibbleVal = 4'b0100;
    [100..199]   : nibbleVal = 4'b1000;
    default      : nibbleVal = 4'b1111;
}

break Statement

v2.2.0

Continues execution after the enclosing innermost loop construct. Syntax:

break;

Warning

The break statement may only appear within loop statements (repeat, repeat-while, while, or foreach) (LRM 20.7.11), so it cannot be used in a constraint or activity block.

1
2
3
4
5
6
int intVal = 1;

while (intVal > 0) {
    intVal += 1;    //  intVal: 1 -> 2 -> 3
    if (intVal > 2) break;
}

continue Statement

v2.2.0

Continues with the next loop iteration. Syntax:

continue;

Warning

The continue statement may only appear within loop statements (repeat, repeat-while, while, or foreach) (LRM 20.7.11), so it cannot be used in a constraint or activity block.

1
2
3
4
5
6
int intVal = 0;

repeat (i : 3) {
    if (i < 2) continue;
    intVal = i;     //  intVal: 0 -> 2
}

randomize Statement

Not supported yet v2.1

Randomizes the specified data attributes or variables.

Syntax:

randomize variable, .., variable with constraints

  • variable: A set of variables of plain-data type to be randomized together.
  • constraints: Optional constraint rules.
1
2
3
4
5
6
bit [4] nibbleVal_0 = 0;
bit [4] nibbleVal_1 = 0;

exec post_solve {
    randomize nibbleVal_0, nibbleVal_1 with {nibbleVal_0 < nibbleVal_1;}
}

exec Block

v1.0.0

1
2
3
4
5
int intVal = 0;

exec body {
    intVal = 2; //  intVal: 0 -> 2
}