1

I am new to SystemVerilog. I started learning from this website. I read the following example, and did not understand the result.

//EXAMPLE: arrays 

module arr; 
bit [2:0][3:0] arr [4:0][5:0]; 

initial 
begin 
  $display(" $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",$left(arr),$right(arr),$low(arr),$high(arr),$increment(arr),$size(arr),$dimensions(arr) ); 
end 
endmodule 

Results:

$left 4 $right 0 $low 0 $high 4 $increment 1 $size 5 $dimensions 4

Please explain the result.

1 Answer 1

3

A SystemVerilog multi-dimensional array has two types of dimension: packed and unpacked. There can be any number of packed dimensions (written before the variable name) and any number of unpacked dimensions (written after the variable name). The dimensions are numbered from 1, left to right, unpacked dimensions first. So, when indexing the elements of the array, the first unpacked dimension becomes the most significant index (written first) and the last packed dimension becomes the least significant index (written last), leading to a strange ordering. This is not as daft as it may first appear, because it is possible to leave indexes for packed dimensions off the end of the list.

So, in you example, the dimensions are numbered like this:

bit [2:0][3:0] arr [4:0][5:0];
      3    4         1    2

There are the following Array Querying Functions in System Verilog:

$left/$right(arrayname, dimension) – returns the left or right bound
$low/$high(arrayname, dimension)   – returns the lowest or highest bound
$increment(arrayname, dimension)   – returns 1 if $left >= $right and -1 otherwise
$size(arrayname, dimension)        – returns the number of elements of the given dimension
$dimensions(arrayname)             – returns the number of dimensions
$unpacked_dimensions(arrayname)    – returns the number of unpacked dimensions 

When you call a system function such as $left without specifying which dimension you mean, it defaults to dimension 1. Hence the output you see; the system functions are outputting the values for dimension 1, because the dimension is not specified. You can also specify a dimension in these system functions. So, this code:

module arr; 

  bit [2:0][3:0] arr [4:0][5:0]; 

  initial begin 
    $display(" DIMENSION 1 (the default) : $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",
               $left(arr),$right(arr),$low(arr),$high(arr),$increment(arr),$size(arr),$dimensions(arr) ); 
    $display(" DIMENSION 1 (specified)   : $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",
               $left(arr,1),$right(arr,1),$low(arr,1),$high(arr,1),$increment(arr,1),$size(arr,1),$dimensions(arr) ); 
    $display(" DIMENSION 2 (specified)   : $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",
               $left(arr,2),$right(arr,2),$low(arr,2),$high(arr,2),$increment(arr,2),$size(arr,2),$dimensions(arr) ); 
    $display(" DIMENSION 3 (specified)   : $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",
               $left(arr,3),$right(arr,3),$low(arr,3),$high(arr,3),$increment(arr,3),$size(arr,3),$dimensions(arr) ); 
    $display(" DIMENSION 4 (specified)   : $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",
               $left(arr,4),$right(arr,4),$low(arr,4),$high(arr,4),$increment(arr,4),$size(arr,4),$dimensions(arr) ); 
    end 

endmodule 

http://www.edaplayground.com/x/4NzY

Outputs this:

DIMENSION 1 (the default) : $left 4 $right 0 $low 0 $high 4 $increment 1 $size 5 $dimensions 4
DIMENSION 1 (specified)   : $left 4 $right 0 $low 0 $high 4 $increment 1 $size 5 $dimensions 4
DIMENSION 2 (specified)   : $left 5 $right 0 $low 0 $high 5 $increment 1 $size 6 $dimensions 4
DIMENSION 3 (specified)   : $left 2 $right 0 $low 0 $high 2 $increment 1 $size 3 $dimensions 4
DIMENSION 4 (specified)   : $left 3 $right 0 $low 0 $high 3 $increment 1 $size 4 $dimensions 4
Sign up to request clarification or add additional context in comments.

8 Comments

thanks Matthew. Tell me, can you reccomend me on a systemVerilog book (which teach from begining)?
@sara8d I can't recommend a SystemVerilog book, I'm afraid.
@ Matthew Taylor- lol. Don't be:) I won't count only on you:) Do you familiar with"SystemVerilog for Verification: A Guide to Learning the Testbench Language Feature"?
@ Matthew Taylor- all this function return the index of bound, not the value?
@sara8dYes - these functions return information about the size of the array, not the contents. The $increment is a funny one: it returns 1 if the array bounds decrement and -1 if they increment. All those in your example decrement, because the left hand bound is higher than the right.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.