Is it possible to create synthesizable code where a constant function can take an array of integers and determine the largest one. The number of elements in the array can change and I realize dynamic arrays and arrays by reference to constant functions are not synthesizable. Is it possible to pass a parameter to constant function that can size the array and then do a loop to determine the largest member while keeping it synthesizable.
Would something like this be synthesizable:
function automatic int max
(
input int size_of_array,
input int array_of_int [size_of_array]
);
int max_value = 0;
for(int i = 0; i < size_of_array, ++i)
if (array_of_int[i] > max_value)
max_value = array_of_int[i];
return(max_value);
You cannot declare a fixed sized array using a variable for its size. There is no reason the following should not be synthesizable except that synthesis tools do not allow dynamic arrays except as declared as a constant.
module top;
function automatic int fmax
(
input int array_of_int []
);
int max_value = array_of_int[0];
for(int i=0; i < $size(array_of_int); ++i)
if (array_of_int[i] > max_value)
max_value = array_of_int[i];
return(max_value);
endfunction
parameter int A[] = {1,2,3,4};
int B[3] = {3,2,1};
logic [31:0] max;
initial begin
max = fmax(A);
$display(max);
max = fmax(B);
$display(max);
end
endmodule
However most synthesis tools accepts static methods of parameterized classes
module top;
class C#(int size);
static function int fmax
(
input int array_of_int [size]
);
int max_value = array_of_int[0];
for(int i=0; i < size; ++i)
if (array_of_int[i] > max_value)
max_value = array_of_int[i];
return(max_value);
endfunction
endclass
parameter int A[] = {1,2,3,4};
int B[3] = {3,2,1};
logic [31:0] max;
initial begin
max = C#($size(A))::fmax(A);
$display(max);
max = C#($size(B))::fmax(B);
$display(max);
end
endmodule
Thanks Dave - this is an elegant solution and I was hesitant using virtual class because I wasn’t sure of the support from synthesis tools but as you pointed out this is being supported now. I will need to test this out all though the synthesis manuals claim support.