MATLAB Answers

Found unsupported type in switch expression; switch expression must be of discrete type

8 views (last 30 days)
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.
When writing the Matlab Code I reffered to this documentation: https://de.mathworks.com/help/hdlcoder/ug/model-a-state-machine-for-hdl-code-generation.html
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.
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
A simple example where the control unit with the state machine is used is attached.
Thanks in advance!
Best Regards

  3 Comments

Kiran Kintali
Kiran Kintali on 21 Sep 2020
Can you share example types or sampel values used for hStart_in,hEnd_in,valid_in? It will help reproduce the issue. You provide this in a testbench file as well.
Tobis Stelzer
Tobis Stelzer on 21 Sep 2020
Thanks for your fast reply! These signals are all boolean and obtained via Pixel Control Bus Selector from the Pixel Control Bus Signal (ctrl) of a Line Buffer (Vision HDL Toolbox).
Tobis Stelzer
Tobis Stelzer on 21 Sep 2020
Here is a simple example/testbench, which uses the same control unit. It is designed for use with an 28x28x1 input image.

Sign in to comment.

Accepted Answer

Tobis Stelzer
Tobis Stelzer on 24 Sep 2020
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

  0 Comments

Sign in to comment.

More Answers (1)

Vasishta Bhargava
Vasishta Bhargava on 21 Sep 2020
Edited: Vasishta Bhargava on 21 Sep 2020
switch statement needs a scalar or character value to evaluate the statements inside it.
You have declared curr_state as persistent. Which by default assigns to empty matrix [] in first time. Additionally you used a space btw switch and () Try as
%if true
% code
%end
switch(curr_state)

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by