# Streaming into dynamic arrays

**URL:** <https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263>\
**Category:** SystemVerilog\
**Tags:** Streaming-Operator, SystemVerilog, Dynamic-Array\
**Created:** [June 30, 2017, 7:42pm UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263 "2017-06-30T19:42:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![theketi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/theketi/32/2054_2.png) [@theketi](https://verificationacademy.com/forums/u/theketi)\
**Post date:** [June 30, 2017, 7:42pm UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/1 "2017-06-30T19:42:18Z")

</div>

Hi

I have a 32bit dynamic array that i want to stream into a dynamic array of bytes.

```verilog
bit [7:0] byte_array[];
bit [31:0] reg_array[];

```

i want to make sure that LSB element reg\_array[0] is assigned to byte\_array[4:0]

```verilog

 reg_array[0] = 01020304;
 reg_array[1] = 05060708;

```

when i do this :

```verilog
byte_array = {>>byte{reg_array}};

```

the order of the values internally is preserved but  
byte\_array = 01020304 05060708

and

```verilog
byte_array = {<<byte{reg_array}};

```

results in  
byte\_array = 08070605 04030201

i want byte\_array to be : 05060708 01020304

Please help me achieve this.

Thanks

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 30, 2017, 8:02pm UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/2 "2017-06-30T20:02:20Z")

</div>

_In reply to [theketi](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263):_  
Try

```verilog
    byte_array = {<<32{reg_array}};

```

---

<div class="post-metadata">

**Author:** ![theketi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/theketi/32/2054_2.png) [@theketi](https://verificationacademy.com/forums/u/theketi)\
**Post date:** [June 30, 2017, 8:18pm UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/3 "2017-06-30T20:18:41Z")

</div>

Thanks a lot! It worked.

I am not clear how this works though , as we are parsing in blocks of 32 bits at a time and the LHS side needs a byte

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 30, 2017, 9:09pm UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/4 "2017-06-30T21:09:36Z")

</div>

\*In reply to [theketi](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/3):\*Think of three separate operations with 2 intermediate streams of bits

1. The individual reg\_array elements get concatenated into a single stream
2. Apply the transformation based on the streaming operator arguments from the first intermediate stream to the second.
3. Assign the second stream to a concatenation of byte\_array elements.

In pseudo code

```verilog
stream1 = {reg_array[0], reg_array[1]};

stream2 = sreamop(stream1); // skip this step for left >> right streaming

{byte[0], byte[1], ...} = stream2;

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 1, 2017, 3:45pm UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/5 "2017-07-01T15:45:43Z")

</div>

_In reply to [sharvil111](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/5):_

I do not think you got my point about the intermediate streams. Think of each stream as an dynamic array of bits sized to the number of bits in the RHS expression; 64 in this case.

Regardless of the specific streaming operator, we create stream1 the same way; as a concatenation of the RHS.

```verilog
{stream1[0], stream1[1], ... stream1[62], stream1[63]} = {reg_array[0], reg_array[1]};

```

For any kind of left-to-right streaming operator (as well as a bit-stream cast), the resulting stream2 is identical to stream1.

```verilog
for(int i=0;b<$bits(stream1);i++) stream2*= stream1[i];

```

Let me hold of on what happens on a right-to-left streaming operator and explain what happens after any resulting stream2. That gets concatenated into the variables on the LHS.

```verilog
{byte_array[0], byte_array[1],... byte_array[7]} = {stream2[0], stream2[1], ... stream2[62], stream2[63]}

```

So you should understand by now that the first and last steps happen the same way for any kind of streaming operator.

Let us go back to the right-to-left streaming operator, where stream2 = {\<\<{stream1}}. With no [I]slice\_size\* specified, bits from stream1 are taken one at a time from right to left into stream2 left-to-right. In another word - reversed.

```verilog
for(int i=0,j=$bits(stream1)-1;i<$bits(stream1);i++,j--)
     stream2*= stream1[j];

```

With a [I]slice\_size\* greater than one, bits are still taken from right–to-left, but in left-to-right(or un-reversed) blocks. So the pseudo-code for stream2 = {\<\<slice\_size{stream1}} would look something like

```verilog
for(int I=0,j=$bits(stream1)-1;i<$bits(stream1);I+=slize_size,j-=slice_sice) 
       stream2[i+:slice_size] = stream1[j-:slice_size];

```

---

<div class="post-metadata">

**Author:** ![sharvil111](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/sharvil111/32/3790_2.png) [@sharvil111](https://verificationacademy.com/forums/u/sharvil111)\
**Post date:** [July 2, 2017, 4:40am UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/6 "2017-07-02T04:40:57Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/6):_  
Thanks Dave for the clear explanation. Now I got it in the correct way. It’s about the way how the bits/slices gets arranged in stream2 that is making all the difference.

> With a slice\_size greater than one, bits are still taken from right–to-left, but in left-to-right(or un-reversed) blocks. So the pseudo-code for stream2 = {\>\>slice\_size{stream1}} would look something like

Just to be on the same page, I believe that here right to left operator {\<\< _slice\_size_{stream1}} will be there. Please correct me if I am wrong.

Thanks again!!

---

<div class="post-metadata">

**Author:** ![aimunteanu](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@aimunteanu](https://verificationacademy.com/forums/u/aimunteanu)\
**Post date:** [July 6, 2017, 10:56am UTC](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/7 "2017-07-06T10:56:53Z")

</div>

_In reply to [sharvil111](https://verificationacademy.com/forums/t/streaming-into-dynamic-arrays/33263/7):_

You might want to take a look at these 2 tutorials about packing and unpacking using streaming operators. They will answer your questions:

[https://www.amiq.com/consulting/2017/06/23/how-to-unpack-data-using-the-systemverilog-streaming-operators/](https://www.amiq.com/consulting/2017/06/23/how-to-unpack-data-using-the-systemverilog-streaming-operators/)

[https://www.amiq.com/consulting/2017/05/29/how-to-pack-data-using-systemverilog-streaming-operators/](https://www.amiq.com/consulting/2017/05/29/how-to-pack-data-using-systemverilog-streaming-operators/)
