Jump to content

ISSUE with 11.4.14 Streaming operators (pack/unpack) if the size of source not even


Recommended Posts

Hello All,

module top;


initial begin
bit data_source [];
byte unsigned pack_data[];

data_source = new[3];
pack_data = new[2];

for(int i = 0 ; i < (data_source.size()) ; i++)begin
    if ((i %2) == 0)data_source[i] = 1'b1;
    $display("INITIALIZED VALUE OF DATA SOURCE [%d] = 0x%x",i,data_source[i]);
end

foreach(pack_data[i])$display("\nUNINITIALIZED VALUE OF PACKED ARRAY[%d] = 0x%x",i,pack_data[i]);

// Casting Little endianess
pack_data = {<<{data_source}};
foreach(pack_data[i])$display("\nVALUE OF {LITTLE ENDIAN} PACKED ARRAY[%d] = 0x%x",i,pack_data[i]);

// Casting BIG endianess
pack_data = {>>{data_source}};
foreach(pack_data[i])$display("\nVALUE OF {BIG ENDIAN} PACKED ARRAY[%d] = 0x%x",i,pack_data[i]);

end

endmodule:top

generates the following results:

 

Both the little endian and big endian results same ???

INITIALIZED VALUE OF DATA SOURCE [          0] = 0x1
INITIALIZED VALUE OF DATA SOURCE [          1] = 0x0
INITIALIZED VALUE OF DATA SOURCE [          2] = 0x1

UNINITIALIZED VALUE OF PACKED ARRAY[          0] = 0x00
UNINITIALIZED VALUE OF PACKED ARRAY[          1] = 0x00

VALUE OF {LITTLE ENDIAN} PACKED ARRAY[          0] = 0xa0

VALUE OF {BIG ENDIAN} PACKED ARRAY[          0] = 0xa0

but if keeping data_source = new[any even number]; // lets say keeping it 4

 

the following results looks fine 

VALUE OF {LITTLE ENDIAN} PACKED ARRAY[          0] = 0x50

VALUE OF {BIG ENDIAN} PACKED ARRAY[          0] = 0xa0

Can anybody help me with this ??

 

Thanks ,

KS

Link to comment
Share on other sites

I think the key sentence is

 

"If the target represents a dynamically sized variable, such as a
queue or dynamic array, the variable is resized to accommodate the entire stream. If, after resizing, the
variable is larger than the stream, the stream is left-aligned and zero-filled on the right."

 

So in your first case, the resulting stream is 101 (length 3) whether you stream it >> or <<. You then assign it to a dynamic array of byte. You therefore end up with

10100000

in a dynamic array is size 1.

 

In the case of data source 4, you start with the pattern

data_source [0] = 1, data_source[1] = 0, data_source[2] = 1, data_source [3] = 0

 

When you stream that with << or >> you get two four bit values, either

 

0101 or

1010

 

When you assign these to the dynamic array you get either

 

01010000

10100000

 

Q.E.D

 

regards

Alan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...