Stream to struct

Hi All,

I am new to system verilog. I followed this tutorial[1] to create my own message deserializer.

The stream is in little endian byte ordering. The struct is a simple 2 bytes size and 1 byte count.
It is ok if streaming field by field but it doesn’t work if directly stream to struct.
Please check the code[2] and run output[3].
Please kindly advise.

Regards,
Ken

[1] How to Unpack Data Using the SystemVerilog Streaming Operators (>>, <<) | AMIQ Consulting

[2] code:

module stream;
  typedef struct {
    shortint size; // 2 bytes
    byte count; // 1 bytes
  } packet;

  logic [23:0] data = {23'h1c0001}; // size = 28, count = 1, little endian
  
  initial
  begin
    static packet msg;
    $display("stream field by field:");
    $display("size = %d", {<<8{data[23:8]}});
    $display("count = %d", {<<8{data[7:0]}});
    $display("stream to struct:");
    {>>{msg}} = {<<8{data[23:0]}}; // please advise how to unpack the data to msg struct
    $display("msg.size = %d", msg.size); 
    $display("msg.count = %d", msg.count);
  end
endmodule

[3]

Loading work.stream

run

stream field by field:

size = 28 // ok

count = 1 // ok

stream to struct:

msg.size = 256 // not ok. expected : 28

msg.count = 28 // not ok. expected : 1