A rarely used operator but very useful in many situations.
What is bit-stream type
From IEEE SV 2017:
Types that can be packed into a stream of bits are called bit-stream types. A bit-stream type is a type consisting of the following:
Any integral, packed, or string type
Unpacked arrays, structures, or classes of the preceding types
Dynamically sized arrays (dynamic, associative, or queues) of any of the preceding types This definition is recursive so that, for example, a structure containing a queue of int is a bit-stream type
Streaming operator
From IEEE SV 2017:
The streaming operators perform packing of bit-stream types into a sequence of bits in a user-specified order.
The slice_size determines the size of each block, measured in bits. If a slice_size is not specified, the default is 1.
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.
intm_pack_var;m_pack_var={>>byte{8'h06,8'h07,8'h08}};$display("array to var %h\n",m_pack_var);// output:// array to var 06070800m_pack_var={<<byte{8'h06,8'h07,8'h08}};$display("array to var %h\n",m_pack_var);// output:// array to var 08070600
Streaming Operator: pack a queue to an integral
automaticbyteq[$]={8'h01,8'h02,8'h03};intm_pack_var;m_pack_var={>>byte{q}};$display("queue to var %h\n",m_pack_var);// output// queue to var 01020300
Streaming Operator: turn an integral to a queue, 8bit slice
Streaming operator at lhs, turn an integral to 3 different vars
bytea,b,c;{<<byte{a,b,c}}=24'h060708;$display("unpack a 0x%0x",a);$display("unpack b 0x%0x",b);$display("unpack c 0x%0x\n",c);{>>byte{a,b,c}}=24'h060708;$display("unpack a 0x%0x",a);$display("unpack b 0x%0x",b);$display("unpack c 0x%0x",c);
Streaming Operator: use 2 Streaming Operator to turn a 32bit queue to a 8bit queue and vice versa
automaticbyteq8[$]={8'h01,8'h02,8'h03,8'h04,8'h05,8'h06,8'h07};automaticbit[31:0]q32[$];q32={>>32{q8}};foreach(q32[i])begin$display("q8 to q32 0x%0x",q32[i]);end//// output:// 0x1020304// 0x5060700q8.delete();q8={>>8{q32}};$display("q32 to q8 %p",q8);// output// '{1, 2, 3, 4, 5, 6, 7, 0}
Streaming Operator: use 2 Streaming Operator to turn a 8bit queue to a 32bit queue with reverse order
// input:// dd 19 df f2 83 e2 5c 4b-f3 a6 cd e0 99 7f 59 33// expected output// 0xf2df19dd - 0x4b5ce283 - 0xe0cda6f3 - 0x33597f99automaticbyteq8[$]={'hdd,'h19,'hdf,'hf2,'h83,'he2,'h5c,'h4b,'hf3,'ha6,'hcd,'he0,'h99,'h7f,'h59,'h33};automaticbit[31:0]q32[$];q32={<<32{{<<8{q8}}}};foreach(q32[i])begin$display("q8 to q32 reverse order 0x%0x",q32[i]);end// q8 to q32 reverse order 0xf2df19dd// q8 to q32 reverse order 0x4b5ce283// q8 to q32 reverse order 0xe0cda6f3// q8 to q32 reverse order 0x33597f99q8.delete();q8={<<8{{<<32{q32}}}};$displayh("q32 to q8 %p",q8);// q32 to q8 '{dd, 19, df, f2, // 83, e2, 5c, 4b, // f3, a6, cd, e0, // 99, 7f, 59, 33}
Streaming Operator: Remove offset and pack to different size
// 32bit data queue, offset 16 bit, need to pack to 28bit data queue// input: --> expected data:// [ 0] 0xa5dc_751c [ 0] 0x135_a5dc// [ 1] 0x23ff_4135 [ 1] 0xc12_3ff4// [ 2] 0x56c8_29c1 [ 2] 0x056_c829automaticbit[31:0]q32[$]={32'ha5dc_751c,32'h23ff_4135,32'h56c8_29c1};automaticbitq1[$];automaticbit[27:0]q28[$];intoffset=16// pack to 1 bit queueq1={<<1{{<<32{q32}}}};// remove unnecessary bit in the offsetfor(inti=0;i<offset;i++)q1.pop_front();for(inti=0;i<offset;i++)q1.push_back(0);// pack to 28bit queueq28={<<28{{<<1{q1}}}};$displayh("q28 %p",q28);// q28 '{135a5dc, c123ff4, 056c829, 0000000}
Finding more information
To have more understanding as well as more examples, check below references: