4

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

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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}.

Comments

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.