I ran into a similar challenge where I needed to apply a reduction operation across a vector signal in Simulink and make sure it is HDL coder compatible. I found that this can be done using the following approach:
- Use a MATLAB Function block inside a Subsystem (this is required for HDL Coder).
- Make sure your input vector is fixed size and set the data type to fixed-point using “fixdt” because double isn’t supported for synthesis.
- Implement the reduction as a simple loop applying the binary function over the vector elements. Keep it purely combinational to avoid algebraic loops.
- Define your binary operation inside a helper function so it’s easy to swap out.
This approach works well with HDL Coder too You may use the following function for reference:
function result = reduce_vector(u)
%#codegen
assert(~isempty(u));
N = length(u);
result = u(1);
for i = 2:N
result = my_binary_op(result, u(i));
end
end
function out = my_binary_op(a, b)
% Change to your own binary operation
out = min(a, b);
end
You can refer to the following documentation for more information on HDL Coder - https://www.mathworks.com/help/hdlcoder/ug/supported-simulink-blocks.html
I hope this helps.
