Hello, I use a Matlab Function block as state machine in my Simulink module. Everything works, except the RTL Code generation in the HDL Workflow Advisor. At this point I get the following error: Found unsupported type in switch expression; switch expression must be of discrete type, Function 'MNIST_CNN/MAXPOOL_BANK_20/CONTROL/MATLAB Function' (#1650.355.365), line 13, column 9. My Matlab Code looks like this: function [hStart,hEnd,valid,reset,enable] = maxpool_fsm(hStart_in,hEnd_in,valid_in) %states: ready = 0; vector = 1; matrix = 2; wait_for_next_line=3; % using persistent keyword to model state registers in hardware persistent curr_state; if isempty(curr_state) curr_state = ready; end % switch to new state based on the value state register switch (curr_state) case ready, if (hStart_in==true && valid_in==true) curr_state=vector; valid=false; reset=true; enable=true; hEnd=false; hStart=true; else curr_state=ready; valid=false; reset=true; enable=true; hEnd=false; hStart=false; end case vector, if (valid_in == true && hEnd_in==true) curr_state=wait_for_next_line; valid=true; reset=false; enable=true; hEnd=true; hStart=false; elseif (valid_in == true) curr_state=matrix; valid=true; reset=false; enable=true; hEnd=false; hStart=false; else curr_state=vector; valid=false; reset=true; enable=true; hEnd=false; hStart=false; end case matrix, if (hEnd_in==true) curr_state=wait_for_next_line; valid = false; reset = false; enable = false; hEnd=true; hStart=false; elseif (valid_in==true) curr_state=vector; valid = false; reset = true; enable = true; hEnd=false; hStart=false; else curr_state=matrix; valid=false; reset=false; enable=false; hEnd=false; hStart=false; end case wait_for_next_line, if (hEnd_in==true) curr_state=ready; valid = false; reset=false; enable = false; hEnd=false; hStart=false; else curr_state=wait_for_next_line; valid = false; reset=false; enable = false; hEnd=false; hStart=false; end otherwise, valid = false; reset=false; enable = false; hEnd=false; hStart=false; end During compilation and simulation everything works. I have no clue, where the error is. In the Model Advisor the following error appeas: Failed The model contains constructs that are unsupported for HDL code generation. Error using hdlcoder.pirctx/invokeBackEnd The model contains constructs that are unsupported for HDL code generation. Error in slhdlcoder.HDLCoder/makehdl Error in slhdlcoder.HDLCoder/makehdlturnkey Error in downstream.DownstreamIntegrationDriver/runIPCoreCodeGen Error in generateIPCore Error in Simulink.ModelAdvisor/executeCheckCallbackFct Error in Simulink.ModelAdvisor/run Error in Simulink.ModelAdvisor/runCheck Error in ModelAdvisor.Node/runTaskAdvisor Error in ModelAdvisor.Node.runtohere>runToBreakpoint Error in ModelAdvisor.Node.runtohere
Prashant Kumar answered .
2025-11-20
I solved the issue by not declaring the state variable as persistent, but instead using a delay outside the matlab function block:
function [hStart,hEnd,valid,reset,enable,curr_state_out] = maxpool_fsm(curr_state_in,hStart_in,hEnd_in,valid_in)
%states:
ready = uint8(0);
vctr = uint8(1);
mtrx = uint8(2);
wait_for_next_line=uint8(3);
% using persistent keyword to model state registers in hardware
%persistent curr_state;
curr_state_out=uint8(0);
if isempty(curr_state_out)
curr_state_out = ready;
end
% switch to new state based on the value state register
switch(curr_state_in)
case ready,
if (hStart_in==true && valid_in==true)
curr_state_out=vctr;
valid=false;
reset=true;
enable=true;
hEnd=false;
hStart=true;
else
curr_state_out=ready;
valid=false;
reset=true;
enable=true;
hEnd=false;
hStart=false;
end
case vctr,
if (valid_in == true && hEnd_in==true)
curr_state_out=wait_for_next_line;
valid=true;
reset=false;
enable=true;
hEnd=true;
hStart=false;
elseif (valid_in == true)
curr_state_out=mtrx;
valid=true;
reset=false;
enable=true;
hEnd=false;
hStart=false;
else
curr_state_out=vctr;
valid=false;
reset=true;
enable=true;
hEnd=false;
hStart=false;
end
case mtrx,
if (hEnd_in==true)
curr_state_out=wait_for_next_line;
valid = false;
reset = false;
enable = false;
hEnd=true;
hStart=false;
elseif (valid_in==true)
curr_state_out=vctr;
valid = false;
reset = true;
enable = true;
hEnd=false;
hStart=false;
else
curr_state_out=mtrx;
valid=false;
reset=false;
enable=false;
hEnd=false;
hStart=false;
end
case wait_for_next_line,
if (hEnd_in==true)
curr_state_out=ready;
valid = false;
reset=false;
enable = false;
hEnd=false;
hStart=false;
else
curr_state_out=wait_for_next_line;
valid = false;
reset=false;
enable = false;
hEnd=false;
hStart=false;
end
otherwise,
valid = false;
reset=false;
enable = false;
hEnd=false;
hStart=false;
end