Trying to display a value of a variable named data (whose width is dependent on the parameter passed into the function, which makes it variable as it is called several times from different parts of the design).
when I display with 0x%X it display the default width of 512 bits.
Is there any easy way I can just display 32 bits of data when the width is defined as 32?
There are a number of ways you can handle this. It really depends on what type of operations you need to do with the data passed to the function.
The simplest is to add an additional argument to your function that has the width of the other argument.
myfunction(data,$bits(data));
Then inside your function, convert the data to a string and truncate the string using substr().
Another option is not to use a default 512 bit vector as a argument and instead use a dynamic array of bits, bytes, or 4-bit nibbles. Use the streaming operator to convert the array to a 512-bit vector when you need it. You will need to loop through the array elements to build the string you need to print.
You can also parameterize the function with the the type of the data you need to pass to it Then %x will have the correct number of digits.
class c #(type T);
static function void myfunction(T data);
$display("%x",data);
endfunction
endclass
c#(type(packet))::myfunction(packat);