How can I print a continuous slice of a dynamic array?

I have a dynamic 2D array and I want to print a continuous range of values from the array, for example array indexes 0 to 3. How can I print them? What I am doing right now is shown below, which is fine for 4 indexes, but I want to print much larger portions of the dynamic array later on. Is there a better way to do this?


logic [7:0] rx_message_array[];
...
$display("ACK HEADER_ID: 0x%X%X%X%X", rx_message_array[0], rx_message_array[1], rx_message_array[2], rx_message_array[3]);

I tried this notiaiton but this did not work:


$display("ACK HEADER_ID: 0x%X", rx_message_array[3:0]);

In reply to ianmurph:

One way would be to use a string, and keep adding/concat-ing to it.


module test();
  logic [7:0] rx_message_array[];
  
  initial begin
    string s = "0x";
    rx_message_array = new[4];
    foreach(rx_message_array[i])
      rx_message_array[i] = i;

    foreach(rx_message_array[i])
      s = $sformatf("%s%X", s, rx_message_array[i]);
    
    $display("%s",s);
    
  end
endmodule

In reply to ianmurph:

Two problems with your example.

A slice of an unpacked dynamic array is still an unpacked array expression, not an integral value. And dynamic arrays get indexed starting from element 0, so it would have to be [0:3].

You could use a cast to convert the unpacked array to packed.

$display("ACK HEADER_ID: 0x%X", 32'( {>>{rx_message_array[0:3]}} ) );

In reply to dave_59:

This looks like a pretty slick solution, but it appears to be giving me this error message:

$display("ACK HEADER_ID: 0x%X", 32'( {>>{rx_message_array[0:3]}} ) ); //cast to convert the unpacked array to packed
                                                       |
ncvlog: *E,SCNOIN (../../tb/testcases/testcase_lwir_uart.sv,181|55): As a temporary implementation restriction, the source data type in a static cast must be an integral, real or class data type.

In reply to ianmurph:

This is a tool specific feature not implemented yet message.