1
\$\begingroup\$

I've been studying mixed arrays. I ran the following code on vcs:

module tb;
  byte stack [2][4] ;
  initial begin
    
    foreach (stack[i])
    
    foreach (stack[i][j]) begin
  
      stack[i][j] = $random;
      
      $display("stack[%0d][%0d] = 0x%0h" , i, j, stack[i][j]);
      
    end
    
    $display("stack = %p" , stack) ;
    
    $display("stack[0][0][2] = 0x%0h" , stack[0][0][2]);
  
  end
endmodule

The console gives the following output :

Compiler version S-2021.09; Runtime version S-2021.09;  Oct 21 11:42 2022

stack[0][0] = 0x24
stack[0][1] = 0x81
stack[0][2] = 0x9
stack[0][3] = 0x63
stack[1][0] = 0xd
stack[1][1] = 0x8d
stack[1][2] = 0x65
stack[1][3] = 0x12

stack = '{'{36, -127, 9, 99}, '{13, -115, 101, 18}} 

stack[0][0][2] = 0x1

Questions:

  1. Why are there negative values in stack {} ?

  2. Why are the items arranged in a group of 4 in stack{} ?

  3. Does declaring the byte data type automatically create an 8-bit packed array?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The byte data type is signed. Refer to IEEE Std 1800-2017, section 6.11 Integer data types:

2-state data type, 8-bit signed integer

It is similar to the following declaration:

bit signed [7:0] stack [2][4] ;

The %p format specifier chooses to display some of the signed values as negative decimal. When I run your code on the Cadence simulator, it displays them in hex notation. Perhaps the behavior is not well defined:

stack = '{'{'h24, 'h81, 'h9, 'h63}, '{'hd, 'h8d, 'h65, 'h12}}

You get 2 groups of 4 because that is how you declared your array.

\$\endgroup\$
0
1
\$\begingroup\$

$random generate a 32-bit random value and you are assigning it to an 8-bit signed byte. It gets truncated--if the resulting bit pattern represents a negative number,, that's what you get.

%p is a pretty-print format that uses an assignment pattern as its format. It is using using a signed decimal format for each number. SystemVerilog has arrays-of-arrays. You have declared an array with two elements, and each element has an array with four elements.

%h is an unsigned radix format.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.