| Simulink® HDL Coder™ | ![]() |
| On this page… |
|---|
Fixed-Point Runtime Library Support |
The coder supports most of the fixed-point runtime library functions supported by the Embedded MATLAB Function block. For a complete list of these functions, see Supported Functions and Limitations of the Fixed-Point Embedded MATLAB Subset in the Fixed-Point Toolbox documentation.
Some functions are not supported, or are subject to some restrictions. These functions are summarized in the following table.
| Function | Restriction | Notes |
|---|---|---|
| disp | Not supported | |
| get | Not supported | This function returns a struct. Struct data types are not supported in this release. |
| pow2 | Not supported | |
| real | Not supported | |
| divide | Supported, with restrictions | The divisor must be a constant and a power of two. |
| fi | Supported, with restrictions | Only the following rounding modes are supported: ceil, fix, floor, nearest. |
| fimath | Supported, with restrictions | Only the following rounding modes are supported: ceil, fix, floor, nearest. |
| subsasgn | Supported, with restrictions | Subscripted assignment supported; see Data Type Usage |
| subsref | Supported, with restrictions | Subscripted reference supported; see Data Type Usage |
This section summarizes supported data types and typing rules for variable and constants, and the use of persistent variables in modeling registers.
When generating code for the Embedded MATLAB Function block, the coder supports a subset of MATLAB data types. The following table summarizes supported and unsupported data types.
| Type(s) | Support | Notes |
|---|---|---|
| Integer | Supported:
| |
| Real | Supported:
| HDL code generated with double or single data types is not synthesizable. |
| Character | Supported: char | |
| Logical | Supported: Boolean | |
| Fixed point | Supported:
| Fixed point numbers with slope (not equal to 1.0) and bias (not equal to 0.0) are not supported. Maximum word size for fixed-point numbers is 32 bits. The convergent and matlab rounding modes are not currently supported. Do not specify these modes in fimath in specifications. |
| Vectors | Supported:
| The maximum number of vector elements allowed is 2^32. A variable must be fully defined before it is subscripted. |
| Matrix | N/A | Matrix data types are not supported in the current release. |
| Struct | N/A | Struct data types are not supported in the current release. |
| Cell arrays | N/A | Cell arrays are not supported in the current release. |
Strong typing rules are applied to Embedded MATLAB Function blocks, as follows:
All input and output port data types must be resolved at model compilation time.
If the data type of an input port is unspecified when the model is compiled, the port is assigned the data type of the signal driving the port.
If the data type of an output port is unspecified when the model is compiled, the output port type is type is determined by the first assignment to the output variable.
Similarly, all constant literals are strongly typed. If you do not specify the data type of a constant explicitly, its type is determined by internal rules. To specify the data type of a constant, use cast functions (e.g., uint8, uint16, etc.) or fi functions using fimath specifications.
After you have defined a variable, do not change its data type. Variable types cannot be changed dynamically by assigning a different value. Dynamic typing will lead to a compile time error.
After you have defined a variable, do not change its size. Variables cannot be grown or resized dynamically.
Do not use output variables to model registered output; Embedded MATLAB Function block outputs are never persistent. Use persistent variables for this purpose, as described in Persistent Variables.
Persistent variables let you model registers. If you need to preserve state between invocations of an Embedded MATLAB Function block, use persistent variables.
Each persistent variable must be initialized with a statement specifying its size and type before it is referenced. You can initialize a persistent variable with either a constant value or a variable, as in the following code listings:
% Initialize with a constant
persistent p;
if isempty(p)
p = fi(0,0,8,0);
end
% Initialize with a variable
initval = fi(0,0,8,0);
persistent p;
if isempty(p)
p = initval;
end
When testing whether a persistent variable has been initialized, it is good practice to use simple logical expressions, as in the preceding examples. Using simple expressions ensures that the HDL code for the test is generated in the reset process, and therefore is executed only once.
You can initialize multiple variables based on a single simple logical expression, as in the following example:
% Initialize with variables
initval1 = fi(0,0,8,0);
initval2 = fi(0,0,7,0);
persistent p;
if isempty(p)
x = initval1;
y = initval2;
end
See also The Incrementer Function Code for an example of the initialization and use of a persistent variable.
Note If persistent variables are not initialized properly, unnecessary sentinel variables can appear in the generated code. |
Limitation on Use of Persistent Variables. As described in Using Persistent Variables to Model State, you can use persistent variables in Embedded MATLAB code to simulate various kinds of delay blocks.
However, note that the ports on the Embedded MATLAB Function block act as direct feedthrough ports during simulation. The delay constructs internal to the Embedded MATLAB Function block are not recognized during simulation. Therefore a feedback loop in the model causes an algebraic loop condition.
To work around this limitation:
Keep the combinatorial logic inside the Embedded MATLAB Function block for one of the blocks in the loop which has a persistent variable for the output or input. Remove the persistent variable.
Place a Unit Delay block external to the Embedded MATLAB Function block.
An Embedded MATLAB function argument can be declared to be a parameter argument by setting its Scope to Parameter in the Ports and Data Manager GUI. Such a parameter argument does not appear as a signal port on the block. Parameter arguments for Embedded MATLAB Function blocks do not take their values from signals in the Simulink model. Instead, their values come from parameters defined in a parent Simulink masked subsystem or variables defined in the MATLAB base workspace.
Only nontunable parameters are supported for HDL code generation. If you declare parameter arguments in Embedded MATLAB function code that is intended for HDL code generation, be sure to clear the Tunable option for each such parameter argument.
See also Parameter Arguments in Embedded MATLAB Functions in the Simulink documentation.
When generating code for the Embedded MATLAB Function block, the coder supports the arithmetic operators (and their M-function equivalents) listed in the following table.
| Operation | Operator Syntax | M-Function Equivalent | Fixed Point Support? |
|---|---|---|---|
| Binary addition | A+B | plus(A,B) | Y |
| Matrix multiplication | A*B | mtimes(A,B) | Y |
| Arraywise multiplication | A.*B | times(A,B) | Y |
| Matrix right division | A/B | mrdivide(A,B) | Y |
| Arraywise right division | A./B | rdivide(A,B) | Y |
| Matrix left division | A\B | mldivide(A,B) | Y |
| Arraywise left division | A.\B | ldivide(A,B) | Y |
| Matrix power | A^B | mpower(A,B) | Y |
| Arraywise power | A.^B | power(A,B) | Y |
| Complex transpose | A' | ctranspose(A) | Y |
| Matrix transpose | A.' | transpose(A) | Y |
| Matrix concat | [A B] | None | Y |
| Matrix index Note: A variable must be fully defined before it is subscripted. | A(r c) | None | Y |
When generating code for the Embedded MATLAB Function block, the coder supports the relational operators (and their M-function equivalents) listed in the following table.
| Relation | Operator Syntax | M Function Equivalent | Fixed-Point Support? |
|---|---|---|---|
| Less than | A<B | lt(A,B) | Y |
| Less than or equal to | A<=B | le(A,B) | Y |
| Greater than or equal to | A>=B | ge(A,B) | Y |
| Greater than | A>B | gt(A,B) | Y |
| Equal | A==B | eq(A,B) | Y |
| Not equal | A~=B | ne(A,B) | Y |
When generating code for the Embedded MATLAB Function block, the coder supports the logical operators (and their M function equivalents) listed in the following table.
| Relation | Operator Syntax | M Function Equivalent | Fixed-Point Support? | Notes |
|---|---|---|---|---|
| Logical And | A&B | and(A,B) | Y | |
| Logical Or | A|B | or(A,B) | Y | |
| Logical Xor | A xor B | xor(A,B) | Y | |
| Logical And (short circuiting) | A&&B | N/A | Y | Use short circuiting logical operators within conditionals. See also Control Flow Statements. |
| Logical Or (short circuiting) | A||B | N/A | Y | Use short circuiting logical operators within conditionals. See also Control Flow Statements. |
| Element complement | ~A | not(A) | Y |
When generating code for the Embedded MATLAB Function block, the coder imposes some restrictions on the use of control flow statements and constructs. The following table summarizes supported and unsupported control flow statements.
| Control Flow Statement | Notes |
|---|---|
break continue return | Do not use these statements within loops. Use of these statements in a loop causes the coder to report the following error: Unstructured flow graph or loop containing [statement type] not supported for HDL |
while | while loops are not supported. Use of while loops causes the coder to report the following error: Unstructured flow graph or loop containing [statement type] not supported for HDL |
for | for loops without static bounds are not supported. Use of for loops without static bounds causes the coder to report the following error: Unstructured flow graph or loop containing [statement type] not supported for HDL Do not use a for loop with an increment other than 1. Use of an increment other than 1 can cause errors in HDL simulation and synthesis. If you require a for loop increment other than 1, you should use the Embedded MATLAB function eml.unroll to unroll the loop, as in the following code example, which unrolls a loop with an increment of 2. for i=eml.unroll(1:2:8)
...
end
See also eml.unroll. Do not use the & and | operators within conditions of a for statement. Instead, use the && and || operators. The Embedded MATLAB Function block does not support nonscalar expressions in the conditions of for statements. Use the all or any functions to collapse logical vectors into scalars. |
| if | Do not use the & and | operators within conditions of an if statement. Instead, use the && and || operators. The Embedded MATLAB Function block does not support nonscalar expressions are not supported in the conditions of if statements. Use the all or any functions to collapse logical vectors into scalars. |
| switch | The HDL code matches the behavior of the switch statement; the first matching case statement is executed. Use only scalars in conditional expressions in a switch statement. Use of fi variables in switch or case conditionals is not supported. For HDL code generation, the usage is restricted to uint8, uint16, uint32, sint8, sint16, and sint32. If multiple case statements make assignments to the same variable, then their numeric type and fimath specification should match that variable. |
![]() | Recommended Practices | Other Limitations | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |