Please explain this SystemVerilog syntax {>>byte{...}}

10,391

Solution 1

The >> operator is not logical shift in this context, but it is called stream operator.

Stream operators determine the order in which blocks of data are streamed: >> causes blocks of data to be streamed in left-to-right order, while << causes blocks of data to be streamed in right-to-left order.

Consider the following lines for example:

$display ("%h",  {>>byte{24'h060708}} );
$display ("%h",  {<<byte{24'h060708}} );

In both, the number 24'h060708 should be first sliced into bytes (called slice_size). The first one prints the bytes from left to right, whereas the second one prints them from right to left. Therefor, the output is:

060708
080706

Now, in line ans = byteq'({>>byte{24'h060708}}); you are using bit-stream casting, which casts 24'h060708 number sliced in bytes represented from left to right into byteq, which is a queue of bytes.

Solution 2

For a full explanation, refer to IEEE Std 1800-2012 § 11.4.14 Streaming operators (pack/unpack)

The slice_size determines the size of each block, measured in bits. If a slice_size is not specified, the default is 1. If specified, it may be a constant integral expression or a simple type. If a type is used, the block size shall be the number of bits in that type. If a constant integral expression is used, it shall be an error for the value of the expression to be zero or negative.

The stream_operator << or >> determines the order in which blocks of data are streamed: >> causes blocks of data to be streamed in left-to-right order, while << causes blocks of data to be streamed in right-to-left order. Left-to-right streaming using >> shall cause the slice_size to be ignored and no re-ordering performed. Right-to-left streaming using << shall reverse the order of blocks in the stream, preserving the order of bits within each block. For right-to-left streaming using <<, the stream is sliced into blocks with the specified number of bits, starting with the right-most bit. If as a result of slicing the last (left-most) block has fewer bits than the block size, the last block has the size of the remaining bits; there is no padding or truncation.

In your case {>>byte{24'h060708} creates the stream {8'h06, 8'h07, 8'h08}. byteq'() is casting the stream into bytes, but it is unnecessary since the stream is already matches the correct size. ans = {>>byte{24'h060708}}; will have the same result. If you change the stream_operator to << the stream would be {8'h08, 8'h07, 8'h06}.

Share:
10,391
mitt2050
Author by

mitt2050

Updated on June 04, 2022

Comments

  • mitt2050
    mitt2050 almost 2 years

    The answer for the following program is {6,7,8} but I don't understand why, please explain a bit:

    module q ();
      typedef byte byteq[$];
      initial begin
        byte ans[$];
    
        ans = byteq'({>>byte{24'h060708}});
        $display("A:expect '{6,7,8} get %p", ans);
      end
    endmodule