Skip to content

Primary Expressions

Bit-selects

v2.2.0

Used to select a particular bit from an integer variable or constant by specifying an index.

Get a bit of a variable:

1
2
3
bit [4] nibbleVal = 4'b0011;

bit bitVal = nibbleVal[1];      //  bitVal: 0 -> 1

Assign a bit of a variable:

1
2
3
bit [4] nibbleVal = 4'b0011;

nibbleVal[1] = 0;               //  nibbleVal: 0b0011 -> 0b0001

Assign a bit of a variable by a non-zero integer:

1
2
3
bit [4] nibbleVal = 4'b0011;

nibbleVal[2] = 2'b10;           //  nibbleVal: 0b0011 -> 0b0111

Operate on a bit of a variable with another bit:

1
2
3
bit [4] nibbleVal = 4'b0011;

nibbleVal[2] |= nibbleVal[1];   //  nibbleVal: 0b0011 -> 0b0111

Illegal: Access out-of-bounds
1
2
3
4
5
bit [4] nibbleVal = 4'b0011;

bit bitVal = nibbleVal[10];     //  ILLEGAL (1)
nibbleVal[10] = 1;              //  ILLEGAL (2)
nibbleVal[-1] = 1;              //  ILLEGAL SYNTAX (3)
  1. The index is not smaller than the bit-width of the variable.
  2. The index is not smaller than the bit-width of the variable.
  3. The index should NOT be negative.

Warning

The index should be a non-negative integer and should NOT access out-of-bounds.


Part-selects

v2.4.0

Used to select a fixed range of contiguous bits of a variable by specifying dual-bound indexes.

Get 2 bits of a variable:

1
2
3
4
bit [4] nibbleVal = 4'b0011;

bit [2] bitVal = nibbleVal[1:0];    //  bitVal: 0b00 -> 0b11
bit [2] bitVal = nibbleVal[0:1];    //  ILLEGAL SYNTAX (1)

  1. The LSB of the index shall be smaller than the MSB.

Assign 2 bits of a variable:

1
2
3
bit [4] nibbleVal = 4'b0011;

nibbleVal[1:0] = 2'b00;             //  nibbleVal: 0b0011 -> 0b0000

Operate on 2 bits of a variable with another 2 bits:

1
2
3
bit [4] nibbleVal = 4'b0011;

nibbleVal[3:2] |= nibbleVal[1:0];   //  nibbleVal: 0b0011 -> 0b1111

Illegal: Access out-of-bounds
1
2
3
4
5
bit [4] nibbleVal = 4'b0011;

bit [2] bitVal = nibbleVal[4:3];    //  ILLEGAL (1)
nibbleVal[4:3] = 2'b01;             //  ILLEGAL (2)
nibbleVal[0:-1] = 2'b01;            //  ILLEGAL SYNTAX (3)
  1. The MSB of the index is not smaller than the bit-width of the variable.
  2. The MSB of the index is not smaller than the bit-width of the variable.
  3. The LSB of the index should NOT be negative.

Warning

The indexes should be non-negative integers and should NOT access out-of-bounds.
The MSB of the index should be larger than the LSB of the index.


Selecting an element from a collection (indexing)

v2.2.0

Used as the index operator [] of array, list, and map collections.

Get an element of a collection (e.g., array, list, map):

1
2
3
4
5
6
7
array<bit [4], 2>      nibbleArray = {       4'b1100, 4'b0110};
list <bit [4]>         nibbleList  = {       4'b1100, 4'b0110};
map  <string, bit [4]> nibbleMap   = {"ONE": 4'b1100         };

bit [4] nibbleArrayVal = nibbleArray[0]  ;  //  nibbleArrayVal: 0 -> 0b1100
bit [4] nibbleListVal  = nibbleList[1]   ;  //  nibbleListVal : 0 -> 0b0110
bit [4] nibbleMapVal   = nibbleMap["ONE"];  //  nibbleMapVal  : 0 -> 0b1100

Assign an element of a collection (e.g., array, list, map):

1
2
3
4
5
6
7
8
array<bit [4], 2>      nibbleArray = {       4'b1100, 4'b0110};
list <bit [4]>         nibbleList  = {       4'b1100, 4'b0110};
map  <string, bit [4]> nibbleMap   = {"ONE": 4'b1100         };

nibbleArray[0]   = 4'b0011;  //  nibbleArray[0]  : 0b1100 -> 0b0011
nibbleList[0]    = 4'b0011;  //  nibbleList[0]   : 0b1100 -> 0b0011
nibbleMap["ONE"] = 4'b0001;  //  nibbleMap["ONE"]: 0b1100 -> 0b0001
nibbleMap["TWO"] = 4'b0110;  //  nibbleMap["TWO"]: {} -> 0b0110 (1)

  1. map will add the element if it does not exist.

Operate on an element of a collection (e.g., array, list, map):

1
2
3
4
5
6
7
array<bit [4], 2>      nibbleArray = {       4'b1100,        4'b0110};
list <bit [4]>         nibbleList  = {       4'b1100,        4'b0110};
map  <string, bit [4]> nibbleMap   = {"ONE": 4'b1100, "TWO": 4'b0110};

nibbleArray[0]   |= nibbleArray[1]  ;  //  nibbleArray[0]  : 0b1100 -> 0b1110
nibbleList[0]    |= nibbleList[1]   ;  //  nibbleList[0]   : 0b1100 -> 0b1110
nibbleMap["ONE"] |= nibbleMap["TWO"];  //  nibbleMap["ONE"]: 0b1100 -> 0b1110

Illegal: Access out-of-bounds
1
2
3
4
5
6
7
array<bit [4], 2>      nibbleArray = {       4'b1100, 4'b0110};
list <bit [4]>         nibbleList  = {       4'b1100, 4'b0110};
map  <string, bit [4]> nibbleMap   = {"ONE": 4'b1100         };

bit [4] nibbleArrayVal = nibbleArray[2]  ;  //  ILLEGAL (1)
bit [4] nibbleListVal  = nibbleList[2]   ;  //  ILLEGAL (2)
bit [4] nibbleMapVal   = nibbleMap["TWO"];  //  ILLEGAL (3)
  1. The index must be within the size.
  2. The index must be within the size.
  3. The element must exist in the map.

Usage

Access a bit of an array
1
2
3
4
array<bit [4], 2> nibbleArray = {4'b1100, 4'b0110};

bit bitOnArray_0_2 = nibbleArray[0][2]; //  bitOnArray_0_2: 0 -> 1
nibbleArray[1][0] = 1;                  //  nibbleArray[1]: 0b0110 -> 0b0111